Games are real-time programs. This means that the game should run all the time and not wait for the user to respond. If the user does respond by pressing a key or button, then the game will update based on the user input. A game is also known as a “state machine” – it needs to check if the user pressed a button to show the menu, is the game on level 1 or level 2, is the game paused or running, etc.
A timer is needed, otherwise the game will run either too fast or too slow depending on the computer hardware. The timer will make sure the game will run the same on all systems. The game also needs to allow messages to go back to the operating system (Windows, Mac OS, iOS).
The game loop with a timer will act as follows:
1. Show the main menu and get user to start the game
2. Initialise the game variables, setup memory, menus, etc.
3. Now start the game and do the following:
3.1 Check if the operating system (Windows, Mac OS, iOS) has any messages for us to process
3.2 Get user input (from keyboard, mouse, game controller, touchscreen, etc)
3.3 Process game logic (maths, physics, update to score, etc go here)
3.4 Draw the graphics scene of the game to a memory buffer (not video)
3.5 When this is ready, quickly show it to the screen
3.6 Wait a little while (fix it at a number frames per second (FPS) using a timer – eg 60 FPS)
3.7 If the game is not finished or user does not quit, go to step 3.1
4. Once the user quits the game, do a cleanup and free memory allocated
5. Either go back to step 1 or exit to the operating system (Windows, Mac OS, iOS)