From 1e0a52b03fe655d7073ef20703dbb2e7646f74d3 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 23 Oct 2024 19:16:19 +0200 Subject: add XY struct for 2d points and offsets --- Canvas.cpp | 56 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'Canvas.cpp') diff --git a/Canvas.cpp b/Canvas.cpp index ce51e5c..b7cc8a5 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -1,7 +1,9 @@ +#include #include #include #include "Canvas.h" +#include "Exception.h" #include "util.h" #include "Museum.h" @@ -9,20 +11,32 @@ using namespace std; Canvas::Canvas(Museum & museum) : museum(museum), tile_behavior(museum), tile_color() { } -Tile & Canvas::get_tile(unsigned x, unsigned y) { - return *this->tiles[this->pos_to_index(x, y)]; +Tile * & Canvas::get_tile_unsafe(const XY & pos) { + size_t index = pos.y * this->data.columns + pos.x; + return this->tiles[index]; } -void Canvas::set_tile(TileData data) { - size_t index = this->pos_to_index(data.x, data.y); - if (this->tiles[index] != nullptr) - delete this->tiles[index]; - this->tiles[index] = new Tile(this->museum, data); +Tile & Canvas::get_tile(const XY & pos) { + Exception outside_range("get_tile: position (%d, %d) outside canvas", pos.x, pos.y); + if (pos.x < 0) throw outside_range; + if (pos.y < 0) throw outside_range; + if (pos.x >= this->data.columns) throw outside_range; + if (pos.y >= this->data.rows) throw outside_range; + + Tile * tile = get_tile_unsafe(pos); + assert(tile != nullptr); + + return *tile; } -size_t Canvas::pos_to_index(unsigned x, unsigned y) { - size_t index = y * this->data.columns + x; - return index; +void Canvas::set_tile(TileData data) { + Tile * & tile = this->get_tile_unsafe({ + static_cast(data.x), + static_cast(data.y) + }); + if (tile != nullptr) + delete tile; + tile = new Tile(this->museum, data); } void Canvas::update() { @@ -33,9 +47,9 @@ void Canvas::update() { void Canvas::set_data(CanvasData data) { this->data = data; this->tiles.resize(this->data.rows * this->data.columns); - for (size_t y = 0; y < this->data.rows; y++) { - for (size_t x = 0; x < this->data.columns; x++) { - if (this->tiles[this->pos_to_index(x, y)] != nullptr) + for (int y = 0; y < this->data.rows; y++) { + for (int x = 0; x < this->data.columns; x++) { + if (this->get_tile_unsafe({ x, y }) != nullptr) continue; this->set_tile({ .x = static_cast(x), @@ -56,15 +70,15 @@ Canvas::~Canvas() { string Canvas::to_string(bool truecolor) { string out = ""; - for (size_t y = 0; y < this->data.rows; y++) { - for (size_t x = 0; x < this->data.columns; x++) { - Tile & tile = this->get_tile(x, y); - string type_str = tile.data.type; + for (int y = 0; y < this->data.rows; y++) { + for (int x = 0; x < this->data.columns; x++) { + Tile * tile = this->get_tile_unsafe({ x, y }); + string type_str = tile->data.type; if (type_str.length() == 0) type_str = "."; if (truecolor) out += stringf("\e[38;2;0;0;0;48;2;%d;%d;%dm", - tile.color.red, tile.color.green, tile.color.blue); + tile->color.red, tile->color.green, tile->color.blue); out += stringf("%-2s ", type_str.c_str()); } @@ -80,7 +94,11 @@ void Canvas::update_steps() { if (artist->step == false) continue; artist->step = false; - this->get_tile(artist->data.x, artist->data.y).behavior->step(artist); + Tile * tile = this->get_tile_unsafe({ + static_cast(artist->data.x), + static_cast(artist->data.y) + }); + tile->behavior->step(artist); } } -- cgit v1.2.3