Game Loop
A game loop is a continuous loop that runs an Update() method and a Draw() method. Below is the simplest example of a game loop:
void GameLoop()
{
while (true)
{
Update();
Draw();
}
}
In practice game loops may have a lot more code. They may wait a certain amount of time between passes, do less processing on certain passes than others, contain exit logic, etc.
Simple Update and Draw
In the simple example above, the game updates and draws at the same rate.
void GameLoop()
{
while (true)
{
Update();
Draw();
}
}
This was a common way to design video games when they were first made, and came with the following pros and cons:
- Pros
- Even when the game slows, down physics and game logic are still processed in a consistent manner.
- Cons
- If either the
Update()or theDraw()took too much time, the game would slow down. - Game may not run at full speed on weaker hardware.
- If either the
Timesteps
There are two main types of game loops timesteps:
- Fixed Step - the loop processes equal intervals of game time
- Variable Step - the loop processes varying intervals of game time