-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.cpp
525 lines (436 loc) · 15.3 KB
/
Core.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "stdafx.h"
#include "Core.h"
#include <chrono>
#include "SceneManager.h"
#include "InputManager.h"
#include "PhysxManager.h"
#include "ContentManager.h"
#include "SoundManager.h"
#include "GameScene.h"
#include "SceneSelector.h"
GameSettings Core::m_GameSettings{};
Core::Core()
: m_WindowClassName{L"GP2_PhysXFramework_2022"}
, m_IsActive{ true }
, m_pSceneManager{ new SceneManager{} }
{
if(!XMVerifyCPUSupport()) Logger::GetInstance()->LogError(L"XMVerifyCPUSupport: failed");
}
Core::~Core()
{
SafeRelease(m_pDXGIAdapter);
SafeRelease(m_pDXGIOutput);
//Game Cleanup
delete m_pSceneManager;
// delete singletons to prevent memleak reports
delete PhysxManager::GetInstance();
delete Logger::GetInstance();
delete ContentManager::GetInstance();
delete SoundManager::GetInstance();
SafeRelease(m_pRenderTargetView);
SafeRelease(m_pDepthStencilView);
SafeRelease(m_pTexDepthStencilBuffer);
SafeRelease(m_pDXGIFactory);
SafeRelease(m_pSwapChain);
if (m_D3D11.pDeviceContext)
{
m_D3D11.pDeviceContext->ClearState();
m_D3D11.pDeviceContext->Flush();
SafeRelease(m_D3D11.pDeviceContext);
}
SafeRelease(m_D3D11.pDevice);
}
void Core::AddScenes()
{
dae::AddScenes(m_pSceneManager);
//GameScene::EnablePhysXFrameStepping(true);
Logger::GetInstance()->LogInfo(L"Press \'Page Up\' or \'Page Down\' to cycle between the different scenes");
Logger::GetInstance()->LogInfo(L"Press \'I\' to toggle the debug renderer.");
Logger::GetInstance()->LogInfo(L"Press \'M\' to do physics stepping.");
Logger::GetInstance()->LogInfo(L"Press \'L\' to stop doing physics stepping.");
m_pSceneManager->Initialize();
Logger::GetInstance()->LogDebug(L"Scenes Initialized!");
}
void Core::StateChanged(StateType state, bool active)
{
switch (state)
{
//WINDOW ACTIVE/INACTIVE
case StateType::window:
if (m_IsActive != active)
{
m_IsActive = active;
m_pSceneManager->WindowStateChanged(active);
}
break;
//INPUT ACTIVE/INACTIVE
case StateType::input:
InputManager::SetEnabled(active);
break;
}
}
HRESULT Core::RunGame(HINSTANCE hInstance)
{
// provide the locator with a reference to my D3D11 object
// objects that need D3D11, use the Locator service
// preventing having to clutter code by passing the d3d11 to everywhere
Locator::SetD3D11(m_D3D11);
m_hInstance = hInstance;
HRESULT hr;
//Initialize Debug Logger (Needed for logging during Initialization)
Logger::GetInstance()->Initialize();
#ifdef _DEBUG
Logger::GetInstance()->StartFileLogging(L"Log.txt");
#endif
//PREPARE GAME SETTINGS:
m_GameSettings.isVSyncOn = true; // bij true, cubes bounce
m_GameSettings.clearColor = XMFLOAT3(DirectX::Colors::CornflowerBlue);
m_GameSettings.width = 1280;
m_GameSettings.height = 720;
//0. Query directx for adapters and output screen information (dpi aware)
hr = InitalizeAdapterAndOutput();
if (FAILED(hr)) return hr;
Logger::GetInstance()->LogDebug(L"InitalizeAdapterAndOutput ok.");
//1. Create the Window Class
hr = CreateWindowClass();
if(FAILED(hr)) return hr;
Logger::GetInstance()->LogDebug(L"Window Class Created");
//2. Open the window
hr = OpenWindow();
if(FAILED(hr)) return hr;
Logger::GetInstance()->LogDebug(L"Window Opened");
//3. Direct3D Init
hr = InitializeDirect3D();
if(FAILED(hr)) return hr;
Logger::GetInstance()->LogDebug(L"Direct3D Initialized");
//4. Initialize Managers
ContentManager::GetInstance()->Initialize(m_D3D11.pDevice);
PhysxManager::GetInstance();
SoundManager::GetInstance();
Logger::GetInstance()->LogDebug(L"Managers Initialized!");
//5. Add Game scenes and initialize the scenes
AddScenes(); // pass width/height for camera aspect ratio
//6. Start the message pump
MSG msg = {0};
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// prepare for rendering, clear background, etc
XMVECTOR clearColor = XMLoadFloat3(&m_GameSettings.clearColor);
m_D3D11.pDeviceContext->ClearRenderTargetView(m_pRenderTargetView, clearColor.m128_f32);
m_D3D11.pDeviceContext->ClearDepthStencilView(m_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
m_pSceneManager->Update();
SoundManager::GetInstance()->Update();
m_pSceneManager->Draw();
// swap front and backbuffer: Display next rendered frame
m_pSwapChain->Present(m_GameSettings.isVSyncOn ? 1 : 0, 0);
}
//7. Finished
Logger::GetInstance()->LogDebug(L"Core Initialization Finished");
return (int)msg.wParam;
}
HRESULT Core::InitalizeAdapterAndOutput()
{
HRESULT hr;
//Create DXGIFactory
hr = CreateDXGIFactory1(__uuidof(IDXGIFactory), (void**)(&m_pDXGIFactory));
if(Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D::CreateDXGIFactory"))
{
return hr;
}
//Allows DXGI to monitor an application's message queue for the alt-enter key sequence
//(which causes the application to switch from windowed to full screen or vice versa).
//hr = pDXGIFactory->MakeWindowAssociation(m_WindowHandle, 0);
//if(AlertOnFail(hr, L"InitializeDirect3D: ::MakeWindowAssociation(...)"))return hr;
//Get all Graphics Adapters
UINT i = 0;
while (m_pDXGIFactory->EnumAdapters(i, &m_pDXGIAdapter) != DXGI_ERROR_NOT_FOUND)
{
DXGI_ADAPTER_DESC adapterDescriptor;
m_pDXGIAdapter->GetDesc(&adapterDescriptor);
Logger::GetInstance()->LogFormat(LogLevel::Debug, L"Adapter %i : %s", i, adapterDescriptor.Description);
++i;
}
Logger::GetInstance()->LogFormat(LogLevel::Debug, L"%i Adapter(s) found", i);
// use the first adapter
hr = m_pDXGIFactory->EnumAdapters(0, &m_pDXGIAdapter);
// store the gi output adapter (has info on desktop coordinates)
IDXGIOutput *tempOutput{};
hr = m_pDXGIAdapter->EnumOutputs(0, &tempOutput);
if (FAILED(hr)) return hr;
hr = tempOutput->QueryInterface(__uuidof(IDXGIOutput), reinterpret_cast<void**>(&m_pDXGIOutput));
if (FAILED(hr)) return hr;
tempOutput->Release();
return hr;
}
HRESULT Core::CreateWindowClass()
{
m_WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
m_WindowClass.hIcon = nullptr;
m_WindowClass.hbrBackground = nullptr;
m_WindowClass.style = CS_HREDRAW | CS_VREDRAW;
m_WindowClass.lpfnWndProc = WindowsProcedureStatic;
m_WindowClass.hInstance = GetModuleHandle(0);
m_WindowClass.lpszClassName = m_WindowClassName.c_str();
if(!RegisterClass( &m_WindowClass))
{
DWORD error = GetLastError();
return HRESULT_FROM_WIN32(error);
}
return S_OK;
}
HRESULT Core::OpenWindow()
{
DXGI_OUTPUT_DESC outputDesc;
auto hr = m_pDXGIOutput->GetDesc(&outputDesc);
if (FAILED(hr))return hr;
Logger::GetInstance()->LogFormat(LogLevel::Debug, L"Desktop resolution: %i x %i", outputDesc.DesktopCoordinates.right, outputDesc.DesktopCoordinates.bottom);
//Calculate position (centered)
RECT R = {0,0,m_GameSettings.width,m_GameSettings.height};
AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
LONG windowWidth = R.right - R.left;
LONG windowHeight = R.bottom - R.top;
//Calculate the position of the window to be centered on the desktop
int posX = outputDesc.DesktopCoordinates.left + ((outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left) / 2) - windowWidth / 2;
int posY = (outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top) / 2 - windowHeight / 2;
m_D3D11.windowHandle = CreateWindowW( m_WindowClassName.c_str(),
m_GameSettings.windowTitle.c_str(),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
posX,
posY,
windowWidth,
windowHeight,
NULL,
0,
m_hInstance,
this);//'this' is retrieved in WindowsProcedureStatic by lpCreateParams
if(m_D3D11.windowHandle == NULL)
{
DWORD error = GetLastError();
return HRESULT_FROM_WIN32(error);
}
//Show the window
ShowWindow(m_D3D11.windowHandle, SW_SHOWDEFAULT);
return S_OK;
}
HRESULT Core::InitializeDirect3D()
{
HRESULT hr;
//4.2.1 Create the Device and Context
UINT createDeviceFlags{ 0 };
#if defined(DEBUG) || defined(_DEBUG)
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0 ;
hr = D3D11CreateDevice(m_pDXGIAdapter,//when pDXGIAdapter is used, nex param must be D3D_DRIVER_TYPE_UNKNOWN
D3D_DRIVER_TYPE_UNKNOWN,
NULL,
createDeviceFlags,
0,0,
D3D11_SDK_VERSION,
&m_D3D11.pDevice,
&featureLevel,
&m_D3D11.pDeviceContext
);
if (Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D: D3D11CreateDevice"))
{
return hr;
}
if (featureLevel < D3D_FEATURE_LEVEL_10_0)
{
Logger::GetInstance()->LogError( L"Feature level 10.0+ not supported on this device!");
exit(-1);
}
if (featureLevel < D3D_FEATURE_LEVEL_11_0)
{
Logger::GetInstance()->LogWarning(L"Feature level 10.1, some DirectX11 specific features won't be available on this device!");
}
//Create Swapchain descriptor
DXGI_SWAP_CHAIN_DESC swapChainDesc{};
swapChainDesc.BufferDesc.Width = m_GameSettings.width;
swapChainDesc.BufferDesc.Height = m_GameSettings.height;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 1;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 60;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = m_D3D11.windowHandle;
swapChainDesc.Windowed = true;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = 0;
hr = m_pDXGIFactory->CreateSwapChain(m_D3D11.pDevice, &swapChainDesc, &m_pSwapChain);
if (FAILED(hr)) return hr;
//release temporary resources
m_pDXGIFactory->Release();
//4.2.5 Create the Render Target View
void *pRenderTargetBuffer{ nullptr };
hr = m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &pRenderTargetBuffer);
if (Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D: m_pSwapChain->GetBuffer"))
{
return hr;
}
hr = m_D3D11.pDevice->CreateRenderTargetView(static_cast<ID3D11Resource*>(pRenderTargetBuffer), 0, &m_pRenderTargetView);
if (Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D: m_pDevice->CreateRenderTargetView"))
{
return hr;
}
if(pRenderTargetBuffer)static_cast<ID3D11Resource*>(pRenderTargetBuffer)->Release();
//4.2.6 Create the Depth/Stencil Buffer and View
D3D11_TEXTURE2D_DESC depthStencilDesc{};
depthStencilDesc.Width = m_GameSettings.width;
depthStencilDesc.Height = m_GameSettings.height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1; // multisampling must match
depthStencilDesc.SampleDesc.Quality = 0; // swap chain values.
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
hr = m_D3D11.pDevice->CreateTexture2D(&depthStencilDesc, 0, &m_pTexDepthStencilBuffer);
if (Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D: m_pDevice->CreateTexture2D m_pTexDepthStencilBuffer"))
{
return hr;
}
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc{};
depthStencilViewDesc.Format = depthStencilDesc.Format;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
hr = m_D3D11.pDevice->CreateDepthStencilView(m_pTexDepthStencilBuffer, &depthStencilViewDesc, &m_pDepthStencilView);
if (Logger::GetInstance()->LogHResult(hr, L"InitializeDirect3D: m_pDevice->CreateDepthStencilView"))
{
return hr;
}
//4.2.7 Bind the Views to the Output Merger Stage
m_D3D11.pDeviceContext->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);
//4.2.8 Set the Viewport
m_Viewport.Width = float(m_GameSettings.width);
m_Viewport.Height = float(m_GameSettings.height);
m_Viewport.TopLeftX = 0;
m_Viewport.TopLeftY = 0;
m_Viewport.MinDepth = 0.0f;
m_Viewport.MaxDepth = 1.0f;
m_D3D11.pDeviceContext->RSSetViewports(1,&m_Viewport);
return S_OK;
}
#pragma region
LRESULT CALLBACK Core::WindowsProcedureStatic(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_CREATE)
{
CREATESTRUCT *pCS = (CREATESTRUCT*)lParam;
auto par = (LONG_PTR)pCS->lpCreateParams;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pCS->lpCreateParams);
}
else
{
Core* pThisGame = (Core*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if(pThisGame) return pThisGame->WindowsProcedure(hWnd, message, wParam, lParam);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void Core::SetWindowTitle(const std::wstring& title)
{
SetWindowText(Locator::GetD3D11()->windowHandle, title.c_str());
}
LRESULT Core::WindowsProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
m_pSceneManager->ProcessMessage(message, wParam, lParam);
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_KEYUP:
{
if ((lParam & 0x80000000) != 0x80000000)
return -1;
// NextScene
if (wParam == VK_NEXT || wParam == 'P')
{
m_pSceneManager->NextScene();
return 0;
}
// PreviousScene
if (wParam == VK_PRIOR || wParam == 'O')
{
m_pSceneManager->PreviousScene();
return 0;
}
// swap physx line rendering
if (wParam == 'I')
{
m_pSceneManager->ToggleDebugRenderer();
return 0;
}
// PhysX enable Time Stepping and make a step
else if (wParam == 'M')
{
GameScene::EnablePhysXFrameStepping(true);
GameScene::SetPhysicsStepTime(1.0f/60);
return 0;
}
// PhysX disable timestepping
else if (wParam == 'L')
{
GameScene::EnablePhysXFrameStepping(false);
return 0;
}
}
// https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-activate
case WM_ACTIVATE:
//If the low - order word of wParam is WA_ACTIVE or WA_CLICKACTIVE, lParam is the handle to the window being deactivated.
if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE)
{
if (HWND(wParam) == m_D3D11.windowHandle)
{
StateChanged(StateType::input, false);
}
else StateChanged(StateType::input, true);
}
//If the low-order word of wParam is WA_INACTIVE, lParam is the handle to the window being activated.
if (wParam == WA_INACTIVE)
{
if (HWND(wParam) == m_D3D11.windowHandle)
{
StateChanged(StateType::input, true);
}
else StateChanged(StateType::input, false);
}
return 0;
//Sent to a window after its size has changed.
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
StateChanged(StateType::input, false);
else if (wParam == SIZE_RESTORED)
StateChanged(StateType::input, true);
return 0;
// Sent to a window after it has gained the keyboard focus. wParam: A handle to the window that has lost the keyboard focus.
case WM_SETFOCUS:
StateChanged(StateType::input, true);
return 0;
// Sent to a window immediately before it loses the keyboard focus. wParam: A handle to the window that receives the keyboard focus.
case WM_KILLFOCUS:
StateChanged(StateType::input, false);
return 0;
case WM_ENTERSIZEMOVE:
StateChanged(StateType::input, false);
StateChanged(StateType::window, false);
return 0;
case WM_EXITSIZEMOVE:
StateChanged(StateType::input, true);
StateChanged(StateType::window, true);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
#pragma endregion Windows Procedures