I was wondering whats the best approach to loading and saving an iso tile map? I have a txt file with all the relevent map data (type, tile) and the coords for the tiles to be drawn at. I then load these coords into an array and tiles into memory and then have the map drawn to the screen using the array. Is there a better way of doing this, like with XML or something else?
In my opinion there’s very little point using XML for data files like this unless you’re wanting it to be human readable. If you’re literally going to be using an array of ints then just write it in binary format into a file…
Also, if you are tiling your map, its probably easier to store your ints in an order that defines the position, e.g. an array of numbers where the x,y index give you a position at that tile. Unless your map is scarcely populated it will be more efficient this way.
Kev
The idea is that I have several layers: background, architecture, characters, foreground. Each layer gets its own section in a txt file:
background:
“path for tile1”
“path for tile2”
tile1@30,30 60,60 320,320
tile2@150,150 …
architecture
.
.
.
the idea being that when the map is first initialized the paths of the tiles are read so as to load the images into memory and an array with the type of tile and all the coords at which that tile is drawn is created. Then when the map is drawn the draw method uses the array. Since the background will not be entirely visible only the tiles that are visible are drawn. Then the buildings and architecture are drawn, then the characters and then the foreground. I am trying to implement this now and was wondering if its a good approach.
Hey Kevglass I was wondering how would I write an array in binary format to a file and how would I read that and convert it back to array data?
Its best to keep the map file as small as possible because these files, if not designed and managed correctly can get huge.
One way to do this is to have a header section where you read in all of your info about the map. width/height, tile names (if you wish), etc…
After you do this then you should read in each layer one after the other. For example our maps have layers for tiles, objects, units. All this contains info about where units can walk and such using positive numbers for tile/object/unit ids and negative numbers telling us movement areas that are blocked with that tile/object/unit (the negative number will match the id’s by simply negating it.
we store this info as byte for tiles, objects and units (128 unique ids/with negation). so we have a greatly compressed file with just that. But this isn’t enough because each layer you add increases your file size by 1/layers.
So we use a simple compression algorithm as follows:
if(readValue == 0)
skipbytes(nextReadValue() );
when you are saving your file and come across a grouping of 0’s then then just write out the first 0 and write out how many zeros follow.
Since with units and objects your map will have a ton of zeros in it then this compression will reduce your file size exponentially.
You will end up with huge maps saved on disk in 10-80k vs 2+ mbs (from my personal experience)