-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.cpp
616 lines (508 loc) · 15.8 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//--------------------------------------------------------------------------------------
// File: Main.cpp
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "Game.h"
#ifdef _GAMING_XBOX
#include <appnotify.h>
#include <XDisplay.h>
#include <XGameRuntimeInit.h>
namespace
{
HANDLE g_plmSuspendComplete = nullptr;
HANDLE g_plmSignalResume = nullptr;
}
bool g_HDRMode = false;
void SetDisplayMode() noexcept;
#elif defined(_XBOX_ONE) && defined(_TITLE)
#include <ppltasks.h>
using namespace concurrency;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
bool g_HDRMode = false;
#else
#include <commdlg.h>
#endif
using namespace DirectX;
#if defined(_XBOX_ONE) && defined(_TITLE)
ref class ViewProvider sealed : public IFrameworkView
{
public:
ViewProvider() :
m_exit(false)
{
}
// IFrameworkView methods
virtual void Initialize(CoreApplicationView^ applicationView)
{
applicationView->Activated +=
ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &ViewProvider::OnActivated);
CoreApplication::Suspending +=
ref new EventHandler<SuspendingEventArgs^>(this, &ViewProvider::OnSuspending);
CoreApplication::Resuming +=
ref new EventHandler<Platform::Object^>(this, &ViewProvider::OnResuming);
CoreApplication::DisableKinectGpuReservation = true;
m_game = std::make_unique<Game>();
if (m_game->RequestHDRMode())
{
// Request HDR mode.
auto determineHDR = Concurrency::create_task(
Windows::Xbox::Graphics::Display::DisplayConfiguration::TrySetHdrModeAsync()
);
// In a real game, you'd do some initialization here to hide the HDR mode switch.
// Finish up HDR mode detection (waiting for async if needed)
g_HDRMode = determineHDR.get()->HdrEnabled;
#ifdef _DEBUG
OutputDebugStringA((g_HDRMode) ? "INFO: Display in HDR Mode\n" : "INFO: Display in SDR Mode\n");
#endif
}
}
virtual void Uninitialize()
{
m_game.reset();
}
virtual void SetWindow(CoreWindow^ window)
{
window->Closed +=
ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &ViewProvider::OnWindowClosed);
m_game->Initialize(reinterpret_cast<IUnknown*>(reinterpret_cast<IUnknown*>(window)));
}
virtual void Load(Platform::String^ entryPoint)
{
}
virtual void Run()
{
while (!m_exit)
{
m_game->Tick();
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
}
protected:
// Event handlers
void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
CoreWindow::GetForCurrentThread()->Activate();
}
void OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
{
auto deferral = args->SuspendingOperation->GetDeferral();
create_task([this, deferral]()
{
m_game->OnSuspending();
deferral->Complete();
});
}
void OnResuming(Platform::Object^ sender, Platform::Object^ args)
{
m_game->OnResuming();
}
void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
{
m_exit = true;
}
private:
bool m_exit;
std::unique_ptr<Game> m_game;
};
ref class ViewProviderFactory : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
{
return ref new ViewProvider();
}
};
// Entry point
[Platform::MTAThread]
int __cdecl main(Platform::Array<Platform::String^>^ /*argv*/)
{
auto viewProviderFactory = ref new ViewProviderFactory();
CoreApplication::Run(viewProviderFactory);
return 0;
}
// Exit helper
void ExitGame() noexcept
{
Windows::ApplicationModel::Core::CoreApplication::Exit();
}
#else
namespace
{
std::unique_ptr<Game> g_game;
}
LPCWSTR g_szAppName = L"DirectXTKModelViewer (DirectX 12)";
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void ExitGame() noexcept;
#ifdef PC
// Indicates to hybrid graphics systems to prefer the discrete part by default
extern "C"
{
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
// Entry point
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
if (!XMVerifyCPUSupport())
return 1;
HRESULT hr = CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);
if (FAILED(hr))
return 1;
#ifdef _GAMING_XBOX
if (FAILED(XGameRuntimeInitialize()))
return 1;
PAPPSTATE_REGISTRATION hPLM = {};
PAPPCONSTRAIN_REGISTRATION hPLM2 = {};
if (wcsstr(lpCmdLine, L"-render4k") != nullptr)
{
Game::SetRender4K();
}
#endif
g_game = std::make_unique<Game>();
// Register class and create window
{
// Register class
WNDCLASSEXW wcex = {};
wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcex.lpszClassName = L"DirectXTKModelViewerWindowClass";
#ifndef _GAMING_XBOX
wcex.hIcon = LoadIconW(hInstance, L"IDI_ICON");
wcex.hIconSm = LoadIconW(wcex.hInstance, L"IDI_ICON");
#endif
if (!RegisterClassExW(&wcex))
return 1;
// Create window
#ifdef _GAMING_XBOX
RECT rc = { 0, 0, 1920, 1080 };
#else
int w, h;
g_game->GetDefaultSize(w, h);
RECT rc = { 0, 0, static_cast<LONG>(w), static_cast<LONG>(h) };
#endif
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowExW(0, L"DirectXTKModelViewerWindowClass", g_szAppName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top,
nullptr, nullptr, hInstance,
g_game.get());
if (!hwnd)
return 1;
ShowWindow(hwnd, nCmdShow);
#ifdef _GAMING_XBOX
SetDisplayMode();
#endif
GetClientRect(hwnd, &rc);
#ifdef _GAMING_XBOX
g_game->Initialize(hwnd);
#else
g_game->Initialize(hwnd, rc.right - rc.left, rc.bottom - rc.top);
#endif
#ifdef _GAMING_XBOX
g_plmSuspendComplete = CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
g_plmSignalResume = CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
if (!g_plmSuspendComplete || !g_plmSignalResume)
return 1;
if (RegisterAppStateChangeNotification([](BOOLEAN quiesced, PVOID context)
{
if (quiesced)
{
ResetEvent(g_plmSuspendComplete);
ResetEvent(g_plmSignalResume);
// To ensure we use the main UI thread to process the notification, we self-post a message
PostMessage(reinterpret_cast<HWND>(context), WM_USER, 0, 0);
// To defer suspend, you must wait to exit this callback
std::ignore = WaitForSingleObject(g_plmSuspendComplete, INFINITE);
}
else
{
SetEvent(g_plmSignalResume);
}
}, hwnd, &hPLM))
return 1;
if (RegisterAppConstrainedChangeNotification([](BOOLEAN constrained, PVOID context)
{
// To ensure we use the main UI thread to process the notification, we self-post a message
SendMessage(reinterpret_cast<HWND>(context), WM_USER + 1, (constrained) ? 1u : 0u, 0);
}, hwnd, &hPLM2))
return 1;
#endif
}
// Main message loop
MSG msg = {};
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_game->Tick();
}
}
g_game.reset();
#ifdef _GAMING_XBOX
UnregisterAppStateChangeNotification(hPLM);
UnregisterAppConstrainedChangeNotification(hPLM2);
CloseHandle(g_plmSuspendComplete);
CloseHandle(g_plmSignalResume);
XGameRuntimeUninitialize();
#endif
CoUninitialize();
return static_cast<int>(msg.wParam);
}
// Windows procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool s_in_sizemove = false;
static bool s_in_suspend = false;
static bool s_minimized = false;
static bool s_fullscreen = false;
auto game = reinterpret_cast<Game*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
switch (message)
{
case WM_CREATE:
if (lParam)
{
auto params = reinterpret_cast<LPCREATESTRUCTW>(lParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(params->lpCreateParams));
}
break;
#ifdef _GAMING_XBOX
case WM_USER:
if (game)
{
game->OnSuspending();
// Complete deferral
SetEvent(g_plmSuspendComplete);
std::ignore = WaitForSingleObject(g_plmSignalResume, INFINITE);
SetDisplayMode();
game->OnResuming();
}
break;
case WM_USER + 1:
if (game && !wParam)
{
SetDisplayMode();
}
break;
#else
case WM_PAINT:
if (s_in_sizemove && game)
{
game->Tick();
}
else
{
PAINTSTRUCT ps;
std::ignore = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DISPLAYCHANGE:
if (game)
{
game->OnDisplayChange();
}
break;
case WM_MOVE:
if (game)
{
game->OnWindowMoved();
}
break;
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
{
if (!s_minimized)
{
s_minimized = true;
if (!s_in_suspend && game)
game->OnSuspending();
s_in_suspend = true;
}
}
else if (s_minimized)
{
s_minimized = false;
if (s_in_suspend && game)
game->OnResuming();
s_in_suspend = false;
}
else if (!s_in_sizemove && game)
{
game->OnWindowSizeChanged(LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_ENTERSIZEMOVE:
s_in_sizemove = true;
break;
case WM_EXITSIZEMOVE:
s_in_sizemove = false;
if (game)
{
RECT rc;
GetClientRect(hWnd, &rc);
game->OnWindowSizeChanged(rc.right - rc.left, rc.bottom - rc.top);
}
break;
case WM_GETMINMAXINFO:
if (lParam)
{
auto info = reinterpret_cast<MINMAXINFO*>(lParam);
info->ptMinTrackSize.x = 320;
info->ptMinTrackSize.y = 200;
}
break;
case WM_POWERBROADCAST:
switch (wParam)
{
case PBT_APMQUERYSUSPEND:
if (!s_in_suspend && game)
game->OnSuspending();
s_in_suspend = true;
return TRUE;
case PBT_APMRESUMESUSPEND:
if (!s_minimized)
{
if (s_in_suspend && game)
game->OnResuming();
s_in_suspend = false;
}
return TRUE;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSKEYDOWN:
Keyboard::ProcessMessage(message, wParam, lParam);
if (wParam == VK_RETURN && (lParam & 0x60000000) == 0x20000000)
{
// Implements the classic ALT+ENTER fullscreen toggle
if (s_fullscreen)
{
SetWindowLongPtr(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, 0);
int width = 800;
int height = 600;
if (game)
game->GetDefaultSize(width, height);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetWindowPos(hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
else
{
SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
}
s_fullscreen = !s_fullscreen;
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
Keyboard::ProcessMessage(message, wParam, lParam);
break;
case WM_INPUT:
case WM_MOUSEHOVER:
Mouse::ProcessMessage(message, wParam, lParam);
break;
case WM_MENUCHAR:
// A menu is active and the user presses a key that does not correspond
// to any mnemonic or accelerator key. Ignore so we don't produce an error beep.
return MAKELRESULT(0, MNC_CLOSE);
case WM_USER:
if (game)
{
static DWORD s_filterIndex = 1;
WCHAR szFile[MAX_PATH] = {};
OPENFILENAME ofn = {};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = 0;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"DirectX SDK Mesh (SDKMESH)\0*.sdkmesh\0Visual Studio Mesh (CMO)\0*.cmo\0Vertex Buffer Object (VBO)\0*.vbo\0All Files\0*.*\0";
ofn.nFilterIndex = s_filterIndex;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
s_filterIndex = ofn.nFilterIndex;
game->OnFileOpen(szFile);
}
}
break;
#endif
case WM_ACTIVATEAPP:
#ifndef _GAMING_XBOX
Keyboard::ProcessMessage(message, wParam, lParam);
#endif
Mouse::ProcessMessage(message, wParam, lParam);
if (game)
{
if (wParam)
{
game->OnActivated();
}
else
{
game->OnDeactivated();
}
}
break;
case WM_ACTIVATE:
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MOUSEWHEEL:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
Mouse::ProcessMessage(message, wParam, lParam);
break;
case WM_MOUSEACTIVATE:
// When you click activate the window, we want Mouse to ignore that event.
return MA_ACTIVATEANDEAT;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
// Exit helper
void ExitGame() noexcept
{
PostQuitMessage(0);
}
#endif
#ifdef _GAMING_XBOX
void SetDisplayMode() noexcept
{
if (g_game && g_game->RequestHDRMode())
{
// Request HDR mode.
auto result = XDisplayTryEnableHdrMode(XDisplayHdrModePreference::PreferHdr, nullptr);
g_HDRMode = (result == XDisplayHdrModeResult::Enabled);
#ifdef _DEBUG
OutputDebugStringA((g_HDRMode) ? "INFO: Display in HDR Mode\n" : "INFO: Display in SDR Mode\n");
#endif
}
}
#endif