#include #include #include #include #include "View.h" #include "ViewController.h" using namespace std; View::View(Museum & m) : controller(m, *this) { 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; continue; } if (e.type == SDL_EVENT_KEY_DOWN) { if (e.key.repeat) continue; this->controller.ev_keydown(static_cast(e.key.scancode)); } if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { this->controller.ev_mousedown(static_cast(e.button.button)); } if (e.type == SDL_EVENT_MOUSE_MOTION) { this->controller.ev_mousemove(e.motion.x, e.motion.y); } } this->draw_begin(); this->controller.update(); 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::window_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::fill_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); } void View::draw_rect(Rectangle r, Color c, unsigned border_width) { SDL_SetRenderDrawColor(this->renderer, c.red, c.green, c.blue, 255); SDL_FRect rect; rect.x = r.x; rect.y = r.y; rect.w = r.width; rect.h = border_width; SDL_RenderFillRect(this->renderer, &rect); rect.y = r.y + r.height - border_width; SDL_RenderFillRect(this->renderer, &rect); rect.y = r.y; rect.h = r.height; rect.w = border_width; SDL_RenderFillRect(this->renderer, &rect); rect.x = r.x + r.width - border_width; SDL_RenderFillRect(this->renderer, &rect); } typedef struct { void (* callback)(std::vector files, void* data); void * data; } tmp_data; void View::dialog_file(void(*callback)(std::vector files, void* data), void* data) { tmp_data * temp = new tmp_data({ .callback = callback, .data = data, }); SDL_ShowOpenFileDialog([](void * userdata, const char * const * files, int) -> void { tmp_data * temp = static_cast(userdata); vector out = {}; for (; *files != NULL; files++) out.push_back(string(*files)); (*temp->callback)(out, temp->data); delete temp; }, temp, this->window, NULL, 0, NULL, true); }