MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 3) - Sprite Drawing with SpriteBatch

4/18/2011 11:47:03 AM

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.

Figure 5. Displaying Jake on a PC screen

Other  
  •  iPhone 3D Programming : Holodeck Sample (part 5) - Overlaying with a Live Camera Image
  •  iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
  •  iPhone 3D Programming : Holodeck Sample (part 3) - Handling the Heads-Up Display
  •  iPhone 3D Programming : Holodeck Sample (part 2) - Rendering the Dome, Clouds, and Text
  •  iPhone 3D Programming : Holodeck Sample (part 1) - Application Skeleton
  •  Building LOB Applications : Printing in a Silverlight LOB Application
  •  Building LOB Applications : Data Validation through Data Annotation
  •  Building LOB Applications : Implementing CRUD Operations in RIA Services
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 2) - Adding Resources to a Project
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)
  •  iPhone 3D Programming : Blending and Augmented Reality - Rendering Anti-Aliased Lines with Textures
  •  Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
  •  Programming with DirectX : Game Math - Bounding Geometry (part 1) - Bounding Boxes
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  
    Most View
    Golden Media Spark One - Plenty To Offer Out Of The Box (Part 1)
    Introducing UEFI BIOS (Part 2)
    How To Automate Your Web With ifttt (Part 1)
    Corsair Carbide 200r - Joy To Build
    Panasonic Lumix DMC-SZ9 - Lots Of Smart Features In A Very Small Camera
    HTC One - A Huge Leap For Android Smartphones
    Windows Server 2003 : Advanced Backup and Restore (part 1) - Backup Options, The Ntbackup Command
    Linux vs Windows 8 (Part 5)
    Gigabyte Osmium Aivia Mechanical Keyboard
    The Complete Guide To Photography On Your Mac! (Part 2)
    Top 10
    Does Microsoft Have An Image Problem? (Part 2)
    Does Microsoft Have An Image Problem? (Part 1)
    Time For A Bigger iPhone?
    99 Mac Secrets (Part 5) - Top ten third-party apps
    99 Mac Secrets (Part 4) - iMovie secrets, GarageBand secrets, iWork secrets
    99 Mac Secrets (Part 3) : Safari secrets, Mail secrets, Safari shortcuts, Mail shortcuts, iPhoto secrets
    99 Mac Secrets (Part 2) : Customizing, Best menu bar add-ons, Quick Look secrets
    99 Mac Secrets (Part 1) : General OS X tips, Security tips, System shortcuts
    iMovie Trailers And Audio Premastered
    PowerTraveller Powerchimp 4A Battery Charger