aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-10 00:01:07 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-10 00:01:07 +0200
commit2fb206efb3dc62508306ca293e808648efbba9f7 (patch)
tree4cd9cc4fff7bc3a71a3b7d1fa1603f36892da8ac
parenta6cba2758e1b8cbc189d623c1bfdbc146a8ba87e (diff)
fix double tiles
-rw-r--r--Canvas.cpp29
-rw-r--r--Canvas.h7
-rw-r--r--Deserializer.cpp24
-rw-r--r--Deserializer.h4
-rw-r--r--People.h2
-rw-r--r--Tile.cpp6
-rw-r--r--TileData.h4
-rw-r--r--XMLParser.cpp4
-rw-r--r--input/test.xml14
9 files changed, 70 insertions, 24 deletions
diff --git a/Canvas.cpp b/Canvas.cpp
index 63dbe74..46d2f03 100644
--- a/Canvas.cpp
+++ b/Canvas.cpp
@@ -1,14 +1,37 @@
+#include <cstdio>
+
#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
@@ -7,14 +7,19 @@
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<Tile> tiles = {};
+ std::vector<Tile *> 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 <string>
-
#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<Artist> artists;
+ std::vector<Artist*> artists;
};
diff --git a/Tile.cpp b/Tile.cpp
index b461bdf..230bd36 100644
--- a/Tile.cpp
+++ b/Tile.cpp
@@ -1,5 +1,11 @@
+#include <cstdio>
+
#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 <string>
-
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 <nodeTypes>");
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 @@
+<?xml version="1.0" ?>
+<canvas rows="3" cols="3">
+ <nodeTypes>
+ <nodeType tag="Y"/>
+ <nodeType tag="B"/>
+ <nodeType tag="R"/>
+ <nodeType tag="G"/>
+ </nodeTypes>
+ <nodes>
+ <Y x="0" y="0"/>
+ <B x="1" y="0"/>
+ <G x="2" y="0"/>
+ </nodes>
+</canvas>