动画闪烁问题

发布于 2024-11-09 23:48:48 字数 827 浏览 3 评论 0原文

这是我的代码和绘图代码中的游戏循环:

 float frames_per_second = 60;

    display_timer = al_create_timer(1/frames_per_second);
    queue = al_create_event_queue();
    al_register_event_source(queue, al_get_timer_event_source(display_timer));
    al_start_timer(display_timer);
    while(!end_game)
    {
      ALLEGRO_EVENT event;
      al_wait_for_event(queue, &event);
      if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break;
      if(event.any.source == al_get_timer_event_source(display_timer))
      {update_display();}
      update_input();
    }

    void update_display()
    {
      al_clear_to_color(al_map_rgb(255, 255,255));
      draw_objects(); //this is just an al_draw_bitmap() call
      al_flip_display();
    }

通过在屏幕上移动对象创建的动画闪烁,我对此感到惊讶,因为我写入屏幕的后台缓冲区,因此我期望双缓冲。我可以采取什么措施来纠正闪烁?谢谢。

This is the game loop in my code and the drawing code:

 float frames_per_second = 60;

    display_timer = al_create_timer(1/frames_per_second);
    queue = al_create_event_queue();
    al_register_event_source(queue, al_get_timer_event_source(display_timer));
    al_start_timer(display_timer);
    while(!end_game)
    {
      ALLEGRO_EVENT event;
      al_wait_for_event(queue, &event);
      if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break;
      if(event.any.source == al_get_timer_event_source(display_timer))
      {update_display();}
      update_input();
    }

    void update_display()
    {
      al_clear_to_color(al_map_rgb(255, 255,255));
      draw_objects(); //this is just an al_draw_bitmap() call
      al_flip_display();
    }

The animation created by moving objects on screen flickers, I'm surprised by this since I write to the back buffer of the screen thus I expect double buffering. What can I do to correct the flickering? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-11-16 23:48:48

与该问题无关,您可以通过查找 ALLEGRO_EVENT_TIMER 事件来检查计时器。如果您有多个计时器,您可以使用 event.timer.source 来检查它是哪个计时器。

我认为这里的主要问题是你以 60fps 的速度绘制图形,但你却以无限的速率更新输入。这其实是本末倒置。您希望以固定速率更新输入。您可以随心所欲地绘制图形...尽管如果没有任何更改,则更新图形是没有意义的。

所以它应该看起来更像:

if(event.type == ALLEGRO_EVENT_TIMER)
{
   update_input();
   update_display();
}

但是,如果速度太慢,这不会实现跳帧。您应该做的是在专用队列(不包含其他事件源)中设置计时器。然后,只要专用计时器队列中有事件,就更新输入。

然后更新显示,假设您已经处理了至少一个刻度。因此,如果事情变得太慢,您可以对每个绘制的帧进行多个输入更新。

Unrelated to the problem, you can check a timer by looking for the ALLEGRO_EVENT_TIMER event. You can use event.timer.source to check which timer it is, if you have more than one.

I think the main problem here is that you are drawing the graphics at 60fps but you are updating the input at an unlimited rate. This is actually backwards. You want to update the input at a fixed rate. You can draw the graphics as often as you like... although it makes no sense to update the graphics if nothing has changed.

So it should look something more like:

if(event.type == ALLEGRO_EVENT_TIMER)
{
   update_input();
   update_display();
}

However, this does not implement frame skipping if things get too slow. What you should do is set up the timer in a dedicated queue (that contains no other event sources). Then as long as there are events sitting in that dedicated timer queue, update the input.

Then update the display, assuming you've processed at least one tick. So you may, if things get too slow, do multiple input updates per drawn frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文