Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » game loop

This thread is locked; no one can reply to it. rss feed Print
game loop
mentalDisorder
Member #13,376
August 2011

How should a good game loop looks like ?
I mean I've got something like:

#SelectExpand
1 while(1) 2 { 3 ALLEGRO_EVENT ev; 4 bool get_event = al_wait_for_event_timed(event_queue, &ev, 0.05f); 5 6 if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 7 break; 8 9parseEvent(ev); 10 11// game logic and stuff 12 }

but let's assume that game logic takes time, lets say 1/4 second. If in that time user press a button or generate any other event it'll be missed ?
Or the event will be generated and will set get_event on true next time al_wait_for_event will be executed?

kazzmir
Member #1,786
December 2001
avatar

lets say 1/4 second

That is an extremely long time for most game loops. Most likely your game loop will take less than 10ms to execute.

Quote:

Or the event will be generated and will set get_event on true next time al_wait_for_event will be executed?

The events will sit in the queue until you get them out, so to answer your question, yes.

mentalDisorder
Member #13,376
August 2011

after testing couple of function I guess that al_wait_for_event_until(...) is best option, right?

Edgar Reynaldo
Member #8,592
May 2007
avatar

There is an example on the wiki somewhere, but this is better than using a timed wait because it always waits until there is an event, processes all the remaining events, and draws when the timer goes off :

#SelectExpand
1 2while (!quit) { 3 do { 4 ALLEGRO_EVENT ev; 5 al_wait_for_event(queue , &ev); 6 7 game.ProcessInput(&ev); 8 if (ev.type == ALLEGRO_EVENT_TIMER) { 9 game.UpdateLogic(1.0/FRAMES_PER_SECOND); 10 redraw = true; 11 } 12 else if (ev.type == ALLEGRO_EVENT_BLAH2) { 13 // do other stuff 14 } 15 } while (!al_is_event_queue_empty(queue)); 16 if (redraw) { 17 redraw = false; 18 game.Display(); 19 } 20}

Go to: