-
Notifications
You must be signed in to change notification settings - Fork 226
/
SimplePLM.cpp
298 lines (244 loc) · 7.02 KB
/
SimplePLM.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
//--------------------------------------------------------------------------------------
// SimplePLM.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimplePLM.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using namespace Windows::ApplicationModel;
using namespace Windows::UI::Core;
using namespace Windows::ApplicationModel::Activation;
using Microsoft::WRL::ComPtr;
Sample::Sample() :
m_useDeferral(false),
m_showToasts(false),
m_consoleIsValid(false)
{
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN);
m_deviceResources->RegisterDeviceNotify(this);
m_console = std::make_unique<DX::TextConsoleImage>();
}
void Sample::ShowInstructions()
{
m_logCache.insert(m_logCache.begin(), ref new Platform::String(L"Simple PLM"));
m_logCache.insert(m_logCache.begin(), ref new Platform::String(L"Toggle Windows notifications with A button or 'N' key (default is off)"));
m_logCache.insert(m_logCache.begin(), ref new Platform::String(L"Toggle suspend deferral with B button or 'D' key (default is off)"));
}
void Sample::LogPLMEvent(const wchar_t* primaryLog, const wchar_t* secondaryData)
{
unsigned int tid = GetCurrentThreadId();
SYSTEMTIME curTime;
GetSystemTime(&curTime);
wchar_t timeAndTid[25];
swprintf_s(timeAndTid, L"[%02d:%02d:%02d:%03d](%d)", curTime.wHour, curTime.wMinute, curTime.wSecond, curTime.wMilliseconds, tid);
Platform::String^ LogLine = ref new Platform::String(timeAndTid) + " " + ref new Platform::String(primaryLog) + " " + ref new Platform::String(secondaryData);
//Output to Debug Console.
OutputDebugStringW(LogLine->Data());
OutputDebugStringW(L"\n");
//Output to screen. We must cache screen logs if a log occurs when there is no valid screen console yet.
if (!m_consoleIsValid)
{
m_logCache.insert(m_logCache.begin(), LogLine);
}
else
{
m_console->WriteLine(LogLine->Data());
}
//Output to Windows Notification Center.
if (m_showToasts)
{
m_toastManager->Show(primaryLog, secondaryData, timeAndTid);
}
}
void Sample::ToggleNotifications()
{
m_showToasts = !m_showToasts;
if (m_showToasts)
{
LogPLMEvent(L"Will log to Windows notifications.");
}
else
{
LogPLMEvent(L"Will not log to Windows notifications.");
}
}
void Sample::ToggleDeferral()
{
m_useDeferral = !m_useDeferral;
if (m_useDeferral)
{
LogPLMEvent(L"Will use a suspend deferral.");
}
else
{
LogPLMEvent(L"Will not use a suspend deferral.");
}
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(IUnknown* window, int width, int height, DXGI_MODE_ROTATION rotation)
{
m_gamePad = std::make_unique<GamePad>();
m_keyboard = std::make_unique<Keyboard>();
m_keyboard->SetWindow(reinterpret_cast<ABI::Windows::UI::Core::ICoreWindow*>(window));
m_toastManager = std::make_unique <DX::ToastManager>();
m_deviceResources->SetWindow(window, width, height, rotation);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
}
// Updates the world.
void Sample::Update(DX::StepTimer const&)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
if (m_gamePadButtons.a == GamePad::ButtonStateTracker::PRESSED)
{
ToggleNotifications();
}
if (m_gamePadButtons.b == GamePad::ButtonStateTracker::PRESSED)
{
ToggleDeferral();
}
}
else
{
m_gamePadButtons.Reset();
}
auto kb = m_keyboard->GetState();
m_keyboardButtons.Update(kb);
if (kb.Escape)
{
ExitSample();
}
if (m_keyboardButtons.IsKeyPressed(Keyboard::N))
{
ToggleNotifications();
}
if (m_keyboardButtons.IsKeyPressed(Keyboard::D))
{
ToggleDeferral();
}
PIXEndEvent();
}
#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;
}
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
m_console->Render();
PIXEndEvent(context);
// Show the new frame.
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
PIXEndEvent();
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto renderTarget = m_deviceResources->GetRenderTargetView();
context->OMSetRenderTargets(1, &renderTarget, nullptr);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnActivated()
{
}
void Sample::OnDeactivated()
{
}
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->ClearState();
m_deviceResources->Trim();
}
void Sample::OnResuming()
{
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
m_keyboardButtons.Reset();
}
void Sample::OnWindowSizeChanged(int width, int height, DXGI_MODE_ROTATION rotation)
{
if (!m_deviceResources->WindowSizeChanged(width, height, rotation))
return;
CreateWindowSizeDependentResources();
}
void Sample::ValidateDevice()
{
m_deviceResources->ValidateDevice();
}
// Properties
void Sample::GetDefaultSize(int& width, int& height) const
{
width = 1280;
height = 720;
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
m_console->RestoreDevice(m_deviceResources->GetD3DDeviceContext(), L"Courier_16.spritefont", L"ATGSampleBackground.DDS");
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
m_console->SetWindow(m_deviceResources->GetOutputSize(), true);
//Now that the Console is valid we can flush any logs to the console.
m_consoleIsValid = true;
while (!m_logCache.empty())
{
m_console->WriteLine(m_logCache.back()->Data());
m_logCache.pop_back();
}
m_console->SetRotation(m_deviceResources->GetRotation());
}
void Sample::OnDeviceLost()
{
m_consoleIsValid = false;
m_console->ReleaseDevice();
}
void Sample::OnDeviceRestored()
{
CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}
#pragma endregion