Repository navigation
Expand file tree
/
Copy pathwin32.cpp
More file actions
438 lines (375 loc) · 12.5 KB
/
Copy pathwin32.cpp
File metadata and controls
438 lines (375 loc) · 12.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <assert.h>
#include <stdlib.h>
#include <direct.h>
#include <windows.h>
#include "graphics.h"
#include "platform.h"
#define LINE_SIZE 256
typedef enum {
FORMAT_LDR,
FORMAT_HDR
} format_t;
typedef struct {
format_t format;
int width, height, channels;
unsigned char *ldr_buffer;
float *hdr_buffer;
} image_t;
image_t* image_create(int width, int height, int channels, format_t format) {
int num_elems = width * height * channels;
image_t* image;
assert(width > 0 && height > 0 && channels >= 1 && channels <= 4);
assert(format == FORMAT_LDR || format == FORMAT_HDR);
image = (image_t*)malloc(sizeof(image_t));
image->format = format;
image->width = width;
image->height = height;
image->channels = channels;
image->ldr_buffer = NULL;
image->hdr_buffer = NULL;
if (format == FORMAT_LDR) {
int size = sizeof(unsigned char) * num_elems;
image->ldr_buffer = (unsigned char*)malloc(size);
memset(image->ldr_buffer, 0, size);
}
else {
int size = sizeof(float) * num_elems;
image->hdr_buffer = (float*)malloc(size);
memset(image->hdr_buffer, 0, size);
}
return image;
}
void image_release(image_t* image) {
free(image->ldr_buffer);
free(image->hdr_buffer);
free(image);
}
struct window {
HWND handle;
HDC memory_dc;
image_t *surface;
/* common data */
int should_close;
char keys[KEY_NUM];
char buttons[BUTTON_NUM];
callbacks_t callbacks;
void *userdata;
float text_width;
float text_height;
HWND text_handle;
};
/* platform initialization */
static int g_initialized = 0;
#ifdef UNICODE
static const wchar_t *const WINDOW_CLASS_NAME = L"Class";
static const wchar_t *const WINDOW_ENTRY_NAME = L"Entry";
#else
static const char *const WINDOW_CLASS_NAME = "Class";
static const char *const WINDOW_ENTRY_NAME = "Entry";
#endif
/*
* for virtual-key codes, see
* https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
*/
static void handle_key_message(window_t *window, WPARAM virtual_key,
char pressed) {
keycode_t key;
switch (virtual_key) {
case 'A': key = KEY_A; break;
case 'D': key = KEY_D; break;
case 'S': key = KEY_S; break;
case 'W': key = KEY_W; break;
case 'Q': key = KEY_Q; break;
case 'E': key = KEY_E; break;
case VK_SPACE: key = KEY_SPACE; break;
default: key = KEY_NUM; break;
}
if (key < KEY_NUM) {
window->keys[key] = pressed;
if (window->callbacks.key_callback) {
window->callbacks.key_callback(window, key, pressed);
}
}
}
static void handle_button_message(window_t *window, button_t button,
char pressed) {
window->buttons[button] = pressed;
if (window->callbacks.button_callback) {
window->callbacks.button_callback(window, button, pressed);
}
}
static void handle_scroll_message(window_t *window, float offset) {
if (window->callbacks.scroll_callback) {
window->callbacks.scroll_callback(window, offset);
}
}
static LRESULT CALLBACK process_message(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) {
window_t *window = (window_t*)GetProp(hWnd, WINDOW_ENTRY_NAME);
if (window == NULL) {
return DefWindowProc(hWnd, uMsg, wParam, lParam);
} else if (uMsg == WM_CLOSE) {
window->should_close = 1;
return 0;
} else if (uMsg == WM_KEYDOWN) {
handle_key_message(window, wParam, 1);
return 0;
} else if (uMsg == WM_KEYUP) {
handle_key_message(window, wParam, 0);
return 0;
} else if (uMsg == WM_LBUTTONDOWN) {
handle_button_message(window, BUTTON_L, 1);
return 0;
} else if (uMsg == WM_RBUTTONDOWN) {
handle_button_message(window, BUTTON_R, 1);
return 0;
} else if (uMsg == WM_LBUTTONUP) {
handle_button_message(window, BUTTON_L, 0);
return 0;
} else if (uMsg == WM_RBUTTONUP) {
handle_button_message(window, BUTTON_R, 0);
return 0;
} else if (uMsg == WM_MOUSEWHEEL) {
float offset = GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
handle_scroll_message(window, offset);
return 0;
} else {
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
static void register_class(void) {
ATOM class_atom;
WNDCLASS window_class;
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = process_message;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = GetModuleHandle(NULL);
window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
window_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
window_class.lpszMenuName = NULL;
window_class.lpszClassName = WINDOW_CLASS_NAME;
class_atom = RegisterClass(&window_class);
assert(class_atom != 0);
((void)(class_atom));
}
static void unregister_class(void) {
UnregisterClass(WINDOW_CLASS_NAME, GetModuleHandle(NULL));
}
static void initialize_path(void) {
#ifdef UNICODE
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
*wcsrchr(path, L'\\') = L'\0';
_wchdir(path);
_wchdir(L"assets");
#else
char path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
*strrchr(path, '\\') = '\0';
_chdir(path);
_chdir("assets");
#endif
}
void platform_initialize(void) {
assert(g_initialized == 0);
register_class();
initialize_path();
g_initialized = 1;
}
void platform_terminate(void) {
assert(g_initialized == 1);
unregister_class();
g_initialized = 0;
}
/* window related functions */
static HWND create_window(const char *title_, int width, int height) {
DWORD style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
RECT rect;
HWND handle;
#ifdef UNICODE
wchar_t title[LINE_SIZE];
mbstowcs(title, title_, LINE_SIZE);
#else
const char *title = title_;
#endif
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
AdjustWindowRect(&rect, style, 0);
width = rect.right - rect.left;
height = rect.bottom - rect.top;
handle = CreateWindow(WINDOW_CLASS_NAME, title, style,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
NULL, NULL, GetModuleHandle(NULL), NULL);
assert(handle != NULL);
return handle;
}
/*
* for memory device context, see
* https://docs.microsoft.com/en-us/windows/desktop/gdi/memory-device-contexts
*/
static void create_surface(HWND handle, int width, int height,
image_t **out_surface, HDC *out_memory_dc) {
BITMAPINFOHEADER bi_header;
HBITMAP dib_bitmap;
HBITMAP old_bitmap;
HDC window_dc;
HDC memory_dc;
image_t *surface;
surface = image_create(width, height, 4, FORMAT_LDR);
free(surface->ldr_buffer);
surface->ldr_buffer = NULL;
window_dc = GetDC(handle);
memory_dc = CreateCompatibleDC(window_dc);
ReleaseDC(handle, window_dc);
memset(&bi_header, 0, sizeof(BITMAPINFOHEADER));
bi_header.biSize = sizeof(BITMAPINFOHEADER);
bi_header.biWidth = width;
bi_header.biHeight = -height; /* top-down */
bi_header.biPlanes = 1;
bi_header.biBitCount = 32;
bi_header.biCompression = BI_RGB;
dib_bitmap = CreateDIBSection(memory_dc, (BITMAPINFO*)&bi_header,
DIB_RGB_COLORS, (void**)&surface->ldr_buffer,
NULL, 0);
assert(dib_bitmap != NULL);
old_bitmap = (HBITMAP)SelectObject(memory_dc, dib_bitmap);
DeleteObject(old_bitmap);
*out_surface = surface;
*out_memory_dc = memory_dc;
}
window_t *window_create(const char *title, int width, int height, int text_width, int text_height) {
window_t *window;
HWND handle;
image_t *surface;
HDC memory_dc;
HWND text_handle;
assert(g_initialized && width > 0 && height > 0);
handle = create_window(title, width, height);
text_handle = CreateWindow(TEXT("static"), TEXT("static"), WS_CHILD | WS_VISIBLE | WS_TABSTOP,
0, 0, text_width, text_height,
handle, HMENU(21), GetModuleHandle(NULL), NULL);
SetBkColor(GetDC(text_handle), RGB(0, 0, 0));
SetTextColor(GetDC(text_handle), RGB(255, 0, 0));
create_surface(handle, width, height, &surface, &memory_dc);
window = (window_t*)malloc(sizeof(window_t));
memset(window, 0, sizeof(window_t));
window->handle = handle;
window->memory_dc = memory_dc;
window->surface = surface;
window->text_width = text_width;
window->text_height = text_height;
window->text_handle = text_handle;
SetProp(handle, WINDOW_ENTRY_NAME, window);
ShowWindow(handle, SW_SHOW);
return window;
}
void window_destroy(window_t *window) {
ShowWindow(window->handle, SW_HIDE);
RemoveProp(window->handle, WINDOW_ENTRY_NAME);
DeleteDC(window->memory_dc);
DestroyWindow(window->handle);
window->surface->ldr_buffer = NULL;
image_release(window->surface);
free(window);
}
int window_should_close(window_t *window) {
return window->should_close;
}
void window_set_userdata(window_t *window, void *userdata) {
window->userdata = userdata;
}
void *window_get_userdata(window_t *window) {
return window->userdata;
}
static void present_surface(window_t *window) {
HDC window_dc = GetDC(window->handle);
HDC memory_dc = window->memory_dc;
image_t *surface = window->surface;
int width = surface->width;
int height = surface->height;
BitBlt(window_dc, window->text_width, 0, width, height, memory_dc, window->text_width, 0, SRCCOPY);
BitBlt(window_dc, 0, window->text_height, width, height, memory_dc, 0, window->text_height, SRCCOPY);
ReleaseDC(window->handle, window_dc);
}
void window_draw_buffer(window_t *window, RenderBuffer *buffer) {
int width = window->surface->width;
int height = window->surface->height;
int r, c;
assert(buffer->width == window->surface->width && buffer->height == window->surface->height);
assert(window->surface->format == FORMAT_LDR && window->surface->channels == 4);
for (r = 0; r < height; r++) {
for (c = 0; c < width; c++) {
int flipped_r = height - 1 - r;
int src_index = (r * width + c) * 4;
int dst_index = (flipped_r * width + c) * 4;
unsigned char* src_pixel = &buffer->color_buffer[src_index];
unsigned char* dst_pixel = &window->surface->ldr_buffer[dst_index];
dst_pixel[0] = src_pixel[2]; /* blue */
dst_pixel[1] = src_pixel[1]; /* green */
dst_pixel[2] = src_pixel[0]; /* red */
}
}
present_surface(window);
}
void window_draw_text(window_t* window, char* text) {
#ifdef UNICODE
wchar_t* wc;
int len = MultiByteToWideChar(CP_ACP, 0, text, strlen(text), NULL, 0);
wc = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, text, strlen(text), wc, len);
wc[len] = '\0';
SetWindowText(window->text_handle, wc);
delete[] wc;
#else
SetWindowText(window->text_handle, text);
#endif // !UNICODE
}
/* input related functions */
void input_poll_events(void) {
MSG message;
while (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
int input_key_pressed(window_t *window, keycode_t key) {
assert(key >= 0 && key < KEY_NUM);
return window->keys[key];
}
int input_button_pressed(window_t *window, button_t button) {
assert(button >= 0 && button < BUTTON_NUM);
return window->buttons[button];
}
void input_query_cursor(window_t *window, float *xpos, float *ypos) {
POINT point;
GetCursorPos(&point);
ScreenToClient(window->handle, &point);
*xpos = (float)point.x;
*ypos = (float)point.y;
}
void input_set_callbacks(window_t *window, callbacks_t callbacks) {
window->callbacks = callbacks;
}
/* misc platform functions */
static double get_native_time(void) {
static double period = -1;
LARGE_INTEGER counter;
if (period < 0) {
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
period = 1 / (double)frequency.QuadPart;
}
QueryPerformanceCounter(&counter);
return counter.QuadPart * period;
}
float platform_get_time(void) {
static double initial = -1;
if (initial < 0) {
initial = get_native_time();
}
return (float)(get_native_time() - initial);
}