-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApp.cpp
More file actions
269 lines (221 loc) · 6.04 KB
/
App.cpp
File metadata and controls
269 lines (221 loc) · 6.04 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
264
265
266
267
268
269
// Game.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "Settings.h"
#include "Engine.h"
#include "App.h"
#pragma comment(lib, "d2d1")
// Forward declarations of functions included in this code module:
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
if (SUCCEEDED(CoInitialize(NULL)))
{
MainApp app;
if (SUCCEEDED(app.Initialize()))
{
app.RunMessageLoop();
}
CoUninitialize();
}
return 0;
}
MainApp::MainApp() : m_hwnd(NULL)
{
engine = new Engine();
}
MainApp::~MainApp()
{
}
void MainApp::RunMessageLoop()
{
MSG msg;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
int frames = 0;
double framesTime = 0;
boolean running = true;
while (running)
{
end = std::chrono::steady_clock::now();
double elapsed_secs = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1000000.0;
begin = end;
// Display FPS
framesTime += elapsed_secs;
frames++;
if (framesTime > 1) {
WCHAR fpsText[32];
swprintf(fpsText, 32, L"Game: %d FPS", frames);
SetWindowText(m_hwnd, fpsText);
frames = 0;
framesTime = 0;
}
// Messages and user input
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
running = false;
}
// Game logic
if (elapsed_secs > 0.1)
{
elapsed_secs = 0.1; // safety so it doesn't go wild
}
engine->Logic(elapsed_secs);
// Drawing
engine->Draw();
}
}
HRESULT MainApp::Initialize()
{
HRESULT hr = S_OK;
// Register the window class.
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = MainApp::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"D2DMainApp";
ATOM x = RegisterClassEx(&wcex);
// Create the window.
m_hwnd = CreateWindowEx(
NULL,
L"D2DMainApp",
L"Game",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
RESOLUTION_X,
RESOLUTION_Y,
NULL,
NULL,
HINST_THISCOMPONENT,
this
);
hr = m_hwnd ? S_OK : E_FAIL;
// Adjust it so the client area is RESOLUTION_X/RESOLUTION_Y
RECT rect1;
GetWindowRect(m_hwnd, &rect1);
RECT rect2;
GetClientRect(m_hwnd, &rect2);
SetWindowPos(
m_hwnd,
NULL,
rect1.left,
rect1.top,
RESOLUTION_X + ((rect1.right - rect1.left) - (rect2.right - rect2.left)),
RESOLUTION_Y + ((rect1.bottom - rect1.top) - (rect2.bottom - rect2.top)),
NULL
);
if (SUCCEEDED(hr))
{
engine->InitializeD2D(m_hwnd);
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
}
return hr;
}
LRESULT CALLBACK MainApp::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
MainApp* pMainApp = (MainApp*)pcs->lpCreateParams;
::SetWindowLongPtrW(
hwnd,
GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(pMainApp)
);
result = 1;
}
else
{
MainApp* pMainApp = reinterpret_cast<MainApp*>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hwnd,
GWLP_USERDATA
)));
bool wasHandled = false;
if (pMainApp)
{
switch (message)
{
case WM_DISPLAYCHANGE:
{
InvalidateRect(hwnd, NULL, FALSE);
}
result = 0;
wasHandled = true;
break;
case WM_KEYDOWN:
{
pMainApp->engine->KeyDown(wParam);
}
result = 0;
wasHandled = true;
break;
case WM_KEYUP:
{
pMainApp->engine->KeyUp(wParam);
}
result = 0;
wasHandled = true;
break;
case WM_MOUSEMOVE:
{
pMainApp->engine->MousePosition(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
}
result = 0;
wasHandled = true;
break;
case WM_LBUTTONUP:
{
pMainApp->engine->MouseButtonUp(true, false);
}
result = 0;
wasHandled = true;
break;
case WM_LBUTTONDOWN:
{
pMainApp->engine->MouseButtonDown(true, false);
}
result = 0;
wasHandled = true;
break;
case WM_RBUTTONUP:
{
pMainApp->engine->MouseButtonUp(false, true);
}
result = 0;
wasHandled = true;
break;
case WM_RBUTTONDOWN:
{
pMainApp->engine->MouseButtonDown(false, true);
}
result = 0;
wasHandled = true;
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
result = 1;
wasHandled = true;
break;
}
}
if (!wasHandled)
{
result = DefWindowProc(hwnd, message, wParam, lParam);
}
}
return result;
}