-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathGameCore.cpp
More file actions
182 lines (142 loc) · 5.25 KB
/
GameCore.cpp
File metadata and controls
182 lines (142 loc) · 5.25 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
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author: James Stanard
//
#include "pch.h"
#include "GameCore.h"
#include "GraphicsCore.h"
#include "SystemTime.h"
#include "GameInput.h"
#include "BufferManager.h"
#include "CommandContext.h"
#include "PostEffects.h"
#include "Display.h"
#include "Util/CommandLineArg.h"
#include <shellapi.h>
#pragma comment(lib, "runtimeobject.lib")
namespace GameCore
{
using namespace Graphics;
bool gIsSupending = false;
void InitializeApplication( IGameApp& game )
{
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
CommandLineArgs::Initialize(argc, argv);
Graphics::Initialize(game.RequiresRaytracingSupport());
SystemTime::Initialize();
GameInput::Initialize();
EngineTuning::Initialize();
game.Startup();
}
void TerminateApplication( IGameApp& game )
{
g_CommandManager.IdleGPU();
game.Cleanup();
GameInput::Shutdown();
}
bool UpdateApplication( IGameApp& game )
{
EngineProfiling::Update();
float DeltaTime = Graphics::GetFrameTime();
GameInput::Update(DeltaTime);
EngineTuning::Update(DeltaTime);
game.Update(DeltaTime);
game.RenderScene();
PostEffects::Render();
GraphicsContext& UiContext = GraphicsContext::Begin(L"Render UI");
UiContext.TransitionResource(g_OverlayBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, true);
UiContext.ClearColor(g_OverlayBuffer);
UiContext.SetRenderTarget(g_OverlayBuffer.GetRTV());
UiContext.SetViewportAndScissor(0, 0, g_OverlayBuffer.GetWidth(), g_OverlayBuffer.GetHeight());
game.RenderUI(UiContext);
UiContext.SetRenderTarget(g_OverlayBuffer.GetRTV());
UiContext.SetViewportAndScissor(0, 0, g_OverlayBuffer.GetWidth(), g_OverlayBuffer.GetHeight());
EngineTuning::Display( UiContext, 10.0f, 40.0f, 1900.0f, 1040.0f );
UiContext.Finish();
Display::Present();
return !game.IsDone();
}
// Default implementation to be overridden by the application
bool IGameApp::IsDone( void )
{
return GameInput::IsFirstPressed(GameInput::kKey_escape);
}
HWND g_hWnd = nullptr;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
int RunApplication( IGameApp& app, const wchar_t* className, HINSTANCE hInst, int nCmdShow )
{
if (!XMVerifyCPUSupport())
return 1;
Microsoft::WRL::Wrappers::RoInitializeWrapper InitializeWinRT(RO_INIT_MULTITHREADED);
ASSERT_SUCCEEDED(InitializeWinRT);
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(hInst, IDI_APPLICATION);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = className;
wcex.hIconSm = LoadIcon(hInst, IDI_APPLICATION);
ASSERT(0 != RegisterClassEx(&wcex), "Unable to register a window");
// Create window
RECT rc = { 0, 0, (LONG)g_DisplayWidth, (LONG)g_DisplayHeight };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindow(className, className, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInst, nullptr);
ASSERT(g_hWnd != 0);
InitializeApplication(app);
ShowWindow( g_hWnd, nCmdShow/*SW_SHOWDEFAULT*/ );
do
{
MSG msg = {};
bool done = false;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
done = true;
}
if (done)
break;
}
while (UpdateApplication(app)); // Returns false to quit loop
TerminateApplication(app);
Graphics::Shutdown();
return 0;
}
//--------------------------------------------------------------------------------------
// Called every time the application receives a message
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_SIZE:
if( wParam != SIZE_MINIMIZED )
Display::Resize((UINT)(UINT64)lParam & 0xFFFF, (UINT)(UINT64)lParam >> 16);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}
}