c - 快板5游戏: game loop that runs at constant speed?

标签 c multithreading game-loop allegro5

在 Allegro 5 中编写游戏循环的最佳方法是什么,它总是以相同的速度运行,并且正确地将绘图逻辑与更新逻辑分开?我应该使用线程吗?我应该使用新的 Allegro 事件系统吗?

最佳答案

取自快板维基:

al_install_timer(1.0 / FPS);

...

while (1) {
        al_wait_for_event(queue, &event);

        /* handle input events */

        if (event.type == ALLEGRO_EVENT_TIMER) {
                handle_game_tick();
                need_redraw = true;
        }

        if (need_redraw && al_event_queue_is_empty(queue)) {
                render_last_frame();
                need_redraw = false;
        }
}

如果您想要跳帧,请在检测到帧落后时跳过 render_last_frame() 命令(例如,通过使用 al_current_time() 函数)。

关于c - 快板5游戏: game loop that runs at constant speed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1212046/

相关文章:

c - 我怎样才能纠正这个代码,以便它给出预期的结果

c - 如何将字符串分配给 char* 指针?

c - c中的基数转换

c - 为什么我在 C 代码中对于某些输入得到错误的答案?

c++ - 在线程中复制文件以防止卡住应用程序

.net - Parallel.ForEach - 优雅的取消

linux - 如何挂起另一个线程(不是当前线程)?

c++ - SFML 不会画任何东西?

c++ - 如何减慢我的程序并获取用户输入?

android游戏循环与渲染线程中的更新