forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNLSAndLocalizationUWP.cpp
More file actions
397 lines (328 loc) · 11.9 KB
/
NLSAndLocalizationUWP.cpp
File metadata and controls
397 lines (328 loc) · 11.9 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
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
//--------------------------------------------------------------------------------------
// NLSAndLocalizationUWP.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "NLSAndLocalizationUWP.h"
#include "ATGColors.h"
extern void ExitSample();
using namespace DirectX;
using Microsoft::WRL::ComPtr;
Sample::Sample()
{
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN);
m_deviceResources->RegisterDeviceNotify(this);
m_textConsole = std::make_unique<DX::TextConsoleImage>();
}
// 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_deviceResources->SetWindow(window, width, height, rotation);
int retVal = GetUserDefaultLocaleName(m_localeName, ARRAYSIZE(m_localeName));
if (retVal == 0)
throw std::exception("Failed to get locale in CreateDeviceDependenciesResources");
swprintf_s(m_imagePrepend, _countof(m_imagePrepend), L"Assets\\Images\\%s\\", m_localeName);
swprintf_s(m_resFileName, _countof(m_resFileName), L"Assets\\Resources\\%s.resources", m_localeName);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_textConsole->SetForegroundColor(ATG::Colors::Orange);
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
InitializeLocalization();
}
void Sample::InitializeLocalization()
{
m_textConsole->Format(L"******* Using the NLS APIs *******\n");
m_textConsole->Format(L"Note: All the strings except the ones with\nthe IDs in the Localization section\nare hard coded in English (not localized)\n\n");
// Get Package Details using the Package Id
Windows::ApplicationModel::Package^ package = Windows::ApplicationModel::Package::Current;
Windows::ApplicationModel::PackageId^ packageId = package->Id;
m_textConsole->Format(L"The Package Full Name is: %s\n", packageId->FullName->Data());
// Get the locale name for the system using GetUserDefaultLocaleName(). This will return the locale selected through the
// Settings app only if that locale has been added to the Resources section of the application's package manifest.
// In case the resource is absent fron the manifest, this API will return the first locale in the Resource Language.
wchar_t userLocaleName[LOCALE_NAME_MAX_LENGTH] = { L'\0' };
wchar_t message[1024] = { L'\0' };
int retVal = GetUserDefaultLocaleName(userLocaleName, ARRAYSIZE(userLocaleName));
if (FunctionSucceeded(retVal, L"GetUserDefaultLocaleName"))
{
swprintf_s(message, L"GetUserDefaultLocaleName succeeded: %s\n", userLocaleName);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetUserDefaultLocaleName failed!");
}
unsigned long lcid = LocaleNameToLCID(userLocaleName, 0);
unsigned short primary = 0, sublang = 0;
if (FunctionSucceeded(lcid, L"LocalNameToLCID"))
{
primary = PRIMARYLANGID(lcid);
sublang = SUBLANGID(lcid);
swprintf_s(message, L"LocaleNameToLCID succeeded: %u\n\tPrimary ID: %u\n\tSublanguage: %u\n", lcid, primary, sublang);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("LocaleNameToLCID failed!");
}
// The GetUserGeoID() API can be used to get the actual country the kit is in.
// It gives you the country selected through the Settings app
long geoID = GetUserGeoID(GEOCLASS_NATION);
wchar_t geoData[LOCALE_NAME_MAX_LENGTH];
retVal = GetGeoInfoW(geoID, GEO_LATITUDE, geoData, ARRAYSIZE(geoData), primary);
if (FunctionSucceeded(retVal, L"GetGeoInfoW"))
{
swprintf_s(message, L"Lattitude query succeeded: %s\n", geoData);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetGeoInfoW failed in GEO_LATTITUDE!");
}
retVal = GetGeoInfoW(geoID, GEO_LONGITUDE, geoData, ARRAYSIZE(geoData), primary);
if (FunctionSucceeded(retVal, L"GetGeoInfoW"))
{
swprintf_s(message, L"Longitude query succeeded: %s\n", geoData);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetGeoInfoW failed on GEO_LONGITUDE!");
}
retVal = GetGeoInfoW(geoID, GEO_NATION, geoData, ARRAYSIZE(geoData), primary);
if (FunctionSucceeded(retVal, L"GetGeoInfoW"))
{
swprintf_s(message, L"Nation query succeeded: %s\n", geoData);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetGeoInfoW failed on GEO_NATION!");
}
wchar_t iso2[3];
retVal = GetGeoInfoW(geoID, GEO_ISO2, iso2, ARRAYSIZE(iso2), primary);
if (FunctionSucceeded(retVal, L"GetGeoInfoW"))
{
swprintf_s(message, L"Iso2 query succeeded: %s\n", iso2);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetGeoInfoW failed on GEO_ISO2!");
}
wchar_t iso3[4];
retVal = GetGeoInfoW(geoID, GEO_ISO3, iso3, ARRAYSIZE(iso3), primary);
if (FunctionSucceeded(retVal, L"GetGeoInfoW"))
{
swprintf_s(message, L"Iso3 query succeeded: %s\n", iso3);
OutputDebugString(message);
m_textConsole->Write(message);
}
else
{
throw std::exception("GetGeoInfoW failed on GEO_ISO3!");
}
// The country values returned from GetUserDefaultLocaleName() and GetUserGeoID() can be compared
// to determine if the country selected by the user is supported by the app or not
std::wstring localeNameStr(userLocaleName);
std::wstring::size_type pos = localeNameStr.find(L'-');
localeNameStr = localeNameStr.substr(pos + 1);
if (_wcsicmp(localeNameStr.c_str(), iso2) == 0 || _wcsicmp(localeNameStr.c_str(), iso3) == 0)
{
m_textConsole->Format(L"Selected locale in manifest. Country: %s\n", iso2);
}
else
{
m_textConsole->Format(L"The selected locale (Country: %s) is NOT present in the manifest, so the fallback locale (Country: %s) is selected for localization\n", iso2, localeNameStr.c_str());
}
wchar_t lcpData[LOCALE_NAME_MAX_LENGTH];
retVal = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SISO639LANGNAME, lcpData, ARRAYSIZE(lcpData));
if (FunctionSucceeded(retVal, L"GetLocaleInfoEx"))
{
m_textConsole->Format(L"GetLocaleInfoEx() - LOCALE_SISO639LANGNAME: %s\n", lcpData);
}
else
{
throw std::exception("GetLocaleInfoEx failed on LOCALE_SISO639LANGNAME!");
}
retVal = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, lcpData, ARRAYSIZE(lcpData));
if (FunctionSucceeded(retVal, L"GetLocaleInfoEx"))
{
m_textConsole->Format(L"GetLocaleInfoEx() - LOCALE_IDEFAULTLANGUAGE: %s\n", lcpData);
}
else
{
throw std::exception("GetLocaleInfoEx failed on LOCALE_IDEFAULTLANGUAGE!");
}
}
#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();
}
}
else
{
m_gamePadButtons.Reset();
}
auto kb = m_keyboard->GetState();
m_keyboardButtons.Update(kb);
if (kb.Escape)
{
ExitSample();
}
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_textConsole->Render();
m_sprites->Begin();
m_sprites->Draw(m_texture.Get(), XMFLOAT2(800, 75), nullptr, Colors::White, 0.f, XMFLOAT2(0,0), 0.2f);
m_sprites->End();
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->ClearRenderTargetView(renderTarget, ATG::Colors::Background);
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()
{
auto context = m_deviceResources->GetD3DDeviceContext();
auto device = m_deviceResources->GetD3DDevice();
// setup the text console for output
m_textConsole->RestoreDevice(context, L"SegoeUI_18.spritefont");
// parse the resource file to find the right localized image to display
ResourceParser resParser(m_localeName, m_imagePrepend);
DX::ThrowIfFailed(resParser.ParseFile(m_resFileName));
Platform::String^ idImage = ref new Platform::String(L"Gamepad");
Platform::String^ imageString = resParser.GetImage(idImage);
DX::ThrowIfFailed(CreateDDSTextureFromFile(device, imageString->Data(), nullptr, m_texture.ReleaseAndGetAddressOf()));
m_sprites = std::make_unique<SpriteBatch>(context);
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
auto rect = m_deviceResources->GetOutputSize();
m_textConsole->SetWindow(rect);
DXGI_MODE_ROTATION rotation = m_deviceResources->GetRotation();
m_textConsole->SetRotation(rotation);
m_sprites->SetRotation(rotation);
}
void Sample::OnDeviceLost()
{
m_textConsole->ReleaseDevice();
m_texture.Reset();
m_sprites.reset();
}
void Sample::OnDeviceRestored()
{
CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}
#pragma endregion
bool Sample::FunctionSucceeded(int returned, const wchar_t* function)
{
if (returned == 0)
{
unsigned long error = GetLastError();
wchar_t message[1024] = { L'\0' };
swprintf_s(message, L"%s failed with error: %u\n", function, error);
OutputDebugString(message);
return false;
}
return true;
}