forked from microsoft/Xbox-GDK-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleUserModel.cpp
More file actions
323 lines (263 loc) · 9.73 KB
/
SimpleUserModel.cpp
File metadata and controls
323 lines (263 loc) · 9.73 KB
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
//--------------------------------------------------------------------------------------
// SimpleUserModel.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleUserModel.h"
#include "ATGColors.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using namespace ATG::UITK;
using Microsoft::WRL::ComPtr;
Sample::Sample() noexcept(false) :
m_frame(0)
{
// Renders only 2D, so no need for a depth buffer.
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN);
XTaskQueueCreate(XTaskQueueDispatchMode::ThreadPool, XTaskQueueDispatchMode::Manual, &m_taskQueue);
// [Simple User Model]
// Register for any change events for users.
// Change events are triggered when:
// - A user's sign-in status changes
// - A user's Gamertag, GamerPicture, or Privileges changed
XUserRegisterForChangeEvent(
m_taskQueue,
this,
&UserChangeEventCallback,
&m_userChangeEventCallbackToken
);
}
Sample::~Sample()
{
XUserUnregisterForChangeEvent(m_userChangeEventCallbackToken, false);
XTaskQueueTerminate(m_taskQueue, false, nullptr, nullptr);
XTaskQueueDispatch(m_taskQueue, XTaskQueuePort::Completion, INFINITE);
XTaskQueueCloseHandle(m_taskQueue);
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(HWND window)
{
m_gamePad = std::make_unique<GamePad>();
m_deviceResources->SetWindow(window);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
InitializeUI();
SignInDefaultUser();
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Frame %llu", m_frame);
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
PIXEndEvent();
m_frame++;
}
// Updates the world.
void Sample::Update(DX::StepTimer const& timer)
{
PIXScopedEvent(PIX_COLOR_DEFAULT, L"Update");
float elapsedTime = float(timer.GetElapsedSeconds());
XTaskQueueDispatch(m_taskQueue, XTaskQueuePort::Completion, 0);
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
}
else
{
m_gamePadButtons.Reset();
}
m_inputState.Update(elapsedTime, *m_gamePad);
m_uiManager.Update(elapsedTime, m_inputState);
}
#pragma endregion
#pragma region Frame Render
// Draws the scene.
void Sample::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
// Prepare the command list to render a new frame.
m_deviceResources->Prepare();
Clear();
auto commandList = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Render");
m_uiManager.Render();
PIXEndEvent(commandList);
// Show the new frame.
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit(m_deviceResources->GetCommandQueue());
PIXEndEvent();
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto commandList = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto rtvDescriptor = m_deviceResources->GetRenderTargetView();
commandList->OMSetRenderTargets(1, &rtvDescriptor, FALSE, nullptr);
commandList->ClearRenderTargetView(rtvDescriptor, ATG::Colors::Background, 0, nullptr);
// Set the viewport and scissor rect.
auto viewport = m_deviceResources->GetScreenViewport();
auto scissorRect = m_deviceResources->GetScissorRect();
commandList->RSSetViewports(1, &viewport);
commandList->RSSetScissorRects(1, &scissorRect);
PIXEndEvent(commandList);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnSuspending()
{
m_deviceResources->Suspend();
}
void Sample::OnResuming()
{
m_deviceResources->Resume();
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
m_inputState.Reset();
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto device = m_deviceResources->GetD3DDevice();
m_graphicsMemory = std::make_unique<GraphicsMemory>(device);
auto styleRenderer = std::make_unique<UIStyleRendererD3D>(*this);
m_uiManager.GetStyleManager().InitializeStyleRenderer(std::move(styleRenderer));
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
auto size = m_deviceResources->GetOutputSize();
m_uiManager.SetWindowSize(size.right, size.bottom);
}
#pragma endregion
// [Simple User Model]
// This method signs in the default user that launched the application
void Sample::SignInDefaultUser()
{
XAsyncBlock* asyncBlock = new XAsyncBlock{};
asyncBlock->queue = m_taskQueue;
asyncBlock->context = this;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
Sample* pThis = static_cast<Sample*>(asyncBlock->context);
// [Simple User Model]
// XUserAddResult is guaranteed to return S_OK when signing in the default user silently with
// "XUserAddOptions::AddDefaultUserSilently".
DX::ThrowIfFailed(XUserAddResult(asyncBlock, &pThis->m_user));
DX::ThrowIfFailed(XUserGetLocalId(pThis->m_user, &pThis->m_userLocalId));
pThis->UpdateUserUIData();
delete asyncBlock;
};
// [Simple User Model]
// The default user that launched the application can be added silently. When using the simple user model,
// this is guaranteed to succeed in acquiring the user later with XUserAddResult.
DX::ThrowIfFailed(XUserAddAsync(XUserAddOptions::AddDefaultUserSilently, asyncBlock));
}
void Sample::InitializeUI()
{
auto layout = m_uiManager.LoadLayoutFromFile("Assets/UILayout.json");
m_uiManager.AttachTo(layout, m_uiManager.GetRootElement());
m_gamertagText = layout->GetTypedChildById<UIStaticText>(ID("Gamertag_Label"));
m_gamerpicImage = layout->GetTypedChildById<UIImage>(ID("Gamerpic"));
}
// [Simple User Model]
// This method updates the user's Gamertag and GamerPicture data that's rendered to the screen.
void Sample::UpdateUserUIData()
{
// Set gamertag
char gamertagBuffer[XUserGamertagComponentUniqueModernMaxBytes + 1] = {};
size_t gamertagSize = 0;
DX::ThrowIfFailed(XUserGetGamertag(m_user, XUserGamertagComponent::UniqueModern, sizeof(gamertagBuffer), gamertagBuffer, &gamertagSize));
m_gamertagText->SetDisplayText(gamertagBuffer);
// Setup gamerpic request
XAsyncBlock* asyncBlock = new XAsyncBlock{};
asyncBlock->queue = m_taskQueue;
asyncBlock->context = this;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
Sample* pThis = static_cast<Sample*>(asyncBlock->context);
// Get buffer size
size_t bufferSize = 0;
DX::ThrowIfFailed(XUserGetGamerPictureResultSize(asyncBlock, &bufferSize));
// Get buffer data
std::vector<uint8_t> buffer;
buffer.resize(bufferSize);
size_t bufferUsed = 0;
DX::ThrowIfFailed(XUserGetGamerPictureResult(asyncBlock, bufferSize, buffer.data(), &bufferUsed));
// Set in UI
pThis->m_gamerpicImage->UseTextureData(buffer.data(), buffer.size());
delete asyncBlock;
};
// Request gamerpic
DX::ThrowIfFailed(XUserGetGamerPictureAsync(m_user, XUserGamerPictureSize::Medium, asyncBlock));
}
// [Simple User Model]
// This callback is called whenever a user event happens. It was registered with XUserRegisterForChangeEvent in the Sample constructor.
void CALLBACK Sample::UserChangeEventCallback(
_In_opt_ void* context,
_In_ XUserLocalId userLocalId,
_In_ XUserChangeEvent event
)
{
// Log the callback
{
char debugString[512] = {};
sprintf_s(debugString, 512, u8"UserChangeEventCallback() : userLocalId = 0x%llx, event = %d\n",
userLocalId.value,
event
);
OutputDebugStringA(debugString);
}
// Only handle events for the default user
Sample* pThis = static_cast<Sample*>(context);
if (userLocalId.value != pThis->m_userLocalId.value)
{
return;
}
// [Simple User Model]
// In the simple user model, these Sign-in/Sign-out events are not intended to be received for the default user by design.
//
// Instead, the title is suspended if the user ever signs out. If a different user were to then attempt to
// start the title, the title is terminated and re-started with the new default user instead of requiring handling
// based on these events.
//
// However, note that they can be caused on a devkit when manually resuming using DevHome or other PLM testing tools.
// These will not be received in retail or if testing using the standard Xbox user's home screens to launch.
//
// See the readme for more in-depth information.
if (event == XUserChangeEvent::SignedInAgain ||
event == XUserChangeEvent::SignedOut ||
event == XUserChangeEvent::SigningOut)
{
throw std::runtime_error("Got an unexpected user change event that shouldn't happen with the simple user model.");
}
// Update user data if it changed
if (event == XUserChangeEvent::GamerPicture ||
event == XUserChangeEvent::Gamertag)
{
pThis->UpdateUserUIData();
}
}