Dynamic Tile Maps 1.0
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.
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
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Lee French
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion