Skip to content

Commit

Permalink
Convert GroundMaterialConfiguration to use smart pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayda2 committed Jul 14, 2018
1 parent e4590d2 commit 29b430d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
27 changes: 9 additions & 18 deletions GroundMaterialConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "GUI.h"
#include "ContentLoader.h"
#include "EnumToString.h"
#include "MiscUtils.h"

using namespace std;
using namespace DFHack;
Expand Down Expand Up @@ -41,16 +42,6 @@ TerrainConfiguration::TerrainConfiguration()
//dont really care about the rest of the sprite right now.
}

TerrainConfiguration::~TerrainConfiguration()
{
uint32_t currentsize = (uint32_t)terrainMaterials.size();
for (uint32_t i = 0; i < currentsize; i++) {
if (terrainMaterials[i] != NULL) {
delete(terrainMaterials[i]);
}
}
}

void DumpInorganicMaterialNamesToDisk()
{
FILE* fp = fopen("dump.txt", "w");
Expand Down Expand Up @@ -261,13 +252,13 @@ void parseWallFloorSpriteElement(TiXmlElement* elemWallFloorSprite, vector<std::
// if that was null we have *really* screwed up earlier
// create a new TerrainMaterialConfiguration if required
// make sure we have room for it first
if (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
while (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
// dont make a full size vector in advance- most of the time
// we will only need the first few
tConfig->terrainMaterials.resize(elemIndex + 1, NULL);
tConfig->terrainMaterials.push_back(nullptr);
}
if (tConfig->terrainMaterials[elemIndex] == NULL) {
tConfig->terrainMaterials[elemIndex] = new TerrainMaterialConfiguration();
if (tConfig->terrainMaterials[elemIndex] == nullptr) {
tConfig->terrainMaterials[elemIndex] = dts::make_unique<TerrainMaterialConfiguration>();
}
// only update if not set by earlier configs,
//FIXME: figure out how to manage priorities here.
Expand Down Expand Up @@ -296,13 +287,13 @@ void parseWallFloorSpriteElement(TiXmlElement* elemWallFloorSprite, vector<std::
//if that was null we have *really* screwed up earlier
//create a new TerrainMaterialConfiguration if required
//make sure we have room for it first
if (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
while (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
//dont make a full size vector in advance- we wont need it except
//for those who insist on Soap Fortresses
tConfig->terrainMaterials.resize(elemIndex + 1, NULL);
tConfig->terrainMaterials.push_back(nullptr);
}
if (tConfig->terrainMaterials[elemIndex] == NULL) {
tConfig->terrainMaterials[elemIndex] = new TerrainMaterialConfiguration();
if (tConfig->terrainMaterials[elemIndex] == nullptr) {
tConfig->terrainMaterials[elemIndex] = dts::make_unique<TerrainMaterialConfiguration>();
}
// add to map (if not already present)
for (int j = 0; j < NUM_FORMS; j++) {
Expand Down
4 changes: 1 addition & 3 deletions GroundMaterialConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ class TerrainMaterialConfiguration
std::vector<std::map<int, std::pair<c_sprite, int>> > overridingMaterials;

TerrainMaterialConfiguration();
~TerrainMaterialConfiguration() {}
};

class TerrainConfiguration
{
public:
std::vector<TerrainMaterialConfiguration*> terrainMaterials;
std::vector<std::unique_ptr<TerrainMaterialConfiguration>> terrainMaterials;
std::vector<std::pair<c_sprite, int>> defaultSprite;
TerrainConfiguration();
~TerrainConfiguration();
};

bool addSingleTerrainConfig( TiXmlElement* elemRoot);
Expand Down
4 changes: 2 additions & 2 deletions SpriteMaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ c_sprite * GetTerrainSpriteMap(int in, t_matglossPair material, vector<std::uni
}
}
// find mat config
TerrainMaterialConfiguration* terrainMat = terrain->terrainMaterials[material.type];
if (terrainMat == NULL) {
TerrainMaterialConfiguration* terrainMat = terrain->terrainMaterials[material.type].get();
if (terrainMat == nullptr) {
if (terrain->defaultSprite[tempform].first.get_sheetindex() == UNCONFIGURED_INDEX) {
return &(terrain->defaultSprite[0].first);
} else {
Expand Down

0 comments on commit 29b430d

Please sign in to comment.