1
\$\begingroup\$

I am trying to break a tileset image from my content folder into individual tiles during program. What I would like to have happen is that i can fill a list (of thing) with each possible tile so I can reference it by calling list(x).

However, in XNA I can't access the bitmap class and I can't Import it either (I might be doing it wrong) nor can I create a texture2d since where this is being called at comes from a different class file.

How do I split apart the tileset image into the separate tiles that I need and store them into a list?

\$\endgroup\$
1
  • \$\begingroup\$ You should definitely be able to use the bitmap class in XNA. Do you have "using System.Drawing;" at the top of your file? Also, check the references on your project for System.Drawing as well. You might have to add it if it's not there. Also, it should be fine to create a texture2D from wherever you are. What is stopping you? You can pass the spritebatch to whatever class needs it. \$\endgroup\$
    – tandersen
    Commented Oct 16, 2015 at 5:40

2 Answers 2

1
\$\begingroup\$

Instead of breaking up the tileset in individual tile textures you should keep a list of rectangles that make up the tiles. The Draw function has a source rectangle parameter that cuts out the part of the larger texture for you to use. So instead of a List<Texture2d>() use a List<Rectangle>() and have the SpriteBatch.Draw() use the source texture and rectangle as parameters.

The main advantages here are:

  • Rectangles take up less resources (you could use a tile class that holds more tile properties than just the rectangle).
  • Performance wise the tileset works as an sprite-atlas: this greatly reduces the required number of draw calls on your GPU because there is less texture switching required (all draw calls that use the same texture can be bundled).

Generally speaking there are very, very few reasons to split up a tileset in indivisual bitmaps and generally shouldn't be done. You'll notice that many games use sprite atlasses (or even generate them from individual sprites) instead of breaking them back up to separate bitmaps.

See also:

Especially tilemaps benefit greatly from having everything in one texture. Learn about atlasses and how to use them, it probably solves your problem.

\$\endgroup\$
0
\$\begingroup\$

I'm using this extention method to cut part of any texture:

public static class Texture2DExtention
{
    public static Texture2D GetPart(this Texture2D pSourceTexture, Rectangle pSourceRectangle)
    {
        Texture2D texture = new Texture2D(pSourceTexture.GraphicsDevice, pSourceRectangle.Width, pSourceRectangle.Height);
        Color[] data = new Color[pSourceRectangle.Width * pSourceRectangle.Height];
        pSourceTexture.GetData<Color>(0, pSourceRectangle, data, 0, data.Length);
        texture.SetData<Color>(data);
        return texture;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .