From 2fb206efb3dc62508306ca293e808648efbba9f7 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 10 Oct 2024 00:01:07 +0200 Subject: fix double tiles --- Canvas.cpp | 29 ++++++++++++++++++++++++++--- Canvas.h | 7 ++++++- Deserializer.cpp | 24 +++++++++++++----------- Deserializer.h | 4 +--- People.h | 2 +- Tile.cpp | 6 ++++++ TileData.h | 4 +--- XMLParser.cpp | 4 ++-- input/test.xml | 14 ++++++++++++++ 9 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 input/test.xml diff --git a/Canvas.cpp b/Canvas.cpp index 63dbe74..46d2f03 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -1,14 +1,37 @@ +#include + #include "Canvas.h" Tile & Canvas::get_tile(unsigned x, unsigned y) { - return this->tiles[this->pos_to_index(x, y)]; + return *this->tiles[this->pos_to_index(x, y)]; } void Canvas::set_tile(unsigned x, unsigned y, TileData t) { - this->tiles[this->pos_to_index(x, y)] = Tile(t); + printf("%s(%d, %d)\n", __FUNCTION__, x, y); + size_t index = this->pos_to_index(x, y); + if (this->tiles[index] != nullptr) + delete this->tiles[index]; + this->tiles[index] = new Tile(t); } size_t Canvas::pos_to_index(unsigned x, unsigned y) { - return y * this->data.columns + x; + size_t index = y * this->data.columns + x; + return index; +} + +void Canvas::update() { + this->tiles.resize(this->data.rows * this->data.columns); + for (size_t i = 0; i < this->tiles.size(); i++) { + if (this->tiles[i] != nullptr) continue; + this->tiles[i] = new Tile(); + } +} + +Canvas::~Canvas() { + for (size_t i = 0; i < this->tiles.size(); i++) { + if (this->tiles[i] == nullptr) continue; + delete this->tiles[i]; + this->tiles[i] = nullptr; + } } diff --git a/Canvas.h b/Canvas.h index 4c959f6..59e8f7e 100644 --- a/Canvas.h +++ b/Canvas.h @@ -6,15 +6,20 @@ #include "Tile.h" class Canvas { +public: + Canvas() = default; + virtual ~Canvas(); + public: virtual Tile & get_tile(unsigned x, unsigned y); virtual void set_tile(unsigned x, unsigned y, TileData data); public: CanvasData data; + void update(); private: - std::vector tiles = {}; + std::vector tiles; private: size_t pos_to_index(unsigned x, unsigned y); diff --git a/Deserializer.cpp b/Deserializer.cpp index 48c4da9..03e58b8 100644 --- a/Deserializer.cpp +++ b/Deserializer.cpp @@ -10,7 +10,6 @@ Deserializer::~Deserializer() { } void Deserializer::finalize() { - // TODO: fill empty squares in canvas with white ones // TODO: remove artists outside the canvas } @@ -22,27 +21,30 @@ Museum & Deserializer::get_target() { } void Deserializer::add_artist(ArtistData data) { + printf("add artist(%1.2f, %1.2f)...\n", data.x, data.y); + Museum & museum = this->get_target(); // museum.people.add_artist(data); - printf("add artist(%1.2f, %1.2f)...\n", data.x, data.y); } void Deserializer::set_canvas(CanvasData data) { - Museum & museum = this->get_target(); - // museum.canvas.data = data; printf("set canvas(%dx%d)...\n", data.rows, data.columns); + + Museum & museum = this->get_target(); + museum.canvas.data = data; + museum.canvas.update(); } void Deserializer::add_tile(unsigned int x, unsigned int y, TileData data) { + printf("add tile(%d,%d) data(%c)...\n", x, y, data.type); + Museum & museum = this->get_target(); - // museum.canvas.set_tile(x, y, data); - printf("add tile(%d,%d) data(%s)...\n", x, y, data.type.c_str()); + museum.canvas.set_tile(x, y, data); } -void Deserializer::add_type(std::string type, Color color, unsigned int weight) { - if (type.length() == 0) return; - - printf("add type(%s) color(#%02x%02x%02x) weight(%d)...\n", type.c_str(), - color.red, color.green, color.blue, weight); +void Deserializer::add_type(char type, Color color, unsigned int weight) { + if (type == '\0') return; + printf("add type(%c) color(#%02x%02x%02x) weight(%d)...\n", type, color.red, + color.green, color.blue, weight); } diff --git a/Deserializer.h b/Deserializer.h index 950692c..7a37518 100644 --- a/Deserializer.h +++ b/Deserializer.h @@ -1,7 +1,5 @@ #pragma once -#include - #include "Museum.h" #include "ArtistData.h" #include "TileData.h" @@ -15,7 +13,7 @@ public: public: void set_target(Museum * m); - void add_type(std::string type, Color color, unsigned int weight); + void add_type(char type, Color color, unsigned int weight); void set_canvas(CanvasData); diff --git a/People.h b/People.h index e4490f7..aecf120 100644 --- a/People.h +++ b/People.h @@ -11,6 +11,6 @@ public: void add_artist(ArtistData data); private: - std::vector artists; + std::vector artists; }; diff --git a/Tile.cpp b/Tile.cpp index b461bdf..230bd36 100644 --- a/Tile.cpp +++ b/Tile.cpp @@ -1,5 +1,11 @@ +#include + #include "Tile.h" +Tile::Tile() { + this->data = {}; +} + Tile::Tile(TileData data) { this->data = data; } diff --git a/TileData.h b/TileData.h index 406947f..d941b80 100644 --- a/TileData.h +++ b/TileData.h @@ -1,8 +1,6 @@ #pragma once -#include - typedef struct { - std::string type; + char type; } TileData; diff --git a/XMLParser.cpp b/XMLParser.cpp index 8dda552..431c71b 100644 --- a/XMLParser.cpp +++ b/XMLParser.cpp @@ -48,7 +48,7 @@ void XMLParser::parse(File & f, Deserializer & d) { throw Exception("missing "); for (xml_node node_type : node_types) { d.add_type( - node_type.attribute("tag").as_string(), + node_type.attribute("tag").as_string()[0], { .red = node_type.attribute("red").as_uint(), .green = node_type.attribute("green").as_uint(), @@ -66,7 +66,7 @@ void XMLParser::parse(File & f, Deserializer & d) { node.attribute("x").as_uint(), node.attribute("y").as_uint(), { - .type = node.name(), + .type = node.name()[0], } ); } diff --git a/input/test.xml b/input/test.xml new file mode 100644 index 0000000..42ed1d2 --- /dev/null +++ b/input/test.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + -- cgit v1.2.3