#include #include #include "View.h" #include "ViewController.h" View::View(Museum & m) : controller(m) { 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_EVENT_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", this->width, this->height, SDL_WINDOW_OPENGL ); this->renderer = SDL_CreateRenderer(this->window, NULL); } void View::window_deinit() { if (this->renderer != nullptr) { SDL_DestroyRenderer(this->renderer); this->renderer = nullptr; } if (this->window != nullptr) { SDL_DestroyWindow(this->window); this->window = nullptr; } 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::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_FRect 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); }