aboutsummaryrefslogtreecommitdiff
path: root/View.cpp
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 /View.cpp
parent12a719a9fd9b8e89159c407c3b612b226c860778 (diff)
begin visualization
Diffstat (limited to 'View.cpp')
-rw-r--r--View.cpp67
1 files changed, 67 insertions, 0 deletions
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);
+}
+