From 31dbd326f69656473067dde6d4f008cd7c04f0b1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 12 Oct 2024 12:42:46 +0200 Subject: begin visualization --- View.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 View.cpp (limited to 'View.cpp') 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 +#include + +#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(r.x), + .y = static_cast(r.y), + .w = static_cast(r.width), + .h = static_cast(r.height), + }; + SDL_RenderFillRect(this->renderer, &rect); +} + -- cgit v1.2.3