Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tile based map  (Read 2040 times)

0 Members and 1 Guest are viewing this topic.

malachipclover

  • Newbie
  • *
  • Posts: 5
    • View Profile
Tile based map
« on: January 17, 2013, 01:33:30 am »
I am working on a 2D tile based game in SFML, and I need some help with the map.  In previous versions, I have always used a 2D array, but only because they were very early prototypes.  I want to make it an infinite map generator, so I don't think an array would do.  I will probably generate the map in chunks, if that changes anything.  I suppose I could use an array the size of a chunk (I am thinking either 16 * 16, 32 * 32, or 64 * 64 tiles) and then just swap the data out with data for the next chunk out of the map file, but I'm worried that might take too many resources.  Also, if the chunks do not divide evenly onto the screen space given(very unlikely it will), this approach would leave black around the sides once the edge of a chunk was reached.  A possible solution would be to make the array, say for example, 2 * 2 chunks, so then if a player was moving down, the top 2 chunks would be overwritten by the bottom 2 in memory, and the previous space occupied by the bottom 2 could be loaded from disk for the chunks below that.  The problem is, this approach would further complicate things, and it still would not eliminate the slowness of replacing the old data in the array.  Both array approaches would also have the problem of further complicating getting/changing tile IDs at a given coordinate because that specific coordinate might not be in the current array, and even if it was, it would be in coordinates relative to the top left of the top left chunk being stored in the array, either requiring an extra function call, or typing out code to convert to global coordinates every time.
    Another approach would be a map, consisting of std::pairs made of an sf::Vector2i and either a number, enum value, or a custom tile class or struct.  I am unsure of the how well a map will perform speed-wise though.  I imagine it will be considerably slower, being that you have to iterate over the items rather than just give it a location.  I was also thinking about having a map that contains other maps.  So I would give an int rather than a vector, and that int would be the X coordinate.  I would then get another map that contains all the Y coordinates for that X coordinates.  There would be fewer iterations (hopefully) but there might be more overhead in returning (or however an map::iterator works) a map.
    Last thing, I need a good way to serialize/deserialize the map, and some other information (mainly entity positions).  I would prefer to not use any external libraries, but if that's what it takes I will use one.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Tile based map
« Reply #1 on: January 17, 2013, 03:19:08 am »
So rather than setting the tile-maps according to what you want yourself (like a .txt or a matrix of ints) you want a tile-map generator that recalculates things when they need to be visible.

The only fast way to draw a tile-map is through a std::vector or an array, as anything else won't be able to draw all the quads in one draw call for max performance. So that would take the std::map method out if the tiled-map is big. I can see nothing else other than recalculating the map whenever needed.

Quote
but I'm worried that might take too many resources.

It certainly is better than losing run-time speed.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

malachipclover

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Tile based map
« Reply #2 on: January 17, 2013, 12:27:29 pm »
OK, I will try the array method, but I need the map to be persistent, so I will have to use a file at some point, whether that be only on save/load a map or during runtime top ease the load on the RAM.