Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sweet Snippets - Public free and open repository of game development source


Like
0Likes
Dislike

Simple XNA Ascii Tile Map Loader

Tags: C# HTML xna map loader
This is a simple chunk of code to load an ascii tile map exported from tIDE (Tilemap Integrated Development Environment). It quite simply parses a small text file and loads the map into an 2-dimensional int array. It can be easily adapted for other platforms.
map.cs - Snippet to load map from file
const int map_width = 20, map_height = 20;
int[,] map = new int[map_width, map_height];


// open a StreamReader to read the index
string path = Path.Combine(StorageContainer.TitleLocation, "Content\\map.txt");
using (StreamReader reader = new StreamReader(path))
{
   int y = 0;
   while (!reader.EndOfStream)
   {
      // get a line
      string line = reader.ReadLine();
 
      // split at the equals sign
      string[] parts = line.Split(',');
 
      for (int x = 0; x < parts.length; x++)
      {
        map[x,y] = Convert.ToInt32(parts[x]);
      }
     
      y++;
   }
}


map.txt - Sample map file
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,2,2,2,0,2,2,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,9
9,0,2,2,2,0,2,2,2,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9
9,0,0,0,0,0,0,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