Game Development Reference
In-Depth Information
Chapter 7
by David “Rez” Graham
Controlling the Main Loop
Every game has a series of operations that run over and over to present and update
the game to the player. This is the heartbeat that lets you know the game is alive.
Games are unlike many forms of software in that even if the player does absolutely
nothing, the game still needs to be constantly thinking and processing. A typical
main loop may receive and process player input, run creature AI, update animations,
update the physics system, run any world simulation that needs to happen, render
the scene, and play music and sound effects. Every main loop is different and tailored
for each individual game. All of these operations occur in one giant loop that can
t
take longer than 33ms per iteration (or 30 iterations per second) at a minimum.
When you exit the main loop, your game shuts down
This is very different than your typical Windows program. Most Windows programs
run a message pump designed to sit there doing nothing until the application receives
an event. It does absolutely no processing until the user triggers something. This
won
'
t work for a game, which will happily go about processing and rendering regard-
less of player input. Even a chess game needs to be allowed to run its AI while the
player is considering his move.
'
Organizing the Main Loop
There are many ways to organize the main loop, and each game has its own tech-
nique. In this chapter, we
'
ll look at a number of different possibilities.
175
 
 
 
 
Search WWH ::




Custom Search