In game development, a “sprite” is simply a 2D image place somewhere on the screen. They generally make up the majority of a 2D game, and in a 3D game they may be used for the targeting reticule, the heads up display, etc. We are going to create a simple Sprite class that will handle drawing an image on screen. We will improve this class later on by allowing it to rotate, change size, etc. The basic Sprite class is written as follows:

public class Sprite : Component
{
    Texture2D spriteTexture;
    string filename;

    Vector2 position = Vector2.Zero;

    public float X
    {
        get { return position.X; }
        set { position.X = value; }
    }

    public float Y
    {
        get { return position.Y; }
        set { position.Y = value; }
    }

    public int Width { get { return spriteTexture.Width; } }
    public int Height { get { return spriteTexture.Height; } }

    public Sprite(float x, float y, string filename)
    {
        this.position = new Vector2(x, y);
        this.filename = filename;
    }

    protected override void Load()
    {
        spriteTexture = Parent.Engine.Content.Load<Texture2D>(filename);
    }

    public override void Draw()
    {
        Parent.Engine.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None);
        Parent.Engine.SpriteBatch.Draw(spriteTexture, position, Color.White);
        Parent.Engine.SpriteBatch.End();

        // Set back certain render states that are changed by the SpriteBatch
        // that interfere with 3D rendering
        resetRenderStates();
    }

    private void resetRenderStates()
    {
        Parent.Engine.GraphicsDevice.RenderState.DepthBufferEnable = true;
        Parent.Engine.GraphicsDevice.RenderState.AlphaBlendEnable = false;
        Parent.Engine.GraphicsDevice.RenderState.AlphaTestEnable = false;
    }
}

Let’s try our new Sprite class out. Because we all need more puppies in our lives, we will draw the following onto the screen:

Save the picture, and drag it into the “Content” directory of your demo game project. We can then modify the Game1 class to create a new instance of the engine, load up a game screen, and create a sprite.

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;

    Engine engine;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void LoadContent()
    {
        engine = new Engine(graphics);

        GameScreen screen = new GameScreen();
        engine.PushGameScreen(screen);

        Sprite sprite = new Sprite(10, 10, "Content/puppy");
        screen.AddComponent(sprite);
    }

    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);

        engine.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        engine.Draw(gameTime);

        base.Draw(gameTime);
    }
}

Download the Code for this Chapter

« »