diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Deserializer.cpp | 2 | ||||
-rw-r--r-- | People.cpp | 40 | ||||
-rw-r--r-- | People.h | 12 | ||||
-rw-r--r-- | ViewController.cpp | 17 | ||||
-rw-r--r-- | ViewController.h | 1 | ||||
-rw-r--r-- | main.cpp | 3 |
7 files changed, 73 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e24e73f..0ac68e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ add_executable(main ViewController.cpp View.cpp Museum.cpp + People.cpp ) target_link_libraries(main diff --git a/Deserializer.cpp b/Deserializer.cpp index 4c4ee91..b6f1cc3 100644 --- a/Deserializer.cpp +++ b/Deserializer.cpp @@ -23,7 +23,7 @@ Museum & Deserializer::get_target() { void Deserializer::add_artist(ArtistData data) { Museum & museum = this->get_target(); - // museum.people.add_artist(data); + museum.people.add_artist(data); } void Deserializer::set_canvas(CanvasData data) { diff --git a/People.cpp b/People.cpp new file mode 100644 index 0000000..1b3bfb9 --- /dev/null +++ b/People.cpp @@ -0,0 +1,40 @@ +#include "People.h" +#include "Exception.h" +#include "util.h" + +using namespace std; + +People::~People() { + for (Artist * artist : this->artists) { + if (artist == nullptr) continue; + delete artist; + } + this->artists.clear(); +} + +void People::add_artist(ArtistData data) { + this->artists.push_back(new Artist(data)); +} + +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]; +} + +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); + } + + return out; +} + @@ -1,16 +1,24 @@ #pragma once -#include <memory> #include <vector> +#include <string> #include "Artist.h" #include "ArtistData.h" class People { public: + People() = default; + virtual ~People(); + std::string to_string(); + +public: void add_artist(ArtistData data); + Artist & get_artist(size_t index); + size_t artists_size(); + private: - std::vector<Artist*> artists; + std::vector<Artist *> artists; }; diff --git a/ViewController.cpp b/ViewController.cpp index 2ed68b5..9723dbd 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -5,12 +5,15 @@ ViewController::ViewController(View & v, Museum & m) : view(v), museum(m) {}; void ViewController::update() { + // set size Canvas & canvas = this->museum.canvas; unsigned width = canvas.data.columns; unsigned height = canvas.data.rows; this->view.set_size(scale * width, scale * height); this->view.draw_begin(); + + // draw tiles for (unsigned y = 0; y < height; y++) { for (unsigned x = 0; x < width; x++) { Tile & tile = canvas.get_tile(x, y); @@ -23,6 +26,20 @@ void ViewController::update() { this->view.draw_rect(rect, tile.color); } } + + // draw artists + People & people = this->museum.people; + for (size_t i = 0; i < people.artists_size(); i++) { + Artist & artist = people.get_artist(i); + Rectangle rect = { + .x = static_cast<unsigned int>(artist.data.x * scale - ((float) artist_size / 2)), + .y = static_cast<unsigned int>(artist.data.y * scale - ((float) artist_size / 2)), + .width = artist_size, + .height = artist_size, + }; + this->view.draw_rect(rect, { 0, 0, 0 }); + } + this->view.draw_end(); } diff --git a/ViewController.h b/ViewController.h index 00db87f..54fa529 100644 --- a/ViewController.h +++ b/ViewController.h @@ -17,5 +17,6 @@ private: private: unsigned int scale = 16; unsigned int line_width = 1; + unsigned int artist_size = 4; }; @@ -42,6 +42,9 @@ int main(int argc, char** argv) { Museum m {}; load_museum(m, argc, argv); + printf("%s", m.canvas.to_string(true).c_str()); + printf("%s", m.people.to_string().c_str()); + View v {}; ViewController vc {v, m}; |