From 6319b7c8bf1689c990f8849c76e1871bcea9584d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 15 Oct 2024 14:42:40 +0200 Subject: use C++-style structs and move tile XY to tile.data --- Artist.h | 4 ++-- ArtistData.h | 12 ++++++------ Canvas.cpp | 11 ++++++----- Canvas.h | 4 ++-- CanvasData.h | 8 ++++---- Color.h | 10 +++++----- Deserializer.cpp | 4 ++-- Deserializer.h | 3 +-- Museum.cpp | 1 + Museum.h | 1 + People.cpp | 1 + Tile.cpp | 4 ++-- Tile.h | 3 --- TileData.h | 8 +++++--- XMLParser.cpp | 13 ++++++------- YellowTileBehavior.cpp | 4 ++-- readme.md | 7 +++++-- 17 files changed, 51 insertions(+), 47 deletions(-) diff --git a/Artist.h b/Artist.h index 80d24cb..49d0393 100644 --- a/Artist.h +++ b/Artist.h @@ -17,8 +17,8 @@ private: void update_edge_collision(); public: - ArtistData data = { 0 }; - Color color = { 0 }; + ArtistData data; + Color color; bool step = false; diff --git a/ArtistData.h b/ArtistData.h index ddee528..1c3c88c 100644 --- a/ArtistData.h +++ b/ArtistData.h @@ -1,9 +1,9 @@ #pragma once -typedef struct { - float x; - float y; - float vx; - float vy; -} ArtistData; +struct ArtistData { + float x = 0.0; + float y = 0.0; + float vx = 0.0; + float vy = 0.0; +}; diff --git a/Canvas.cpp b/Canvas.cpp index 94ffe14..423670d 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -13,13 +13,11 @@ 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 data) { - size_t index = this->pos_to_index(x, y); +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); - this->tiles[index]->x = x; - this->tiles[index]->y = y; } size_t Canvas::pos_to_index(unsigned x, unsigned y) { @@ -39,7 +37,10 @@ void Canvas::set_data(CanvasData data) { for (size_t x = 0; x < this->data.columns; x++) { if (this->tiles[this->pos_to_index(x, y)] != nullptr) continue; - this->set_tile(x, y, {}); + this->set_tile({ + .x = static_cast(x), + .y = static_cast(y), + }); } } } diff --git a/Canvas.h b/Canvas.h index 2859bc0..03ab00a 100644 --- a/Canvas.h +++ b/Canvas.h @@ -16,10 +16,10 @@ public: public: virtual Tile & get_tile(unsigned x, unsigned y); - virtual void set_tile(unsigned x, unsigned y, TileData data); + virtual void set_tile(TileData data); public: - CanvasData data = { 0 }; + CanvasData data; void update(); void set_data(CanvasData data); diff --git a/CanvasData.h b/CanvasData.h index 19812da..d2b0666 100644 --- a/CanvasData.h +++ b/CanvasData.h @@ -1,7 +1,7 @@ #pragma once -typedef struct { - unsigned rows; - unsigned columns; -} CanvasData; +struct CanvasData { + unsigned rows = 0; + unsigned columns = 0; +}; diff --git a/Color.h b/Color.h index 858b641..87d27b0 100644 --- a/Color.h +++ b/Color.h @@ -1,8 +1,8 @@ #pragma once -typedef struct { - unsigned int red; - unsigned int green; - unsigned int blue; -} Color; +struct Color { + unsigned int red = 0; + unsigned int green = 0; + unsigned int blue = 0; +}; diff --git a/Deserializer.cpp b/Deserializer.cpp index 3c1e580..7215dd4 100644 --- a/Deserializer.cpp +++ b/Deserializer.cpp @@ -11,8 +11,8 @@ void Deserializer::set_canvas(CanvasData data) { this->museum.canvas.set_data(data); } -void Deserializer::add_tile(unsigned int x, unsigned int y, TileData data) { - this->museum.canvas.set_tile(x, y, data); +void Deserializer::set_tile(TileData data) { + this->museum.canvas.set_tile(data); } void Deserializer::add_type(std::string type, Color color, unsigned int weight) { diff --git a/Deserializer.h b/Deserializer.h index bdbcde0..50ae57d 100644 --- a/Deserializer.h +++ b/Deserializer.h @@ -14,8 +14,7 @@ public: void add_type(std::string type, Color color, unsigned int weight); void set_canvas(CanvasData); - - void add_tile(unsigned int x, unsigned int y, TileData); + void set_tile(TileData); void add_artist(ArtistData); private: diff --git a/Museum.cpp b/Museum.cpp index 361afaf..0003162 100644 --- a/Museum.cpp +++ b/Museum.cpp @@ -27,6 +27,7 @@ void Museum::work() { auto next = chrono::steady_clock::now() + this->tick_interval; this->update(); this_thread::sleep_until(next); + this->tick++; } } diff --git a/Museum.h b/Museum.h index 9ded7a1..33ad7e1 100644 --- a/Museum.h +++ b/Museum.h @@ -23,6 +23,7 @@ public: void update(); private: + unsigned long long tick = 0; bool paused = false; private: diff --git a/People.cpp b/People.cpp index a6e65d5..4cd8a05 100644 --- a/People.cpp +++ b/People.cpp @@ -40,6 +40,7 @@ string People::to_string() { out += stringf("%d artists\n", this->artist_count); for (Artist * artist : this->artists) { + if (artist == nullptr) continue; out += stringf("- at (%.2f,%.2f)\n", artist->data.x, artist->data.y); } diff --git a/Tile.cpp b/Tile.cpp index 383ef05..a891f15 100644 --- a/Tile.cpp +++ b/Tile.cpp @@ -32,8 +32,8 @@ void Tile::step(Artist * artist) { Tile * Tile::get_neighbor(int dx, int dy) { Canvas & canvas = this->museum.canvas; - int x = this->x + dx; - int y = this->y + dy; + int x = this->data.x + dx; + int y = this->data.y + dy; if (x < 0) return nullptr; if (x >= canvas.data.columns) return nullptr; if (y < 0) return nullptr; diff --git a/Tile.h b/Tile.h index 1ddbc7a..446b991 100644 --- a/Tile.h +++ b/Tile.h @@ -17,9 +17,6 @@ public: Color color; std::unique_ptr behavior = nullptr; - unsigned int x = 0; - unsigned int y = 0; - public: void set_data(TileData & data); void set_type(const std::string & type); diff --git a/TileData.h b/TileData.h index 406947f..87d9003 100644 --- a/TileData.h +++ b/TileData.h @@ -2,7 +2,9 @@ #include -typedef struct { - std::string type; -} TileData; +struct TileData { + unsigned int x = 0; + unsigned int y = 0; + std::string type = ""; +}; diff --git a/XMLParser.cpp b/XMLParser.cpp index 3d6e6cf..6be26af 100644 --- a/XMLParser.cpp +++ b/XMLParser.cpp @@ -5,6 +5,7 @@ #include "XMLParser.h" #include "Exception.h" #include "Parser.h" +#include "TileData.h" using namespace std; @@ -62,13 +63,11 @@ void XMLParser::parse(FileStrategy & f, Deserializer & d) { if (!nodes) throw Exception("missing "); for (xml_node node : nodes) { - d.add_tile( - node.attribute("x").as_uint(), - node.attribute("y").as_uint(), - { - .type = node.name(), - } - ); + d.set_tile({ + .x = node.attribute("x").as_uint(), + .y = node.attribute("y").as_uint(), + .type = node.name(), + }); } } diff --git a/YellowTileBehavior.cpp b/YellowTileBehavior.cpp index 0c2ee35..4e35733 100644 --- a/YellowTileBehavior.cpp +++ b/YellowTileBehavior.cpp @@ -26,8 +26,8 @@ void YellowTileBehavior::update(Tile & tile) { for (unsigned int i = 0; i < new_artists; i++) { if (i >= 2) break; ArtistData new_data = { - .x = static_cast(tile.x), - .y = static_cast(tile.y), + .x = static_cast(tile.data.x), + .y = static_cast(tile.data.y), }; float velocity = random_float(rng); random_bool(rng) ? new_data.vx = velocity : new_data.vy = velocity; diff --git a/readme.md b/readme.md index 6254800..8437c65 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,10 @@ # TODO +DPA: + - state snapshots (memento) -- send commands from GUI + +ALGA: + - artist-artist collision behavior -- artist steps on tile behavior -- cgit v1.2.3