Game main loop logic

1.7k Views Asked by At

I'm writing a game in c++ using allegro 5. Allegro 5 has events which are stacked in an event queue(like mouse clicked or timer ticked after 1/FSP time). So my question is how should be the logic of the main loop of my game, or since it's event based I can implement it without the main loop??

Any ideas how real games do it? Links will be good.

2

There are 2 best solutions below

0
On BEST ANSWER

I don't have any experiences with Allegro too but logic would be the same.

(so called) Real games also have game loops but the diference is they use threads which are working parallel but within different time intervals. For instance there are different threads for physic calculations, AI, gameplay, sound, rendering... as user events are usually concers gameplay events are getting collected before it (as Max suggests) and consumed until the next frame (actually some collects it in for instance 5 frames).

As a frame might get too long, all the events coming from OS are getting collected by the game for that reason these inputs are called buffered input. There is also one another method which is called unbuffered input which doesn't work discrete but instead you test it during gameloop at the very instances it is queried.

If the user input is very important and you dont want to loose any inputs at all then you can use buffered otherwise unbuffered. However unbuffered might be tricky especially during debug.

here are some links
book excerpt game engine
Game Loops on IOS

0
On

I have no experience with Allegro, but when using SFML and rendering a game with OpenGL, I myself poll the event queue as part of my main loop. Something like the below pseudo-code (but more abstracted):

while(game_on)
{
   auto events = poll_occured_events();
   for_each(events, do_somewithng_with_event);

   render_game();
}

Seems to work fine so far... I'd guess something similar is possible in Allegro. Event driven games are tricky, since you need to continually update the game.

You could (possibly) have the main loop in another thread and then synchronize between event thread and game thread...