作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
bool running = true;
int width = al_get_display_width(display);
while (running) {
for (;;) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 760, 375, 0, "Play (Spacebar)");
al_flip_display();
al_rest(1.5);
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
al_rest(0.5);
}
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
running = false;
}
}
如您所见,我有一个无限循环,它阻塞了整个程序以使文本闪烁。问题是我如何进行闪烁,以便其他事情像后续事件一样继续工作(当用户点击 X 时窗口关闭)
最佳答案
最好的方法是在绘制文本时检查要绘制的状态(闪烁或关闭)。这可以从当前时间得出。像这样的东西:
while (running) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(bitmap, 0, 0, 0);
if (fmod(al_get_time(), 2) < 1.5) { // Show the text for 1.5 seconds every 2 seconds.
al_draw_text(font, al_map_rgb(0, 0, 0), 760, 375, 0, "Play (Spacebar)");
}
al_flip_display();
// Handle events in a non-blocking way, for example
// using al_get_next_event (not al_wait_for_event).
}
关于c++ - 如何在不阻塞 C++ Allegro 中的整个程序的情况下使文本闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233825/
我是一名优秀的程序员,十分优秀!