Do we really need a game loop?

Started by
13 comments, last by CJM 18 years, 11 months ago
Guys, I am very new to game programming. I have written a tetris clone in Java, but I did this without first reading articles/books on the subject. Now that I have done some reading, I know that the game loop is a fundamental game programming concept. But there is no game loop in the game that I wrote. Instead of constantly looping, my game simply waits for events to occur and then acts accordingly. Events can be either a keystroke (which causes the currently falling block to be moved/dropped/rotated) or a Timer event (which will cause the currently falling block to drop down one position ie. simulates 'gravity'). In either case, my game will process the event, check for collisions, update the score and spawn a new falling block at the top of the screen. It then goes back to sleep and waits for the next event. My question is, why do we actually need a game loop? Why are more games not designed using this event-driven model? I have given it some thought and the main problem I can see is that it wouldn't scale very well in more complex games where there are multiple entities in the game that are constantly moving or changing state. Since each entity would require a separate event (eg. a timer event), would the game perhaps become choked with events? I hope the question makes sense and sorry for being such a novice. Cheers M
Advertisement
Your tetris game does have a game loop, it's the timer.
Event-driven approaches can be used in games that don't require high frame-rates.
Constantly moving entities or entities that change their state often, don't necessarily need separate timer events, though.
One update-timer for fixed-step logic processing is sufficient.
The main reason for a game loop are fast response time and smooth rendering (e.g. high frame rates).
Timer cannot provide this, since event processing and rendering aren't always constant in their time consumption. Some events might take longer to process than the rate at which the timer event is sent. This will lead to synchronisation issues and frame-skips (e.g. timer event x needs 1/20 of the update rate too long to process, so timer event x+1 cannot be processed and only timer event x+2 is processed again).


HTH,
Pat
Quote:Original post by Chris81
Your tetris game does have a game loop, it's the timer.

On the lowest level this is true, but vore2005's point is the event-driven approach and not technical nitpicking [smile]
Your timer event is essestially a hack for an update() call to be called constantly. It adds unnecessary overhead to implement that through events. In no way is that better than a game loop.
Your game actually has a game loop, only the way input is processed is different. In the traditional game loop, the input (i.e. from user or from events) is asynchronous. This means that if there is no input, the loop continues and high frame rates can be achieved. In your design the input is synchronous. This means when there is no input the game waits for the next input to occur. Contrast
// Traditionalwhile( true )  if ( input )    process( input )  update()  render()

// Yourswhile( true )  while( !input )    ;  process( input )  update()  render()

It is quite alike, only the latter is more flexible. Apart from higher frame rates it also allows for more features. Suppose you want to run a timer in the screen at a certain rate (higher then your timer's rate). In your design you'd have to increase the timer rate. In the latter design it does not alter the global game loop.

Greetz

Illco
Thanks a lot for all your replies guys, you've been very helpful.

M
Quote:Original post by DrEvil
Your timer event is essestially a hack for an update() call to be called constantly. It adds unnecessary overhead to implement that through events. In no way is that better than a game loop.


Untrue.

vore2005's game will not drain the life from a laptop battery in the same way that a conventional event loop does. (my own laptop lasts three times as long if not running such programs)
"There is only one everything"
Quote:Original post by DrEvil
Your timer event is essestially a hack for an update() call to be called constantly. It adds unnecessary overhead to implement that through events. In no way is that better than a game loop.


Wrong. No update is being called constantly. His program is simply inactive until an event is available.

Yeah, the event-based model may be less resource-intensive. Unfortunately it's also unlikely to be as responsive and isn't much use when you need the screen (or indeed anything else, like AI) updating at very rapid intervals.

This topic is closed to new replies.

Advertisement