Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Flashing word + waiting for a key to be pressed

This thread is locked; no one can reply to it. rss feed Print
Flashing word + waiting for a key to be pressed
diverswife
Member #16,770
November 2017

Hi,

I'm making a game in C++ and Allegro 5 and I'm having trouble with waiting for event, I think.

What I want is for a bitmap to keep flashing/blinking until the key enter is pressed.

This is what I have right now

#SelectExpand
1while (blinks) 2{ 3 ALLEGRO_EVENT events; 4 al_wait_for_event(queue, &events); 5 if (events.type == ALLEGRO_EVENT_TIMER) 6 { 7 for (alfa = 255; alfa >= 30; alfa--) 8 { 9 al_clear_to_color(al_map_rgb(255, 255, 255)); 10 al_draw_bitmap(background, 0, 0, 0); 11 al_draw_tinted_bitmap(press_enter_please, al_map_rgba(alfa, alfa, alfa, alfa), (x / 2) - (171), (y / 2) + 156, 0); 12 al_flip_display(); 13 al_rest(0.005); 14 } 15 for (alfa = 0; alfa <= 255; alfa++) 16 { 17 al_clear_to_color(al_map_rgb(255, 255, 255)); 18 al_draw_bitmap(background, 0, 0, 0); 19 al_draw_tinted_bitmap(press_enter_please, al_map_rgba(alfa, alfa, alfa, alfa), (x / 2) - (171), (y / 2) + 156, 0); 20 al_flip_display(); 21 al_rest(0.001); 22 } 23 } 24 25 if (events.type == ALLEGRO_EVENT_KEY_DOWN) 26 { 27 switch (events.keyboard.keycode) 28 { 29 case ALLEGRO_KEY_ENTER: 30 blinks = false; 31 break; 32 } 33 } 34 35}

The flashing and finishing the while(blinks) loop are working just fine but only
separately... When I play it, the bitmap just blinks forever no matter what I press.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Try and restructure your event loop a bit :

pseudocode :

while (!quit) {
   if (redraw) {
      Draw(bitmap , al_map_rgba(alpha,alpha,alpha,alpha));
      redraw = false;
   }
   do {
      ALLEGRO_EVENT ev;
      wait_for_event(q , &ev);
      if (ev.type == ALLEGRO_EVENT_TIMER) {
         redraw = true;
         if (glow) {
            alpha += 1;
            if (alpha >= 255) {glow = false;}
         }
         else {
            alpha -= 1;
            if (alpha < 1) {glow = true;}
         }
      }
      if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {quit = true;}
   } while (HaveEvents(q));
}

diverswife
Member #16,770
November 2017

Thanks! But what function do you mean by HaveEvents? (I'm new to programming, sorry)

torhu
Member #2,727
September 2002
avatar

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

diverswife
Member #16,770
November 2017

I get the pseudocode part, it just wasn't super intuitive, gee.

Thanks!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Okay. ;)

Something that should be pointed out is that logic should be kept separate from drawing as much as possible. By moving the alpha altering (logic) code out of the drawing section, you clean it up considerably.

Welcome to Allegro! Happy coding. ;)

Go to: