-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
190 lines (163 loc) · 4.24 KB
/
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
#include "Window.hpp"
#include "System/String.hpp"
namespace nu
{
#ifdef VK_USE_PLATFORM_WIN32_KHR
namespace
{
enum UserMessage
{
USER_MESSAGE_RESIZE = WM_USER + 1,
USER_MESSAGE_QUIT,
USER_MESSAGE_MOUSE_CLICK,
USER_MESSAGE_MOUSE_MOVE,
USER_MESSAGE_MOUSE_WHEEL
};
} // namespace
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
PostMessage(hWnd, USER_MESSAGE_MOUSE_CLICK, 0, 1);
break;
case WM_LBUTTONUP:
PostMessage(hWnd, USER_MESSAGE_MOUSE_CLICK, 0, 0);
break;
case WM_RBUTTONDOWN:
PostMessage(hWnd, USER_MESSAGE_MOUSE_CLICK, 1, 1);
break;
case WM_RBUTTONUP:
PostMessage(hWnd, USER_MESSAGE_MOUSE_CLICK, 1, 0);
break;
case WM_MOUSEMOVE:
PostMessage(hWnd, USER_MESSAGE_MOUSE_MOVE, LOWORD(lParam), HIWORD(lParam));
break;
case WM_MOUSEWHEEL:
PostMessage(hWnd, USER_MESSAGE_MOUSE_WHEEL, HIWORD(wParam), 0);
break;
case WM_SIZE:
case WM_EXITSIZEMOVE:
PostMessage(hWnd, USER_MESSAGE_RESIZE, wParam, lParam);
break;
case WM_KEYDOWN:
if (VK_ESCAPE == wParam) {
PostMessage(hWnd, USER_MESSAGE_QUIT, wParam, lParam);
}
break;
case WM_CLOSE:
PostMessage(hWnd, USER_MESSAGE_QUIT, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Window::Window()
{
mParameters.platformConnection = GetModuleHandle(nullptr);
WNDCLASSEX windowClass =
{
sizeof(WNDCLASSEX), // UINT cbSize
/* Win 3.x */
CS_HREDRAW | CS_VREDRAW, // UINT style
WindowProcedure, // WNDPROC lpfnWndProc
0, // int cbClsExtra
0, // int cbWndExtra
mParameters.platformConnection, // HINSTANCE hInstance
nullptr, // HICON hIcon
LoadCursor(nullptr, IDC_ARROW), // HCURSOR hCursor
(HBRUSH)(COLOR_WINDOW + 1), // HBRUSH hbrBackground
nullptr, // LPCSTR lpszMenuName
"NumeaEngine", // LPCSTR lpszClassName
/* Win 4.0 */
nullptr // HICON hIconSm
};
if (!RegisterClassEx(&windowClass))
{
return;
}
}
Window::Window(const std::string& windowTitle, int x, int y, int width, int height)
: Window()
{
create(windowTitle, x, y, width, height);
}
Window::~Window()
{
close();
if (mParameters.platformConnection)
{
UnregisterClass("NumeaEngine", mParameters.platformConnection);
}
}
bool Window::create(const std::string& windowTitle, int x, int y, int width, int height)
{
mParameters.platformWindow = CreateWindow("NumeaEngine", windowTitle.c_str(), WS_OVERLAPPEDWINDOW, x, y, width, height, nullptr, nullptr, mParameters.platformConnection, nullptr);
if (!mParameters.platformWindow)
{
return false;
}
ShowWindow(mParameters.platformWindow, SW_SHOWNORMAL);
UpdateWindow(mParameters.platformWindow);
mOpen = true;
return true;
}
void Window::close()
{
mOpen = false;
if (mParameters.platformWindow)
{
DestroyWindow(mParameters.platformWindow);
mParameters.platformWindow = NULL;
}
}
bool Window::isOpen() const
{
return mOpen;
}
bool Window::pollEvent(Event& event)
{
MSG message;
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
switch (message.message)
{
case USER_MESSAGE_MOUSE_CLICK:
event.type = EventType::MouseClick;
event.param1 = (int)message.wParam;
event.param2 = (int)(message.lParam > 0);
break;
case USER_MESSAGE_MOUSE_MOVE:
event.type = EventType::MouseMove;
event.param1 = (int)message.wParam;
event.param2 = (int)message.lParam;
break;
case USER_MESSAGE_MOUSE_WHEEL:
event.type = EventType::MouseWheel;
event.param1 = (int)message.wParam;
event.param2 = 0;
break;
case USER_MESSAGE_RESIZE:
event.type = EventType::Resize;
event.param1 = 0;
event.param2 = 0;
break;
case USER_MESSAGE_QUIT:
event.type = EventType::Close;
event.param1 = 0;
event.param2 = 0;
break;
}
TranslateMessage(&message);
DispatchMessage(&message);
return true;
}
return false;
}
const VulkanWindowParameters& Window::getParameters() const
{
return mParameters;
}
#endif
} // namespace nu