You’ve done a lot of hard work, but your program
still can’t draw any pictures. If you run the solution that you’ve
created, you get the familiar blue screen. Next, you have to write some
C# code that fetches the image resource and draws it on the screen at a
particular location.
1. Loading XNA Textures
Within XNA, images that you want to draw in your games are called textures.
Textures can be drawn as flat pictures, and they can also be wrapped
around 3-D models. You’ve already seen how to use the XNA Color
type, which lets you manipulate color information. Now you’ll use
another type of XNA data variable so that you can work with your picture
as a texture. XNA provides a range of types that are used to deal with
textures. The type you’ll use is called Texture2D. This holds a texture that you manipulate in two dimensions; that is, it is drawn on the screen as if it were a flat surface.
You use the same program
structure that you used for previous games. Members of your game class
represent the "game world." These are updated by the UpdateDraw method to draw the output. The game data takes the form of a single variable that holds the texture, as shown here: method and used by the
// The Game World
Texture2D jakeTexture;
The Draw method draws this texture on the screen, and you could use the Update method to make the image move around the screen by changing the draw position.
You also can use another method that lets the program take control when the graphics need to be loaded. Figure 1 shows how this works. It shows that there is also a LoadContent method that is called by XNA when a game starts running.
You can think of LoadContent
as another person in the Game1 office. That person has his or her own
telephone. When the phone rings, that person must load all the content
and make it ready for use, as follows:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
In addition to loading the content that the game needs, the LoadContent method also creates a SpriteBatch for the program to use. You will use this later to draw the texture on the
screen. You’ve even been given a comment to tell you where to place the
code that loads your texture. This is the place where the program must
ask the Content Manager to fetch the texture:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
jakeTexture = this.Content.Load<Texture2D>("jake");
}
When the game starts, XNA calls the LoadContent method to fetch content for use in the game. The method then performs the statement that loads the texture content:
jakeTexture = this.Content.Load<Texture2D>("jake");
The Load method is a kind of multipurpose tool called a generic method. Because it’s generic, it can be used to fetch any kind of item, from textures to audio files to 3-D models. You tell Load to fetch a Texture2D
by placing the name of the type you want after the method name. You
then give the method the asset that you want it to fetch. If you select
the Jake.jpg item in Solution Explorer,
and then look in the XNA Game Studio Properties pane (which should be
in the lower right of the XNA Game Studio window), you can see that the
asset name has been taken from the file name of the resource. Figure 2 shows the property information for the Jake image resource.
This property information
tells XNA Game Studio where the image file is located, what to do with
the file when the project is built, and the name to use in the program.
So, once the Load method has completed,
you have a copy of the image in the texture in your game. If the game
had lots of different images, you would declare additional Texture2D items in your game world and assign them to textures using the LoadContent method as well.
If
you get the name of the texture wrong, the game program fails in this
method, as it is looking for an asset that is not there. The program
fails by throwing an exception. Figure 3 shows the error that is produced if the asset name of a content item is incorrect.
Later, you’ll find
out how to take control when things go wrong like this; for now, you
should make sure that the asset name you use in the call of Load matches the name of the content item.
Note:
Our Great
Programmer spends a lot of time worrying about things that might go
wrong. She figures that in a commercial application, such as one that
might be used in a bank, she has to write at least as much program code
to deal with all the potential errors as she writes to perform the
actual job. Game programs are probably not as critical as bank code in
that, if they go wrong, nobody actually loses any money, but if a game
constantly crashes, it will never become popular. Later, you’ll see how
to make sure that your program fails as seldom as possible.