Generating a 2D Map

Discussion in 'Fangaming Discussion' started by lotzi, Nov 13, 2017.

  1. lotzi

    lotzi

    Member
    6
    0
    0
    What is the best way to build a 2D tile map using code? My idea is to have a file that contains the layout of a map. My code then reads each line from the file and places the specific tile in the correct row/column depending on the character at the specific spot. Is there an easier way to build a map than the idea I suggested?
     
  2. Aerosol

    Aerosol

    FML and FU2 Moderator
    10,614
    79
    28
    Not where I want to be.
    Sonic (?): Coming summer of 2055...?
    Tilengine. Too lazy to hyperlink it for you. Practice your Szechuan-style Google Fu.
     
  3. Liraxus

    Liraxus

    Some shmuck Member
    83
    0
    0
    Idiotville
    Nothing, really.
    This was the last place I was expecting to laugh this hard.
     
  4. Okay, here's a similar question.

    Which would be more efficient for a game made in Game Maker?

    1: Small Tiles, along the lines of 16x16 or 32x32, used to make a level on par with the Classic games length-wise
    2: Level Chunks, built to the same size as the classic games (either the large chunks in Sonic 1 or the smaller ones used in S3+K)
    3: Fully pre-made single-image background map used for the entire level

    I've been messing with making some really nice graphics, but I'm not sure how well they'd work in a traditional tileset without making hundreds of seemingly-identical tiles with minute shading differences. If I break it down into small tiles, I may have to start simplifying and removing stuff to make it easier to work with. If I go with level chunks, I'll have the same issue, albeit to a much lesser extent. If I just keep the entire level's graphics as a single massive image, I have really nice hand-made shading on the graphics, but I'm not sure if that will hurt level performance very much.
     
  5. Techokami

    Techokami

    For use only on NTSC Genesis systems Researcher
    1,317
    3
    18
    HoleNet!
    Sonic Edge
    Chunks is the smartest way to go, really. If you have gargantuan image files serving as the full level layout, it would be rough on the GPU. And since Game Maker doesn't offer safety nets for everything, it could get ugly real quick.
     
  6. Alright, thanks. I wasn't even originally planning on doing a game with these graphics, I was just having some fun screwing around with Paint.NET and adding a bunch of transparency gradients and shading, and realized that it actually looked pretty cool. Then I realized just how complicated it was going to be to try and break it up into something usable if I went the traditional tile-based route.
     
  7. lotzi

    lotzi

    Member
    6
    0
    0
    Thank you for giving me my new "About Me" description
     
  8. lotzi

    lotzi

    Member
    6
    0
    0
    Since I did a terrible job explaining my idea at the beginning of this post, I've decided to explain my idea with better detail.

    I'm going to be writing my map generator using C++ and SFML. The idea is to read a txt file that contains characters that represent different types of tiles. For example, X could represent a wall, O can be the floor, and D can be a door. My txt file can look like the following:

    XXXXXXXXXXXXXXXXXX
    XOOOOOOOOOOOOOOX
    XOOOXXXXXXXXXOOOX
    XOOOXOOOOOOXOOOX
    XOOOXOOOOOOXOOOX
    XOOOXOOOOOOXOOOX
    XOOOXOOOOOOXOOOX
    XOOOXXXDDXXXXOOOX
    XOOOOOOOOOOOOOOX
    XXXXXXXXXXXXXXXXXX

    I can write a nested loop that will first loop through each line of the txt file, while the second iterates through each character in the line. Depending on the character, a specific type of tile is created with it's own characteristics.

    What is your guy's opinion on this idea? Are there better ways of approaching this problem?
     
  9. Aerosol

    Aerosol

    FML and FU2 Moderator
    10,614
    79
    28
    Not where I want to be.
    Sonic (?): Coming summer of 2055...?
    Why do you want to reinvent the wheel? I mean the idea is sound enough, sure. It might be better to use XML to describe the tilemap, but Tilengine (in conjunction with Tiled Map Editor's map and tileset format, which are just xml's basically) already do these things.

    I mean if you use C# it's even easier. I think it includes an XML parsing library now? And if you're adamant on using C++, I'm sure there are XML parsing libraries you can tack onto your project.

    Your approach is, as far as I can tell, how tilemaps are usually rendered.
     
  10. winterhell

    winterhell

    Member
    1,165
    7
    18
    Don't use a text editor to edit the levels. That is lazy and counterproductive, and you'll waste more time in the end.
    Instead you can integrate the level editor. You set keys that change the position of the current tile you are working on ( say IJKL, but could be WASD or left right up down), and a way to alter that tile, perhaps with a simple +/- key combination, and or with an image showing the list of all tiles. You press F5 and you save to a binary file with 16 bit integers, or press F8 to load the level.
    It might take you half an hour or so to add the editor.
    Here is an example of how it might look with the tile list on the left and the level on the right. For what is worth it took me about 3-4 hours to dial in the whole MHZ Act 1 with that back in the Sonic 3 HD's tech demo. I used an image from zone0 as a red background blueprints to know what I'm doing.
    [​IMG]
     
  11. Amnimator

    Amnimator

    Member
    224
    0
    16
    If you're going to store tile data and implement it into a level editor, or what not, you'll probably want to do so by having tile IDs. For the most part, they'd start at 0 at one corner of the image (lower left for example), sweep in the x axis (left to right for example) assigning IDs (0,1,2), then repeating the process by doing it again moving up in the y axis. This way you can just assign what tile you need using IDs.

    You can also get the tile's x and y position from an ID using a formula like this: (id % size.x), (id / size.x).
    So, let's say there's a 3x3 grid.

    ID of 0: Modulo (fancy way of saying the remainder) of 0/3 is 0. Int division of 0/3 is 0. So (0,0).

    ID of 1: Modulo of 1/3 is 1. Int division of 1/3 is 0. So (1,0).

    ID of 2: Modulo of 2/3 is 2. Int division of 2/3 is 0. So (2,0)

    ID of 3: Modulo of 3/3 is 0. Int division of 3/3 is 1. So (0,1).

    EDIT: Fixed the formula, had a y instead of an x.
     
  12. lotzi

    lotzi

    Member
    6
    0
    0
    Would I need to implement my own level editor, or is there software online I can use? Also, does anyone have any books to recommend?
     
  13. Amnimator

    Amnimator

    Member
    224
    0
    16
    There are some tilemap editors that would be independent of the engine, like Tiled. Implementing it yourself sounds like it can have its benefits, but I'm not the most knowledgeable person here regarding creating game engines themselves. I follow a strict "learn whatever whenever I need it" case by case basis :V