开发者

Animation flickering problem

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_que开发者_Python百科ue();
    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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜