diff options
Diffstat (limited to 'View.cpp')
-rw-r--r-- | View.cpp | 69 |
1 files changed, 47 insertions, 22 deletions
@@ -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); } |