Jump to content
  • Advertisement
  • 09/15/99 05:34 PM
    Sign in to follow this  

    Dynamic Tile Maps 1.0

    General and Gameplay Programming

    Myopic Rhino
    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:-

    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.

    March 1999[hr]

      Report Article
    Sign in to follow this  

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!