-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgfx_window.cpp
More file actions
563 lines (492 loc) · 20 KB
/
Copy pathgfx_window.cpp
File metadata and controls
563 lines (492 loc) · 20 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/****************************************************************************
MIT License
Copyright (c) 2026 Guillaume Boissé
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
****************************************************************************/
#include "gfx_window.h"
# ifdef GFX_ENABLE_GUI
#include "gfx_imgui.h" // for (optional) ImGui integration
# ifdef GFX_IMGUI_SOURCE
#include "backends/imgui_impl_win32.h"
# else
#include "imgui_impl_win32.h"
# endif
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
# endif
namespace
{
struct MonitorEnumData
{
size_t targetIndex;
size_t currentIndex;
HMONITOR result;
};
BOOL CALLBACK EnumMonitorProc(HMONITOR hMon, HDC, LPRECT, LPARAM lParam)
{
auto *data = reinterpret_cast<MonitorEnumData *>(lParam);
if(data->currentIndex == data->targetIndex)
{
data->result = hMon;
return FALSE; // stop once the target monitor has been found
}
++data->currentIndex;
return TRUE;
}
} // namespace
class GfxWindowInternal
{
GFX_NON_COPYABLE(GfxWindowInternal);
HWND window_ = {};
bool is_minimized_ = false;
bool is_maximized_ = false;
bool is_close_requested_ = false;
bool is_full_screen_ = false;
bool is_key_down_[VK_OEM_CLEAR] = {};
bool is_previous_key_down_[VK_OEM_CLEAR] = {};
void (*drop_callback_)(char const *, uint32_t, void *) = nullptr;
void *callback_data_ = nullptr;
GfxCreateWindowFlags flags_;
uint32_t window_width_;
uint32_t window_height_;
uint32_t original_window_width_;
uint32_t original_window_height_;
uint8_t monitor_index_;
public:
GfxWindowInternal(GfxWindow &window) { window.handle = reinterpret_cast<uint64_t>(this); }
~GfxWindowInternal() { terminate(); }
GfxResult initialize(GfxWindow &window,
uint32_t window_width,
uint32_t window_height,
char const *window_title,
GfxCreateWindowFlags flags,
uint8_t monitor_index)
{
flags_ = flags;
window_width_ = window_width;
window_height_ = window_height;
original_window_width_ = window_width_;
original_window_height_ = window_height_;
monitor_index_ = monitor_index;
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
window_title = (!window_title ? "gfx" : window_title);
WNDCLASSEX
window_class = {};
window_class.cbSize = sizeof(window_class);
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = WindowProc;
window_class.hInstance = GetModuleHandle(nullptr);
window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
window_class.lpszClassName = window_title;
RegisterClassEx(&window_class);
DWORD window_style_ex;
DWORD const window_style = getWindowStyle(window_style_ex, flags_);
window_ = CreateWindowEx(window_style_ex,
window_title,
window_title,
window_style,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
nullptr,
nullptr,
GetModuleHandle(nullptr),
nullptr);
if((flags_ & kGfxCreateWindowFlag_FullscreenWindow) != 0)
{
setFullscreen();
is_full_screen_ = true;
}
else
{
setWindowed(window_style, window_style_ex);
is_full_screen_ = false;
}
SetWindowLongPtrA(window_, GWLP_USERDATA, (LONG_PTR)this);
ShowWindow(window_, (flags_ & kGfxCreateWindowFlag_MaximizeWindow) != 0 ? SW_SHOWMAXIMIZED :
(flags_ & kGfxCreateWindowFlag_HideWindow ) != 0 ? SW_HIDE :
SW_SHOWDEFAULT);
window.hwnd = window_;
return kGfxResult_NoError;
}
GfxResult terminate()
{
if(window_)
DestroyWindow(window_);
# ifdef GFX_ENABLE_GUI
if(ImGui::GetCurrentContext() != nullptr && ImGui::GetIO().BackendPlatformUserData != nullptr)
ImGui_ImplWin32_Shutdown();
# endif
return kGfxResult_NoError;
}
GfxResult pumpEvents()
{
MSG msg = {};
memcpy(is_previous_key_down_, is_key_down_, sizeof(is_key_down_));
while(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return kGfxResult_NoError;
}
inline bool getIsKeyDown(uint32_t key_code) const
{
return (key_code < ARRAYSIZE(is_key_down_) ? is_key_down_[key_code] : false);
}
inline bool getIsKeyPressed(uint32_t key_code) const
{
return (key_code < ARRAYSIZE(is_key_down_) ? is_key_down_[key_code] && !is_previous_key_down_[key_code] : false);
}
inline bool getIsKeyReleased(uint32_t key_code) const
{
return (key_code < ARRAYSIZE(is_key_down_) ? !is_key_down_[key_code] && is_previous_key_down_[key_code] : false);
}
inline bool getIsCloseRequested() const
{
return is_close_requested_;
}
inline bool getIsMinimized() const
{
return is_minimized_;
}
inline bool getIsMaximized() const
{
return is_maximized_;
}
inline void registerDropCallback(void (*callback)(char const *, uint32_t, void *), void *data)
{
drop_callback_ = callback;
callback_data_ = data;
}
inline void unregisterDropCallback()
{
drop_callback_ = nullptr;
callback_data_ = nullptr;
}
inline float getDPIScale() const
{
UINT dpi = GetDpiForWindow(window_);
return (float)dpi / (float)USER_DEFAULT_SCREEN_DPI;
}
inline HMONITOR selectMonitor() const
{
MonitorEnumData enumData{};
enumData.targetIndex = monitor_index_; // target monitor
enumData.currentIndex = 0;
enumData.result = nullptr;
EnumDisplayMonitors(nullptr, nullptr, EnumMonitorProc,
reinterpret_cast<LPARAM>(&enumData));
HMONITOR targetMonitor = enumData.result;
// Fallback if target monitor wasn't found
if(!targetMonitor)
targetMonitor = MonitorFromWindow(window_, MONITOR_DEFAULTTONEAREST);
return targetMonitor;
}
inline void setFullscreen(bool creation = true) const
{
MONITORINFO mi;
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
GetMonitorInfo(selectMonitor(), &mi);
SetWindowPos(window_, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOZORDER | (creation ? 0 : (SWP_FRAMECHANGED | SWP_SHOWWINDOW)));
}
inline void setWindowed(DWORD window_style, DWORD window_style_ex, bool creation = true) const
{
MONITORINFO mi = {};
mi.cbSize = sizeof(mi);
GetMonitorInfo(selectMonitor(), &mi);
RECT window_rect = { 0, 0, (LONG)original_window_width_, (LONG)original_window_height_ };
UINT dpi = GetDpiForWindow(window_);
AdjustWindowRectExForDpi(&window_rect, window_style, FALSE, window_style_ex, dpi);
// Normalize to width/height
LONG width = window_rect.right - window_rect.left;
LONG height = window_rect.bottom - window_rect.top;
const RECT& work_area = mi.rcWork;
LONG work_area_width = work_area.right - work_area.left;
LONG work_area_height = work_area.bottom - work_area.top;
if(work_area_width > width)
{
window_rect.left = work_area.left + (work_area_width - width) / 2;
}
else if((flags_ & kGfxCreateWindowFlag_ShrinkToScreen) != 0)
{
width = work_area_width;
window_rect.left = work_area.left;
}
else
{
window_rect.left = work_area.left;
}
if(work_area_height > height)
{
window_rect.top = work_area.top + (work_area_height - height) / 2;
}
else if((flags_ & kGfxCreateWindowFlag_ShrinkToScreen) != 0)
{
height = work_area_height;
window_rect.top = work_area.top;
}
else
{
// Align to top of the work area
window_rect.top = work_area.top;
}
SetWindowPos(window_, NULL, window_rect.left, window_rect.top, width, height,
SWP_NOZORDER | (creation ? SWP_NOACTIVATE : (SWP_FRAMECHANGED | SWP_SHOWWINDOW)));
}
inline void toggleFullscreen()
{
if(!is_full_screen_)
{
DWORD window_style_ex;
DWORD const window_style = getWindowStyle(window_style_ex, flags_ | kGfxCreateWindowFlag_FullscreenWindow);
SetWindowLong(window_, GWL_STYLE, window_style);
SetWindowLong(window_, GWL_EXSTYLE, window_style_ex);
setFullscreen(false);
is_full_screen_ = true;
}
else
{
DWORD window_style_ex;
DWORD const window_style = getWindowStyle(window_style_ex, flags_ & ~kGfxCreateWindowFlag_FullscreenWindow);
SetWindowLong(window_, GWL_STYLE, window_style);
SetWindowLong(window_, GWL_EXSTYLE, window_style_ex);
setWindowed(window_style, window_style_ex, false);
is_full_screen_ = false;
}
}
static inline GfxWindowInternal *GetGfxWindow(GfxWindow window) { return reinterpret_cast<GfxWindowInternal *>(window.handle); }
private:
static inline DWORD getWindowStyle(DWORD &window_style_ex, GfxCreateWindowFlags flags)
{
bool const isPopup = (flags & (kGfxCreateWindowFlag_FullscreenWindow | kGfxCreateWindowFlag_BorderlessWindow)) != 0;
DWORD const window_style = isPopup ? WS_POPUP :
(WS_OVERLAPPEDWINDOW & ~((flags & kGfxCreateWindowFlag_NoResizeWindow) != 0 ?
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX : 0));
window_style_ex = (isPopup ? 0 : WS_EX_OVERLAPPEDWINDOW) |
((flags & kGfxCreateWindowFlag_AcceptDrop) != 0 ? WS_EX_ACCEPTFILES : 0);
return window_style;
}
inline void updateKeyBinding(uint32_t message, uint32_t key_code)
{
bool is_down;
switch(message)
{
case WM_KEYUP: case WM_SYSKEYUP:
is_down = false;
break;
case WM_KEYDOWN: case WM_SYSKEYDOWN:
is_down = true;
break;
default:
return;
}
is_key_down_[key_code] = is_down;
}
inline void resetKeyBindings()
{
for(uint32_t i = 0; i < ARRAYSIZE(is_key_down_); ++i)
is_key_down_[i] = false;
}
static LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM w_param, LPARAM l_param)
{
GfxWindowInternal *gfx_window = (GfxWindowInternal *)GetWindowLongPtrA(window, GWLP_USERDATA);
if(gfx_window != nullptr)
{
# ifdef GFX_ENABLE_GUI
if(ImGui::GetCurrentContext() != nullptr && ImGui::GetIO().BackendPlatformUserData == nullptr && gfxImGuiIsInitialized())
ImGui_ImplWin32_Init(gfx_window->window_);
# endif
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
switch(message)
{
case WM_SIZE:
{
gfx_window->is_minimized_ = IsIconic(gfx_window->window_);
gfx_window->is_maximized_ = IsZoomed(gfx_window->window_);
gfx_window->window_width_ = LOWORD(l_param);
gfx_window->window_height_ = HIWORD(l_param);
}
break;
case WM_DESTROY:
{
gfx_window->is_close_requested_ = true;
}
return 0;
case WM_KILLFOCUS:
{
gfx_window->resetKeyBindings();
}
break;
case WM_DROPFILES:
{
HDROP hdrop = (HDROP)w_param;
if(gfx_window->drop_callback_ != nullptr)
{
char file_name[MAX_PATH];
// Get the number of files dropped onto window
uint32_t const file_count = DragQueryFileA(hdrop, 0xFFFFFFFFu, file_name, MAX_PATH);
for(uint32_t i = 0; i < file_count; i++)
{
// Get i'th filename
DragQueryFileA(hdrop, i, file_name, MAX_PATH);
// Pass to callback function
(*gfx_window->drop_callback_)(file_name, i, gfx_window->callback_data_);
}
}
DragFinish(hdrop);
}
break;
case WM_GETDPISCALEDSIZE:
{
DWORD const dpi = (WORD)w_param;
RECT window_rect = { 0, 0, (LONG)gfx_window->window_width_, (LONG)gfx_window->window_height_ };
DWORD const window_style = WS_OVERLAPPEDWINDOW & ~((gfx_window->flags_ & kGfxCreateWindowFlag_NoResizeWindow) != 0 ?
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX : 0);
DWORD const window_style_ex = WS_EX_OVERLAPPEDWINDOW & ((gfx_window->flags_ & kGfxCreateWindowFlag_AcceptDrop) != 0 ? WS_EX_ACCEPTFILES : 0);
AdjustWindowRectExForDpi(&window_rect, window_style, FALSE, window_style_ex, dpi);
SIZE* const out = (SIZE*)l_param;
out->cx = window_rect.right - window_rect.left;
out->cy = window_rect.bottom - window_rect.top;
return TRUE;
}
# ifdef GFX_ENABLE_GUI
case WM_DPICHANGED:
{
UINT dpi = HIWORD(w_param);
float dpi_scale_factor = (float)dpi / (float)USER_DEFAULT_SCREEN_DPI;
gfxImGuiSetDPIScale(dpi_scale_factor);
}
break;
# endif
case WM_CHAR:
case WM_SETCURSOR:
case WM_DEVICECHANGE:
case WM_KEYUP: case WM_SYSKEYUP:
case WM_KEYDOWN: case WM_SYSKEYDOWN:
case WM_LBUTTONUP: case WM_RBUTTONUP:
case WM_MBUTTONUP: case WM_XBUTTONUP:
case WM_MOUSEWHEEL: case WM_MOUSEHWHEEL:
case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
if(w_param < ARRAYSIZE(is_key_down_))
gfx_window->updateKeyBinding(message, (uint32_t)w_param);
# ifdef GFX_ENABLE_GUI
if(ImGui::GetCurrentContext() != nullptr && ImGui::GetIO().BackendPlatformUserData != nullptr)
ImGui_ImplWin32_WndProcHandler(gfx_window->window_, message, w_param, l_param);
# endif
break;
default:
break;
}
}
return DefWindowProc(window, message, w_param, l_param);
}
};
GfxWindow gfxCreateWindow(uint32_t window_width, uint32_t window_height, char const *window_title, GfxCreateWindowFlags flags, uint8_t monitor_index)
{
GfxResult result;
GfxWindow window = {};
GfxWindowInternal *gfx_window = new GfxWindowInternal(window);
if(!gfx_window) return window; // out of memory
result = gfx_window->initialize(window, window_width, window_height, window_title, flags, monitor_index);
if(result != kGfxResult_NoError)
{
window = {};
delete gfx_window;
GFX_PRINT_ERROR(result, "Failed to create new window");
}
return window;
}
GfxResult gfxDestroyWindow(GfxWindow window)
{
delete GfxWindowInternal::GetGfxWindow(window);
return kGfxResult_NoError;
}
GfxResult gfxWindowPumpEvents(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return kGfxResult_InvalidParameter;
return gfx_window->pumpEvents();
}
bool gfxWindowIsKeyDown(GfxWindow window, uint32_t key_code)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsKeyDown(key_code);
}
bool gfxWindowIsKeyPressed(GfxWindow window, uint32_t key_code)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsKeyPressed(key_code);
}
bool gfxWindowIsKeyReleased(GfxWindow window, uint32_t key_code)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsKeyReleased(key_code);
}
bool gfxWindowIsCloseRequested(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsCloseRequested();
}
bool gfxWindowIsMinimized(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsMinimized();
}
bool gfxWindowIsMaximized(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
return gfx_window->getIsMaximized();
}
bool gfxWindowRegisterDropCallback(GfxWindow window, void (*callback)(char const *, uint32_t, void *), void *data)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
gfx_window->registerDropCallback(callback, data);
return true;
}
bool gfxWindowUnregisterDropCallback(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return false; // invalid window handle
gfx_window->unregisterDropCallback();
return true;
}
float gfxWindowGetDPIScale(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return 1.0f; // invalid window handle
return gfx_window->getDPIScale();
}
void gfxWindowToggleFullscreen(GfxWindow window)
{
GfxWindowInternal *gfx_window = GfxWindowInternal::GetGfxWindow(window);
if(!gfx_window) return; // invalid window handle
gfx_window->toggleFullscreen();
}