For a game I'm working on I want my terrain to be represented by a 2-d array of TerrainSquare objects. I first started looking at boost::multi_array, but I found the syntax to be somewhat confusing and I also heard it isn't too fast in comparison with a regular 2-d array.
I realised I didn't really need to be able to resize. However I needed to array size to be defined at runtime by means of a constructor. I came across an example of how to do this using a double pointer and I came to the following implmentation:
Code:Terrain.h #include "TerrainSquare.h" class Terrain { public: Terrain(int x, int y); ~Terrain(void); TerrainSquare **terrainArray; };Code:Terrain.cpp #include "Terrain.h" Terrain::Terrain(int x, int y) { terrainArray = new TerrainSquare*[x]; for (int i=0; i<x; i++) { terrainArray[i] = new TerrainSquare[y]; } } Terrain::~Terrain() { delete this->terrainArray; }Code:TerrainSquare.h class TerrainSquare { public: TerrainSquare(); TerrainSquare(int type); ~TerrainSquare(void); int type; };My questions are:Code:TerrainSquare.cpp #include "TerrainSquare.h" TerrainSquare::TerrainSquare() { this->type = 0; } TerrainSquare::TerrainSquare(int type) { this->type = type; } TerrainSquare::~TerrainSquare(void) { }
1./ Is this a good way to implement a 2-d array for my purposes. I must tell you that this array will be read alot (every render cycle, to draw the terrain), so the access speed is probably the most important aspect.
The data will also frequently be modified, ie. a fire in the game could change the terrain type from forest to wasteland.
2./ Am I deleting the 2-d array in the proper way in the destructor of the Terrain class?




Reply With Quote
