[A5] Clearing Keyboard Buffer/Event Queue
Neil Roy

I have been working on a game using Allegro 5, and so far I love it. When playing my game, and my character dies and runs out of lives, the game displays a GAME OVER message then goes back to the main title screen which awaits the player to press any key to continue.

The problem is that sometimes it will display the title screen and then quickly skip past it because the player was holding down an arrow key when they died. Even though they let go of the key before a title screen is displayed, it still seems to be buffered, or in the event queue. A similar problem happens once in a blue moon at start up. The character will show up as already moved off their starting spot, I think due to the same problem.

I want to know is there a way I can clear the keyboard event queue? I have inserted code to wait in a loop if while a key is being pressed, but this will cause possible delays and seems like a hack when clearing the queue/buffer would be a more eloquent solution.

This is what I have been inserting so far...

   do {
      al_wait_for_event(event_queue, &event);
   } while(event.type == ALLEGRO_EVENT_KEY_DOWN);

Any ideas? Or am I stuck with the above code?

Thomas Fjellstrom

You can just drain the entire queue ( al_flush_event_queue ). You probably don't care one bit about the events queued up once you hit the game over screen.

also you don't get repeated key down events if a key is held. you get one when the key is pressed, and then a key up event when it's released. you get KEY_CHAR for repeats IIRC.

Neil Roy

Thanks for that, I'll give it a shot.
This should be much cleaner and there will be no chance the game will hang if a key is held down or whatever.

Dizzy Egg

You probably want to ignore the release events if you're in the 'game over' state. My guess is they're holding a key, then releasing it on game over triggers the code in the release event part; as Thomas said, holding down a key will only trigger an event once, when it's first pressed, it wont keep oscillating.

Neil Roy

Ah, okay, that's good to know. I wasn't certain if it would queue up several keys or what.

Thomas Fjellstrom

This is the nice thing about events. You get one DOWN even for a key going down, and one event for a key coming UP. No more, no less. no more having to guess at it with key[].

Thread #612299. Printed from Allegro.cc