diff --git a/src/definition/AtmosphereDefinition.cpp b/src/definition/AtmosphereDefinition.cpp index 7e74e9c..b760ca8 100644 --- a/src/definition/AtmosphereDefinition.cpp +++ b/src/definition/AtmosphereDefinition.cpp @@ -4,7 +4,7 @@ #include "RandomGenerator.h" AtmosphereDefinition::AtmosphereDefinition(BaseDefinition* parent): - BaseDefinition(parent) + BaseDefinition(parent, "atmosphere") { } diff --git a/src/definition/BaseDefinition.cpp b/src/definition/BaseDefinition.cpp index bc0e890..8a7f942 100644 --- a/src/definition/BaseDefinition.cpp +++ b/src/definition/BaseDefinition.cpp @@ -2,12 +2,13 @@ #include "PackStream.h" -BaseDefinition::BaseDefinition(BaseDefinition* parent): - parent(parent) +BaseDefinition::BaseDefinition(BaseDefinition* parent, const std::string &name): + parent(parent), name(name) { if (parent) { root = parent->root; + parent->addChild(this); } else { @@ -17,9 +18,18 @@ BaseDefinition::BaseDefinition(BaseDefinition* parent): BaseDefinition::~BaseDefinition() { + if (parent) + { + parent->removeChild(this); + parent = NULL; + } + for (auto child:children) { - delete child; + if (child->getParent() == this) + { + delete child; + } } } @@ -40,6 +50,24 @@ Scenery* BaseDefinition::getScenery() } } +std::string BaseDefinition::toString(int indent) const +{ + std::string result; + for (int i = 0; i < indent; i++) + { + result += " "; + } + result += name; + if (not children.empty()) + { + for (auto &child: children) + { + result += "\n" + child->toString(indent + 1); + } + } + return result; +} + void BaseDefinition::save(PackStream* stream) const { stream->write(name); @@ -87,6 +115,7 @@ void BaseDefinition::removeChild(BaseDefinition* child) std::vector::iterator it = std::find(children.begin(), children.end(), child); if (it != children.end()) { + child->parent = NULL; children.erase(it); } else diff --git a/src/definition/BaseDefinition.h b/src/definition/BaseDefinition.h index 53ee936..76c8a86 100644 --- a/src/definition/BaseDefinition.h +++ b/src/definition/BaseDefinition.h @@ -12,7 +12,7 @@ namespace definition { class DEFINITIONSHARED_EXPORT BaseDefinition { public: - BaseDefinition(BaseDefinition* parent); + BaseDefinition(BaseDefinition* parent, const std::string &name); virtual ~BaseDefinition(); virtual void save(PackStream* stream) const; @@ -29,6 +29,11 @@ public: inline const BaseDefinition* getParent() const {return parent;} inline const BaseDefinition* getRoot() const {return root;} + /** + * Return a string representation of the tree (mainly for debugging purposes). + */ + virtual std::string toString(int indent = 0) const; + protected: void addChild(BaseDefinition* child); void removeChild(BaseDefinition* child); diff --git a/src/definition/CameraDefinition.cpp b/src/definition/CameraDefinition.cpp index 1f43199..1d9ca31 100644 --- a/src/definition/CameraDefinition.cpp +++ b/src/definition/CameraDefinition.cpp @@ -4,8 +4,8 @@ #include "PackStream.h" #include "BoundingBox.h" -CameraDefinition::CameraDefinition(): - BaseDefinition(NULL) +CameraDefinition::CameraDefinition(BaseDefinition *parent): + BaseDefinition(parent, "camera") { location.x = 0.0; location.y = 0.0; diff --git a/src/definition/CameraDefinition.h b/src/definition/CameraDefinition.h index 3268381..9d928e2 100644 --- a/src/definition/CameraDefinition.h +++ b/src/definition/CameraDefinition.h @@ -22,7 +22,7 @@ typedef struct class DEFINITIONSHARED_EXPORT CameraDefinition: public BaseDefinition { public: - CameraDefinition(); + CameraDefinition(BaseDefinition *parent = NULL); virtual void save(PackStream* pack) const override; virtual void load(PackStream* pack) override; diff --git a/src/definition/CloudLayerDefinition.cpp b/src/definition/CloudLayerDefinition.cpp index e53e793..23922bd 100644 --- a/src/definition/CloudLayerDefinition.cpp +++ b/src/definition/CloudLayerDefinition.cpp @@ -6,7 +6,7 @@ #include "PackStream.h" CloudLayerDefinition::CloudLayerDefinition(BaseDefinition* parent): - BaseDefinition(parent) + BaseDefinition(parent, "layer") { type = CIRRUS; altitude = 0.5; diff --git a/src/definition/CloudsDefinition.cpp b/src/definition/CloudsDefinition.cpp index 633828e..9ccb67f 100644 --- a/src/definition/CloudsDefinition.cpp +++ b/src/definition/CloudsDefinition.cpp @@ -8,9 +8,8 @@ static BaseDefinition* _layerConstructor(Layers* parent) } CloudsDefinition::CloudsDefinition(BaseDefinition* parent): - Layers(parent, _layerConstructor) + Layers(parent, "clouds", _layerConstructor) { - setName("clouds"); } void CloudsDefinition::applyPreset(CloudsPreset preset) diff --git a/src/definition/Layers.cpp b/src/definition/Layers.cpp index fa6285a..27dd6d1 100644 --- a/src/definition/Layers.cpp +++ b/src/definition/Layers.cpp @@ -1,7 +1,9 @@ #include "Layers.h" -Layers::Layers(BaseDefinition* parent, LayerConstructor layer_constructor): - BaseDefinition(parent), layer_constructor(layer_constructor) +#include "Logs.h" + +Layers::Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor): + BaseDefinition(parent, name), layer_constructor(layer_constructor) { max_layer_count = 100; null_layer = layer_constructor(this); @@ -35,7 +37,7 @@ void Layers::copy(BaseDefinition* destination_) const Layers* Layers::newCopy() const { - Layers* result = new Layers(NULL, layer_constructor); + Layers* result = new Layers(NULL, getName(), layer_constructor); copy(result); return result; } @@ -59,7 +61,7 @@ BaseDefinition* Layers::getLayer(int position) const } else { - qWarning("Asked for a undefined layer %d on a total of %d", position, (int)layers.size()); + logWarning("Asked for a undefined layer %d on a total of %d", position, (int)layers.size()); return null_layer; } } @@ -75,7 +77,7 @@ int Layers::findLayer(BaseDefinition* layer) const } i++; } - qWarning("Layer %p (%s) not found, on a total of %d", layer, layer->getName().c_str(), (int)layers.size()); + logWarning("Layer %p (%s) not found, on a total of %d", layer, layer->getName().c_str(), (int)layers.size()); return -1; } @@ -89,7 +91,7 @@ int Layers::addLayer(BaseDefinition* layer) } else { - qWarning("Add layer ignored because limit of %d reached", max_layer_count); + logWarning("Add layer ignored because limit of %d reached", max_layer_count); return -1; } } @@ -110,7 +112,7 @@ void Layers::removeLayer(int position) } else { - qWarning("Removing unknown layer %d on %d from '%s'", position, (int)layers.size(), getName().c_str()); + logWarning("Removing unknown layer %d on %d from '%s'", position, (int)layers.size(), getName().c_str()); } } diff --git a/src/definition/Layers.h b/src/definition/Layers.h index efa888d..e940228 100644 --- a/src/definition/Layers.h +++ b/src/definition/Layers.h @@ -16,7 +16,7 @@ typedef BaseDefinition* (*LayerConstructor)(Layers* parent); class DEFINITIONSHARED_EXPORT Layers:public BaseDefinition { public: - Layers(BaseDefinition* parent, LayerConstructor layer_constructor); + Layers(BaseDefinition* parent, const std::string &name, LayerConstructor layer_constructor); virtual ~Layers(); virtual void copy(BaseDefinition* destination) const override; diff --git a/src/definition/PaintedGrid.cpp b/src/definition/PaintedGrid.cpp index e2e88b1..696040b 100644 --- a/src/definition/PaintedGrid.cpp +++ b/src/definition/PaintedGrid.cpp @@ -7,7 +7,7 @@ #include "PaintedGridBrush.h" PaintedGrid::PaintedGrid(BaseDefinition *parent): - BaseDefinition(parent) + BaseDefinition(parent, "grid") { merged_data = new PaintedGridData; brush_data = new PaintedGridData; diff --git a/src/definition/Scenery.cpp b/src/definition/Scenery.cpp index e9fa39c..402251e 100644 --- a/src/definition/Scenery.cpp +++ b/src/definition/Scenery.cpp @@ -15,7 +15,7 @@ static const double APP_HEADER = 19866544632.125; static const int DATA_VERSION = 1; Scenery::Scenery(): - BaseDefinition(NULL) + BaseDefinition(NULL, "scenery") { addChild(atmosphere = new AtmosphereDefinition(this)); addChild(camera = new CameraDefinition); diff --git a/src/definition/TerrainDefinition.cpp b/src/definition/TerrainDefinition.cpp index 45c5a18..36b0f4a 100644 --- a/src/definition/TerrainDefinition.cpp +++ b/src/definition/TerrainDefinition.cpp @@ -5,7 +5,7 @@ #include "PackStream.h" TerrainDefinition::TerrainDefinition(BaseDefinition* parent): - BaseDefinition(parent) + BaseDefinition(parent, "terrain") { height = 1.0; scaling = 1.0; diff --git a/src/definition/TextureLayerDefinition.cpp b/src/definition/TextureLayerDefinition.cpp index 9eb143a..ebf3688 100644 --- a/src/definition/TextureLayerDefinition.cpp +++ b/src/definition/TextureLayerDefinition.cpp @@ -8,7 +8,7 @@ #include "TerrainDefinition.h" TextureLayerDefinition::TextureLayerDefinition(BaseDefinition* parent): - BaseDefinition(parent) + BaseDefinition(parent, "texture") { terrain_zone = new Zone; _displacement_noise = new NoiseGenerator; diff --git a/src/definition/TexturesDefinition.cpp b/src/definition/TexturesDefinition.cpp index 31d12ee..9c1dc94 100644 --- a/src/definition/TexturesDefinition.cpp +++ b/src/definition/TexturesDefinition.cpp @@ -7,8 +7,8 @@ static BaseDefinition* _layer_constructor(Layers* parent) return new TextureLayerDefinition(parent); } -TexturesDefinition::TexturesDefinition(BaseDefinition *parent) - : Layers(parent, _layer_constructor) +TexturesDefinition::TexturesDefinition(BaseDefinition *parent): + Layers(parent, "textures", _layer_constructor) { } diff --git a/src/definition/WaterDefinition.cpp b/src/definition/WaterDefinition.cpp index e88ea03..6f98ff4 100644 --- a/src/definition/WaterDefinition.cpp +++ b/src/definition/WaterDefinition.cpp @@ -6,7 +6,7 @@ #include "SurfaceMaterial.h" WaterDefinition::WaterDefinition(BaseDefinition* parent): - BaseDefinition(parent) + BaseDefinition(parent, "water") { material = new SurfaceMaterial; depth_color = new Color; diff --git a/src/definition/Zone.cpp b/src/definition/Zone.cpp index 04cf686..b109052 100644 --- a/src/definition/Zone.cpp +++ b/src/definition/Zone.cpp @@ -6,7 +6,7 @@ #include "Vector3.h" Zone::Zone(BaseDefinition *parent): - BaseDefinition(parent) + BaseDefinition(parent, "zone") { value_by_height = new Curve; absolute_height = 1; diff --git a/src/interface/modeler/quickapp/MainModelerWindow.cpp b/src/interface/modeler/quickapp/MainModelerWindow.cpp index a96f5c3..4013b81 100644 --- a/src/interface/modeler/quickapp/MainModelerWindow.cpp +++ b/src/interface/modeler/quickapp/MainModelerWindow.cpp @@ -1,5 +1,6 @@ #include "MainModelerWindow.h" +#include "Logs.h" #include "OpenGLView.h" #include "Scenery.h" #include "OpenGLRenderer.h" @@ -17,6 +18,9 @@ MainModelerWindow::MainModelerWindow() { scenery = new Scenery(); scenery->autoPreset(); + + logDebug("Initialized scenery:\n%s", QString::fromStdString(scenery->toString()).toLatin1().data()); + renderer = new OpenGLRenderer(scenery); render_preview_provider = new RenderPreviewProvider(); diff --git a/src/tests/BaseDefinition_Test.cpp b/src/tests/BaseDefinition_Test.cpp new file mode 100644 index 0000000..6e2e269 --- /dev/null +++ b/src/tests/BaseDefinition_Test.cpp @@ -0,0 +1,14 @@ +#include "BaseTestCase.h" + +#include "BaseDefinition.h" + +TEST(BaseDefinition, toString) +{ + BaseDefinition root(NULL, "root"); + BaseDefinition branch(&root, "branch"); + BaseDefinition leaf1(&branch, "leaf1"); + BaseDefinition leaf2(&branch, "leaf2"); + + EXPECT_EQ("root\n branch\n leaf1\n leaf2", root.toString()); + EXPECT_EQ("branch\n leaf1\n leaf2", branch.toString()); +} diff --git a/src/tests/Layers_Test.cpp b/src/tests/Layers_Test.cpp index 6f6a057..9fa1dfa 100644 --- a/src/tests/Layers_Test.cpp +++ b/src/tests/Layers_Test.cpp @@ -5,25 +5,24 @@ BaseDefinition* _construc1(Layers*) { - return new BaseDefinition(NULL); + return new BaseDefinition(NULL, "test"); } BaseDefinition* _construc2(Layers* parent) { - BaseDefinition* result = new BaseDefinition(parent); - result->setName("test"); + BaseDefinition* result = new BaseDefinition(parent, "test"); return result; } TEST(Layers, Constructor) { - Layers layers1(NULL, _construc1); + Layers layers1(NULL, "test", _construc1); EXPECT_EQ(0, layers1.count()); } TEST(Layers, NullLayer) { - Layers layers1(NULL, _construc1); + Layers layers1(NULL, "test", _construc1); for (int i = -2; i < 5; i++) { @@ -33,7 +32,7 @@ TEST(Layers, NullLayer) EXPECT_EQ(NULL, layer->getParent()); EXPECT_EQ(layer, layer->getRoot()); - EXPECT_EQ("", layer->getName()); + EXPECT_EQ("test", layer->getName()); } EXPECT_EQ(0, layers1.count()); @@ -41,12 +40,12 @@ TEST(Layers, NullLayer) TEST(Layers, copy) { - Layers layers1(NULL, _construc2); + Layers layers1(NULL, "test", _construc2); layers1.addLayer(); EXPECT_EQ(1, layers1.count()); // Copy with the same constructor - Layers layers2(NULL, _construc2); + Layers layers2(NULL, "test", _construc2); EXPECT_EQ(0, layers2.count()); layers1.copy(&layers2); EXPECT_EQ(1, layers2.count()); @@ -59,7 +58,7 @@ TEST(Layers, copy) TEST(Layers, maxLayerCount) { - Layers layers1(NULL, _construc1); + Layers layers1(NULL, "test", _construc1); layers1.setMaxLayerCount(2); EXPECT_EQ(0, layers1.count()); diff --git a/src/tests/tests.pro b/src/tests/tests.pro index cc1d578..66802c7 100644 --- a/src/tests/tests.pro +++ b/src/tests/tests.pro @@ -22,7 +22,8 @@ SOURCES += main.cpp \ CanvasPortion_Test.cpp \ CanvasPreview_Test.cpp \ AtmosphereDefinition_Test.cpp \ - Scenery_Test.cpp + Scenery_Test.cpp \ + BaseDefinition_Test.cpp HEADERS += \ BaseTestCase.h