-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·305 lines (241 loc) · 10.9 KB
/
main.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
//Based off Empty Project in DX SDK Samples
#include "DXUT.h"
#include "DebugText.h"
#include "DevConsole.h"
#include "Gui.h"
#include "Renderer.h"
#include "SDKmisc.h"
#include "DXUTgui.h"
#include <stdlib.h>
#include <sstream>
#define FOCUSED_GUI 0
#define FOCUSED_CONSOLE 1
#define FOCUSED_RENDERER 2
CDXUTDialogResourceManager dxutDialogResourceManager; // manager for shared resources of dialogs
DebugText debugText = DebugText();
DevConsole devConsole = DevConsole(&debugText);
Gui gui = Gui(&debugText);
Renderer renderer(&devConsole);
RendererMessageProcessor* rMProc;
UINT focusedUI = FOCUSED_GUI;
//--------------------------------------------------------------------------------------
// Reject any D3D11 devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo,
DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
devConsole.log(L"IsD3D11DeviceAcceptable");
return true;
}
//--------------------------------------------------------------------------------------
// Called right before creating a D3D9 or D3D11 device, allowing the app to modify the device settings as needed
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
{
devConsole.log(L"ModifyDeviceSettings");
pDeviceSettings->d3d11.AutoCreateDepthStencil = FALSE;
return true;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that aren't dependent on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
devConsole.log(L"OnD3D11CreateDevice");
HRESULT hr;
ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
V_RETURN(dxutDialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) );
V_RETURN(debugText.OnD3D11CreateDevice(pd3dDevice,&dxutDialogResourceManager));
V_RETURN(gui.OnD3D11CreateDevice(pd3dDevice));
V_RETURN(renderer.OnD3D11CreateDevice(pd3dDevice,pBackBufferSurfaceDesc));
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
devConsole.log(L"OnD3D11ResizedSwapChain");
std::wstringstream wss;
wss << L"New Window Size = " << pBackBufferSurfaceDesc->Width << L"x" << pBackBufferSurfaceDesc->Height;
devConsole.log(&wss);
HRESULT hr;
V_RETURN(dxutDialogResourceManager.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
V_RETURN(gui.OnD3D11ResizedSwapChain(pd3dDevice,pBackBufferSurfaceDesc));
V_RETURN(renderer.OnD3D11ResizedSwapChain(pd3dDevice,pBackBufferSurfaceDesc));
return S_OK;
}
//--------------------------------------------------------------------------------------
// Handle updates to the scene. This is called regardless of which D3D API is used
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
renderer.OnFrameMove( fTime, fElapsedTime, pUserContext );
}
//--------------------------------------------------------------------------------------
// Render the scene using the D3D11 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext,
double fTime, float fElapsedTime, void* pUserContext )
{
//Draw settings dialog above all else
if (gui.IsSettingsDialogueActive()) {
gui.RenderSettingsDialogue(fElapsedTime);
}
else {
//Draw what the renderer wants to draw
renderer.OnD3D11FrameRender(pd3dDevice,pd3dImmediateContext,fTime,fElapsedTime,pUserContext);
//Draw the overlays
if (focusedUI == FOCUSED_GUI) {
gui.OnD3D11FrameRender(fElapsedTime);
}
else if (focusedUI == FOCUSED_CONSOLE) {
devConsole.OnD3D11FrameRender();
}
}
}
//--------------------------------------------------------------------------------------
// Release D3D11 resources created in OnD3D11ResizedSwapChain
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext )
{
devConsole.log(L"OnD3D11ReleasingSwapChain");
dxutDialogResourceManager.OnD3D11ReleasingSwapChain();
}
//--------------------------------------------------------------------------------------
// Release D3D11 resources created in OnD3D11CreateDevice
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
{
devConsole.log(L"OnD3D11DestroyDevice");
dxutDialogResourceManager.OnD3D11DestroyDevice();
gui.OnD3D11DestroyDevice();
debugText.OnD3D11DestroyDevice();
renderer.OnD3D11DestroyDevice();
}
//--------------------------------------------------------------------------------------
// Handle messages to the application
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool* pbNoFurtherProcessing, void* pUserContext )
{
//std::wstringstream wss;
//wss << L"uMsg:" << uMsg << L" wParam:" << wParam << L" lParam:" << lParam;
//devConsole.log(&wss);
//Settings dialog takes message priority
if (gui.IsSettingsDialogueActive()) {
gui.SettingsDialogueMsgProc(hWnd,uMsg,wParam,lParam,pbNoFurtherProcessing,pUserContext);
return 0;
}
//If tilde gets pressed, toggle between console/gui display modes
if (uMsg == WM_CHAR && wParam == 96) {
if (focusedUI == FOCUSED_RENDERER) {
focusedUI = FOCUSED_GUI;
}
else if (focusedUI == FOCUSED_GUI) {
focusedUI = FOCUSED_CONSOLE;
}
else if (focusedUI == FOCUSED_CONSOLE) {
focusedUI = FOCUSED_RENDERER;
}
*pbNoFurtherProcessing = true;
}
if (*pbNoFurtherProcessing) {
return 0;
}
if (focusedUI == FOCUSED_GUI) {
gui.MsgProc(hWnd,uMsg,wParam,lParam,pbNoFurtherProcessing,pUserContext);
}
else if (focusedUI == FOCUSED_CONSOLE) {
devConsole.MsgProc(hWnd,uMsg,wParam,lParam,pbNoFurtherProcessing,pUserContext);
}
if (*pbNoFurtherProcessing) {
return 0;
}
if (focusedUI!= FOCUSED_CONSOLE) {
if (uMsg == WM_KEYDOWN) {
if (wParam == 'J') {
devConsole.processConsoleInput(L"cue = cue - 1;");
devConsole.processConsoleInput(L"run(\"cues.lua\")");
*pbNoFurtherProcessing = true;
}
else if (wParam == 'K') {
devConsole.processConsoleInput(L"cue = cue + 1;");
devConsole.processConsoleInput(L"run(\"cues.lua\")");
*pbNoFurtherProcessing = true;
}
else if (wParam == 'L') {
devConsole.processConsoleInput(L"step()");
*pbNoFurtherProcessing = true;
}
}
if (*pbNoFurtherProcessing) {
return 0;
}
renderer.MsgProc(hWnd, uMsg, wParam, lParam, pbNoFurtherProcessing, pUserContext);
}
return 0;
}
//--------------------------------------------------------------------------------------
// Handle key presses. For raw key input, this is always called before MsgProc
//--------------------------------------------------------------------------------------
void CALLBACK OnKeyboard( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Handle mouse button presses
//--------------------------------------------------------------------------------------
void CALLBACK OnMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown,
bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta,
int xPos, int yPos, void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Call if device was removed. Return true to find a new device, false to quit
//--------------------------------------------------------------------------------------
bool CALLBACK OnDeviceRemoved( void* pUserContext )
{
devConsole.log(L"OnDeviceRemoved");
return true;
}
//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
// Set general DXUT callbacks
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCallbackKeyboard( OnKeyboard );
DXUTSetCallbackMouse( OnMouse );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
DXUTSetCallbackDeviceRemoved( OnDeviceRemoved );
// Set the D3D11 DXUT callbacks. Remove these sets if the app doesn't need to support D3D11
DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
// Perform any application-level initialization here
gui.init(&dxutDialogResourceManager);
renderer.init();
DXUTInit( true, true, NULL ); // Parse the command line, show msgboxes on error, no extra command line params
DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
DXUTCreateWindow( L"Renderer" );
DXUTCreateDevice( D3D_FEATURE_LEVEL_11_0, true, 640, 480 );
//Must be initialised here as static initialisation causes an assertion failure
RendererMessageProcessor rMP = RendererMessageProcessor(&devConsole,&renderer);
rMProc = &rMP;
devConsole.setMessageProcessor(rMProc);
DXUTMainLoop(); // Enter into the DXUT render loop
// Perform any application-level cleanup here
renderer.OnExit();
return DXUTGetExitCode();
}