Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A5 Event Queue

Credits go to Mark Oates for helping out!
This thread is locked; no one can reply to it. rss feed Print
A5 Event Queue
Otto Borden
Member #12,058
June 2010

Running through some tutorials and I'm wondering if a single event queue can have multiple sources? Can I store events from the keyboard and the display in the same event queue?

Mark Oates
Member #1,146
March 2001
avatar

Absolutely. For most applications, I recommend putting all your events into one queue.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Otto Borden
Member #12,058
June 2010

Sweet! Thanks

Dario ff
Member #10,065
August 2008
avatar

Actually, I would try splitting different events in different queues to avoid delay.

To sum up what SiegeLord said there, you should have one queue simply for timer events, and another one for whatever you need(input).

The reason is that some input events(for example, quit button on window) might get delayed because of the timer events.

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Evert
Member #794
November 2000
avatar

On the other hand, you don't want input events to pile up either.
Typically, I collect all events into a single thread, but drain the queue completely whenever any one event occurs.

Thomas Fjellstrom
Member #476
June 2000
avatar

Evert said:

Typically, I collect all events into a single thread, but drain the queue completely whenever any one event occurs.

Same here. It simplifies things a lot, and gets rid of any delay issues.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Mark Oates
Member #1,146
March 2001
avatar

Dario ff said:

To sum up what SiegeLord said there, you should have one queue simply for timer events, and another one for whatever you need(input).

That doesn't actually solve the problem, though. It just puts the 'trouble kid' in a different room so he doesn't bother anybody else.

You'll want to do something more like Evert suggested.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Otto Borden
Member #12,058
June 2010

So I should call pop on the event queue after performing the actions the event called for?

Thomas Fjellstrom
Member #476
June 2000
avatar

So I should call pop on the event queue after performing the actions the event called for?

What I do is use al_wait_for_event, which automatically always pops the event off the queue. And handle the event right away. and run in a loop waiting for the event queue to be empty.

see here for a simple example of what I mean.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Otto Borden
Member #12,058
June 2010

So as long as I use al_wait_for_event the queue will be automatically cleared?

Mark Oates
Member #1,146
March 2001
avatar

Here's how I do it:

Let's say you have a keyboard event source and display event source going into your event_queue:

#SelectExpand
1while(!game_over) 2{ 3 ALLEGRO_EVENT al_event; 4 al_wait_for_event(event_queue, &al_event); 5 6 switch(al_event.type) 7 { 8 case ALLEGRO_EVENT_KEYBOARD: 9 if (al_event.keyboard.key == ALLEGRO_KEY_ESCAPE) game_over = true; 10 else if (al_event.keyboard.key == ALLEGRO_KEY_R) { ... }; 11 break; 12 case ALLEGRO_EVENT_TIMER: 13 { 14 al_clear_to_color(al_map_rgb(0,0,0)); 15 // do stuff 16 al_flip_display(); 17 18 // other stuff... 19 20 // at the very end of this event: 21 // if the next event is another timer click, flush it out.
22 while (al_peek_next_event(event_queue, &al_event) && al_event.type == ALLEGRO_EVENT_TIMER)
23 al_drop_next_event(event_queue);
24 } 25 break; 26 default: 27 break; 28 } 29}

This assumes you're using only one timer. The key is line 22-23.
If you have more than one timer, then it's pretty much the same; you would flush out events that came from that timer.
I haven't tried to compile this code, btw. It might have errors.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Thomas Fjellstrom
Member #476
June 2000
avatar

So as long as I use al_wait_for_event the queue will be automatically cleared?

If you always drain all the events yes. If you aren't always fetching all events, then no.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: