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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include "app.hpp"
extern "C" {
#include "memory.h"
#include "plat.h"
}
#ifdef plat_x11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
Key key_from_xkey(unsigned key) {
switch (key) {
case 0x61: return key_A;
case 0x62: return key_B;
case 0x63: return key_C;
case 0x64: return key_D;
case 0x65: return key_E;
case 0x66: return key_F;
case 0x67: return key_G;
case 0x68: return key_H;
case 0x69: return key_I;
case 0x6A: return key_J;
case 0x6B: return key_K;
case 0x6C: return key_L;
case 0x6D: return key_M;
case 0x6E: return key_N;
case 0x6F: return key_O;
case 0x70: return key_P;
case 0x71: return key_Q;
case 0x72: return key_R;
case 0x73: return key_S;
case 0x74: return key_T;
case 0x75: return key_U;
case 0x76: return key_V;
case 0x77: return key_W;
case 0x78: return key_X;
case 0x79: return key_Y;
case 0x7A: return key_Z;
case XK_F1: return key_f1;
case XK_F2: return key_f2;
case XK_F3: return key_f3;
case XK_F4: return key_f4;
case XK_F5: return key_f5;
case XK_F6: return key_f6;
case XK_F7: return key_f7;
case XK_F8: return key_f8;
case XK_F9: return key_f9;
case XK_F10: return key_f10;
case XK_F11: return key_f11;
case XK_F12: return key_f12;
case XK_Down: return key_down;
case XK_Left: return key_left;
case XK_Right: return key_right;
case XK_Up: return key_up;
case XK_Escape: return key_escape;
case XK_Return: return key_return;
case XK_BackSpace: return key_backspace;
case XK_Linefeed: return key_return;
case XK_Tab: return key_tab;
case XK_Delete: return key_delete;
case XK_Home: return key_home;
case XK_End: return key_end;
case XK_Page_Up: return key_page_up;
case XK_Page_Down: return key_page_down;
case XK_Insert: return key_insert;
case XK_Shift_L: return key_shift;
case XK_Shift_R: return key_shift;
case XK_Control_L: return key_control;
case XK_Control_R: return key_control;
case XK_Super_L: return key_super;
case XK_Super_R: return key_super;
case XK_Alt_L: return key_alt;
case XK_Alt_R: return key_alt;
case XK_space: return key_space;
case XK_period: return key_period;
case XK_0: return key_0;
case XK_1: return key_1;
case XK_2: return key_2;
case XK_3: return key_3;
case XK_4: return key_4;
case XK_5: return key_5;
case XK_6: return key_6;
case XK_7: return key_7;
case XK_8: return key_8;
case XK_9: return key_9;
default: return key_unknown;
}
}
struct App_Internal {
Display* d;
Window wi;
Atom wm_delete;
void init(App* app, const char* name) {
const int default_w = 1920;
const int default_h = 1080;
XWindowAttributes wa;
XSetWindowAttributes attribs{};
attribs.event_mask =
ExposureMask |
KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
PointerMotionMask
;
Window root;
Visual* v;
d = XOpenDisplay(0);
wm_delete = XInternAtom(
d,
"WM_DELETE_WINDOW",
0
);
root = DefaultRootWindow(d);
v = XDefaultVisual(d, 0);
wi = XCreateWindow(
d,
root,
0,
0,
default_w,
default_h,
0,
0,
InputOutput,
v,
CWEventMask,
&attribs
);
XSetWMProtocols(d, wi, &wm_delete, 1);
XStoreName(d, wi, name);
XMapRaised(d, wi);
XGetWindowAttributes(d, wi, &wa);
app->w = wa.width;
app->h = wa.height;
}
void begin(App* app) {
while (XPending(d)) {
XEvent e;
XWindowAttributes wa;
KeySym sym;
Key key;
int btn;
XNextEvent(d, &e);
switch (e.type) {
case ClientMessage:
if (
(Atom)e.xclient.data.l[0] ==
wm_delete
) {
app->running = 0;
}
break;
case Expose:
XGetWindowAttributes(d, wi, &wa);
if (app->w != wa.width || app->h != wa.height) {
app->w = wa.width;
app->h = wa.height;
app->on_resize();
}
break;
case MotionNotify:
app->mx = e.xmotion.x;
app->my = e.xmotion.y;
break;
case ButtonPress:
switch (e.xbutton.button) {
case 1:
case 2:
case 3:
app->mbtn_states[e.xbutton.button - 1] |=
key_state_pressed |
key_state_just_pressed;
break;
case 4:
app->scrolly++;
break;
case 5:
app->scrolly--;
break;
case 6:
app->scrollx++;
break;
case 7:
app->scrollx--;
break;
}
break;
case ButtonRelease:
switch (e.xbutton.button) {
case 1:
case 2:
case 3:
btn = e.xbutton.button - 1;
app->mbtn_states[btn] &= ~key_state_pressed;
app->mbtn_states[btn] |= key_state_just_released;
break;
case 4:
app->scrolly++;
break;
case 5:
app->scrolly--;
break;
case 6:
app->scrollx++;
break;
case 7:
app->scrollx--;
break;
}
break;
case KeyPress:
sym = XLookupKeysym(&e.xkey, 0);
key = key_from_xkey(sym);
app->key_states[key] |=
key_state_pressed |
key_state_just_pressed;
break;
case KeyRelease:
sym = XLookupKeysym(&e.xkey, 0);
key = key_from_xkey(sym);
app->key_states[key] &= ~key_state_pressed;
app->key_states[key] |= key_state_just_released;
break;
}
}
}
void end(App* app) {
(void)app;
}
};
#endif
void App::init(const char* name) {
internal = (App_Internal*)arena_alloc(
arena,
sizeof *internal
);
internal->init(this, name);
}
void* App::alloc(int size, Arena*& arena) {
char* mem = (char*)malloc(app_memory_size);
arena = (Arena*)mem;
mem += sizeof *arena;
init_arena(arena, mem, app_memory_size - sizeof *arena);
return arena_alloc(arena, size);
}
void App::destroy() {
free(arena);
}
void App::begin() {
int j;
for (j = 0; j < key_count; j++)
key_states[j] &= ~(
key_state_just_pressed | key_state_just_released
);
for (j = 0; j < mbtn_count; j++)
mbtn_states[j] &= ~(
key_state_just_pressed | key_state_just_released
);
internal->begin(this);
}
void App::end() {
internal->end(this);
}
Key_State App::ks(Key k) {
return (Key_State)key_states[k];
}
Key_State App::ms(Mbtn k) {
return (Key_State)mbtn_states[k];
}
bool App::kp(Key k) {
return key_states[k] & key_state_pressed;
}
bool App::kjp(Key k) {
return key_states[k] & key_state_just_pressed;
}
bool App::kjr(Key k) {
return key_states[k] & key_state_just_released;
}
bool App::mp(Mbtn b) {
return mbtn_states[b] & key_state_pressed;
}
bool App::mjp(Mbtn b) {
return mbtn_states[b] & key_state_just_pressed;
}
bool App::mjr(Mbtn b) {
return mbtn_states[b] & key_state_just_released;
}
void App::get_vk_exts(const char** exts, int& count) {
exts[count++] = "VK_KHR_surface";
exts[count++] = "VK_KHR_xlib_surface";
}
extern "C" {
#define VK_USE_PLATFORM_XLIB_KHR
#include "glad_vk.h"
VkSurfaceKHR app_create_vk_surface(App* app, VkInstance inst) {
App_Internal* i;
VkSurfaceKHR s;
VkXlibSurfaceCreateInfoKHR ci{};
VkResult r;
i = app->internal;
ci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
ci.dpy = i->d;
ci.window = i->wi;
r = vkCreateXlibSurfaceKHR(inst, &ci, 0, &s);
if (r != VK_SUCCESS) {
print_err("Failed to create a Vulkan surface.");
pbreak(r);
}
return s;
}
void app_destroy_vk_surface(
App* app,
VkInstance inst,
VkSurfaceKHR surf
) {
(void)app;
vkDestroySurfaceKHR(inst, surf, 0);
}
}
|