forked from john-h-k/UWPPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
186 lines (158 loc) · 5.89 KB
/
App.cs
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
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using TerraFX.Interop;
using static TerraFX.Interop.Windows;
using static TerraFX.Interop.DXGIDebug;
using static TerraFX.Interop.DXGI;
using UWPPlayground.Common;
namespace UWPPlayground
{
/// <inheritdoc />
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
internal sealed unsafe class App : IFrameworkView
{
private static class ReturnCodes
{
public const int Success = 0;
}
private static int Main()
{
var direct3DApplicationSource = new Direct3DApplicationSource();
CoreApplication.Run(direct3DApplicationSource);
return ReturnCodes.Success;
}
private UWPDirectXMain _main;
private bool _windowClosed = false;
private bool _windowVisible = true;
private DeviceResources _deviceResources;
public void Initialize(CoreApplicationView applicationView)
{
applicationView.Activated += OnActivated;
CoreApplication.Suspending += OnSuspending;
CoreApplication.Resuming += OnResuming;
}
public void SetWindow(CoreWindow window)
{
window.SizeChanged += OnWindowSizeChanged;
window.VisibilityChanged += OnVisibilityChanged;
window.Closed += OnWindowClosed;
DisplayInformation currentDisplayInformation = DisplayInformation.GetForCurrentView();
currentDisplayInformation.DpiChanged += OnDpiChanged;
currentDisplayInformation.OrientationChanged += OnOrientationChanged;
DisplayInformation.DisplayContentsInvalidated += OnDisplayContentsInvalidated;
}
public void Load(string entryPoint)
{
if (_main is null)
_main = new UWPDirectXMain();
}
public void Run()
{
while (!_windowClosed)
{
if (_windowVisible)
{
CoreWindow.GetForCurrentThread().Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessAllIfPresent);
ID3D12CommandQueue* commandQueue = GetDeviceResources().CommandQueue;
_main.Update();
if (_main.Render())
{
GetDeviceResources().Present();
}
}
else
{
CoreWindow.GetForCurrentThread().Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessOneAndAllPending);
}
}
}
public void Uninitialize()
{
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();
_main.OnSuspending();
deferral.Complete();
}
private void OnResuming(object sender, object e)
{
_main.OnResuming();
}
private void OnActivated(CoreApplicationView sender, IActivatedEventArgs args)
{
CoreWindow.GetForCurrentThread().Activate();
}
private void OnDisplayContentsInvalidated(DisplayInformation sender, object args)
{
GetDeviceResources().ValidateDevice();
}
private void OnOrientationChanged(DisplayInformation sender, object args)
{
GetDeviceResources().SetCurrentOrientation(sender.CurrentOrientation);
}
private void OnDpiChanged(DisplayInformation sender, object args)
{
GetDeviceResources().SetDpi(sender.LogicalDpi);
_main.OnWindowSizeChanged();
}
private void OnWindowClosed(CoreWindow sender, CoreWindowEventArgs args)
{
_windowClosed = true;
}
private void OnVisibilityChanged(CoreWindow sender, VisibilityChangedEventArgs args)
{
_windowVisible = args.Visible;
}
private void OnWindowSizeChanged(CoreWindow sender, WindowSizeChangedEventArgs args)
{
GetDeviceResources().SetLogicalSize(new Size(sender.Bounds.Width, sender.Bounds.Height));
_main.OnWindowSizeChanged();
}
private DeviceResources GetDeviceResources()
{
if (_deviceResources != null && _deviceResources.IsDeviceRemoved)
{
// All references to the existing D3D device must be released before a new device
// can be created.
_deviceResources = null;
_main.OnDeviceRemoved();
#if DEBUG
ComPtr<IDXGIDebug1> dxgiDebug = default;
Guid iid = IID_IDXGIDebug1;
if (SUCCEEDED(DXGIGetDebugInterface1(0, &iid, (void**)dxgiDebug.GetAddressOf())))
{
dxgiDebug.Ptr->ReportLiveObjects(new Guid(0xe48ae283, 0xda80, 0x490b, 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x8),
DXGI_DEBUG_RLO_FLAGS.DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_FLAGS.DXGI_DEBUG_RLO_IGNORE_INTERNAL);
}
#endif
}
if (_deviceResources == null)
{
_deviceResources = new DeviceResources();
_deviceResources.SetWindow(CoreWindow.GetForCurrentThread());
_main.CreateRenderers(_deviceResources);
}
return _deviceResources;
}
}
internal class Direct3DApplicationSource : IFrameworkViewSource
{
public IFrameworkView CreateView()
{
return new App();
}
}
}