diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 19:16:19 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 19:16:19 +0200 |
commit | 1e0a52b03fe655d7073ef20703dbb2e7646f74d3 (patch) | |
tree | f1709c2e9565d78c791653e71e6a4b26b3138423 /Canvas.cpp | |
parent | 277157b3e06b2deeacbdbc8bf6190de19f88169d (diff) |
add XY struct for 2d points and offsets
Diffstat (limited to 'Canvas.cpp')
-rw-r--r-- | Canvas.cpp | 56 |
1 files changed, 37 insertions, 19 deletions
@@ -1,7 +1,9 @@ +#include <cassert> #include <cstdio> #include <string> #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<int>(data.x), + static_cast<int>(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<unsigned int>(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<int>(artist->data.x), + static_cast<int>(artist->data.y) + }); + tile->behavior->step(artist); } } |