1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include <SDL3/SDL.h>
#include <SDL3/SDL_events.h>
#include <thread>
#include <vector>
#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<KeyboardCode>(e.key.scancode));
}
if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
this->controller.ev_mousedown(static_cast<MouseCode>(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::draw_rect(Rectangle r, Color c) {
SDL_SetRenderDrawColor(this->renderer, c.red, c.green, c.blue, 255);
SDL_FRect rect = {
.x = static_cast<float>(r.x),
.y = static_cast<float>(r.y),
.w = static_cast<float>(r.width),
.h = static_cast<float>(r.height),
};
SDL_RenderFillRect(this->renderer, &rect);
}
typedef struct {
void (* callback)(std::vector<std::string> files, void* data);
void * data;
} tmp_data;
void View::dialog_file(void(*callback)(std::vector<std::string> 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<tmp_data*>(userdata);
vector<string> out = {};
for (; *files != NULL; files++)
out.push_back(string(*files));
(*temp->callback)(out, temp->data);
delete temp;
}, temp, this->window, NULL, 0, NULL, true);
}
|