This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
Main.cpp
410 lines (319 loc) · 12.4 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
//--------------------------------------------------------------------------------------
// Main.cpp
//
// Entry point for Universal Windows Platform (UWP) app.
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleTriangleUWP.h"
#include "Telemetry.h"
using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::ApplicationModel::Core;
using namespace winrt::Windows::ApplicationModel::Activation;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::UI::Input;
using namespace winrt::Windows::UI::ViewManagement;
using namespace winrt::Windows::System;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Graphics::Display;
using namespace DirectX;
class ViewProvider : public winrt::implements<ViewProvider, IFrameworkView>
{
public:
ViewProvider() :
m_exit(false),
m_visible(true),
m_in_sizemove(false),
m_DPI(96.f),
m_logicalWidth(800.f),
m_logicalHeight(600.f),
m_nativeOrientation(DisplayOrientations::None),
m_currentOrientation(DisplayOrientations::None)
{
}
// IFrameworkView methods
void Initialize(CoreApplicationView const & applicationView)
{
applicationView.Activated({ this, &ViewProvider::OnActivated });
CoreApplication::Suspending({ this, &ViewProvider::OnSuspending });
CoreApplication::Resuming({ this, &ViewProvider::OnResuming });
m_sample = std::make_unique<Sample>();
// Sample Usage Telemetry
//
// Disable or remove this code block to opt-out of sample usage telemetry
//
if (ATG::EventRegisterATGSampleTelemetry() == ERROR_SUCCESS)
{
wchar_t exeName[MAX_PATH + 1] = {};
if (!GetModuleFileNameW(nullptr, exeName, MAX_PATH))
{
wcscpy_s(exeName, L"Unknown");
}
wchar_t fname[_MAX_FNAME] = {};
wchar_t ext[_MAX_EXT] = {};
(void)_wsplitpath_s(exeName, nullptr, 0, nullptr, 0, fname, _MAX_FNAME, ext, _MAX_EXT);
(void)_wmakepath_s(exeName, nullptr, nullptr, fname, ext); // keep only the filename + extension
ATG::EventWriteSampleLoaded(exeName);
}
}
void Uninitialize()
{
m_sample.reset();
}
void SetWindow(CoreWindow const & window)
{
window.SizeChanged({ this, &ViewProvider::OnWindowSizeChanged });
#if defined(NTDDI_WIN10_RS2) && (NTDDI_VERSION >= NTDDI_WIN10_RS2)
try
{
window.ResizeStarted([this](auto&&, auto&&) { m_in_sizemove = true; });
window.ResizeCompleted([this](auto&&, auto&&) { m_in_sizemove = false; HandleWindowSizeChanged(); });
}
catch (...)
{
// Requires Windows 10 Creators Update (10.0.15063) or later
}
#endif
window.VisibilityChanged({ this, &ViewProvider::OnVisibilityChanged });
window.Closed([this](auto&&, auto&&) { m_exit = true; });
auto dispatcher = CoreWindow::GetForCurrentThread().Dispatcher();
dispatcher.AcceleratorKeyActivated({ this, &ViewProvider::OnAcceleratorKeyActivated });
auto navigation = SystemNavigationManager::GetForCurrentView();
// UWP on Xbox One triggers a back request whenever the B button is pressed
// which can result in the app being suspended if unhandled
navigation.BackRequested([](const winrt::Windows::Foundation::IInspectable&, const BackRequestedEventArgs& args)
{
args.Handled(true);
});
auto currentDisplayInformation = DisplayInformation::GetForCurrentView();
currentDisplayInformation.DpiChanged({ this, &ViewProvider::OnDpiChanged });
currentDisplayInformation.OrientationChanged({ this, &ViewProvider::OnOrientationChanged });
DisplayInformation::DisplayContentsInvalidated({ this, &ViewProvider::OnDisplayContentsInvalidated });
m_DPI = currentDisplayInformation.LogicalDpi();
m_logicalWidth = window.Bounds().Width;
m_logicalHeight = window.Bounds().Height;
m_nativeOrientation = currentDisplayInformation.NativeOrientation();
m_currentOrientation = currentDisplayInformation.CurrentOrientation();
int outputWidth = ConvertDipsToPixels(m_logicalWidth);
int outputHeight = ConvertDipsToPixels(m_logicalHeight);
DXGI_MODE_ROTATION rotation = ComputeDisplayRotation();
if (rotation == DXGI_MODE_ROTATION_ROTATE90 || rotation == DXGI_MODE_ROTATION_ROTATE270)
{
std::swap(outputWidth, outputHeight);
}
auto windowPtr = static_cast<::IUnknown*>(winrt::get_abi(window));
m_sample->Initialize(windowPtr, outputWidth, outputHeight, rotation);
Mouse::SetDpi(m_DPI);
}
void Load(winrt::hstring const &)
{
}
void Run()
{
while (!m_exit)
{
if (m_visible)
{
m_sample->Tick();
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
else
{
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
}
}
protected:
// Event handlers
void OnActivated(CoreApplicationView const & /*applicationView*/, IActivatedEventArgs const & args)
{
if (args.Kind() == ActivationKind::Launch)
{
auto launchArgs = (const LaunchActivatedEventArgs*)(&args);
if (launchArgs->PrelaunchActivated())
{
// Opt-out of Prelaunch
CoreApplication::Exit();
return;
}
}
int w, h;
m_sample->GetDefaultSize(w, h);
m_DPI = DisplayInformation::GetForCurrentView().LogicalDpi();
ApplicationView::PreferredLaunchWindowingMode(ApplicationViewWindowingMode::PreferredLaunchViewSize);
// Change to ApplicationViewWindowingMode::FullScreen to default to full screen
auto desiredSize = Size(ConvertPixelsToDips(w), ConvertPixelsToDips(h));
ApplicationView::PreferredLaunchViewSize(desiredSize);
auto view = ApplicationView::GetForCurrentView();
auto minSize = Size(ConvertPixelsToDips(320), ConvertPixelsToDips(200));
view.SetPreferredMinSize(minSize);
CoreWindow::GetForCurrentThread().Activate();
view.FullScreenSystemOverlayMode(FullScreenSystemOverlayMode::Minimal);
view.TryResizeView(desiredSize);
}
void OnSuspending(IInspectable const & /*sender*/, SuspendingEventArgs const & args)
{
auto deferral = args.SuspendingOperation().GetDeferral();
auto f = std::async(std::launch::async, [this, deferral]()
{
m_sample->OnSuspending();
deferral.Complete();
});
}
void OnResuming(IInspectable const & /*sender*/, IInspectable const & /*args*/)
{
m_sample->OnResuming();
}
void OnWindowSizeChanged(CoreWindow const & sender, WindowSizeChangedEventArgs const & /*args*/)
{
m_logicalWidth = sender.Bounds().Width;
m_logicalHeight = sender.Bounds().Height;
if (m_in_sizemove)
return;
HandleWindowSizeChanged();
}
void OnVisibilityChanged(CoreWindow const & /*sender*/, VisibilityChangedEventArgs const & args)
{
m_visible = args.Visible();
if (m_visible)
m_sample->OnActivated();
else
m_sample->OnDeactivated();
}
void OnAcceleratorKeyActivated(CoreDispatcher const &, AcceleratorKeyEventArgs const & args)
{
if (args.EventType() == CoreAcceleratorKeyEventType::SystemKeyDown
&& args.VirtualKey() == VirtualKey::Enter
&& args.KeyStatus().IsMenuKeyDown
&& !args.KeyStatus().WasKeyDown)
{
// Implements the classic ALT+ENTER fullscreen toggle
auto view = ApplicationView::GetForCurrentView();
if (view.IsFullScreenMode())
view.ExitFullScreenMode();
else
view.TryEnterFullScreenMode();
args.Handled(true);
}
}
void OnDpiChanged(DisplayInformation const & sender, IInspectable const & /*args*/)
{
m_DPI = sender.LogicalDpi();
HandleWindowSizeChanged();
Mouse::SetDpi(m_DPI);
}
void OnOrientationChanged(DisplayInformation const & sender, IInspectable const & /*args*/)
{
auto resizeManager = CoreWindowResizeManager::GetForCurrentView();
resizeManager.ShouldWaitForLayoutCompletion(true);
m_currentOrientation = sender.CurrentOrientation();
HandleWindowSizeChanged();
resizeManager.NotifyLayoutCompleted();
}
void OnDisplayContentsInvalidated(DisplayInformation const & /*sender*/, IInspectable const & /*args*/)
{
m_sample->ValidateDevice();
}
private:
bool m_exit;
bool m_visible;
bool m_in_sizemove;
float m_DPI;
float m_logicalWidth;
float m_logicalHeight;
std::unique_ptr<Sample> m_sample;
winrt::Windows::Graphics::Display::DisplayOrientations m_nativeOrientation;
winrt::Windows::Graphics::Display::DisplayOrientations m_currentOrientation;
inline int ConvertDipsToPixels(float dips) const
{
return int(dips * m_DPI / 96.f + 0.5f);
}
inline float ConvertPixelsToDips(int pixels) const
{
return (float(pixels) * 96.f / m_DPI);
}
DXGI_MODE_ROTATION ComputeDisplayRotation() const
{
DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
switch (m_nativeOrientation)
{
case DisplayOrientations::Landscape:
switch (m_currentOrientation)
{
case DisplayOrientations::Landscape:
rotation = DXGI_MODE_ROTATION_IDENTITY;
break;
case DisplayOrientations::Portrait:
rotation = DXGI_MODE_ROTATION_ROTATE270;
break;
case DisplayOrientations::LandscapeFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE180;
break;
case DisplayOrientations::PortraitFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE90;
break;
}
break;
case DisplayOrientations::Portrait:
switch (m_currentOrientation)
{
case DisplayOrientations::Landscape:
rotation = DXGI_MODE_ROTATION_ROTATE90;
break;
case DisplayOrientations::Portrait:
rotation = DXGI_MODE_ROTATION_IDENTITY;
break;
case DisplayOrientations::LandscapeFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE270;
break;
case DisplayOrientations::PortraitFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE180;
break;
}
break;
}
return rotation;
}
void HandleWindowSizeChanged()
{
int outputWidth = ConvertDipsToPixels(m_logicalWidth);
int outputHeight = ConvertDipsToPixels(m_logicalHeight);
DXGI_MODE_ROTATION rotation = ComputeDisplayRotation();
if (rotation == DXGI_MODE_ROTATION_ROTATE90 || rotation == DXGI_MODE_ROTATION_ROTATE270)
{
std::swap(outputWidth, outputHeight);
}
m_sample->OnWindowSizeChanged(outputWidth, outputHeight, rotation);
}
};
class ViewProviderFactory : public winrt::implements<ViewProviderFactory, IFrameworkViewSource>
{
public:
IFrameworkView CreateView()
{
return winrt::make<ViewProvider>();
}
};
// Entry point
int WINAPI wWinMain(
_In_ HINSTANCE /*hInstance*/,
_In_ HINSTANCE /*hPrevInstance*/,
_In_ LPWSTR /*lpCmdLine*/,
_In_ int /*nCmdShow*/
)
{
if (!XMVerifyCPUSupport())
{
throw std::exception("XMVerifyCPUSupport");
}
auto viewProviderFactory = winrt::make<ViewProviderFactory>();
CoreApplication::Run(viewProviderFactory);
return 0;
}
// Exit helper
void ExitSample() noexcept
{
winrt::Windows::ApplicationModel::Core::CoreApplication::Exit();
}