aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Artist.cpp20
-rw-r--r--Artist.h10
-rw-r--r--Canvas.cpp6
-rw-r--r--Canvas.h5
-rw-r--r--Deserializer.cpp30
-rw-r--r--Deserializer.h9
-rw-r--r--FileReader.cpp8
-rw-r--r--FileReader.h3
-rw-r--r--HTTPFile.cpp2
-rw-r--r--LocalFile.cpp2
-rw-r--r--Museum.cpp8
-rw-r--r--Museum.h5
-rw-r--r--People.cpp4
-rw-r--r--People.h4
-rw-r--r--ViewController.cpp6
-rw-r--r--ViewController.h2
-rw-r--r--main.cpp53
17 files changed, 97 insertions, 80 deletions
diff --git a/Artist.cpp b/Artist.cpp
index a01f9e2..cf61bdb 100644
--- a/Artist.cpp
+++ b/Artist.cpp
@@ -1,6 +1,24 @@
#include "Artist.h"
+#include "Museum.h"
-void Artist::update() {
+void Artist::update(Museum & m) {
+ this->update_edge_collision(m);
+ this->update_movement(m);
+}
+
+void Artist::update_edge_collision(Museum & museum) {
+ float next_x = this->data.x + this->data.vx;
+ float next_y = this->data.y + this->data.vy;
+
+ // +0.5 is for own size (the assignment explicitly defines the x,y position
+ // of artists as the top-left corner)
+ if ((next_x + 0.5) > museum.canvas.data.columns || next_x < 0)
+ this->data.vx *= -1;
+ if ((next_y + 0.5) > museum.canvas.data.rows || next_y < 0)
+ this->data.vy *= -1;
+}
+
+void Artist::update_movement(Museum & museum) {
this->data.x += this->data.vx;
this->data.y += this->data.vy;
}
diff --git a/Artist.h b/Artist.h
index 8f5ba2e..be66230 100644
--- a/Artist.h
+++ b/Artist.h
@@ -1,13 +1,21 @@
#pragma once
#include "ArtistData.h"
+#include "Color.h"
+
+class Museum;
class Artist {
public:
- void update();
+ void update(Museum & m);
+
+private:
+ void update_movement(Museum & m);
+ void update_edge_collision(Museum & m);
public:
ArtistData data;
+ Color color;
};
diff --git a/Canvas.cpp b/Canvas.cpp
index c2c5362..723ebff 100644
--- a/Canvas.cpp
+++ b/Canvas.cpp
@@ -22,7 +22,11 @@ size_t Canvas::pos_to_index(unsigned x, unsigned y) {
return index;
}
-void Canvas::update() {
+void Canvas::update(Museum & museum) {
+}
+
+void Canvas::set_data(CanvasData data) {
+ this->data = data;
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;
diff --git a/Canvas.h b/Canvas.h
index 2c417ad..7917d95 100644
--- a/Canvas.h
+++ b/Canvas.h
@@ -6,6 +6,8 @@
#include "CanvasData.h"
#include "Tile.h"
+class Museum;
+
class Canvas {
public:
Canvas() = default;
@@ -18,7 +20,8 @@ public:
public:
CanvasData data;
- void update();
+ void update(Museum & m);
+ void set_data(CanvasData data);
private:
std::vector<Tile *> tiles;
diff --git a/Deserializer.cpp b/Deserializer.cpp
index b6f1cc3..3c1e580 100644
--- a/Deserializer.cpp
+++ b/Deserializer.cpp
@@ -1,40 +1,18 @@
#include "Deserializer.h"
-#include "Exception.h"
#include "TileAppearance.h"
-void Deserializer::set_target(Museum * museum) {
- this->museum = museum;
-}
-
-Deserializer::~Deserializer() {
- this->finalize();
-}
-
-void Deserializer::finalize() {
- // TODO: remove artists outside the canvas
-}
-
-Museum & Deserializer::get_target() {
- if (this->museum == nullptr)
- throw Exception("no museum given to Deserializer");
-
- return *this->museum;
-}
+Deserializer::Deserializer(Museum & museum) : museum(museum) { }
void Deserializer::add_artist(ArtistData data) {
- Museum & museum = this->get_target();
- museum.people.add_artist(data);
+ this->museum.people.add_artist(data);
}
void Deserializer::set_canvas(CanvasData data) {
- Museum & museum = this->get_target();
- museum.canvas.data = data;
- museum.canvas.update();
+ this->museum.canvas.set_data(data);
}
void Deserializer::add_tile(unsigned int x, unsigned int y, TileData data) {
- Museum & museum = this->get_target();
- museum.canvas.set_tile(x, y, data);
+ this->museum.canvas.set_tile(x, y, data);
}
void Deserializer::add_type(std::string type, Color color, unsigned int weight) {
diff --git a/Deserializer.h b/Deserializer.h
index 655caca..bdbcde0 100644
--- a/Deserializer.h
+++ b/Deserializer.h
@@ -8,11 +8,9 @@
class Deserializer {
public:
- Deserializer() = default;
- virtual ~Deserializer();
+ Deserializer(Museum & m);
public:
- void set_target(Museum * m);
void add_type(std::string type, Color color, unsigned int weight);
void set_canvas(CanvasData);
@@ -20,10 +18,7 @@ public:
void add_tile(unsigned int x, unsigned int y, TileData);
void add_artist(ArtistData);
- void finalize();
-
private:
- Museum & get_target();
- Museum * museum = nullptr;
+ Museum & museum;
};
diff --git a/FileReader.cpp b/FileReader.cpp
index 2c7a50e..d2d888f 100644
--- a/FileReader.cpp
+++ b/FileReader.cpp
@@ -1,10 +1,14 @@
+#include <memory>
+
#include "FileReader.h"
#include "FileStrategy.h"
-FileStrategy & FileReader::open(const std::string url) {
+using namespace std;
+
+unique_ptr<FileStrategy> FileReader::open(const std::string url) {
FileStrategy * reader = find_reader(url)->clone();
reader->open(url);
- return *reader;
+ return unique_ptr<FileStrategy>(reader);
}
void FileReader::register_strategy(const std::string type, const FileStrategy * node) {
diff --git a/FileReader.h b/FileReader.h
index 90d5e32..1cf62ba 100644
--- a/FileReader.h
+++ b/FileReader.h
@@ -2,6 +2,7 @@
#include <string>
#include <map>
+#include <memory>
#include "FileStrategy.h"
@@ -9,7 +10,7 @@ using FactoryMap = std::map<std::string, const FileStrategy *>;
class FileReader {
public:
- static FileStrategy & open(const std::string url);
+ static std::unique_ptr<FileStrategy> open(const std::string url);
private:
FileReader() = default;
diff --git a/HTTPFile.cpp b/HTTPFile.cpp
index fded2d3..6da0919 100644
--- a/HTTPFile.cpp
+++ b/HTTPFile.cpp
@@ -15,7 +15,7 @@ const std::string HTTPFile::read() {
}
HTTPFile::~HTTPFile() {
- close();
+ this->close();
}
HTTPFile * HTTPFile::clone() const {
diff --git a/LocalFile.cpp b/LocalFile.cpp
index c21988e..bdc25ab 100644
--- a/LocalFile.cpp
+++ b/LocalFile.cpp
@@ -42,7 +42,7 @@ const std::string LocalFile::read() {
}
LocalFile::~LocalFile() {
- close();
+ this->close();
if (this->file != nullptr) {
delete this->file;
diff --git a/Museum.cpp b/Museum.cpp
index 6962e20..76931b7 100644
--- a/Museum.cpp
+++ b/Museum.cpp
@@ -16,8 +16,8 @@ Museum::~Museum() {
}
void Museum::update() {
- this->people.update();
- // this->canvas.update();
+ this->people.update(*this);
+ this->canvas.update(*this);
}
void Museum::work() {
@@ -30,3 +30,7 @@ void Museum::work() {
}
}
+void Museum::set_pause(bool paused) {
+ this->paused = paused;
+}
+
diff --git a/Museum.h b/Museum.h
index ce4db3a..378e449 100644
--- a/Museum.h
+++ b/Museum.h
@@ -18,10 +18,11 @@ public:
Canvas canvas;
public:
- bool paused = false;
+ void set_pause(bool paused);
+ void update();
private:
- void update();
+ bool paused = false;
private:
static constexpr std::chrono::milliseconds tick_interval = 10ms;
diff --git a/People.cpp b/People.cpp
index 80d2c8a..5bc11f7 100644
--- a/People.cpp
+++ b/People.cpp
@@ -38,10 +38,10 @@ string People::to_string() {
return out;
}
-void People::update() {
+void People::update(Museum & museum) {
for (Artist * artist : this->artists) {
if (artist == nullptr) continue;
- artist->update();
+ artist->update(museum);
}
}
diff --git a/People.h b/People.h
index ea5dcf4..0b7277b 100644
--- a/People.h
+++ b/People.h
@@ -6,6 +6,8 @@
#include "Artist.h"
#include "ArtistData.h"
+class Museum;
+
class People {
public:
People() = default;
@@ -18,7 +20,7 @@ public:
Artist & get_artist(size_t index);
size_t artists_size();
- void update();
+ void update(Museum & m);
private:
std::vector<Artist *> artists;
diff --git a/ViewController.cpp b/ViewController.cpp
index e6ffe2e..ea84bb8 100644
--- a/ViewController.cpp
+++ b/ViewController.cpp
@@ -37,12 +37,12 @@ void ViewController::update_artists(View & view) {
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)),
+ .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, { 0, 0, 0 });
+ view.draw_rect(rect, artist.color);
}
}
diff --git a/ViewController.h b/ViewController.h
index 8cf46d5..bffd0b2 100644
--- a/ViewController.h
+++ b/ViewController.h
@@ -20,7 +20,7 @@ private:
private:
unsigned int scale = 16;
- unsigned int line_width = 1;
+ unsigned int line_width = 0;
unsigned int artist_size = (scale - line_width) / 2;
};
diff --git a/main.cpp b/main.cpp
index 50565a0..40be196 100644
--- a/main.cpp
+++ b/main.cpp
@@ -10,39 +10,37 @@
using namespace std;
-void load_museum(Museum & museum, int argc, char** argv) {
- Deserializer d {};
- d.set_target(&museum);
-
- for (int i = 1; i < argc; i++) {
- char * url = argv[i];
- FileStrategy * f = nullptr;
-
- try {
- f = &FileReader::open(url);
- } catch (Exception & e) {
- printf("FileStrategy open error: %s\n", e.what());
- exit(EXIT_FAILURE);
- }
-
- try {
- Parser::parse(*f, d);
- } catch (Exception & e) {
- printf("Parser error: %s (%s)\n", e.what(), url);
- exit(EXIT_FAILURE);
- }
-
- f->close();
+static unique_ptr<FileStrategy> open(const char * url) noexcept {
+ try {
+ unique_ptr<FileStrategy> file = FileReader::open(url);
+ return file;
+ } catch (Exception & e) {
+ printf("FileStrategy open error: %s\n", e.what());
+ exit(EXIT_FAILURE);
}
+}
- d.finalize();
+static void parse(FileStrategy & file, Deserializer & deserializer, const char * url) noexcept {
+ try {
+ Parser::parse(file, deserializer);
+ } catch (Exception & e) {
+ printf("Parser error: %s (%s)\n", e.what(), url);
+ exit(EXIT_FAILURE);
+ }
}
int main(int argc, char** argv) {
Museum museum {};
- museum.paused = true;
+ museum.set_pause(true);
- load_museum(museum, argc, argv);
+ Deserializer deserializer { museum };
+
+ for (int i = 1; i < argc; i++) {
+ char * url = argv[i];
+
+ unique_ptr<FileStrategy> file = open(url);
+ parse(*file, deserializer, url);
+ }
// printf("%s", museum.canvas.to_string(true).c_str());
// printf("%s", museum.people.to_string().c_str());
@@ -50,7 +48,8 @@ int main(int argc, char** argv) {
ViewController controller { museum };
View view { controller };
- museum.paused = false;
+ museum.set_pause(false);
+
while (view.open);
return EXIT_SUCCESS;