aboutsummaryrefslogtreecommitdiff
path: root/Canvas.cpp
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 /Canvas.cpp
parenta6cba2758e1b8cbc189d623c1bfdbc146a8ba87e (diff)
fix double tiles
Diffstat (limited to 'Canvas.cpp')
-rw-r--r--Canvas.cpp29
1 files changed, 26 insertions, 3 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;
+ }
}