aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-13 14:05:11 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-13 14:05:11 +0200
commit165c1ae6e4a4eea35d7ea2f2a6518ff36cf0112f (patch)
tree78ab1efe1154551581808814ff962154b053ae23
parent1a30375e369d2d872cb3fd6ecdc6019136c7b1a4 (diff)
separate view and museum thread
-rw-r--r--Artist.cpp7
-rw-r--r--CMakeLists.txt1
-rw-r--r--Museum.cpp27
-rw-r--r--Museum.h18
-rw-r--r--People.cpp7
-rw-r--r--People.h2
-rw-r--r--View.cpp69
-rw-r--r--View.h18
-rw-r--r--ViewController.cpp37
-rw-r--r--ViewController.h12
-rw-r--r--input/test.csv3
-rw-r--r--main.cpp24
12 files changed, 166 insertions, 59 deletions
diff --git a/Artist.cpp b/Artist.cpp
new file mode 100644
index 0000000..a01f9e2
--- /dev/null
+++ b/Artist.cpp
@@ -0,0 +1,7 @@
+#include "Artist.h"
+
+void Artist::update() {
+ this->data.x += this->data.vx;
+ this->data.y += this->data.vy;
+}
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ac68e5..681aae6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,6 +41,7 @@ add_executable(main
View.cpp
Museum.cpp
People.cpp
+ Artist.cpp
)
target_link_libraries(main
diff --git a/Museum.cpp b/Museum.cpp
index 7c2a722..6962e20 100644
--- a/Museum.cpp
+++ b/Museum.cpp
@@ -1,5 +1,32 @@
#include "Museum.h"
+using namespace std;
+
+Museum::Museum() {
+ this->worker = new std::thread(&Museum::work, this);
+}
+
+Museum::~Museum() {
+ this->working = false;
+ this->worker->join();
+ if (this->worker != nullptr) {
+ delete this->worker;
+ this->worker = nullptr;
+ }
+}
+
void Museum::update() {
+ this->people.update();
+ // this->canvas.update();
+}
+
+void Museum::work() {
+ while (this->working) {
+ if (this->paused) continue; // wait if paused
+
+ auto next = chrono::steady_clock::now() + this->tick_interval;
+ this->update();
+ this_thread::sleep_until(next);
+ }
}
diff --git a/Museum.h b/Museum.h
index 2677041..ce4db3a 100644
--- a/Museum.h
+++ b/Museum.h
@@ -1,14 +1,32 @@
#pragma once
+#include <thread>
+#include <chrono>
+
+using namespace std::chrono_literals;
+
#include "People.h"
#include "Canvas.h"
class Museum {
public:
+ Museum();
+ virtual ~Museum();
+
+public:
People people;
Canvas canvas;
public:
+ bool paused = false;
+
+private:
void update();
+
+private:
+ static constexpr std::chrono::milliseconds tick_interval = 10ms;
+ bool working = true;
+ std::thread * worker = nullptr;
+ void work();
};
diff --git a/People.cpp b/People.cpp
index 1b3bfb9..80d2c8a 100644
--- a/People.cpp
+++ b/People.cpp
@@ -38,3 +38,10 @@ string People::to_string() {
return out;
}
+void People::update() {
+ for (Artist * artist : this->artists) {
+ if (artist == nullptr) continue;
+ artist->update();
+ }
+}
+
diff --git a/People.h b/People.h
index 5b3728c..ea5dcf4 100644
--- a/People.h
+++ b/People.h
@@ -18,6 +18,8 @@ public:
Artist & get_artist(size_t index);
size_t artists_size();
+ void update();
+
private:
std::vector<Artist *> artists;
};
diff --git a/View.cpp b/View.cpp
index 5671c1f..8ca7d94 100644
--- a/View.cpp
+++ b/View.cpp
@@ -2,30 +2,63 @@
#include <thread>
#include "View.h"
+#include "ViewController.h"
-View::View() {
+View::View(ViewController & vc) : controller(vc) {
+ this->worker = new std::thread(&View::work, this);
+}
+
+View::~View() {
+ this->open = false;
+ this->worker->join();
+ if (this->worker != nullptr) {
+ delete this->worker;
+ this->worker = nullptr;
+ }
+}
+
+void View::work() {
+ this->window_init();
+
+ while (this->open) {
+ for (SDL_Event e; SDL_PollEvent(&e);) {
+ if (e.type == SDL_QUIT) {
+ this->open = false;
+ return;
+ }
+ }
+
+ this->draw_begin();
+ this->controller.update(*this);
+ this->draw_end();
+ }
+
+ this->window_deinit();
+}
+
+void View::window_init() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
this->window = SDL_CreateWindow(
- "dpa",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- this->width,
- this->height,
- SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+ "dpa",
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ this->width,
+ this->height,
+ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
);
this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED);
- this->worker = new std::thread(&View::work, this);
}
-View::~View() {
- if (this->renderer != nullptr)
+void View::window_deinit() {
+ if (this->renderer != nullptr) {
SDL_DestroyRenderer(this->renderer);
- if (this->window != nullptr)
+ this->renderer = nullptr;
+ }
+ if (this->window != nullptr) {
SDL_DestroyWindow(this->window);
- if (this->worker != nullptr) {
- this->open = false;
- this->worker->join();
+ this->window = nullptr;
}
+
SDL_Quit();
}
@@ -37,14 +70,6 @@ void View::set_size(unsigned int width, unsigned int height) {
this->height = height;
}
-void View::work() {
- while (this->open) {
- SDL_Event e;
- while (SDL_PollEvent(&e))
- if (e.type == SDL_QUIT) this->open = false;
- }
-}
-
void View::draw_begin() {
SDL_RenderClear(this->renderer);
}
diff --git a/View.h b/View.h
index e26c56c..58dc33d 100644
--- a/View.h
+++ b/View.h
@@ -1,25 +1,35 @@
#pragma once
-#include "Color.h"
-#include "Rectangle.h"
#include <SDL2/SDL.h>
#include <thread>
+#include "Color.h"
+#include "Rectangle.h"
+
+class ViewController;
+
class View {
public:
- View();
+ View(ViewController & vc);
virtual ~View();
public:
virtual void set_size(unsigned int width, unsigned int height);
- virtual void draw_begin();
virtual void draw_rect(Rectangle r, Color c);
+
+private:
+ virtual void draw_begin();
virtual void draw_end();
+ virtual void window_init();
+ virtual void window_deinit();
public:
bool open = true;
private:
+ ViewController & controller;
+
+private:
unsigned int width = 0, height = 0;
SDL_Window * window = nullptr;
SDL_Renderer * renderer = nullptr;
diff --git a/ViewController.cpp b/ViewController.cpp
index 9723dbd..e6ffe2e 100644
--- a/ViewController.cpp
+++ b/ViewController.cpp
@@ -2,32 +2,37 @@
#include "View.h"
#include "Museum.h"
-ViewController::ViewController(View & v, Museum & m) : view(v), museum(m) {};
+ViewController::ViewController(Museum & m) : 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);
+void ViewController::update(View & view) {
+ this->update_size(view);
+ this->update_tiles(view);
+ this->update_artists(view);
+}
- this->view.draw_begin();
+void ViewController::update_size(View & view) {
+ view.set_size(
+ this->scale * this->museum.canvas.data.columns,
+ this->scale * this->museum.canvas.data.rows
+ );
+}
- // draw tiles
- for (unsigned y = 0; y < height; y++) {
- for (unsigned x = 0; x < width; x++) {
- Tile & tile = canvas.get_tile(x, y);
+void ViewController::update_tiles(View & view) {
+ for (unsigned y = 0; y < this->museum.canvas.data.rows; y++) {
+ for (unsigned x = 0; x < this->museum.canvas.data.columns; x++) {
+ Tile & tile = this->museum.canvas.get_tile(x, y);
Rectangle rect = {
.x = x * scale,
.y = y * scale,
.width = scale - line_width,
.height = scale - line_width,
};
- this->view.draw_rect(rect, tile.color);
+ view.draw_rect(rect, tile.color);
}
}
+}
- // draw artists
+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);
@@ -37,9 +42,7 @@ void ViewController::update() {
.width = artist_size,
.height = artist_size,
};
- this->view.draw_rect(rect, { 0, 0, 0 });
+ view.draw_rect(rect, { 0, 0, 0 });
}
-
- this->view.draw_end();
}
diff --git a/ViewController.h b/ViewController.h
index 54fa529..8cf46d5 100644
--- a/ViewController.h
+++ b/ViewController.h
@@ -5,18 +5,22 @@ class Museum;
class ViewController {
public:
- ViewController(View & v, Museum & m);
+ ViewController(Museum & m);
public:
- void update();
+ void update(View & v);
+
+private:
+ void update_size(View & v);
+ void update_tiles(View & v);
+ void update_artists(View & v);
private:
- View & view;
Museum & museum;
private:
unsigned int scale = 16;
unsigned int line_width = 1;
- unsigned int artist_size = 4;
+ unsigned int artist_size = (scale - line_width) / 2;
};
diff --git a/input/test.csv b/input/test.csv
new file mode 100644
index 0000000..d7d5ae3
--- /dev/null
+++ b/input/test.csv
@@ -0,0 +1,3 @@
+x,y,vx,vy
+0.5,0.5,0.05,0.0
+0.5,0.5,0.0,0.05
diff --git a/main.cpp b/main.cpp
index 3e151ea..50565a0 100644
--- a/main.cpp
+++ b/main.cpp
@@ -10,9 +10,9 @@
using namespace std;
-void load_museum(Museum & m, int argc, char** argv) {
+void load_museum(Museum & museum, int argc, char** argv) {
Deserializer d {};
- d.set_target(&m);
+ d.set_target(&museum);
for (int i = 1; i < argc; i++) {
char * url = argv[i];
@@ -39,19 +39,19 @@ void load_museum(Museum & m, int argc, char** argv) {
}
int main(int argc, char** argv) {
- Museum m {};
- load_museum(m, argc, argv);
+ Museum museum {};
+ museum.paused = true;
- printf("%s", m.canvas.to_string(true).c_str());
- printf("%s", m.people.to_string().c_str());
+ load_museum(museum, argc, argv);
- View v {};
- ViewController vc {v, m};
+ // printf("%s", museum.canvas.to_string(true).c_str());
+ // printf("%s", museum.people.to_string().c_str());
- while (v.open) {
- m.update();
- vc.update();
- }
+ ViewController controller { museum };
+ View view { controller };
+
+ museum.paused = false;
+ while (view.open);
return EXIT_SUCCESS;
}