-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
362 lines (308 loc) · 19 KB
/
Main.cpp
File metadata and controls
362 lines (308 loc) · 19 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "stdafx.h"
#pragma once
#include "Main.h"
#include "graphics.h"
#include "Camera.h"
#include "Keyboard.h"
#include "ImageRenderer.h"
#include "WorldRenderer.h"
#include "DynTriangleRenderer.h"
HWND hwnd = nullptr;
LPCTSTR WindowName = L"DxWindow";
LPCTSTR WindowTitle = L"DX Window";
RECT currentWinRect;
bool fullScreen = false;
float fps = 0.0;
void mainloop();
void wm_input(WPARAM, LPARAM);
void wm_keydown(WPARAM, LPARAM);
void toggleFullscreen();
void updateWin();
void setNewWinSize();
void Benchmark(int);
bool loadSplashscreen();
Graphics G;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprevinst, LPSTR cmdline, int ncmdshow) {
RAWINPUTDEVICE Rid[1];
ZeroMemory(&Rid, sizeof(RAWINPUTDEVICE));
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02; // adds mouse
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
//registration failed. Call GetLastError for the cause of the error.
Log::Error(L"Register Raw input - Failed");
return 0;
}
hwnd = InitWindow(hInstance, WindowTitle, DXGE_WINDOW_DEFAULT_WIDTH, DXGE_WINDOW_DEFAULT_HEIGHT, fullScreen, 0);
if (!hwnd) {
Log::Error(L"Window Initialization - Failed");
return 0;
}
double PCFreq = 0;
__int64 CounterStart = 0;
double timeInMs = 0.0;
LARGE_INTEGER li;
if (!QueryPerformanceFrequency(&li)) {
Log::Error("QueryPerformance failed");
return 0;
}
PCFreq = double(li.QuadPart) / 1000;
// take startTime
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
loadSplashscreen();
// init Graphics with world renderer here
if (G.Init(hwnd, DXGE_WINDOW_DEFAULT_WIDTH, DXGE_WINDOW_DEFAULT_HEIGHT, m_vertCount, new WorldRenderer())) {
QueryPerformanceCounter(&li);
timeInMs = double(li.QuadPart - CounterStart) / PCFreq;
if (timeInMs < 2000) {
Sleep(2000 - timeInMs);
}
Camera::SetCameraPosition(XMFLOAT3(10.0f, 1.8f, 14.0f));
mainloop();
}
return 0;
}
void mainloop() {
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
// Message Loop until window quits
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
// run game code
double delta = m_timer.GetFrameDelta();
if (!G.Render(delta)) {
// End programm on error
break;
}
m_splashscreen = false;
}
}
G.Release();
}
bool loadSplashscreen() {
m_splashscreen = true;
while (ShowCursor(false) == 0);
if (!G.Init(hwnd, DXGE_WINDOW_DEFAULT_WIDTH, DXGE_WINDOW_DEFAULT_HEIGHT, m_vertCount, new ImageRenderer())) {
Log::Error("Init m_splashscreen failed");
return false;
}
Camera::SetCameraPosition(XMFLOAT3(0.0f, 0.0f, 2.0f));
double delta = m_timer.GetFrameDelta();
if (!G.Render(delta)) {
// End programm on error
return false;
}
G.Release();
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_INPUT:
if (!m_splashscreen) {
wm_input(wParam, lParam);
}
break;
case WM_KEYDOWN:
if (!m_splashscreen) {
// Register keydown actions
Keyboard::KeyDown(wParam);
wm_keydown(wParam, lParam);
}
break;
case WM_KEYUP:
Keyboard::KeyUp(wParam);
break;
case WM_DESTROY:
// Destroy the window
PostQuitMessage(0);
return 0;
case WM_MOUSEMOVE:
// Hide the cursor inside the window
while (ShowCursor(false) == 0);
break;
default:
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void wm_input(WPARAM wParam, LPARAM lParam) {
HRESULT hr;
UINT dwSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, nullptr, &dwSize, sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == nullptr) {
Log::Error(L"lpb returns null - Failed");
return;
}
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize) {
Log::Error("GetRawInputData does not return correct size !");
return;
}
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE) {
//lLastY/Y = signed relative or absolute motion in the X/Y direction
Camera::Rotate(raw->data.mouse.lLastX, raw->data.mouse.lLastY);
}
delete[] lpb;
}
void wm_keydown(WPARAM wParam, LPARAM lParam) {
switch (wParam) {
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
case VK_F11:
// toggle fullscreen
toggleFullscreen();
// Set the new window size
setNewWinSize();
// Resize and init with new width/height
updateWin();
break;
case 0x48:
// 0x48 = H-Key
Log::Info("");
Log::Info("#############################");
Log::Info("# HELP #");
Log::Info("#___________________________#");
Log::Info("# #");
Log::Info("# W: Move forward #");
Log::Info("# A: Move left #");
Log::Info("# S: Move backward #");
Log::Info("# D: Move right #");
Log::Info("# H: Show this help #");
Log::Info("# I: Show current fps #");
Log::Info("# F11: Toggle fullscreen #");
Log::Info("# ESC: Close the programm #");
Log::Info("# #");
Log::Info("#############################");
Log::Info("");
break;
case 0x49:
// 0x48 = I-Key
Log::Info("FPS: " + std::to_string(m_timer.fps));
break;
default:
break;
}
}
void toggleFullscreen() {
WINDOWPLACEMENT wndpl;
GetWindowPlacement(hwnd, &wndpl);
// Maximize the window
wndpl.showCmd = wndpl.showCmd == SW_SHOWMAXIMIZED ? SW_SHOWNORMAL : SW_SHOWMAXIMIZED;
SetWindowPlacement(hwnd, &wndpl);
}
void updateWin() {
G.Release();
G.Init(hwnd, m_width, m_height, m_vertCount, new WorldRenderer());
}
void setNewWinSize() {
GetWindowRect(hwnd, ¤tWinRect);
m_width = currentWinRect.right - currentWinRect.left;
m_height = currentWinRect.bottom - currentWinRect.top;
}
HWND InitWindow(HINSTANCE h_instance, LPCWSTR window_title, UINT width, UINT height, bool full_screen,
int background_color) {
HWND hwnd = nullptr;
// RECT is used to determine the window position
RECT rc;
GetClientRect(GetDesktopWindow(), &rc);
WNDCLASS wc;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = h_instance;
wc.hIcon = LoadIcon(h_instance, MAKEINTRESOURCE(IDI_ICON_THM));
wc.hCursor = nullptr;
wc.hbrBackground = CreateSolidBrush(background_color);
wc.lpszMenuName = nullptr;
wc.lpszClassName = WindowName;
if (!RegisterClass(&wc)) {
Log::Error(L"Error registering class");
return nullptr;
}
hwnd = CreateWindow(
wc.lpszClassName,
window_title,
WS_POPUPWINDOW,
// set rc left and top to center window
(rc.right - width) * 0.5,
(rc.bottom - height) * 0.5,
width, height,
NULL, // handle to the parent or owner window of the window being created
NULL, // handle to a menu, or specifies a child-window identifier depending on the window style
h_instance,
NULL // A pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member)
// pointed to by the lParam param of the WM_CREATE message.
// This message is sent to the created window by this function before it returns.
// lpParam may be NULL if no additional data is needed
);
if (!hwnd) {
Log::Error(L"Error creating window");
return nullptr;
}
ShowWindow(hwnd, full_screen ? SHOW_FULLSCREEN : SHOW_OPENWINDOW);
UpdateWindow(hwnd);
return hwnd;
}
void Benchmark(int type) {
float x = 0.0;
float y = 0.0;
float z = 0.0;
float v = 13;
float t = 13;
double PCFreq = 0;
__int64 CounterStart = 0;
double timeInMs = 0.0;
LARGE_INTEGER li;
XMFLOAT4X4 target = {};
XMFLOAT4 vectorTarget = {};
if (!QueryPerformanceFrequency(&li)) {
Log::Error("QueryPerformance failed");
return;
}
PCFreq = double(li.QuadPart) / 1000;
// take startTime
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
if (type == 1) {
for (int i = 0; i < 10000; ++i) {
x += v * t * i;
y += v * t * (i + 1);
z += v * t * (i + 2);
}
} else if (type == 2) {
for (int i = 0; i < 10000; ++i) {
x += v * t * i;
y += v * t * (i + 1);
z += v * t * (i + 2);
XMFLOAT3 coord = {x, y, z};
XMMATRIX matrix = XMMatrixRotationAxis(XMLoadFloat3(&coord), i);
XMStoreFloat4x4(&target, matrix);
}
} else if (type == 3) {
for (int i = 0; i < 10000; ++i) {
x += v * t * i;
y += v * t * (i + 1);
z += v * t * (i + 2);
XMMATRIX matrix = XMMatrixRotationRollPitchYaw(x, y, z);
XMStoreFloat4x4(&target, matrix);
}
} else if (type == 4) {
for (int i = 0; i < 10000; ++i) {
x += v * t * i;
y += v * t * (i + 1);
z += v * t * (i + 2);
XMVECTOR vector = XMQuaternionRotationRollPitchYaw(x, y, z);
XMStoreFloat4(&vectorTarget, vector);
}
}
QueryPerformanceCounter(&li);
timeInMs = double(li.QuadPart - CounterStart) / PCFreq;
//take endTime
Log::Info("Benchmark type " + std::to_string(type) + " finished in " + std::to_string(timeInMs) + "ms");
}