-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl2.cpp
241 lines (210 loc) · 7.2 KB
/
sdl2.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "native_window.h"
#include "native_window_drawing.h"
#include "environment.h"
#include "wintypes.h"
using namespace wintypes;
#include "user32.h"
#include "SDL.h"
#include <mutex>
#include <array>
namespace native_window {
;
std::mutex init_mut;
bool sdl_initialized = false;
void sdl_init() {
std::lock_guard<std::mutex> l(init_mut);
if (!sdl_initialized) {
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
sdl_initialized = true;
} else {
log("SDL_Init failed: %s\n", SDL_GetError());
}
}
}
static const int sdl_scancode_to_vk_table[] = {
0,0,0,0,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x31,0x32,
0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0xd,0x1b,0x8,0x9,0x20,0xbb,0xdb,0xdd,0xba,0xbf,0,0xc0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
struct window_impl {
SDL_Window* window = nullptr;
window_impl() {
sdl_init();
}
~window_impl() {
if (window) SDL_DestroyWindow(window);
}
bool create(const char* title, DWORD style, DWORD ex_style, int x, int y, int width, int height) {
Uint32 flags = 0;
if (~style & user32::WS_VISIBLE) flags |= SDL_WINDOW_HIDDEN;
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags);
if (!window) log("SDL_CreateWindow failed: %s\n", SDL_GetError());
if (window) {
SDL_StartTextInput();
}
return window != nullptr;
}
void get_cursor_pos(int* x, int* y) {
SDL_GetMouseState(x, y);
}
std::array<int, 0x100> key_state {};
bool peek_message(MSG* msg) {
SDL_Event e;
if (!SDL_PollEvent(&e)) return false;
switch (e.type) {
case SDL_MOUSEMOTION:
msg->message = user32::WM_MOUSEMOVE;
msg->wParam = 0;
msg->lParam = MAKELPARAM(e.motion.x, e.motion.y);
return true;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
bool down = e.type == SDL_MOUSEBUTTONDOWN;
bool left = e.button.button == SDL_BUTTON_LEFT;
bool middle = e.button.button == SDL_BUTTON_MIDDLE;
bool right = e.button.button == SDL_BUTTON_RIGHT;
bool x1 = e.button.button == SDL_BUTTON_X1;
bool x2 = e.button.button == SDL_BUTTON_X2;
WPARAM w = 0;
auto state = SDL_GetMouseState(nullptr, nullptr);
if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) w |= 1;
if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) w |= 2;
msg->lParam = MAKELPARAM(e.button.x, e.button.y);
if (left) {
msg->message = down ? user32::WM_LBUTTONDOWN : user32::WM_LBUTTONUP;
return true;
} else if (right) {
msg->message = down ? user32::WM_RBUTTONDOWN : user32::WM_RBUTTONUP;
return true;
}
}
case SDL_KEYDOWN:
case SDL_KEYUP: {
bool key_down = e.type == SDL_KEYDOWN;
unsigned int vkey = sdl_scancode_to_vk_table[e.key.keysym.scancode];
bool extended = false;
if (vkey & (1 << 16)) {
extended = true;
vkey &= ~(1 << 16);
}
key_state[vkey] = key_down ? 1 : 0;
msg->message = key_down ? user32::WM_KEYDOWN : user32::WM_KEYUP;
msg->wParam = (uint8_t)vkey;
msg->lParam = (uint32_t)(uint16_t)e.key.repeat | ((uint32_t)(uint8_t)e.key.keysym.scancode << 16);
if (extended) msg->lParam |= 1 << 24;
if (key_down && e.key.repeat) msg->lParam |= 1 << 30;
if (!key_down) msg->lParam |= 1 << 30;
if (!key_down) msg->lParam |= 1 << 31;
return true;
}
case SDL_TEXTINPUT: {
// This is the wrong way to generate WM_CHAR messages, it should be done
// in TranslateMessage
msg->message = user32::WM_CHAR;
msg->wParam = e.text.text[0];
msg->lParam = 0;
return true;
break;
}
}
return false;
}
bool show_cursor(bool show) {
return SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE) ? true : false;
}
bool get_key_state(int vkey) {
return key_state[vkey] ? true : false;
}
};
window::window() {
impl = std::make_unique<window_impl>();
}
window::~window() {
}
bool window::create(const char* title, DWORD style, DWORD ex_style, int x, int y, int width, int height) {
return impl->create(title, style, ex_style, x, y, width, height);
}
void window::get_cursor_pos(int* x, int* y) {
return impl->get_cursor_pos(x, y);
}
bool window::peek_message(MSG* msg) {
return impl->peek_message(msg);
}
bool window::show_cursor(bool show) {
return impl->show_cursor(show);
}
bool window::get_key_state(int vkey) {
return impl->get_key_state(vkey);
}
}
namespace native_window_drawing {
;
struct palette_impl : palette {
SDL_Palette* pal = nullptr;
palette_impl() {
pal = SDL_AllocPalette(256);
}
virtual ~palette_impl() override {
SDL_FreePalette(pal);
}
virtual void set_colors(color colors[256]) {
std::array<SDL_Color, 256> col;
for (size_t i = 0; i < 256; ++i) {
col[i].r = colors[i].r;
col[i].g = colors[i].g;
col[i].b = colors[i].b;
col[i].a = colors[i].a;
}
if (SDL_SetPaletteColors(pal, col.data(), 0, 256)) fatal_error("SDL_SetPaletteColors failed: %s", SDL_GetError());
}
};
struct surface_impl : surface {
SDL_Surface* window_s = nullptr;
SDL_Surface* surf = nullptr;
SDL_Window* window = nullptr;
virtual void create(wintypes::HWND wnd) override {
window = user32::get_native_window(wnd)->impl->window;
window_s = SDL_GetWindowSurface(window);
if (!window_s) fatal_error("SDL_GetWindowSurface failed: %s", SDL_GetError());
surf = SDL_ConvertSurfaceFormat(window_s, SDL_PIXELFORMAT_INDEX8, 0);
if (!surf) fatal_error("SDL_ConvertSurfaceFormat failed: %s", SDL_GetError());
}
virtual void set_palette(palette* pal) override {
if (SDL_SetSurfacePalette(surf, ((palette_impl*)pal)->pal)) fatal_error("SDL_SetSurfacePalette failed: %s", SDL_GetError());
}
virtual void* lock() override {
if (SDL_LockSurface(surf)) fatal_error("SDL_LockSurface failed: %s", SDL_GetError());
return surf->pixels;
}
virtual void unlock() override {
SDL_UnlockSurface(surf);
SDL_BlitSurface(surf, nullptr, window_s, nullptr);
SDL_UpdateWindowSurface(window);
}
};
palette* new_palette() {
return new palette_impl();
}
void delete_palette(palette* pal) {
delete pal;
}
surface* new_surface() {
return new surface_impl();
}
void delete_surface(surface* surf) {
delete surf;
}
};