Dynamic Tile Maps 1.0
by Lee French
March 1999

I've heard a lot of people recently asking about dynamic arrays for maps of any size within tile editors. Well here is a tutorial that should help you get started pronto.

First of all, lets consider what we want as map data. We want a list of tile ID's that correspond to tiles within a datafile, we want the tiles type (e.g. whether it is walkable, animated etc).

So we'd define a structure such as:-


typedef struct
{
    unsigned char TileID;   // The ID of this tile
    unsigned char Type;     // What type of tile is this
} MAPTILE:

So we have created a new type called map tile. Now we need to know how to declare our map tile. If we had fixed map sizes we could declare it as follows:-

MAPTILE Map[MAPX][MAPY];

Since we want a variable sized map, we have to think a bit more cleverly. Also, we want to implement variable layers into the map. In order to use variable map sizes we have to setup the Map like this:-

MAPTILE ***Map;

Looks strange doesn't it? Trust me, I thought this at first. What we now want to do is dynamically add each element to the Map pointer:-


// Allocate memory for the maps Layers elements
Map = malloc(sizeof(int**) * MapLayers);

// Allocate memory for the maps X elements
for(lyr = 0; lyr < MapLayers; lyr++)
{
   Map[lyr] = malloc(sizeof(int*) * MapWidth);

      // Allocate memory for the maps Y elements
      for(x = 0; x < MapWidth; x++)
      {
         Map[lyr][x] = malloc(sizeof(int) * MapHeight);
      }
   }
}

And that's it, simple. No error checking has been included here but a cannot overemphasize enough how important this is.

Now we want to access the map pointer to initialize the values:-


// Initialize the elements of the map
for(lyr = 0; lyr < MapLayers; lyr++)
{
   for(x = 0; x < MapWidth; x++)
   {
      for(y = 0; y < MapHeight; y++)
      {
         Map[lyr][x][y].TileID = NOTILE;
         Map[lyr][x][y].Walkable = WALKABLE;
      }
   }
}

And that's it, a simple way to create a dynamic map array.

Please do not hesitate to contact me if you have any comments to make on this tutorial. In fact this is my first ever attempt at a tutorial so be easy :)

Dynamic Tile Maps 1.0 is copyright 1999 Lee French. All rights reserved. Freely distributable if unmodified and in its entirety.


http://members.tripod.co.uk/lpfglc/
leef@pronexus.co.uk

Discuss this article in the forums


Date this article was posted to GameDev.net: 9/15/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
General

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!