如何在C++ Allegro 中使文本闪烁而不会阻塞整个程序
How to make text blinking without blocking the whole program in C++ Allegro?
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).
}
在你的主循环之外:
-
创建计时器:
timer = al_create_timer(...);
-
创建事件队列:
event_queue = al_create_event_queue();
-
在主循环的顶部:
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
// do your blinking stuff here
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 为什么当我输入大量数字时,我的计算器程序开始闪烁和滚动
- 加载的 obj 模型闪烁并崩溃应用程序
- 在 d3d->CreateDevice 中D3DERR_INVALIDCALL,导致在 Allegro 5 D3D 程序中启动时窗口闪烁
- 如何在C++ Allegro 中使文本闪烁而不会阻塞整个程序
- 如何在控制台应用程序中停止闪烁
- WM_PAINT处理程序未按预期执行,则会出现类似闪烁的效果
- 尽管应用了已知的防闪烁技术,但我的应用程序在调整大小时略有闪烁
- C++:在MFC应用程序中使用WM_ERASEBKGND-Message避免闪烁