The main game loop handles three major tasks:
- Get user input
- Update the game state
- Draw the game
A simple game loop just mushes these three tasks into one while loop. This has some undesired results:
- Game runs at different speeds on different computers.
- CPU is stuck at 100% usage.
- "Game states"/menus are missing or mixed with game code.
- Main game loop is very long and hard to maintain.
- Code is difficult to extend/port to other platforms.
Advanced gamed loops address the issues listed above. Here are some useful articles:
For an excellent example game loop, take a look at the Allegro skater demo game:
Game loops often do the same type of work for most games, so I have been thinking of a way to make a generalized game framework. It is better to write one implementation of a game loop and share it between games. It saves work when creating a new game, and improvements to the shared game loop can be shared by all games (for example, adding a FPS counter or screen capture feature).