-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNouWindow.cpp
More file actions
179 lines (143 loc) · 3.66 KB
/
NouWindow.cpp
File metadata and controls
179 lines (143 loc) · 3.66 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
#include "NouWindow.h"
#include "resource.h"
#ifdef USE_IMGUI
#include "ImGUI/imgui.h"
#include "ImGUI/imgui_impl_win32.h"
#endif
#include <sstream>
NouWindow::WindowClass NouWindow::WindowClass::wndClass;
int NouWindow::LastQuitCode;
NouWindow::WindowClass::WindowClass() noexcept
: hInst(GetModuleHandle(nullptr))
{
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC;
wc.lpfnWndProc = HandleMsgSetup;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetInstance();
wc.hCursor = nullptr;
wc.hIcon = static_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDI_APPICON), IMAGE_ICON, 128, 128, 0));
wc.hIconSm = static_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDI_APPICON), IMAGE_ICON, 32, 32, 0));
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = GetName();
RegisterClassEx(&wc);
}
NouWindow::WindowClass::~WindowClass() noexcept
{
UnregisterClass(GetName(), GetInstance());
}
const char* NouWindow::WindowClass::GetName() noexcept
{
return wndClassName;
}
HINSTANCE NouWindow::WindowClass::GetInstance() noexcept
{
return wndClass.hInst;
}
// #### NouWindow
NouWindow::NouWindow(int width, int height, const char* name)
:
width(width),
height(height)
{
RECT wr;
wr.left = 100;
wr.right = width + wr.left;
wr.top = 100;
wr.bottom = height + wr.top;
DWORD style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
if (AdjustWindowRect(&wr, style, FALSE) == 0) {
throw NOU_LAST_EXCEPT();
}
this->width = wr.right - wr.left;
this->height = wr.bottom - wr.top;
hWnd = CreateWindow(
WindowClass::GetName(), name,
style,
CW_USEDEFAULT, CW_USEDEFAULT,
wr.right - wr.left,
wr.bottom - wr.top,
nullptr, nullptr,
WindowClass::GetInstance(),
this
);
if (hWnd == nullptr)
{
throw NOU_LAST_EXCEPT();
}
ShowWindow(hWnd, SW_SHOWDEFAULT);
pGfx = new GraphicsD11(hWnd, width, height);
}
NouWindow::~NouWindow() {
DestroyWindow(hWnd);
}
void NouWindow::SetTitle(const char* title)
{
if (SetWindowText(hWnd, title) == 0)
{
throw NOU_LAST_EXCEPT();
}
}
GraphicsD11& NouWindow::Gfx()
{
return *pGfx;
}
int NouWindow::GetWidth()
{
return width;
}
int NouWindow::GetHeight()
{
return height;
}
bool NouWindow::ProcessMessage()
{
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
NouWindow::LastQuitCode = (int)msg.wParam;
return true;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return false;
}
LRESULT CALLBACK NouWindow::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
if (msg == WM_NCCREATE)
{
const CREATESTRUCTW* const cr = reinterpret_cast<CREATESTRUCTW*>(lParam);
NouWindow* const window = static_cast<NouWindow*>(cr->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(window));
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&NouWindow::HandleMsgThunk));
return window->HandleMsg(hWnd, msg, wParam, lParam);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
LRESULT CALLBACK NouWindow::HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
NouWindow* window = reinterpret_cast<NouWindow*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
return window->HandleMsg(hWnd, msg, wParam, lParam);
}
#ifdef USE_IMGUI
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
#endif
LRESULT NouWindow::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
#ifdef USE_IMGUI
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
#endif
switch (msg) {
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}