Advertisement

Dynamically allocating multi-dimensional arrays...

Started by July 27, 2001 08:45 PM
2 comments, last by shdaga 23 years, 6 months ago
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:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>

Here is one way to do it. I''ll present C code, but you can easily modify it to C++.

  // initial creationint width  = <whatever>;int height = <whatever>;int layers = <whatever>;int* map = (int*)malloc(width*height*layers);// to reference an elementint z = <desired layer>;int y = <desired y>;int x = <desired x>;int element = map[z*width*height + y*width + x]; // to change the size (this will trash the map)if(map) free(map);int width  = <new width>;int height = <new height>;int layers = <new layers>;int* map = (int*)malloc(width*height*layers);  


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 ];  





Advertisement
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
  class Tile{public:	unsigned int id;	char			 width;	char			 height;	int			 flags;	char		 *extra;};class Map{public:	Tile ***tiles;	int width, height,layers;public:	~Map();	Allocate();};Map::Allocate(){	tiles = new Tile **[sizeof(Tile**) * layers]; 	for(int lyr = 0; lyr < layers; lyr++)	{	   tiles[lyr] = new Tile *[sizeof(Tile*) * height];		for(int y = 0; y < height; x++)	      {	         tiles[lyr][y] = new Tile [sizeof(Tile) * width];	      }	  	}}//Free MemoryMap::~Map(){	for(int z = 0; z < layers; z++)	{			for(int y = 0; y < height; x++)		{		free(tiles[z][y]);		}	free(tiles[z]);	}	free(tiles);}  

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
Thanks guys, I really apreciate the help.
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>

This topic is closed to new replies.

Advertisement