Allegro 5 游戏:如何实现以恒定速度运行的游戏循环?

6

如何在 Allegro 5 中编写游戏循环,以保证其始终以相同的速度运行,并正确地将绘图逻辑与更新逻辑分离?我应该使用线程吗?我应该利用新的 Allegro 事件系统吗?

2个回答

4

摘自 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;
        }
}

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


啊,那么你应该利用事件。线程能够潜在地提高图像稳定性吗?事件队列是线程安全的吗? - amarillion
线程对此毫无帮助。通常情况下,如果您利用渲染时的确切时间(再次提醒,al_current_time()是您的好朋友),它可以帮助您。这将允许诸如在与游戏逻辑相关的确切位置插值显示某些内容之类的事情。您还应该记住,al_flip_display()将等待垂直同步(如果启用了垂直同步,但是为了完美流畅的动画,必须打开垂直同步)。而A5是100%线程安全的 - 事件队列尤其如此,因为它们是跨线程通信的一种手段。 - allefant
以下是有关编程的内容,请将其从英语翻译成中文。仅返回已翻译的文本:链接至提到的维基页面:http://wiki.allegro.cc/index.php?title=Porting_from_A4_to_A5 - Leftium

3

这里有Allefant回答的更完整版本(点击链接查看详细的逐行解释):

#include <stdio.h>
#include <allegro5/allegro.h>

const float FPS = 60;

int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;
   ALLEGRO_TIMER *timer = NULL;
   bool redraw = true;

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

   timer = al_create_timer(1.0 / FPS);
   if(!timer) {
      fprintf(stderr, "failed to create timer!\n");
      return -1;
   }

   display = al_create_display(640, 480);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      al_destroy_timer(timer);
      return -1;
   }

   event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }

   al_register_event_source(event_queue, al_get_display_event_source(display));

   al_register_event_source(event_queue, al_get_timer_event_source(timer));

   al_clear_to_color(al_map_rgb(0,0,0));

   al_flip_display();

   al_start_timer(timer);

   while(1)
   {
      ALLEGRO_EVENT ev;
      al_wait_for_event(event_queue, &ev);

      if(ev.type == ALLEGRO_EVENT_TIMER) {
         redraw = true;
      }
      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }

      if(redraw && al_event_queue_is_empty(event_queue)) {
         redraw = false;
         al_clear_to_color(al_map_rgb(0,0,0));
         al_flip_display();
      }
   }

   al_destroy_timer(timer);
   al_destroy_display(display);
   al_destroy_event_queue(event_queue);

   return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接