Search code examples
c#xnatile

XNA tilemap tileset


I'm making a 2d sidescrolling game and im trying to use a 2dimensional or so-to-say tileset. My friend gave me some code that he had been using for his games but his code only supports 1 dimension.

   public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            for (int y = 0; y < map.GetLength(0); y++)
            {
                spriteBatch.Draw(tilesheet, new Vector2((x * 64) , (y * 64)), new Rectangle(map[y, x] * 64, 0, 64, 64), Color.White);
            }
        }
    }

this is where the problem appears, because it's a group project and we recieve graphics from a graphics designer and we have too many tiles(64x64 in size) to fit into one row of texture that is supported for xna(4096 pixels i think). I barely understand the code that my friend gave me and that's probably where the issue comes from. If anyone knows how to make the draw method support 2 dimensions so that i can use tilesets the are the dimensions(512x512 as an example) instead of 64x4992(which my current tileset is at).

    public Level()
    {
        map = new int[14, 50]
        {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,18,19,20,21,22,23,24,17,18,19,20,21,22,23,24,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31,32,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,40,33,34,35,36,37,38,39,40,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,42,43,44,45,46,47,48,41,42,43,44,45,46,47,48,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,5,0,0,0,0,49,50,51,52,53,54,55,56,49,50,51,52,53,54,55,56,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,58,59,60,61,62,63,64,57,58,59,60,61,62,63,64,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,11,0,0,0,0,0,0,0,0,0,65,66,67,68,69,70,71,72,65,66,67,68,69,70,71,72,2},
        {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,7,2,12,13,3,3,3,3,3,3,3,3,3,73,74,75,76,77,78,79,80,73,74,75,76,77,78,79,80,2},
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}};
        overlay = new int[14, 50];
        background = new int[14,50];
    }
    public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            for (int y = 0; y < map.GetLength(0); y++)
            {
                //http://imgur.com/1r9VUUN
                spriteBatch.Draw(tilesheet, new Vector2((x * 64), (y * 64)), new Rectangle((map[x, y] % 8) * 64, (map[x, y] / 8) * 64, 64, 64), Color.White);
            }
        }
    }

So im editing my maps in Tiled and i realized that tiled counts 1-2-3-4-5, not 0-1-2-3-4-5. So if you looks at my tileset(commented away in the draw method imgur link) you see that i have a blank space before the first tile, but this means that every tile has their positionvalue + 1 in xna, which might be why im having issues drawing it, because the tile im trying to draw doesn't exist in the tileset.


Solution

  • Since i don't know exactly how your Tile Map looks like i will simply give a step by step explained example:

    First let say we have the following Tile Map Sheet

    enter image description here

    this map is 8x3 and lets suppose each tile is 64X64 px, to refer to one of these tile you could use its coordinate (x,y) in the MapSheet (for example the last tile has the coordinate (7,2) -0 based index-), or more cleverly you could simply refer to it as one number by counting from the first tile (for example the previous tile (7,2) represent the tile 23)

    enter image description here

    Second define your Map Matrix

     map = new int[,]
     {                            
         {8,8,1,8,8,1},
         {1,8,0,8,8,8},
         {1,8,8,8,8,8},
         {1,7,8,8,8,8},
         {1,8,0,23,4,4},
         {1,8,8,1,8,8}
     };
    

    each element in the above Matrix is the coordinate of the tile in the TileMap,

    Third the drawing part,

    public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            for (int y = 0; y < map.GetLength(0); y++)
            {
                spriteBatch.Draw(tilesheet, new Vector2((x * 64) , (y * 64)), new Rectangle((map[x,y]%8) * 64, (map[x,y]/8)*64, 64, 64), Color.White);
            }
        }
    }
    

    in the above code new Rectangle((map[x,y]%8) * 64, (map[x,y]/8)*64, 64, 64), represent what's called the Source Rectangle, that Rect is used to locate the corresponding tile Rectangle in the TileMap