如何在不阻塞C ++ Allegro中的整个程序的情况下使文本闪烁?

时间:2018-01-12 20:48:58

标签: 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时窗口关闭)

2 个答案:

答案 0 :(得分:1)

在主循环之外:

  1. 创建计时器:timer = al_create_timer(...);

  2. 创建事件队列:event_queue = al_create_event_queue();

  3. 在主循环的顶部:

  4. al_wait_for_event(event_queue, &ev);
    
    if (ev.type == ALLEGRO_EVENT_TIMER)
    {
    // do your blinking stuff here
    }
    

答案 1 :(得分:1)

最好的方法是在绘制文本时检查要绘制的状态(闪烁开或关)。这可以从当前时间得出。类似的东西:

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).
}