help! best method to obtain Map data?

Started by
9 comments, last by Zahlman 19 years, 3 months ago
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
Things has been changed but the just the way it is....
Advertisement
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
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

Things has been changed but the just the way it is....
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]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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.
FTA, my 2D futuristic action MMORPG
well if he has just 10 maps then there is around 25 megs out the window also i agree with graveyard filla on this
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
well, i sincerley hope hes not hard-codeing a 240,000 element array into his app, let alone 10 of them [grin].
FTA, my 2D futuristic action MMORPG
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...





Things has been changed but the just the way it is....
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.
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

This topic is closed to new replies.

Advertisement