Game Development Reference
In-Depth Information
The second collection of tasks is related to visualizing the game world to the
player. In the case of the Pacman game, this means drawing the labyrinth, the ghosts,
pacman, but also information about the game that is important for the player to
know, such as how many points he has scored, how many lives he has left, and so
on. This information can be displayed in different areas of the game screen, such
as the top or the bottom. This part of the display is also called the heads-up display
(HUD). Modern 3D games have a much more complicated set of drawing tasks.
These games need to deal with lighting and shadows, reflections, culling, visual
effects like explosions, and much more. We will call the part of the game loop that
deals with all the tasks related to visualizing the game world to the player the
Draw
method.
3.2.3 The Game Loop in XNA
In the first chapter of this topic, we have already seen how we can create a simple
game application by using the template provided on the website. The program that is
supplied with this template is given in Listing
3.1
. By looking at this program, you
can already see that there is a part of the code called
Update
(lines 26-28) and a part
called
Draw
(lines 30-33). These two parts correspond to the two main tasks of the
game loop: updating the game world and drawing the game world. Every game that
you are going to make in this topic follows the same basic structure. When you run
this program, the
Update
and
Draw
methods are continuously executed: update, draw,
update, draw, update, draw, update, draw, update, draw, and so on. Furthermore, this
happens at a very high speed. In the standard configuration of the game engine, it
tries to do exactly sixty runs through the loop in one second. We also call this kind
of loop a
fixed timestep
loop. It is possible to change this configuration so that, for
example, the game engine tries to execute the loop as many times as possible instead
of exactly sixty times per second.
Next to
Update
and
Draw
, game engines generally incorporate a few other useful
methods. The most important method is
LoadContent
. This method is executed once,
before the update-draw sequence of the game loop starts. The main thing that we do
in this method is loading any game assets that are needed, such as sprites, sounds,
or other files that the game needs. If we wanted to, we could also load the sprites
and the sounds in the
Update
or
Draw
method. However, this would significantly
affect the performance of our game, since we would load the files sixty times per
second. Game assets are better loaded only once into the internal memory. After that,
the
Update
and
Draw
methods read the information from the internal memory and
update it, or draw it on the screen. The
LoadContent
method also has a counterpart
UnloadContent
that is called after the game loop ends (which happens when the player
quits the game). In this method, you can clean up any memory that was allocated for
game assets. Finally, there is another (similar) pair of methods,
Initialize
and
Finalize
.
In
Initialize
you can perform initialization tasks needed for the game, such as setting
up an input device or opening a network connection. The
Finalize
method can be