-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
174 lines (145 loc) · 3.36 KB
/
Window.cpp
File metadata and controls
174 lines (145 loc) · 3.36 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
#include "Window.h"
// Main message handler for the program
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg) {
case WM_CREATE:
{
// Event fired when the window is created
break;
}
case WM_SIZE:
{
// Event fired when the windows is resized
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (window) window->onResize();
break;
}
case WM_SETFOCUS:
{
// Event fired when the window get focus
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (window) window->onFocus();
break;
}
case WM_KILLFOCUS:
{
// Event fired when the window lost focus
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
window->onKillFocus();
break;
}
case WM_DESTROY:
{
// Event fired when the window is destroyed
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
window->onDestroy();
::PostQuitMessage(0);
break;
}
// Handle any messages the switch statement didn't
default:
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
return NULL;
}
Window::Window()
{
// Setting up WNDCLASSEX object - holds information for the window class
WNDCLASSEX wc;
// Clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbClsExtra = NULL;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
wc.hInstance = nullptr;
wc.lpszClassName = L"MyWindowClass";
wc.lpszMenuName = L"";
wc.style = NULL;
wc.lpfnWndProc = WndProc;
if (!::RegisterClassEx(&wc)) {
throw std::exception("Window not created successfully");
}
// Creation of the window
mHwnd = ::CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
L"MyWindowClass",
L"DirectX Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
1024, 768,
nullptr, nullptr, nullptr, nullptr
);
// If the creation fails throw exception
if (!mHwnd) {
throw std::exception("Window not created successfully");
}
// Show the window
::ShowWindow(mHwnd, SW_SHOW);
::UpdateWindow(mHwnd);
// Indicate that the window is initialized and running
mIsRunning = true;
}
Window::~Window() = default;
bool Window::broadcast()
{
MSG msg;
if (!this->mIsInit) {
// Changes an attribute of the specified window.
// The function also sets a value at the specified offset in the extra window memory.
SetWindowLongPtr(mHwnd, GWLP_USERDATA, (LONG_PTR)this);
this->onCreate();
this->mIsInit = true;
}
this->onUpdate();
if (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(1);
return true;
}
bool Window::isRunning()
{
if (mIsRunning) broadcast();
return mIsRunning;
}
RECT Window::getClientWindowRect()
{
RECT rc;
::GetClientRect(this->mHwnd, &rc);
::ClientToScreen(this->mHwnd, (LPPOINT)&rc.left);
::ClientToScreen(this->mHwnd, (LPPOINT)&rc.right);
return rc;
}
RECT Window::getSizeScreen()
{
RECT rc;
rc.right = ::GetSystemMetrics(SM_CXSCREEN);
rc.bottom = ::GetSystemMetrics(SM_CYSCREEN);
return rc;
}
void Window::onCreate()
{
}
void Window::onUpdate()
{
}
void Window::onDestroy()
{
mIsRunning = false;
}
void Window::onFocus()
{
}
void Window::onKillFocus()
{
}
void Window::onResize()
{
}