diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-10 00:01:07 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-10 00:01:07 +0200 |
commit | 2fb206efb3dc62508306ca293e808648efbba9f7 (patch) | |
tree | 4cd9cc4fff7bc3a71a3b7d1fa1603f36892da8ac /Canvas.cpp | |
parent | a6cba2758e1b8cbc189d623c1bfdbc146a8ba87e (diff) |
fix double tiles
Diffstat (limited to 'Canvas.cpp')
-rw-r--r-- | Canvas.cpp | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -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; + } } |