From 165c1ae6e4a4eea35d7ea2f2a6518ff36cf0112f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 13 Oct 2024 14:05:11 +0200 Subject: separate view and museum thread --- View.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 22 deletions(-) (limited to 'View.cpp') diff --git a/View.cpp b/View.cpp index 5671c1f..8ca7d94 100644 --- a/View.cpp +++ b/View.cpp @@ -2,30 +2,63 @@ #include #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); } -- cgit v1.2.3