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 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'Canvas.cpp') 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; + } } -- cgit v1.2.3