🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Dynamic Tile Maps 1.0

Published September 15, 1999 by Lee French, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement

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
March 1999

Cancel Save
0 Likes 0 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

Dynamically allocated tilemaps sizes got you confused? Don't know where to start? This article has an easy to understand example of how to make a dynamically sized 2D map.

Advertisement
Advertisement