#include #include #include "Canvas.h" #include "util.h" using namespace std; Tile & Canvas::get_tile(unsigned x, unsigned y) { return *this->tiles[this->pos_to_index(x, y)]; } void Canvas::set_tile(unsigned x, unsigned y, TileData 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) { 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; } } 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; if (type_str.length() == 0) type_str = "."; if (truecolor) out += stringf("\e[48;2;%d;%d;%dm", tile.color.red, tile.color.green, tile.color.blue); out += stringf("%-2s ", type_str.c_str()); } if (truecolor) out += "\e[0m"; out += "\n"; } return out; }