Summary
Real-time games update many times per second via a loop called the game loop. Each iteration of this loop is a frame. For example, 60 frames per second means that there are 60 iterations of the game loop per second. The game loop has three main phases that it completes every frame: processing input, updating the game world, and generating output. Input involves not only input devices such as the keyboard and mouse but networking data, replay data, and so on. Outputs include graphics, audio, and force feedback controllers.
Most displays use raster graphics, where the display contains a grid of pixels. The size of the grid depends on the resolution of the display. The game maintains a color buffer that saves color data for every pixel. Most games use double buffering, where there are two color buffers, and the game and display alternate between using these buffers. This helps reduce the amount of screen tearing (that is, the screen showing parts of two frames at once). To eliminate screen tearing, you also must enable vertical synchronization, which means the buffers swap only when the display is ready.
For a game to work properly at variable frame rates, you need to write all game logic as a function of delta time—the time interval between frames. Thus, the “update game world” phase of the game loop should account for delta time. You can further add frame limiting to ensure that the frame rate does not go over some set cap.
In this chapter, you have combined all these different techniques to create a simple version of the classic video game Pong.