Article Index
Introduction
Game Engine
Game Logic
Modding

Game Engine

A "game engine" is the base of code that allows a programmer to display images, play sounds, get input, etc.  It contains everything needed to manifest a game, but it doesn't contain any game-specific logic: Game engines are inherently generic.  Many different kinds of games can be created with a given engine, and the same is true of the "Dr. Dobb's Challenge" game engine.  It could be used to create a platform game, an RPG, a shooter, or anything else that involves 2D graphics, sounds, and input.

2008_02_25_screen3.jpg

The core of the game engine is the game loop; it can be found in the main.cpp file, within the WinMain function.  The game loop is what dictates the game's frame rate; in the "Dr. Dobb's Challenge" game, a fixed frame rate is used.  You'll see that the game loop waits until a specified number of milliseconds have passed before allowing the game logic to process another frame.  Using a fixed frame rate is not required; many other games simply display as many frames per second as the processor can handle.  However, using a fixed frame rate can make game logic programming somewhat simpler, which is why it is employed here.

Within the game loop, you'll see that various calls are made.  These ensure that input is received and that game logic and graphics are updated each frame.  It is these repeated calls to input, game logic, and graphics that drive the rest of the game code.

The game engine displays graphics via its sprite class, which is found in the sprite.cpp file.  The game engine also displays on-screen text via its text rendering system, found in the text.cpp file.  The game engine plays WAV sound effects via its sound class, which is found in the sound.cpp file.  The game engine's DirectX code could be extended to provide the capability to play music files, too.

On startup, the sprite class's static Init method loads all of the game's image files from the hard drive, and stores them in memory.  On shutdown, the sprite class's static Release method unloads all of the game's images from memory.  Instances of the sprite class can be created in order to display animated sequences of images, move images around the screen, hide images, pause animation, etc.

On startup, the sound class's static init method loads all of the game's sound files from the hard drive, and stores them in memory.  On shutdown, the sound class's static Release method unloads all of the game's sound effects from memory.  Instances of the sound class can be created in order to play individual sound effects.

The game engine receives mouse and keyboard input via its input class, which is found in the input.cpp file.  The input class's Update method checks the current state of the keyboard, and stores that information for future use.  Other parts of the code may request this keyboard data from the input class via its KeyDown and KeyPressed methods which report whether or not a key is being held down, and whether or not it was pressed during the current frame.  Similarly, the Update method also checks the current state of the mouse, and stores that information for future use.  Mouse state information can be requested by other parts of the code via the MouseButtonDown and MouseButtonPressed methods.  The input class is also responsible for updating the X/Y location of the mouse cursor, and ensuring that the mouse cursor does not leave the confines of the game window, when the game is played in windowed mode.




DDJ