Thread: Map file formats and linked lists

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    22

    Map file formats and linked lists

    Hi, I'm currently working on a 2d rpg-ish type game (might be implementing stats/etc. later on) and I've been thinking about how I'm going to go about storing my maps.

    Obviously I've been searching around and I found a few really great tutorials outlining the basics of a good map file format.
    The only problem is that I've already decided to use an array of linked lists, this is so I can do things like stack 10 or so layers on one map element with only 2 or 3 on the tile next to it.

    I'm not too familiar with linked lists and so I'm having trouble figuring out how I can store all this information.
    Maybe I should just drop the whole linked lists idea and go with simple arrays?

    If anybody could give me a bit of advice on coming up with a good map format using either that whole linked list idea or just plain old arrays that would be great! Thanks in advance!!

    (I'd post what code I have so far but it's pretty pathetic, I'm still pretty much figuring out how it's all going to work)

    edit: Sorry, I meant to say an array of linked lists, not just a linked list :P
    Last edited by Spitball; 03-04-2004 at 11:34 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well you can use a linked list but there are several issues you are going to run into. First you are never guaranteed that the tile number 1 is going to be in the upper left hand corner of the screen. This would require iterating through the list until you reached the correct tile ID which wastes cycles. The absolute best system I could think of is a quad tree but coding that might be beyond where you are. Not to say that you could not do it, but why make it hard.

    Here is a scheme I came up with and have also seen used on many tutorial websites.

    First of all you know that this array:

    012
    345
    678

    does not have to be a two dimensional array. If you take the row*width+column you can get to any location in that array but you can use a 1 dimensional array.

    So let's start with 20x20 tiles. Each tile is 400 bytes (in 256 color mode) or unsigned char Tile[400].

    But we want several tiles -> Tiles[maxtiles][sizeinbytes]

    So for tile 1 element 200 - Tiles[1][200]

    The second element in your 2D array is the bitmap data only it is in a linear array. You would do y*20+x to access it correctly.

    So in this way you could load all of your tiles into the array and use their tile ID as the index into the array.

    So if grass is 1 and tree is 2 -> Tile[grass][y*20+x] and Tile[tree][y*20+x]. We get the first value from the tilemap.

    Here is a basic tilemap system - note the tilemap can also be a one dimensional array.

    Grass=1
    Tree=2
    Road=3
    Rocks=4

    Code:
    unsigned int TileMap[]={1,1,1,1,1,1,
                            1,2,1,2,1,2,
                            3,3,3,3,3,3,
                            4,4,4,4,4,4,
                            1,1,1,1,1,1};
    unsigned char Tiles[NumTiles][TileSizeInBytes];
    Then all you do is draw

    Tiles[(TileMap[playery*mapwidth+playerx])][TileData];

    I would not use a linked list, but multiple arrays. Tiles1 would be layer 1 tiles, Tiles2 would be 2nd level, etc. etc.
    They are easier and faster to access and easier to code for. However they do not lend themselves well to inserting, deleting, etc.., but most tilemaps are constant so this shouldn't matter.

    Not sure why the board mangled my Tiles[] line but it did.
    Last edited by VirtualAce; 03-04-2004 at 05:01 PM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Okay thanks for the help. I'm not sure I completely understand what you're suggesting, but I'll give it a go anyway. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM