diff options
-rw-r--r-- | Canvas.cpp | 9 | ||||
-rw-r--r-- | Museum.cpp | 1 | ||||
-rw-r--r-- | People.cpp | 22 | ||||
-rw-r--r-- | People.h | 9 | ||||
-rw-r--r-- | Tile.cpp | 2 | ||||
-rw-r--r-- | Tile.h | 2 | ||||
-rw-r--r-- | ViewController.cpp | 9 | ||||
-rw-r--r-- | readme.md | 8 |
8 files changed, 30 insertions, 32 deletions
@@ -75,12 +75,11 @@ string Canvas::to_string(bool truecolor) { } void Canvas::update_steps() { - for (size_t i = 0; i < this->museum.people.artists_size(); i++) { - Artist & artist = this->museum.people.get_artist(i); - if (artist.step == false) continue; - artist.step = false; + for (Artist * artist : this->museum.people.get_artists()) { + if (artist->step == false) continue; + artist->step = false; - this->get_tile(artist.data.x, artist.data.y).step(artist); + this->get_tile(artist->data.x, artist->data.y).step(*artist); } } @@ -26,6 +26,7 @@ void Museum::work() { auto next = chrono::steady_clock::now() + this->tick_interval; this->update(); + printf("%lu artists\n", this->people.get_artists().size()); this_thread::sleep_until(next); } } @@ -1,7 +1,6 @@ #include <algorithm> #include "People.h" -#include "Exception.h" #include "util.h" using namespace std; @@ -16,8 +15,8 @@ People::~People() { this->artists.clear(); } -void People::add_artist(ArtistData data) { - if (this->artists.size() > 30e3) return; +void People::add_artist(const ArtistData & data) { + if (this->artists.size() >= 30e3) return; this->artists.push_back(new Artist(this->museum, data)); } @@ -27,23 +26,16 @@ void People::remove_artist(Artist & target) { this->artists.erase(it); } -size_t People::artists_size() { - return this->artists.size(); -} - -Artist & People::get_artist(size_t index) { - if (index >= this->artists_size()) - throw Exception("No artist with index %lu", index); - return *this->artists[index]; +list<Artist *> People::get_artists() { + return this->artists; } string People::to_string() { string out = ""; - out += stringf("%d artists\n", this->artists_size()); - for (size_t i = 0; i < this->artists_size(); i++) { - Artist & artist = this->get_artist(i); - out += stringf("[%d] at (%.2f,%.2f)\n", i, artist.data.x, artist.data.y); + out += stringf("%d artists\n", this->artists.size()); + for (Artist * artist : this->artists) { + out += stringf("- at (%.2f,%.2f)\n", artist->data.x, artist->data.y); } return out; @@ -1,6 +1,6 @@ #pragma once -#include <vector> +#include <list> #include <string> #include "Artist.h" @@ -15,16 +15,15 @@ public: std::string to_string(); public: - void add_artist(ArtistData data); + void add_artist(const ArtistData & data); void remove_artist(Artist & artist); - Artist & get_artist(size_t index); - size_t artists_size(); + std::list<Artist *> get_artists(); void update(); private: - std::vector<Artist *> artists; + std::list<Artist *> artists; Museum & museum; }; @@ -9,7 +9,7 @@ Tile::Tile(Museum & museum, TileData data) : museum(museum) { this->set_data(data); } -void Tile::set_data(TileData data) { +void Tile::set_data(TileData & data) { this->data = data; this->color = TileAppearance::get_color(this->data.type); this->behavior = TileBehavior::get_strategy(this->data.type).clone(this->museum); @@ -21,7 +21,7 @@ public: unsigned int y = 0; public: - void set_data(TileData data); + void set_data(TileData & data); void update(); void step(Artist &); Tile * get_neighbor(int dx, int dy); diff --git a/ViewController.cpp b/ViewController.cpp index ea84bb8..cafccae 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -34,15 +34,14 @@ void ViewController::update_tiles(View & view) { void ViewController::update_artists(View & view) { People & people = this->museum.people; - for (size_t i = 0; i < people.artists_size(); i++) { - Artist & artist = people.get_artist(i); + for (Artist * artist : people.get_artists()) { Rectangle rect = { - .x = static_cast<unsigned int>(artist.data.x * scale), - .y = static_cast<unsigned int>(artist.data.y * scale), + .x = static_cast<unsigned int>(artist->data.x * scale), + .y = static_cast<unsigned int>(artist->data.y * scale), .width = artist_size, .height = artist_size, }; - view.draw_rect(rect, artist.color); + view.draw_rect(rect, artist->color); } } @@ -5,3 +5,11 @@ - artist-artist collision behavior - artist steps on tile behavior +stability issues: + +- `BlueTileBehavior::update_neighbor` causes segfault pretty consistently when + >15k artists exist +- The view very rarely reads artists that were just deleted by the museum + worker thread (set to nullptr first (atomic), delete later) + + |