How to: Draw a Sprite
This article demonstrates how to draw a sprite by using the SpriteBatch class.
To draw a sprite on screen
Derive a class from Game.
Define a SpriteBatch object as a field on your game class.
In your LoadGraphicsContent method, construct the SpriteBatch object in the loadAllContent section, passing the current graphics device.
Load the textures that will be used for drawing sprites in LoadGraphicsContent. In this case, the example uses the content member to load a texture from the XNA Framework Content Pipeline. The texture must be in the project, with the same name passed to Load.
private Texture2D SpriteTexture;
private SpriteBatch ForegroundBatch; private Rectangle TitleSafe; protected override void LoadGraphicsContent( bool loadAllContent ) { if (loadAllContent) { ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice ); SpriteTexture = content.Load<Texture2D>( "Sprite" ); } TitleSafe = GetTitleSafeArea( .8f ); }
After Clear, call Begin on your SpriteBatch object.
Create a Vector2 to represent the screen position of the sprite. On Xbox 360, you should be careful not to draw foreground sprites on the outer 10 or 20 percent of the screen, since some televisions may obscure the edge of the screen. The GetTitleSafeArea function calculates the area of the current Viewport that is safe, given a specified safety percentage.
protected Rectangle GetTitleSafeArea( float percent ) { Rectangle retval = new Rectangle( graphics.GraphicsDevice.Viewport.X, graphics.GraphicsDevice.Viewport.Y, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height );
#if XBOX
// Find Title Safe area of Xbox 360.
float border = (1 - percent) / 2;
retval.X = (int)(border * retval.Width);
retval.Y = (int)(border * retval.Height);
retval.Width = (int)(percent * retval.Width);
retval.Height = (int)(percent * retval.Height);
return retval;
#else
return retval;
#endif
}
Call Draw on your SpriteBatch object, passing the texture to draw, the screen position, and the color to apply. Use Color.White to draw the texture without any color effects.
When all the sprites have been drawn, call End on your SpriteBatch object.
protected override void Draw( GameTime gameTime )
{ graphics.GraphicsDevice.Clear( Color.CornflowerBlue ); ForegroundBatch.Begin(); Vector2 pos = new Vector2( TitleSafe.Left, TitleSafe.Top ); ForegroundBatch.Draw( SpriteTexture, pos, Color.White ); ForegroundBatch.End(); base.Draw( gameTime ); }
The Complete Example
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
public Game1()
{
graphics = new GraphicsDeviceManager( this );
content = new ContentManager( Services );
}
protected override void Initialize()
{
base.Initialize();
}
private Texture2D SpriteTexture;
private SpriteBatch ForegroundBatch;
private Rectangle TitleSafe;
protected override void LoadGraphicsContent( bool loadAllContent )
{
if (loadAllContent)
{
ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
SpriteTexture = content.Load<Texture2D>( "Sprite" );
}
TitleSafe = GetTitleSafeArea( .8f );
}
protected override void UnloadGraphicsContent( bool unloadAllContent )
{
if (unloadAllContent == true)
{
content.Unload();
}
}
protected override void Update( GameTime gameTime )
{
// Allows the default game to exit on Xbox 360 and Windows.
if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update( gameTime );
}
protected Rectangle GetTitleSafeArea( float percent )
{
Rectangle retval = new Rectangle( graphics.GraphicsDevice.Viewport.X,
graphics.GraphicsDevice.Viewport.Y,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height );
#if XBOX
// Find Title Safe area of Xbox 360.
float border = (1 - percent) / 2;
retval.X = (int)(border * retval.Width);
retval.Y = (int)(border * retval.Height);
retval.Width = (int)(percent * retval.Width);
retval.Height = (int)(percent * retval.Height);
return retval;
#else
return retval;
#endif
}
protected override void Draw( GameTime gameTime )
{
graphics.GraphicsDevice.Clear( Color.CornflowerBlue );
ForegroundBatch.Begin();
Vector2 pos = new Vector2( TitleSafe.Left, TitleSafe.Top );
ForegroundBatch.Draw( SpriteTexture, pos, Color.White );
ForegroundBatch.End();
base.Draw( gameTime );
}
}
See Also
Concepts
2D Graphics Overview
Xbox 360 Programming Considerations