修改函数而不失去意义

问题描述 投票:0回答:1

我正在尝试在 csfml 中打开和关闭一个窗口,我的代码没有错误,但条件块中不能有超过 3 个分支。我很难在不出错的情况下修改它。

这是我尝试过的:

int main(void)
{
    sfRenderWindow *window;
    sfVideoMode video_mode;
    sfEvent event;
    video_mode.width = 1920;
    video_mode.height = 1080;
    video_mode.bitsPerPixel = 32;

    window = sfRenderWindow_create(video_mode,"MyWindow", sfDefaultStyle, NULL);
    while (sfRenderWindow_isOpen(window)) {
        sfRenderWindow_display(window);
        while (sfRenderWindow_pollEvent(window, &event)) {
            if (event.type == sfEvtClosed)
                sfRenderWindow_close(window);
        }
    }
}

c sfml
1个回答
0
投票

我在 CSFML 中做了几个项目,就我个人而言,我使用结构来存储所有信息。

这是我的代码的一部分,用于创建您想要的窗口:

#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <SFML/System.h>
#include <unistd.h>

typedef struct window {
    sfVideoMode video_mode;
    sfRenderWindow *window;
} window_t;

typedef struct global {
    sfEvent event;
    window_t window;
} global_t;

void create_window(global_t *all)
{
    all->window.video_mode.height = 769;
    all->window.video_mode.width = 1280;
    all->window.video_mode.bitsPerPixel = 32;
    all->window.window = sfRenderWindow_create(all->window.video_mode, "My Hunter",
     sfClose | sfResize, NULL);
    sfRenderWindow_setFramerateLimit(all->window.window, 60);
}

void analyse_event(global_t *all)
{
    if (all->event.type == sfEvtClosed || sfKeyboard_isKeyPressed(sfKeyEscape))
        sfRenderWindow_close(all->window.window);
}

void game_loop(void)
{
    global_t all;
    create_window(&all);
    while (sfRenderWindow_isOpen(all.window.window)) {
        while (sfRenderWindow_pollEvent(all.window.window, &all.event)) {
            analyse_event(&all);
        }
        sfRenderWindow_display(all.window.window);
        sfRenderWindow_clear(all.window.window, sfBlack);
    }
}

int main()
{
    game_loop();
    return 0;
}

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.