0

On the allegro wiki(http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Events) it shows this code.

    bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);

I get what the code does but why wait for an event or until 60 milliseconds is up? WHy not just one or the other?

user701329
  • 127
  • 2
  • 7
  • The tutorial is trying to be simple by not introducing timers. The timeout is just used in place of a timer ticking at 60fps. It needs to break out to draw the window, since it might be invalidated by the user if it is dragged off screen, minimized, etc. – Matthew Feb 29 '12 at 17:43

2 Answers2

1

If you waited for an event, that could be forever.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • But it doesn't do anything else otherwise right? What so bad about just waiting forever until an event comes? – user701329 Feb 28 '12 at 23:57
0

The question is answered by asking a simple question: what happens if the event never comes? Do you want your program to sit there waiting on something that won't happen?

Some events are more or less guaranteed to happen; them not happening means that bad things are going on. Not getting the v-sync event means that the display no longer exists. Or something. Not getting a timer event means that the Allegro timer system is broken (or someone set the timer to a really long duration). But some events may not happen at all.

The timeout is there in case the event is never fired. That way, you can detect it (via the return value) and do something about it.

What so bad about just waiting forever until an event comes?

Because a program needs to be responsive. Presumably you're using the Allegro Game Programming library to make a game.

Do you really want your game to stop for 2 minutes because of some event? Do you want your display to sit frozen on an image for 5 minutes, with the user not knowing what's going on? Or do you want to be able to pop up a dialog box saying, "Hey, I'm waiting for something. Hold on a bit." And then display an animated wait cursor.

Waiting forever is never a good idea.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982