#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); }