动画闪烁问题
这是我的代码和绘图代码中的游戏循环:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与该问题无关,您可以通过查找
ALLEGRO_EVENT_TIMER
事件来检查计时器。如果您有多个计时器,您可以使用 event.timer.source 来检查它是哪个计时器。我认为这里的主要问题是你以 60fps 的速度绘制图形,但你却以无限的速率更新输入。这其实是本末倒置。您希望以固定速率更新输入。您可以随心所欲地绘制图形...尽管如果没有任何更改,则更新图形是没有意义的。
所以它应该看起来更像:
但是,如果速度太慢,这不会实现跳帧。您应该做的是在专用队列(不包含其他事件源)中设置计时器。然后,只要专用计时器队列中有事件,就更新输入。
然后更新显示,假设您已经处理了至少一个刻度。因此,如果事情变得太慢,您可以对每个绘制的帧进行多个输入更新。
Unrelated to the problem, you can check a timer by looking for the
ALLEGRO_EVENT_TIMER
event. You can useevent.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:
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.