3. Sprite Drawing with SpriteBatch
You now have all the information about your sprite and are ready to draw it. Next, you need to take control in the Draw
method and put your image onto the screen. But before you can do the
drawing, you need to take some time out and discover more about how game
consoles work.
A modern game console
is not one powerful computer; in fact, it is several. Some of these run
the game itself, whereas other special graphics processors drive the
display. The graphics processor unit (GPU) contains optimized hardware
to allow it to update the screen as fast as possible. When the Draw
method runs, the method assembles a bunch of instructions for the GPU
and sends the instructions into the GPU. The GPU then follows those
instructions to put a picture on the screen. Complex games contain many
images that may be drawn at several different positions on the screen.
It is important that the transfer of the position information and
associated images is organized as efficiently as possible. XNA provides a
special class called SpriteBatch to batch up a set of sprite-drawing instructions. Your program calls methods on a SpriteBatch variable to get the drawing done. This means that a SpriteBatch needs to be created for the program to use. When XNA Game Studio creates a new project, it adds the statements to the LoadContent method that create a SpriteBatch for you to use. The variable is called spriteBatch.
Note:
It might look as if you
have two items with the same name in your program. However, if you look
carefully, you see that the class SpriteBatch starts with an uppercase S, but the spriteBatch
variable starts with a lowercase s. This works because the C# compiler
considers the case of the letter as significant in an identifier. In
other words, your program could have two variables, Fred and fred, and they would not be confused.
Now you can use spriteBatch to draw the sprite. You must tell spriteBatch when you’ve started drawing sprites and when you’ve finished:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(jakeTexture, jakeRect, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
You call methods on the spriteBatch variable to begin the draw process, draw the sprite, and then end the drawing. The Draw method is part of the SpriteBatch
class and is given parameters that identify the image to be drawn, the
rectangle to place it in, and the color of the light to "shine" on the
texture.
Note:
The game class contains a Draw method, which is used to draw the entire game. The SpriteBatch class also contains a Draw
method, which is used to draw textures. Although the methods have the
same name and are both involved in the draw process, they actually do
different things. However, both are performing a drawing operation in
their own way, so it is appropriate for the designers of XNA to call
them Draw methods.
If you put a program
together with the previously described methods, you can finally run a
program that will display an image on the screen.
Figure 5 shows the output that you get when you run your program to display Jake on the screen.
If you change the content of the file Jake.jpg, you can make this program display other pictures.
If you run this
program on a Zune device you will find that the picture is too large to
fit on the Zune display, which is limited to 240 by 320 pixels. This
will not cause XNA to report an error, but not all of the image will be
displayed. You can fix this by reducing the width and height of the
rectangle to 180 and 120 respectively. This preserves the aspect ratio,
but makes sure the image will fit on the screen.