Dynamically allocating multi-dimensional arrays...
Im working on my map editor, but I''m stumped when it comes to allocating dynamic multidemsional arrays. I want to do a drop down list of different sized maps, and then generate an array according to the chosen size. So, given that I need to generate an array with the following 3 things describing the array:
map[width][height][layers] or width,height and number of layers
each one is an int...
what would the c++ code look like?
Thanks for any help.
"Kiss my Converse!"
-Shogun of Harlem
-Barry Gordon''s "The Last Dragon"
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
July 27, 2001 11:42 PM
Here is one way to do it. I''ll present C code, but you can easily modify it to C++.
|
There are other ways to do it, such as creating an array of pointers to arrays, but this is harder to setup. If you make sure the width and height of the map are powers of two, you can access an element very quickly using bit-shifting using the above method.
Let''s say the width is 256 (2^8) tiles, and the height is 128 (2^7) tiles. To access an element, you can do:
int element = map[ (z<<15) + (y<<8) + x ];
Here''s the code I use not the prettiest and its C++ I''ll just throw out my whole class here for ya least the parts you want
in your program just do something like Map map;
then when you want just map.width = 20; map.height = 50; map.layer = 3; then just map.Allocate(); and walla ya got it to access it you can use something like map.tiles[layers][y][x] to access any elements of the class tiles you can also replace tiles with like unsigned char or whatever just make sure you changed the sizeof in the allocate too a pretty simple way to make a dynamic 3d array Hope that helps need any more info just drop a line to derilect@hotmail.com
|
in your program just do something like Map map;
then when you want just map.width = 20; map.height = 50; map.layer = 3; then just map.Allocate(); and walla ya got it to access it you can use something like map.tiles[layers][y][x] to access any elements of the class tiles you can also replace tiles with like unsigned char or whatever just make sure you changed the sizeof in the allocate too a pretty simple way to make a dynamic 3d array Hope that helps need any more info just drop a line to derilect@hotmail.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement