1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     Util.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 47228 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <wchar.h>
19 #include "Util.h"
20 
21 //////////////////////////////////////////////////////////////////////////////////////////
22 // Variable Definitions
23 //////////////////////////////////////////////////////////////////////////////////////////
24 
25 namespace
26 {
27     // Heap
28     nn::fnd::ExpHeap* s_pExpHeap = NULL;
29     // Heap object buffer
30     bit8 s_expHeapObjectBuffer[sizeof (nn::fnd::ExpHeap)] NN_ATTRIBUTE_ALIGN(8);
31     // Title
32     const size_t TITLE_LENGTH = 255;
33     wchar_t s_Title[TITLE_LENGTH + 1];
34 }
35 
36 //////////////////////////////////////////////////////////////////////////////////////////
37 
38 namespace scene
39 {
40 
41 // Get the length of a wide string.
wstrlen(const wchar_t * pText)42 u32 wstrlen(const wchar_t* pText)
43 {
44     u32 length = 0;
45 
46     while (*pText++ != '\0')
47     {
48         length++;
49     }
50 
51     return length;
52 }
53 
54 // Convert a wide string to a numerical value.
wtoi(const wchar_t * pText)55 u32 wtoi(const wchar_t* pText)
56 {
57     // Numeric value
58     u32 value = 0;
59 
60     while (*pText != '\0' && *pText >= '0' && *pText <= '9')
61     {
62         value = value * 10 + (*pText - '0');
63         pText++;
64     }
65 
66     return value;
67 }
68 
69 // Initialize memory.
SetupMemory(u32 deviceMemorySize)70 void SetupMemory(u32 deviceMemorySize)
71 {
72     // Create a heap object
73     s_pExpHeap = new(s_expHeapObjectBuffer) nn::fnd::ExpHeap();
74     // Assign device memory to a heap
75     s_pExpHeap->Initialize(nn::os::GetDeviceMemoryAddress(), deviceMemorySize );
76 }
77 
78 // Deallocate memory.
ReleaseMemory()79 void ReleaseMemory()
80 {
81     s_pExpHeap->Invalidate();
82     s_pExpHeap->Finalize();
83 }
84 
85 // Get the heap.
GetHeap()86 nn::fnd::ExpHeap* GetHeap()
87 {
88     return s_pExpHeap;
89 }
90 
91 // Allocates memory.
Allocate(size_t size,s32 alignment)92 void* Allocate(size_t size, s32 alignment)
93 {
94     return s_pExpHeap->Allocate(size, alignment);
95 }
96 
97 // Deallocate memory.
Free(void * pBuffer)98 void Free(void* pBuffer)
99 {
100     if (pBuffer)
101     {
102         s_pExpHeap->Free(pBuffer);
103     }
104 }
105 
106 // Render the title.
SetTitle(const wchar_t * pTitle,size_t length)107 void SetTitle(const wchar_t* pTitle, size_t length)
108 {
109     NN_ASSERT(length < TITLE_LENGTH);
110 
111     wcsncpy(s_Title, pTitle, length);
112     s_Title[length] = L'\0';
113 }
DrawTitle(TextWriter * pTextWriter)114 void DrawTitle(TextWriter* pTextWriter)
115 {
116     f32 scale = pTextWriter->GetFontScale();
117     pTextWriter->SetFontScale(DEFAULT_FONT_SCALE);
118     pTextWriter->SetTextColor(102, 153, 255);
119     pTextWriter->Printf(2, 2, s_Title);
120     pTextWriter->SetFontScale(scale);
121 }
122 
DrawTitleForApplication(TextWriter * pTextWriter)123 void DrawTitleForApplication(TextWriter* pTextWriter)
124 {
125     pTextWriter->SetTextColor(102, 153, 255);
126     pTextWriter->Printf(2, 2, L"追加コンテンツ購入アプリケーション");
127 }
128 
DrawTitleForApplet(TextWriter * pTextWriter)129 void DrawTitleForApplet(TextWriter* pTextWriter)
130 {
131     pTextWriter->SetTextColor(0, 255, 0);
132     pTextWriter->Printf(2, 2, L"追加コンテンツ購入アプレット");
133 }
134 
135 // Render the scene ID.
DrawSceneId(TextWriter * pTextWriter,const wchar_t * pSceneId)136 void DrawSceneId(TextWriter* pTextWriter, const wchar_t* pSceneId)
137 {
138 #ifdef DEBUG_SHOW_SCENE_ID
139     pTextWriter->SetTextColor(255, 255, 0);
140     pTextWriter->PutString(346, 2, pSceneId);
141 #else
142     NN_UNUSED_VAR(pTextWriter);
143     NN_UNUSED_VAR(pSceneId);
144 #endif
145 }
146 
147 // Render Result.
DrawResult(TextWriter * pTextWriter,const nn::Result & result)148 void DrawResult(TextWriter* pTextWriter, const nn::Result& result)
149 {
150 #ifdef DEBUG_SHOW_RESULT
151     if (result.IsFailure())
152     {
153         pTextWriter->SetTextColor(255, 0, 0);
154         pTextWriter->Printf(2, 214, L"RESULT: %08X", result.GetPrintableBits());
155     }
156 #else
157     NN_UNUSED_VAR(pTextWriter);
158     NN_UNUSED_VAR(result);
159 #endif
160 }
161 
162 // Render the busy icon.
DrawBusyIcon(TextWriter * pTextWriter,u16 x,u16 y,u32 counter)163 void DrawBusyIcon(TextWriter* pTextWriter, u16 x, u16 y, u32 counter)
164 {
165     // Index
166     u32 idnex = (counter / 10) % 8;
167     // Busy icon
168     wchar_t busyIcon[] = {0xE020 + idnex, '\0'};
169     // Offset (adjust with offset since only the 4th character is shifted)
170     f32 offset = (idnex == 3) ? 0.6f : 0.0f;
171 
172     // Current font scale
173     f32 fontScale = pTextWriter->GetFontScale();
174 
175     pTextWriter->SetFontScale(1.0f);
176     pTextWriter->SetTextColor(0, 162, 232);
177     pTextWriter->Printf(x + offset, y, busyIcon);
178 
179     // Restore font scale to original value
180     pTextWriter->SetFontScale(fontScale);
181 }
182 
183 // Render the confirmation message.
DrawConfirmMessage(TextWriter * pTextWriter,const wchar_t * pMessage1,const wchar_t * pMessage2)184 void DrawConfirmMessage(TextWriter* pTextWriter, const wchar_t* pMessage1, const wchar_t* pMessage2)
185 {
186     pTextWriter->SetTextColor(0, 255, 0);
187 
188     // Up
189     {
190         // Text size
191         f32 width  = pTextWriter->CalculateStringWidth(pMessage1);
192         f32 height = pTextWriter->CalculateStringHeight(pMessage1);
193         // Display position
194         f32 x = ((f32)NN_GX_DISPLAY1_HEIGHT - width) / 2.0f;
195         f32 y = ((f32)NN_GX_DISPLAY1_WIDTH - 45.0f) / 2.0f - (height + 1.0f);
196 
197         pTextWriter->Printf(x, y, pMessage1);
198     }
199     // Down
200     {
201         // Text size
202         f32 width  = pTextWriter->CalculateStringWidth(pMessage2);
203         // Display position
204         f32 x = ((f32)NN_GX_DISPLAY1_HEIGHT - width) / 2.0f;
205         f32 y = ((f32)NN_GX_DISPLAY1_WIDTH - 45.0f) / 2.0f + 1.0f;
206 
207         pTextWriter->Printf(x, y, pMessage2);
208     }
209 }
210 
211 // Get the DateTime from the UNIX time.
GetDateTimeFromUnixTime(s64 unixTime)212 nn::fnd::DateTime GetDateTimeFromUnixTime(s64 unixTime)
213 {
214     // Time
215     nn::fnd::DateTime dateTime = nn::fnd::DateTime(1970, 1, 1);
216     // Add the UNIX time
217     dateTime += nn::fnd::TimeSpan::FromMilliSeconds(unixTime);
218 
219     return dateTime;
220 }
221 
222 // Add the left button.
AddLButton(ControlManager * pManager,u32 id,const wchar_t * pCaption)223 void AddLButton(ControlManager* pManager, u32 id, const wchar_t* pCaption)
224 {
225     pManager->Add(new Button(id, 2, 208, 212, 30, pCaption));
226 }
227 
228 // Add the right button.
AddRButton(ControlManager * pManager,u32 id,const wchar_t * pCaption)229 void AddRButton(ControlManager* pManager, u32 id, const wchar_t* pCaption)
230 {
231     pManager->Add(new Button(id, 218, 208, 100, 30, pCaption));
232 }
233 
234 // Add the YES/NO button.
AddYesNoButtons(ControlManager * pManager,u32 idYes,u32 idNo,const wchar_t * pCaptionYes,const wchar_t * pCaptionNo)235 void AddYesNoButtons(ControlManager* pManager, u32 idYes, u32 idNo, const wchar_t* pCaptionYes, const wchar_t* pCaptionNo)
236 {
237     pManager->Add(new Button(idYes,   2, 208, 156, 30, pCaptionYes));
238     pManager->Add(new Button(idNo,  162, 208, 156, 30, pCaptionNo));
239 }
240 
241 } // namespace scene
242