-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoxaNovusWindow.cpp
More file actions
263 lines (222 loc) · 6.75 KB
/
VoxaNovusWindow.cpp
File metadata and controls
263 lines (222 loc) · 6.75 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
#include "VoxaNovusWindow.h"
#include "resource.h"
#include "imgui/imgui_impl_win32.h"
#include <sstream>
// Window class
Window::WindowClass Window::WindowClass::wndClass;
Window::WindowClass::WindowClass() noexcept : hInst(GetModuleHandle(nullptr)) {
WNDCLASSEX WindowClass = { 0 };
WindowClass.cbSize = sizeof(WindowClass);
WindowClass.style = CS_OWNDC;
WindowClass.lpfnWndProc = HandleMsgSetup;
WindowClass.cbClsExtra = 0;
WindowClass.hInstance = GetInstance();
WindowClass.hIcon = nullptr;
WindowClass.hCursor = nullptr;
WindowClass.hIcon = static_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32,32,0));
WindowClass.hbrBackground = nullptr;
WindowClass.lpszMenuName = nullptr;
WindowClass.lpszClassName = GetName();
WindowClass.hIconSm = static_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0));;
RegisterClassEx(&WindowClass);
}
Window::WindowClass::~WindowClass() {
UnregisterClass(wndClassName, GetInstance());
}
const char* Window::WindowClass::GetName() noexcept {
return wndClassName;
}
HINSTANCE Window::WindowClass::GetInstance() noexcept {
return wndClass.hInst;
}
Window::Window(int width, int height, const char* name) : width(width), height(height) {
// calc the windowsize
RECT WindowRectangle;
WindowRectangle.left = 100;
WindowRectangle.right = width + WindowRectangle.left;
WindowRectangle.top = 100;
WindowRectangle.bottom = height + WindowRectangle.top;
if (AdjustWindowRect(&WindowRectangle, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, FALSE) == 0)
throw VNWND_LAST_EXCEPT();
hWnd = CreateWindow(
WindowClass::GetName(),
name,
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
WindowRectangle.right - WindowRectangle.left,
WindowRectangle.bottom - WindowRectangle.top,
nullptr,
nullptr,
WindowClass::GetInstance(),
this
);
if (hWnd == nullptr)
throw VNWND_LAST_EXCEPT();
ShowWindow(hWnd, SW_SHOWDEFAULT);
ImGui_ImplWin32_Init(hWnd);
pGfx = std::make_unique<Graphics>(hWnd);
}
Window::~Window() {
ImGui_ImplWin32_Shutdown();
DestroyWindow(hWnd);
}
std::optional<int> Window::ProcessMessages() {
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
return (int)msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return {};
}
Graphics& Window::Gfx()
{
if (!pGfx)
throw VNWND_NOGFX_EXCEPT();
return *pGfx;
}
void Window::SetTitle(const std::string& title) noexcept {
if (SetWindowText(hWnd, title.c_str()) == 0)
throw VNWND_LAST_EXCEPT();
}
LRESULT WINAPI Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
if (msg == WM_NCCREATE) {
const CREATESTRUCTW* const pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
Window* const pWnd = static_cast<Window*>(pCreate->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&Window::HandleMsgThunk));
return pWnd->HandleMsg(hWnd, msg, wParam, lParam);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
LRESULT WINAPI Window::HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
Window* const pWnd = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
return pWnd->HandleMsg(hWnd, msg, wParam, lParam);
}
LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
{
return true;
}
const auto& imio = ImGui::GetIO();
POINTS pt;
switch (msg) {
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_KILLFOCUS:
keyboard.ClearState();
break;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
if (imio.WantCaptureKeyboard) break;
if (!(lParam & 0x40000000) || keyboard.AutorepeatIsEnabled())
keyboard.OnKeyPressed(static_cast<unsigned char>(wParam));
break;
case WM_SYSKEYUP:
case WM_KEYUP:
if (imio.WantCaptureKeyboard) break;
keyboard.OnKeyReleased(static_cast<unsigned char>(wParam));
break;
case WM_CHAR:
if (imio.WantCaptureKeyboard) break;
keyboard.OnChar(static_cast<unsigned char>(wParam));
break;
case WM_MOUSEMOVE:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
if (pt.x >= 0 && pt.x < width && pt.y >= 0 && pt.y < height) {
mouse.OnMouseMove(pt.x, pt.y);
if (!mouse.IsInWindow()) {
SetCapture(hWnd);
mouse.OnMouseEnter();
}
}
else {
if (wParam & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON))
mouse.OnMouseMove(pt.x, pt.y);
else {
ReleaseCapture();
mouse.OnMouseLeave();
}
}
break;
case WM_LBUTTONDOWN:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnLeftPressed(pt.x, pt.y);
break;
case WM_LBUTTONUP:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnLeftReleased(pt.x, pt.y);
break;
case WM_RBUTTONDOWN:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnRightPressed(pt.x, pt.y);
break;
case WM_RBUTTONUP:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnRightReleased(pt.x, pt.y);
break;
case WM_MBUTTONDOWN:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnWheelPressed(pt.x, pt.y);
break;
case WM_MBUTTONUP:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
mouse.OnWheelReleased(pt.x, pt.y);
break;
case WM_MOUSEWHEEL:
if (imio.WantCaptureMouse) break;
pt = MAKEPOINTS(lParam);
const int delta = GET_WHEEL_DELTA_WPARAM(wParam);
mouse.OnWheelDelta(pt.x, pt.y, delta);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// Window Exceptions
Window::Exception::Exception(int line, const char* file, HRESULT hr) noexcept
: VoxaNovusException(line, file), hr(hr) { }
const char* Window::Exception::what() const noexcept {
std::ostringstream sb;
sb << GetType() << std::endl
<< GetErrorCode() << ":" << GetOriginString() << std::endl;
whatBuffer = sb.str();
return whatBuffer.c_str();
}
const char* Window::Exception::GetType() const noexcept {
return "Voxa Novus has crashed with code:";
}
std::string Window::Exception::TranslateErrorCode(HRESULT hr) noexcept {
char* pMsgBuf = nullptr;
DWORD nMsgLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&pMsgBuf),
0,
nullptr
);
if (nMsgLen == 0) return "Unidentified error code.";
std::string errorString = pMsgBuf;
LocalFree(pMsgBuf);
return errorString;
}
HRESULT Window::Exception::GetErrorCode() const noexcept {
return hr;
}
std::string Window::Exception::GetErrorString() const noexcept {
return TranslateErrorCode(hr);
}
const char* Window::NoGfxException::GetType() const noexcept
{
return "VoxaNovus Crashed - No Graphics Card";
}