Problem with game loop

As usual I have been away for a while and now that I have some free time I have been tinkering with my game. However I am at a loss as to what is going on with my events and update/redraw (The game is a top-down adventure game like Zelda)

My game loop boils down to the following:

1 while(!doExit) 2 { 3 al_wait_for_event_until(event_queue, &ev, &timeout); 4 5 switch(ev.type){ 6 case ALLEGRO_EVENT_TIMER: 7 if(ev.any.source == al_get_timer_event_source(logicTimer)) 8 { 9 doUpdate(); 10 } 11 else if(ev.any.source == al_get_timer_event_source(animTimer)) 12 { 13 redraw = true; 14 } 15 break; 16 case ALLEGRO_KEY_DOWN: 17 //key presses etc.... 18 } 19 20 if(redraw && al_is_event_queue_empty(event_queue)) 21 { 22 redraw = false; 23 plot(); 24 } 25 26 }

Which as far as I can see is the same as what other discussions have suggested as a game loop (not just here on this forum). Now I run the animation at 60 FPS and the logic at 120 'clicks' per second (the animTimer and logicTimer respectively).

However, and this is the problem: quite frequently the game 'jumps'. What happens is my character is walking along in a room and then suddenly ends up across the other side of the room in the direction he was walking collided with the wall. It is as if his speed has suddenly gone through the roof. This also affects enemies too. Now I know that the logic 'ticks' are constant as is the redraw timer's, as well as the character's speed (as I print these on the screen for debugging). I also know that even if I reduce the logic updates to 10 times a second I still get the same effect!

The only thing I can think is that the game is updating far more often than it should. I know this because when I print a counter of the number of updates it runs OK when everything is fine, but runs really quickly when I see these jumps.

What can cause the game to speed up so quickly all of a sudden? Is there a glaring error in my code or something I missed? Any help would be appreciated!


If al_wait_for_event_until fails, it will return false, and the event will hold it's previous value. Just use al_wait_for_event if you're not planning to check the return value.

Also, this forum uses XML-style tags, not BBCode like the forum, see the "Formatting Help" button on how to structure code.


Sorry I submitted the post accidentally before I completed it and previewed it :$!


Thank you! That's solved the problem that has been bugging me for ages!:D


nvm, thought you hadn't fully read my post ;)

Thread #612270. Printed from