1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - nandApp - demos - SubBanner
3   File:     main.c
4 
5   Copyright 2007-2008 Nintendo. 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   $Date:: 2009-02-06#$
14   $Rev: 9986 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <twl/na.h>
20 #include <DEMO.h>
21 
22 #define MENU_ELEMENT_NUM 4  // Number of menu items
23 #define MENU_TEXT_SIZE 8
24 
25 #define COLOR_WHITE GX_RGBA(31, 31, 31, 1)
26 #define COLOR_RED   GX_RGBA(31,  0,  0, 1)
27 #define COLOR_GREEN GX_RGBA( 0, 31,  0, 1)
28 #define COLOR_BLUE  GX_RGBA( 0,  0, 31, 1)
29 #define COLOR_BLACK GX_RGBA( 0,  0,  0, 1)
30 
31 // Menu element coordinates
32 typedef struct MenuPos
33 {
34     BOOL   enable;
35     int    x;
36     int    y;
37 }MenuPos;
38 
39 // Structure holding parameters that determine menu composition
40 typedef struct MenuParam
41 {
42     int      num;
43     GXRgb    normal_color;
44     GXRgb    select_color;
45     GXRgb    disable_color;
46     u16      padding;
47     MenuPos *pos;
48     const char **str_elem;
49 }MenuParam;
50 
51 static const char *pStrMenu[ MENU_ELEMENT_NUM ] =
52 {
53     "write sub banner file",
54     "edit sub banner file",
55     "delete sub banner file",
56     "return to launcher",
57 };
58 
59 static MenuPos menuPos[] =
60 {
61     { TRUE,  3,   8 },
62     { TRUE,  3,   9 },
63     { TRUE,  3,   10 },
64     { TRUE,  3,   11 },
65 };
66 
67 static const MenuParam menuParam =
68 {
69     MENU_ELEMENT_NUM,
70     COLOR_BLACK,
71     COLOR_GREEN,
72     COLOR_RED,
73     0,
74     &menuPos[ 0 ],
75     (const char **)&pStrMenu,
76 };
77 
78 static void PrintBootType();
79 static void InitDEMOSystem();
80 static void InitInteruptSystem();
81 static void InitAllocSystem();
82 static void InitFileSystem();
83 
84 #define MESSAGE_SIZE 50
85 
86 static char message[MESSAGE_SIZE]="";
87 
88 /*---------------------------------------------------------------------------*
89   Name:         EditSubBanner
90 
91   Description:  Switches the sub-banner's RGB colors and flips it in the horizontal direction.
92 
93   Arguments:    None.
94 
95   Returns:      None.
96  *---------------------------------------------------------------------------*/
EditSubBanner(NASubBanner * banner)97 static BOOL EditSubBanner(NASubBanner* banner)
98 {
99     int i;
100     NASubBannerAnime* anime = &banner->anime;
101 
102     if (!NA_LoadSubBanner(banner))
103     {
104         return FALSE;
105     }
106 
107     // Rotate the RGB values for the palette data
108     for (i = 0; i < NA_SUBBANNER_PALETTE_NUM; i++)
109     {
110         anime->pltt[0][i] = (GXRgb)((GX_RGB_R_MASK & anime->pltt[0][i]) << 5 |  // R to G
111                                     (GX_RGB_G_MASK & anime->pltt[0][i]) << 5 |  // G to B
112                                     (GX_RGB_B_MASK & anime->pltt[0][i]) >> 10); // B to R
113     }
114     anime->control[0].frameInfo.flipH = !anime->control[0].frameInfo.flipH;
115 
116     NA_MakeSubBannerHeader(banner);
117     return TRUE;
118 }
119 
120 /*---------------------------------------------------------------------------*
121   Name:         TwlMain
122 
123   Description:  Main function.
124 
125   Arguments:    None.
126 
127   Returns:      None.
128  *---------------------------------------------------------------------------*/
TwlMain(void)129 void TwlMain(void)
130 {
131     int cursor = 0;
132     int i;
133 
134     OS_Init();
135     InitInteruptSystem();
136     InitFileSystem();
137     InitAllocSystem();
138     InitDEMOSystem();
139 
140     // Print usage
141     PrintBootType();
142     OS_TPrintf("=================================\n");
143     OS_TPrintf("USAGE: SubBanner demo\n");
144     OS_TPrintf(" UP DOWN : select menu\n");
145     OS_TPrintf(" A       : execute menu\n");
146     OS_TPrintf("=================================\n");
147 
148     for (;;)
149     {
150         DEMOReadKey();
151         if( DEMO_IS_TRIG(PAD_KEY_DOWN) )
152         {   // Move cursor
153             if( ++cursor == MENU_ELEMENT_NUM )
154             {
155                 cursor=0;
156             }
157         }
158         if( DEMO_IS_TRIG(PAD_KEY_UP) )
159         {
160             if( --cursor & 0x80 )
161             {
162                 cursor=MENU_ELEMENT_NUM - 1;
163             }
164         }
165 
166         // Render the menu
167         {
168             const MenuParam *param = &menuParam;
169 
170             DEMOFillRect(0, 0, 256, 192, COLOR_WHITE);
171             DEMOSetBitmapGroundColor(COLOR_WHITE);
172             DEMOSetBitmapTextColor( COLOR_BLUE );
173             DEMODrawText( 1 * MENU_TEXT_SIZE, 0 * MENU_TEXT_SIZE, "SubBanner");
174 
175             for( i = 0; i < param->num; i++)
176             {
177                 DEMOSetBitmapTextColor( i == cursor ? param->select_color : param->normal_color );
178                 DEMODrawText( param->pos[i].x * MENU_TEXT_SIZE, param->pos[i].y * MENU_TEXT_SIZE,
179                             ( i == cursor ? "=>%s" : "  %s" ), param->str_elem[i] );
180             }
181 
182             DEMOSetBitmapTextColor( COLOR_RED );
183             DEMODrawText( 1 * MENU_TEXT_SIZE, 3 * MENU_TEXT_SIZE, message);
184         }
185 
186         if( DEMO_IS_TRIG(PAD_BUTTON_A) )
187         {    // Branch to menu items
188              switch( cursor )
189              {
190                  NASubBanner subBanner;
191                  FSFile file[1];
192                  s32 readLen;
193                case 0:
194                  //Write the sub-banner
195                  FS_InitFile(file);
196                  if (FS_OpenFileEx(file, "rom:/sub_banner.bnr", FS_FILEMODE_R) )
197                  {
198                      readLen = FS_ReadFile(file, &subBanner, sizeof(NASubBanner));
199                      (void)FS_CloseFile(file);
200                      if( readLen == sizeof(NASubBanner) )
201                      {
202                          // Success
203                          if ( NA_SaveSubBanner( &subBanner ) )
204                          {
205                              (void)STD_CopyLString( message, "write succeed.", MESSAGE_SIZE );
206                          }
207                          else
208                          {
209                              (void)STD_CopyLString( message, "write failed.", MESSAGE_SIZE );
210                          }
211                      }
212                      else
213                      {
214                          (void)STD_CopyLString( message, "read rom failed.", MESSAGE_SIZE );
215                      }
216                  }
217                  else
218                  {
219                      (void)STD_CopyLString( message, "open rom failed.", MESSAGE_SIZE );
220                  }
221                  break;
222                case 1:
223                  if (EditSubBanner( &subBanner ))
224                  {   // Success
225                      if ( NA_SaveSubBanner( &subBanner ) )
226                      {
227                          (void)STD_CopyLString( message, "edit succeed.", MESSAGE_SIZE );
228                      }
229                      else
230                      {
231                          (void)STD_CopyLString( message, "edit failed.", MESSAGE_SIZE );
232                      }
233                  }
234                  else
235                  {   // Failed
236                      (void)STD_CopyLString( message, "load failed.", MESSAGE_SIZE );
237                  }
238                  break;
239                case 2:
240                  //Delete the sub-banner
241                  if ( NA_DeleteSubBanner(&subBanner) )
242                  {
243                      (void)STD_CopyLString( message, "delete succeed", MESSAGE_SIZE );
244                  }
245                  else
246                  {
247                      (void)STD_CopyLString( message, "delete failed", MESSAGE_SIZE );
248                  }
249                  break;
250                case 3:
251                  (void)OS_JumpToSystemMenu();
252                  //Restart
253                  break;
254              }
255         }
256         DEMO_DrawFlip();
257         OS_WaitVBlankIntr();
258     }
259     OS_Terminate();
260 }
261 
262 /*---------------------------------------------------------------------------*
263   Name:         PrintBootType
264 
265   Description:  Prints the BootType.
266 
267   Arguments:    None.
268 
269   Returns:      None.
270  *---------------------------------------------------------------------------*/
PrintBootType()271 static void PrintBootType()
272 {
273     const OSBootType btype = OS_GetBootType();
274 
275     switch( btype )
276     {
277       case OS_BOOTTYPE_ROM:   OS_TPrintf("OS_GetBootType:OS_BOOTTYPE_ROM\n"); break;
278       case OS_BOOTTYPE_NAND:  OS_TPrintf("OS_GetBootType:OS_BOOTTYPE_NAND\n"); break;
279     default:
280         {
281             OS_Warning("unknown BootType(=%d)", btype);
282         }
283         break;
284     }
285 }
286 
287 /*---------------------------------------------------------------------------*
288   Name:         InitDEMOSystem
289 
290   Description:  Configures display settings for console screen output.
291 
292   Arguments:    None.
293 
294   Returns:      None.
295  *---------------------------------------------------------------------------*/
InitDEMOSystem()296 static void InitDEMOSystem()
297 {
298     // Initialize screen display
299     DEMOInitCommon();
300     DEMOInitVRAM();
301     DEMOInitDisplayBitmap();
302     DEMOHookConsole();
303     DEMOSetBitmapTextColor(GX_RGBA(31, 31, 0, 1));
304     DEMOSetBitmapGroundColor(DEMO_RGB_CLEAR);
305     DEMOStartDisplay();
306 }
307 
308 /*---------------------------------------------------------------------------*
309   Name:         InitInteruptSystem
310 
311   Description:  Initializes interrupts.
312 
313   Arguments:    None.
314 
315   Returns:      None.
316  *---------------------------------------------------------------------------*/
InitInteruptSystem()317 static void InitInteruptSystem()
318 {
319     // Enable master interrupt flag
320     (void)OS_EnableIrq();
321 
322     // Allow IRQ interrupts
323     (void)OS_EnableInterrupts();
324 }
325 
326 /*---------------------------------------------------------------------------*
327   Name:         InitAllocSystem
328 
329   Description:  Creates a heap and makes OS_Alloc usable.
330 
331   Arguments:    None.
332 
333   Returns:      None.
334  *---------------------------------------------------------------------------*/
InitAllocSystem()335 static void InitAllocSystem()
336 {
337     void* newArenaLo;
338     OSHeapHandle hHeap;
339 
340     // Initialize the main arena's allocation system
341     newArenaLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
342     OS_SetMainArenaLo(newArenaLo);
343 
344     // Create a heap in the main arena
345     hHeap = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
346     (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hHeap);
347 }
348 
349 /*---------------------------------------------------------------------------*
350   Name:         InitFileSystem
351 
352   Description:  Initializes the file system and makes the ROM accessible.
353                 The InitInteruptSystem function must have been called before this function is.
354 
355 
356   Arguments:    None.
357 
358   Returns:      None.
359  *---------------------------------------------------------------------------*/
InitFileSystem()360 static void InitFileSystem()
361 {
362     // Initialize file system
363     FS_Init( FS_DMA_NOT_USE );
364 }
365