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 226
/
Copy pathMain.cpp
227 lines (192 loc) · 7.14 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
//--------------------------------------------------------------------------------------
// Main.cpp
//
// Entry point for Xbox One exclusive title.
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimplePLM.h"
#include <ppltasks.h>
#include "Telemetry.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;
using namespace DirectX;
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::ResourceAvailabilityChanged +=
ref new EventHandler<Platform::Object^>(this, &ViewProvider::OnResourceAvailabilityChanged);
m_sample = std::make_unique<Sample>();
m_sample->ShowInstructions();
m_sample->LogPLMEvent(L"Initialize()");
// Sample Usage Telemetry
//
// Disable or remove this code block to opt-out of sample usage telemetry
//
if (EventRegisterATGSampleTelemetry() == ERROR_SUCCESS)
{
wchar_t exePath[MAX_PATH + 1] = {};
if (!GetModuleFileNameW(nullptr, exePath, MAX_PATH))
{
wcscpy_s(exePath, L"Unknown");
}
EventWriteSampleLoaded(exePath);
}
}
virtual void Uninitialize()
{
m_sample.reset();
}
virtual void SetWindow(CoreWindow^ window)
{
window->Closed +=
ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &ViewProvider::OnWindowClosed);
window->VisibilityChanged +=
ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &ViewProvider::OnVisibilityChanged);
window->Activated +=
ref new TypedEventHandler<CoreWindow^, WindowActivatedEventArgs^>(this, &ViewProvider::OnWindowActivated);
// Default window thread to CPU 0
SetThreadAffinityMask(GetCurrentThread(), 0x1);
m_sample->Initialize(reinterpret_cast<IUnknown*>(window));
m_sample->LogPLMEvent(L"SetWindow()");
}
virtual void Load(Platform::String^ entryPoint)
{
m_sample->LogPLMEvent(L"Load()", entryPoint->Data());
}
virtual void Run()
{
m_sample->LogPLMEvent(L"Run()");
while (!m_exit)
{
m_sample->Tick();
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
}
protected:
// Event handlers
void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
if (args->Kind == ActivationKind::Launch)
{
auto launchArgs = static_cast<LaunchActivatedEventArgs^>(args);
Platform::String^ argsData = args->Kind.ToString() + " : " + launchArgs->Arguments;
m_sample->LogPLMEvent(L"OnActivated()", argsData->Data());
}
CoreWindow::GetForCurrentThread()->Activate();
}
void OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
{
if (m_sample->GetUseDeferral())
{
//GetDeferral must be called on the Core Window thread. The deferral can then be passed to other threads.
auto deferral = args->SuspendingOperation->GetDeferral();
//The deferral can be used to complete suspending on other threads after the OnSuspending handler has returned.
concurrency::create_task([this, deferral]()
{
m_sample->LogPLMEvent(L"OnSuspending()", L"using a deferral");
m_sample->OnSuspending();
deferral->Complete();
});
}
else
{
//Without a deferral, the application will complete suspending as soon as it returns from the OnSuspending handler.
m_sample->LogPLMEvent(L"OnSuspending()", L"not using a deferral");
m_sample->OnSuspending();
}
}
void OnResuming(Platform::Object^ sender, Platform::Object^ args)
{
m_sample->LogPLMEvent(L"OnResuming()");
m_sample->OnResuming();
}
void OnResourceAvailabilityChanged(Platform::Object^ sender, Platform::Object^ args)
{
switch (CoreApplication::ResourceAvailability)
{
case ResourceAvailability::FullWithExtendedSystemReserve :
m_sample->LogPLMEvent(L"OnResourceAvailabilityChanged()", L"FullWithExtendedSystemReserve"); break;
case ResourceAvailability::Full:
m_sample->LogPLMEvent(L"OnResourceAvailabilityChanged()", L"Full"); break;
case ResourceAvailability::Constrained:
m_sample->LogPLMEvent(L"OnResourceAvailabilityChanged()", L"Constrained"); break;
default:
break;
}
}
void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
{
if (args->Visible)
{
m_sample->LogPLMEvent(L"OnVisibilityChanged()", L"Visible");
}
else
{
m_sample->LogPLMEvent(L"OnVisibilityChanged()", L"Not Visible");
}
}
void OnWindowActivated(CoreWindow^ sender, WindowActivatedEventArgs^ args)
{
switch (args->WindowActivationState)
{
case CoreWindowActivationState::CodeActivated :
m_sample->LogPLMEvent(L"OnWindowActivated()", L"CodeActivated"); break;
case CoreWindowActivationState::PointerActivated :
m_sample->LogPLMEvent(L"OnWindowActivated()", L"PointerActivated"); break;
case CoreWindowActivationState::Deactivated :
m_sample->LogPLMEvent(L"OnWindowActivated()", L"Deactivated"); break;
default:
break;
}
}
void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
{
m_exit = true;
}
private:
bool m_exit;
std::unique_ptr<Sample> m_sample;
};
ref class ViewProviderFactory : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
{
return ref new ViewProvider();
}
};
// Entry point
[Platform::MTAThread]
int __cdecl main(Platform::Array<Platform::String^>^ /*argv*/)
{
// Default main thread to CPU 0
SetThreadAffinityMask(GetCurrentThread(), 0x1);
auto viewProviderFactory = ref new ViewProviderFactory();
CoreApplication::Run(viewProviderFactory);
return 0;
}
// Exit helper
void ExitSample() noexcept
{
Windows::ApplicationModel::Core::CoreApplication::Exit();
}