Browse Source

Use boost::multiarray instead of manual memory management in CMap

Ivan Savenko 2 years ago
parent
commit
0901c8feaf
2 changed files with 11 additions and 79 deletions
  1. 6 32
      lib/mapping/CMap.cpp
  2. 5 47
      lib/mapping/CMap.h

+ 6 - 32
lib/mapping/CMap.cpp

@@ -129,9 +129,10 @@ bool TerrainTile::isWater() const
 }
 
 CMap::CMap()
-	: checksum(0), grailPos(-1, -1, -1), grailRadius(0), terrain(nullptr),
-	guardingCreaturePositions(nullptr),
-	uidCounter(0)
+	: checksum(0)
+	, grailPos(-1, -1, -1)
+	, grailRadius(0)
+	, uidCounter(0)
 {
 	allHeroes.resize(allowedHeroes.size());
 	allowedAbilities = VLC->skillh->getDefaultAllowed();
@@ -142,22 +143,6 @@ CMap::CMap()
 CMap::~CMap()
 {
 	getEditManager()->getUndoManager().clearAll();
-	
-	if(terrain)
-	{
-		for(int z = 0; z < levels(); z++)
-		{
-			for(int x = 0; x < width; x++)
-			{
-				delete[] terrain[z][x];
-				delete[] guardingCreaturePositions[z][x];
-			}
-			delete[] terrain[z];
-			delete[] guardingCreaturePositions[z];
-		}
-		delete [] terrain;
-		delete [] guardingCreaturePositions;
-	}
 
 	for(auto obj : objects)
 		obj.dellNull();
@@ -572,19 +557,8 @@ void CMap::removeObject(CGObjectInstance * obj)
 
 void CMap::initTerrain()
 {
-	int level = levels();
-	terrain = new TerrainTile**[level];
-	guardingCreaturePositions = new int3**[level];
-	for(int z = 0; z < level; ++z)
-	{
-		terrain[z] = new TerrainTile*[width];
-		guardingCreaturePositions[z] = new int3*[width];
-		for(int x = 0; x < width; ++x)
-		{
-			terrain[z][x] = new TerrainTile[height];
-			guardingCreaturePositions[z][x] = new int3[height];
-		}
-	}
+	terrain.resize(boost::extents[levels()][width][height]);
+	guardingCreaturePositions.resize(boost::extents[levels()][width][height]);
 }
 
 CMapEditManager * CMap::getEditManager()

+ 5 - 47
lib/mapping/CMap.h

@@ -142,14 +142,14 @@ public:
 	std::map<si32, ObjectInstanceID> questIdentifierToId;
 
 	std::unique_ptr<CMapEditManager> editManager;
-
-	int3 ***guardingCreaturePositions;
+	boost::multi_array<int3, 3> guardingCreaturePositions;
 
 	std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
 
 private:
 	/// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
-	TerrainTile*** terrain;
+	boost::multi_array<TerrainTile, 3> terrain;
+
 	si32 uidCounter; //TODO: initialize when loading an old map
 
 public:
@@ -170,50 +170,8 @@ public:
 		h & questIdentifierToId;
 
 		//TODO: viccondetails
-		const int level = levels();
-		if(h.saving)
-		{
-			// Save terrain
-			for(int z = 0; z < level; ++z)
-			{
-				for(int x = 0; x < width; ++x)
-				{
-					for(int y = 0; y < height; ++y)
-					{
-						h & terrain[z][x][y];
-						h & guardingCreaturePositions[z][x][y];
-					}
-				}
-			}
-		}
-		else
-		{
-			// Load terrain
-			terrain = new TerrainTile**[level];
-			guardingCreaturePositions = new int3**[level];
-			for(int z = 0; z < level; ++z)
-			{
-				terrain[z] = new TerrainTile*[width];
-				guardingCreaturePositions[z] = new int3*[width];
-				for(int x = 0; x < width; ++x)
-				{
-					terrain[z][x] = new TerrainTile[height];
-					guardingCreaturePositions[z][x] = new int3[height];
-				}
-			}
-			for(int z = 0; z < level; ++z)
-			{
-				for(int x = 0; x < width; ++x)
-				{
-					for(int y = 0; y < height; ++y)
-					{
-
-						h & terrain[z][x][y];
-						h & guardingCreaturePositions[z][x][y];
-					}
-				}
-			}
-		}
+		h & terrain;
+		h & guardingCreaturePositions;
 
 		h & objects;
 		h & heroesOnMap;