aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-12 12:42:46 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-12 12:42:46 +0200
commit31dbd326f69656473067dde6d4f008cd7c04f0b1 (patch)
tree6cc6df2ef6eaedefad81532c9e6f3202b4d0fdd6
parent12a719a9fd9b8e89159c407c3b612b226c860778 (diff)
begin visualization
-rw-r--r--CMakeLists.txt3
-rw-r--r--Museum.cpp5
-rw-r--r--Museum.h3
-rw-r--r--Rectangle.h9
-rw-r--r--View.cpp67
-rw-r--r--View.h25
-rw-r--r--ViewController.cpp12
-rw-r--r--ViewController.h15
-rw-r--r--docs/class-diag.puml10
-rw-r--r--main.cpp10
10 files changed, 155 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index aaf2d07..e24e73f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,6 +37,9 @@ add_executable(main
BlueTileBehavior.cpp
RedTileBehavior.cpp
GrayTileBehavior.cpp
+ ViewController.cpp
+ View.cpp
+ Museum.cpp
)
target_link_libraries(main
diff --git a/Museum.cpp b/Museum.cpp
new file mode 100644
index 0000000..7c2a722
--- /dev/null
+++ b/Museum.cpp
@@ -0,0 +1,5 @@
+#include "Museum.h"
+
+void Museum::update() {
+}
+
diff --git a/Museum.h b/Museum.h
index d7ff57b..2677041 100644
--- a/Museum.h
+++ b/Museum.h
@@ -7,5 +7,8 @@ class Museum {
public:
People people;
Canvas canvas;
+
+public:
+ void update();
};
diff --git a/Rectangle.h b/Rectangle.h
new file mode 100644
index 0000000..a3d0a0d
--- /dev/null
+++ b/Rectangle.h
@@ -0,0 +1,9 @@
+#pragma once
+
+typedef struct {
+ unsigned int x;
+ unsigned int y;
+ unsigned int width;
+ unsigned int height;
+} Rectangle;
+
diff --git a/View.cpp b/View.cpp
new file mode 100644
index 0000000..5671c1f
--- /dev/null
+++ b/View.cpp
@@ -0,0 +1,67 @@
+#include <SDL2/SDL.h>
+#include <thread>
+
+#include "View.h"
+
+View::View() {
+ 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
+ );
+ this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED);
+ this->worker = new std::thread(&View::work, this);
+}
+
+View::~View() {
+ if (this->renderer != nullptr)
+ SDL_DestroyRenderer(this->renderer);
+ if (this->window != nullptr)
+ SDL_DestroyWindow(this->window);
+ if (this->worker != nullptr) {
+ this->open = false;
+ this->worker->join();
+ }
+ SDL_Quit();
+}
+
+void View::set_size(unsigned int width, unsigned int height) {
+ if (this->width == width && this->height == height) return;
+
+ SDL_SetWindowSize(this->window, width, height);
+ this->width = width;
+ 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);
+}
+
+void View::draw_end() {
+ SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255);
+ SDL_RenderPresent(this->renderer);
+}
+
+void View::draw_rect(Rectangle r, Color c) {
+ SDL_SetRenderDrawColor(this->renderer, c.red, c.green, c.blue, 255);
+ SDL_Rect rect = {
+ .x = static_cast<int>(r.x),
+ .y = static_cast<int>(r.y),
+ .w = static_cast<int>(r.width),
+ .h = static_cast<int>(r.height),
+ };
+ SDL_RenderFillRect(this->renderer, &rect);
+}
+
diff --git a/View.h b/View.h
index aa7861e..e26c56c 100644
--- a/View.h
+++ b/View.h
@@ -1,6 +1,31 @@
#pragma once
+#include "Color.h"
+#include "Rectangle.h"
+#include <SDL2/SDL.h>
+#include <thread>
+
class View {
+public:
+ View();
+ 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);
+ virtual void draw_end();
+
+public:
+ bool open = true;
+
+private:
+ unsigned int width = 0, height = 0;
+ SDL_Window * window = nullptr;
+ SDL_Renderer * renderer = nullptr;
+private:
+ std::thread * worker = nullptr;
+ void work();
};
diff --git a/ViewController.cpp b/ViewController.cpp
new file mode 100644
index 0000000..256be90
--- /dev/null
+++ b/ViewController.cpp
@@ -0,0 +1,12 @@
+#include "ViewController.h"
+#include "View.h"
+
+ViewController::ViewController(View & v, Museum & m) : view(v), museum(m) {};
+
+void ViewController::update() {
+ this->view.set_size(100, 100);
+ this->view.draw_begin();
+ this->view.draw_rect({ 10, 10, 50, 50 }, { 255, 0, 255 });
+ this->view.draw_end();
+}
+
diff --git a/ViewController.h b/ViewController.h
index 3f59c93..418082f 100644
--- a/ViewController.h
+++ b/ViewController.h
@@ -1,2 +1,17 @@
#pragma once
+class View;
+class Museum;
+
+class ViewController {
+public:
+ ViewController(View & v, Museum & m);
+
+public:
+ void update();
+
+private:
+ View & view;
+ Museum & museum;
+};
+
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index a59f531..0da5b0f 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -191,12 +191,18 @@ rectangle Group_Model as "Model" <<group>> {
}
rectangle "Visualization" <<group>> {
+ struct Rectangle {
+ x : unsigned int
+ y : unsigned int
+ width : unsigned int
+ height : unsigned int
+ }
+
package SDL2 { }
class View {
-
}
class ViewController {
-
+ + update()
}
ViewController --> View
diff --git a/main.cpp b/main.cpp
index 97d3b17..55fb627 100644
--- a/main.cpp
+++ b/main.cpp
@@ -6,6 +6,7 @@
#include "Deserializer.h"
#include "Parser.h"
#include "View.h"
+#include "ViewController.h"
using namespace std;
@@ -46,10 +47,15 @@ int main(int argc, char** argv) {
Museum m {};
load_museum(m, argc, argv);
+ printf("%s", m.canvas.to_string(true).c_str());
+
View v {};
- load_view(v, m);
+ ViewController vc {v, m};
- printf("%s", m.canvas.to_string(true).c_str());
+ while (v.open) {
+ m.update();
+ vc.update();
+ }
return EXIT_SUCCESS;
}