2
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;
}
}

As you can see I have this endless loop which blocks the whole program in order for the text to blink. The question is how do I do the blinking so the others things keep working like the event which follows up (it's here for the window to close, when the user clicks X)

John
  • 105
  • 10
  • 1
    Use some kind of timer and draw the blink effect when that timer expires, at all other times do whatever it is your program does. – SoronelHaetir Jan 12 '18 at 20:54

2 Answers2

1

Outside your main loop:

  1. Create timer: timer = al_create_timer(...);

  2. Create event queue: event_queue = al_create_event_queue();

  3. At the top within your main loop:

 

al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_TIMER)
{
// do your blinking stuff here
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
StPiere
  • 4,113
  • 15
  • 24
1

The best way is to check which state (blink on or off) to draw when drawing the text. This can be derived from the current time. Something like:

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).
}
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I understand what you mean, and that's what the code looks like now: bool running = true; 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(); ALLEGRO_EVENT event; al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { running = false; } } Surprisingly, it does not work! – John Jan 14 '18 at 17:26
  • As the text does not blink – John Jan 14 '18 at 17:26
  • Changed this line: if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) to if (event.type != ALLEGRO_EVENT_DISPLAY_CLOSE) and now when I quickly click the exit button the text is blinking! – John Jan 14 '18 at 17:28
  • There's some weird behaviour with this event. Do you know why? – John Jan 14 '18 at 17:29
  • Ah yes, I didn't notice you had `al_wait_for_event`. This way of blinking expects the drawing to happen continuously so it can redraw when the blink state changes, but `al_wait_for_event` stops the execution until an event has occurred. Try `al_wait_for_event_timed` with a small enough value for the `secs` argument, or `al_get_next_event` to have no waiting happen at all. – Emil Laine Jan 14 '18 at 18:05