Datasheet
The following are the three most important methods in the game class (see Figure 1-5). For the final
release, Microsoft also added the
LoadGraphicsContent and UnloadGraphicsContent helper meth-
ods by default to the
Game1.cs class, which is created automatically for you when you create a new
XNA project, but in most samples of this book these methods are not used because it is much simpler
to have all initialization and loading code at one place in the
Initialize function.
❑
Initialize ()
❑ Update (GameTime time)
❑ Draw (GameTime time)
Figure 1-5
You can probably already guess what all these do.
Initialize loads all your game content, sets all your
startup settings, and initializes everything you need. If you want to follow the design patterns Microsoft
provides for XNA, you would do all the loading in the
LoadGraphicsContent method. Update is called
before each frame is drawn to update your game time, input, sound, and everything else that is not visible
on the screen. If your game is GPU-limited, it can very well happen that
Update is called more often than
Draw, but your update code should run independent of the drawing code anyway. None of the samples
in this book will need special care for the number of times
Update and Draw are called. And finally, Draw is
called each frame to draw everything to the screen. The separation of
Update and Draw might not always
be important and can almost always be ignored for unit tests, but for the final game it is important to make
sure the game logic runs independent of the draw code. For example, on the Windows platform, the user
could press Alt and Tab or minimize the game, in which case Draw does not need to be called anymore. Even
with Draw not called anymore you may still want the game to continue to run in the background via the
Update method. This is especially important for network games to make sure the player is still synchronized.
Additionally, you can add GameComponent classes to your game class, which again have an Update and
a
Draw method. Both of these methods are automatically called from your game Update and Draw meth-
ods. The initialization can happen directly in the constructor there. Initially Microsoft wanted the devel-
opers to create and add game components with the designer of Visual Studio, which can be seen in the
first beta of XNA Game Studio Express (released August 30, 2006). The designer feature was later removed
because it did not work well, was not supported for the Xbox 360 platform, and because not many devel-
opers used it anyway.
The idea with the game components is to reuse parts of your code and make it very easy to just plug
them into your games. Examples of game components include a frame counter or maybe a sky cube
mapping renderer for the 3D background. In my opinion, there are two major drawbacks: No standard
game components are shipped with XNA, and it is not really hard to code such an application model
yourself and even extend it. I do not use many
GameComponent classes in this book, but feel free to plug
them in on your own. Read Chapter 4 for more details of the
GameComponent class and learn about its
advantages and disadvantages. Because the game class has a
Components property, it is very easy to
add more components.
7
Chapter 1: Introducing XNA
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 7