Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Quickly Draw Lots of Tiles by Saving Them to a Texture First
Date 3/10/2009    Tags XNA    (0)

My game is heavily tile based. I use 16x16 tiles for all the foreground images along with 32x32 tiles for a background layer. For most levels in my game, drawing this was fine. But a select few that have a lot of tiles are slow during this process. This is because there are often several hundred tiles that my game has to draw.

Since these tiles only change when a level is first loaded, I determined that I could save a Texture2D representation of all my tiles and then just draw that each iteration through my game rather than looping through all the tiles.

First, I created a Tile class to hold my tiles. It's pretty basic, just location, size, and texture information. I populate each Tile from a file and pass in a character that determines which texture gets assigned to the tile.


public class Tile
{
    public enum TileStatus
    {
        Visble,
        Hidden
    }

    private Texture2D tileTexture;

    public int X
    {
        get;
        set;
    }

    public int Y
    {
        get;
        set;
    }

    public int TileWidth
    {
        get;
        set;
    }

    public int TileHeight
    {
        get;
        set;
    }

    public TileStatus currentStatus
    {
        get;
        set;
    }

    public Tile(char initialTileID, ContentManager contentManager)
    {
        TileHeight = 32;
        TileWidth = 32;

        switch (initialTileID.ToString())
        {
            case "1":
            {
                tileTexture = contentManager.Load<Texture2D>("tiles\\GrayTile");
                break;
            }
            case "2":
            {
                tileTexture = contentManager.Load<Texture2D>("tiles\\MaroonTile");
                break;
            }
            default:
            {
                tileTexture = null;
                break;
            }
        }
    }

    public virtual void Draw(SpriteBatch spriteBatch)
    {
        if (this.currentStatus == TileStatus.Visble)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(tileTexture, new Rectangle(X, Y, TileWidth, TileHeight), Color.White);
            spriteBatch.End();
        }
    }
}
Additionally I created a collection object so that I could group these tiles together as one and call a single draw method to draw them all.

public class Tiles : System.Collections.ObjectModel.Collection<Tile>
{
    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < this.Count; i++)
        {
            this[i].Draw(spriteBa
                    
                    


This article has been view 3589 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel


Tags
ASP.net (18)  Fin (1)  Video Games (7)  Game Dev (11)  Abduction Action (1)  WP7 (8)  Visual Studio (1)  Hypership (28)  Advise (14)  C# (14)  FIN (20)  World of Chalk (2)  Absurd (2)  Abduction Action! (27)  Nasty (34)  PC (1)  Cool (2)  Sports (11)  Rant (50)  VolChaos (1)  Development (13)  Design (2)  Volchaos (11)  XNA (40)  Nastier (4)  Xbox (1)  iOS (3)  SQL (1)  XBLIG (32)  Trivia or Die (3)  Web (19)  Trivia Or Die (1)  Abdction Action! (1)