-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathgfx_window.cpp
381 lines (332 loc) · 13.8 KB
/
gfx_window.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
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
/****************************************************************************
MIT License
Copyright (c) 2024 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
class GfxWindowInternal
{
GFX_NON_COPYABLE(GfxWindowInternal);
HWND window_ = {};
bool is_minimized_ = false;
bool is_maximized_ = false;
bool is_close_requested_ = 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;
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)
{
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);
RECT window_rect = { 0, 0, (LONG)window_width, (LONG)window_height };
DWORD const window_style = WS_OVERLAPPEDWINDOW & ~((flags & kGfxCreateWindowFlag_NoResizeWindow) != 0 ?
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX : 0);
AdjustWindowRect(&window_rect, window_style, FALSE);
window_ = CreateWindowEx((flags & kGfxCreateWindowFlag_AcceptDrop) != 0 ? WS_EX_ACCEPTFILES : 0,
window_title,
window_title,
window_style,
CW_USEDEFAULT,
CW_USEDEFAULT,
window_rect.right - window_rect.left,
window_rect.bottom - window_rect.top,
nullptr,
nullptr,
GetModuleHandle(nullptr),
nullptr);
if((flags & kGfxCreateWindowFlag_FullscreenWindow) != 0)
{
WINDOWPLACEMENT g_wpPrev;
memset(&g_wpPrev, 0, sizeof(g_wpPrev));
g_wpPrev.length = sizeof(g_wpPrev);
DWORD dwStyle = GetWindowLong(window_, GWL_STYLE);
if((dwStyle & WS_OVERLAPPEDWINDOW) != 0)
{
MONITORINFO mi;
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
if(GetWindowPlacement(window_, &g_wpPrev) &&
GetMonitorInfo(MonitorFromWindow(window_, MONITOR_DEFAULTTOPRIMARY), &mi))
{
SetWindowLong(window_, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(window_, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
else
{
SetWindowLong(window_, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
SetWindowPlacement(window_, &g_wpPrev);
SetWindowPos(window_, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
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;
}
static inline GfxWindowInternal *GetGfxWindow(GfxWindow window) { return reinterpret_cast<GfxWindowInternal *>(window.handle); }
private:
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
switch(message)
{
case WM_SIZE:
{
gfx_window->is_minimized_ = IsIconic(gfx_window->window_);
gfx_window->is_maximized_ = IsZoomed(gfx_window->window_);
}
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_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)
{
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);
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;
}