/*---------------------------------------------------------------------------* Project: Sample demo program for NAND library File: banner.c Programmer: HIRATSU Daisuke Copyright (C) 2006 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: banner.c,v $ Revision 1.7 2006/11/22 14:02:53 hiratsu Now banner sample demo creates not only "banner.bin" but also "savefile.dat". Revision 1.6 2006/10/24 09:47:29 hiratsu USE_TPL switch is available. Now messages are displayed to TV screen. Revision 1.5 2006/10/19 04:15:24 hiratsu Banner/icon images are available. Revision 1.4 2006/09/19 12:43:12 hiratsu Banner file size for NANDCheck() was wrong. Fixed. Revision 1.3 2006/09/18 13:19:50 hiratsu Added NANDCheck() judgement. Revision 1.2 2006/09/18 04:54:33 hiratsu Used NANDMove() API for safe banner creation. Revision 1.1 2006/09/14 13:06:57 hiratsu Initial check-in. *---------------------------------------------------------------------------*/ #define USE_TPL #include #include #include #include #include #ifdef USE_TPL #include #endif #include "util.h" #define PERM (NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE) // Owner can read/write #define ATTR 0x00 // No attributes. #define ICONS 4 #define SAVEFILE_SIZE (20*1024) // 20KB (requries 2 FS blocks) #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 static BOOL verify(const char *name, u32 size); static BOOL createSaveFile(void); static BOOL createBannerFile(void); static BOOL moveToHome(const char *srcPath); static BOOL nandCheck(u32 fsBlocks, u32 inodes); static BOOL getFileSize(const char *path, u32 *length); static void displayMessage(char *msg); #ifdef USE_TPL static TPLPalettePtr tplIcons; static TPLDescriptorPtr tdpIcons; #else static u8 s_bannerTexture[NAND_BANNER_TEXTURE_SIZE]; static u8 s_iconTexture[ICONS][NAND_BANNER_ICON_SIZE]; #endif static GXColor Blue = { 0, 0, 64, 0 }; int main(void) { char message[1024] = ""; DEMOInit(0); if(verify("savefile.dat", SAVEFILE_SIZE) && verify("banner.bin", NAND_BANNER_SIZE(ICONS))) // Both files already exist { displayMessage ( "Save file and banner file are ready.\n" "You can delete the Banner file \n" "on Data Management Screen. " ); } else if(verify("savefile.dat", SAVEFILE_SIZE)) // Only save file exists { if(nandCheck(3, 1)) // 4-icons banner needs 3 FS blocks. { if(createBannerFile() && moveToHome("/tmp/banner.bin")) { displayMessage ( "Success. Created banner file. \n" "Please confirm it on Data Management\n" "Screen. " ); } else { displayMessage("Failed to create save file."); } } else { displayMessage("NANDCheck() rejected your request."); } } else // No files { if(nandCheck(3+2, 1+1)) // Total FS blocks and total i-nodes are required. { if(createSaveFile() && moveToHome("/tmp/savefile.dat") && createBannerFile() && moveToHome("/tmp/banner.bin")) { displayMessage ( "Success. \n" "Created save file and banner file. \n" "Please confirm it on Data Management\n" "Screen. " ); } else { displayMessage("Failed to create files."); } } else { displayMessage("NANDCheck() rejected your request."); } } OSShutdownSystem(); return EXIT_SUCCESS; // Never reach here. } BOOL verify(const char *name, u32 size) { char path[NAND_MAX_PATH]=""; s32 ret = NAND_RESULT_FATAL_ERROR; u32 length = 0; ret = NANDGetHomeDir(path); if(ret!=NAND_RESULT_OK) { return FALSE; } strcat(path, "/"); strcat(path, name); if(getFileSize(path, &length)) { if(length==size) { return TRUE; } else { return FALSE; } } else { return FALSE; } } BOOL nandCheck(const u32 fsBlocks, const u32 inodes) { u32 answer = 0xffffffff; s32 ret = NANDCheck(fsBlocks, inodes, &answer); if(ret==NAND_RESULT_OK) { if(answer==0) { return TRUE; } else { return FALSE; } } else { OSReport("NANDCheck() failed. Result code: %d\n", ret); return FALSE; } } BOOL createSaveFile(void) { s32 ret = NAND_RESULT_FATAL_ERROR; NANDFileInfo info; u8 buf[SAVEFILE_SIZE] ATTRIBUTE_ALIGN(32); memset(buf, 0, SAVEFILE_SIZE); ret = NANDCreate("/tmp/savefile.dat", PERM, ATTR); if(ret != NAND_RESULT_OK) { printErrMsg(ret); OSReport("NANDCreate() failed.\n"); return FALSE; } ret = NANDOpen("/tmp/savefile.dat", &info, NAND_ACCESS_WRITE); if(ret != NAND_RESULT_OK) { printErrMsg(ret); OSReport("NANDOpen() failed. Result code: %d\n", ret); return FALSE; } ret = NANDWrite(&info, buf, SAVEFILE_SIZE); if(ret != SAVEFILE_SIZE) { printErrMsg(ret); OSReport("NANDWrite() failed. Result code: %d\n", ret); NANDClose(&info); return FALSE; } ret = NANDClose(&info); if(ret != NAND_RESULT_OK) { printErrMsg(ret); OSReport("NANDClose() failed. Result code: %d\n", ret); return FALSE; } return TRUE; } BOOL createBannerFile(void) { s32 ret = NAND_RESULT_FATAL_ERROR; NANDFileInfo info; static NANDBanner s_bnr ATTRIBUTE_ALIGN(32); ret = NANDCreate("/tmp/banner.bin", PERM, ATTR); if(ret != NAND_RESULT_OK) { printErrMsg(ret); OSReport("NANDCreate() failed.\n"); return FALSE; } ret = NANDOpen("/tmp/banner.bin", &info, NAND_ACCESS_WRITE); if(ret != NAND_RESULT_OK) { printErrMsg(ret); OSReport("NANDOpen() failed. Result code: %d\n", ret); return FALSE; } // Prepare banner data { int i = 0; NANDInitBanner( &s_bnr, NAND_BANNER_FLAG_ANIM_BOUNCE, L"banner demo", L"(nanddemo)" ); #ifdef USE_TPL // banner TPLGetPalette(&tplIcons, "/nanddemo/wii.tpl"); tdpIcons = TPLGet(tplIcons, (u32) 0); memcpy(s_bnr.bannerTexture, tdpIcons->textureHeader->data, NAND_BANNER_TEXTURE_SIZE); // icon TPLReleasePalette(&tplIcons); TPLGetPalette(&tplIcons, "/nanddemo/yoshi.tpl"); for (i=0; itextureHeader->data, NAND_BANNER_ICON_SIZE); NANDSetIconSpeed(&s_bnr, i, NAND_BANNER_ICON_ANIM_SPEED_SLOW); } NANDSetIconSpeed(&s_bnr, ICONS, NAND_BANNER_ICON_ANIM_SPEED_END); s_bnr.flag |= NAND_BANNER_FLAG_ANIM_BOUNCE; #else // banner memcpy(s_bnr.bannerTexture, s_bannerTexture, NAND_BANNER_TEXTURE_SIZE); // icon for(i=0; i