# help! best method to obtain Map data?

This topic is 4794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I had create a class for TILING on the background, while i am storing whole map data from a file into 3 dimensional array with limited numbers of tiles e.g. int tiles[300][400][2]. But suppose if i want to obtain the memory on runtime rather then on compile time so it would give me a chance to load unlimited number of map data... now problem is that i have 3 ways to obtain the map data dynamically ... 1)By using STL's MAP or SET and store a structure like below struct tiledata{ int tilesWidth; int tilesHeight; int tilesLayers; } then SET<tiledata> mydata; 2)By making my own link list class which will use new operator on run time to obtain the data and allocate the memory .. 3) simple static way of array tiles[300][400][2]. which could be limited always,.. I am confuse which method should i follow to get dynamic array map data , or any of you are using any better technique for it...I heard some where that using stl will make my program slow cuz every time it will need to insert the data.. Thanks in advance

##### Share on other sites
If you're using C++, I'd use the new and delete operators to create a new array as needed. I think a linked list would be overkill for tilemap data. Also, in arrays it's generally good pracise not to diminsionalise. I mean for a 10x10 map use array[y*WIDTH+x] not array[x][y]. The latter is personal preference, but I know a few other programmers who prefer single dimension arrays.

Mark
Cornutopia Games
http://www.cornutopia.net

##### Share on other sites
thanks for the reply , but how u can save data in one array i am bit confuse on it ,suppose map is of 5X5

1=represent grass 2=trees 3=water

11111
11333
11111
12222
12222

how can i store all this info in one array and how could i differentiate it that which one is width and which one is height, plus i want to show atleast 2 leyars of map , like some of the part should hide the apperiance of character game like sonic etc...

map leayrs 1
character
map leyars 2

##### Share on other sites
umm you do know thats 240000 cells right? that is alot of memory and if your going to have more then one of these i would highly recomend saving the data in a text file that way your program wont hog all the ram. what i did is just have a map class that has one 2d array (it can be 3d) then i just read in the maps from the text files when needed if you have any questions about how to implement this or if your question was about dynamic 2d arrays then let me know

hope i helped [smile]

##### Share on other sites
really, even 240,000 of those 'tiledata' objects is still under 3 MB of ram (unless i screwed up the math [smile]).

however, i prefer to not use 3 dimensions to get a layer effects. its much more efficent and flexible if instead you give your tiledata class some sort of "layers" array (read: vector, not actual array!). check out this thread where i explained in detail what im talking about.

##### Share on other sites
well if he has just 10 maps then there is around 25 megs out the window also i agree with graveyard filla on this

##### Share on other sites
well, i sincerley hope hes not hard-codeing a 240,000 element array into his app, let alone 10 of them [grin].

##### Share on other sites
Thanks guys for your fast replies ,
no way i am not hardcoding the data ... i have a map class that read the data from the data file by using I/O functions. But i actully want to create 3d type dynamic array so that whole array will be filled with map file data. but still i dont understand should i use STL or should i make my own class or should i use Dynamic array.. Cuz i dont wana hard code my array's total elements in its declaration. Suppose some of my map files are 900 width , height 400 and atleast 2 layers... and some of my map files are like having 300 width data, 100 height data and 2 layears.... so u can now get my point that i have to write some sort of dynamic code that could allocate memory according to map's requirement... but which way to go.... graveyard filla thanks for the thread now i am looking at the thread you gave...

##### Share on other sites
I'd just go ahead and use a single-dimensional array and code the offset arithmetic. It's the cleanest way and you can just write a wrapper around the whole thing anyway:
class Map{  /* ... */  int Get(int x, int y, int z) const  {      return data[z*width*height + y*width + x];  }  void Set(int x, int y, int z, int value)  {      data[z*width*height + y*width + x] = value;  }  /* ... */  int width, height, layers;  int* data;};

You can add bounds checking if you want, and even cache constants like (width*height) if you're looking to pinch. It's simple and it works.

##### Share on other sites
I would do similar, but use a std::vector instead of new and delete to manage the array. SGI has a great resource on STL:

http://www.sgi.com/tech/stl/Vector.html

Hope this helps

Pete

##### Share on other sites
If you want to enforce a rectangular array, you might be interested in boost::multi_array.