|
game loop |
mentalDisorder
Member #13,376
August 2011
|
How should a good game loop looks like ?
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 ? |
kazzmir
Member #1,786
December 2001
|
mentalDisorder said: 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
Major Reynaldo
May 2007
|
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 : 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}
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|