/*---------------------------------------------------------------------------* Project: Revolution strapimage demo File: strapcntdemo.c Copyright 1998 - 2008 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: strapcntdemo.c,v $ Revision 1.1.2.1 2008/07/23 06:43:53 nakano_yoshinobu Moved from build/demos/strapimagedemo. *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Header files *---------------------------------------------------------------------------*/ #include #include #include #include /*---------------------------------------------------------------------------* Macro definitions *---------------------------------------------------------------------------*/ #define MAX_SCOORD 0x4000 #define MAX_TCOORD 0x4000 #define TEX_WSCALE 1.0 #define TEX_FADE_MS_TIME 500 #define TEX_CHANGE_MS_TIME 5000 #define END_MS_TIME 20000 /*---------------------------------------------------------------------------* Structure definitions *---------------------------------------------------------------------------*/ // for camera typedef struct { Vec location; Vec up; Vec target; f32 left; f32 top; f32 znear; f32 zfar; } CameraConfig; typedef struct { CameraConfig cfg; Mtx view; Mtx44 proj; } MyCameraObj; // for texture typedef struct { GXTexObj tobj; u32 width; u32 height; u32 alpha0, alpha1; GXColor color; } MyTexObj; // for entire scene control typedef struct { MyCameraObj cam; MyTexObj texture; Mtx modelCtrl; f32 modelScale; u32 texNumber; u32 texFileNumber; u32 startTick; u32 switchTick; } MySceneCtrlObj; /*---------------------------------------------------------------------------* Forward references *---------------------------------------------------------------------------*/ void main ( void ); static void LoadSIContentData( TPLPalettePtr *buffer, u32 language ); static void *LoadTexDataLZ ( void* buf ); static void GetTplTexture ( TPLPalettePtr buffer, MyTexObj* to, u32 num ); static void DrawInit ( MySceneCtrlObj* sc ); static void DrawTick ( MySceneCtrlObj* sc ); static void AnimTick ( TPLPalettePtr *buffer, MySceneCtrlObj* sc ); static void DrawTexQuad ( void ); static void SetCamera ( MyCameraObj* cam ); static void PrintIntro ( void ); /*---------------------------------------------------------------------------* Model Data *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Data arrays for indexed primitives must be 32B aligned. Normally, memory for the arrays would be OSAlloc'd (which returns 32B aligned pointers) and the data would be loaded from ROM. The pragma variable_align provides a convenient way to align initialized arrays. *---------------------------------------------------------------------------*/ static s16 TopVerts[] ATTRIBUTE_ALIGN(32) = { -1, -1, 1, // 0 1, -1, 1, // 1 1, 1, 1, // 2 -1, 1, 1, // 3 -1, -1, -1, // 4 1, -1, -1, // 5 1, 1, -1, // 6 -1, 1, -1 // 7 }; /*---------------------------------------------------------------------------* Camera configuration *---------------------------------------------------------------------------*/ static CameraConfig DefaultCamera = { { 0.0F, -5000.0F, 0.0F }, // location { 0.0F, 0.0F, 1.0F }, // up { 0.0F, 0.0F, 0.0F }, // tatget -320.0F, // left 240.0F, // top 0.0F, // near 10000.0F // far }; /*---------------------------------------------------------------------------* File List in Content *---------------------------------------------------------------------------*/ // the number of content to be used in this program #define TARGET_CONTENT 3 // The names of the files we are going to read. static char* strapImage[] = { "strapImage_jp_LZ.bin", "strapImage_En_LZ.bin", "strapImage_Fr_LZ.bin", "strapImage_Ge_LZ.bin", "strapImage_It_LZ.bin", "strapImage_Sp_LZ.bin", "strapImage_Du_LZ.bin" }; enum { JP, EN, FR, GE, IT, SP, DU }; #define NUM_OF_FILES_IN_CONTENT 7 #define NUM_OF_FILES_IN_TPL 2 CNTHandle Cnt; /*---------------------------------------------------------------------------* Application main loop *---------------------------------------------------------------------------*/ void main ( void ) { static MySceneCtrlObj SceneCtrl; // scene control parameters static TPLPalettePtr MyTplObj; // texure palette DEMOInit( NULL ); CNTInit(); CNTInitHandle( TARGET_CONTENT, &Cnt, &DemoAllocator1 ); // load content data LoadSIContentData( &MyTplObj, JP ); // Print demo directions PrintIntro(); // Initialize vertex formats, array pointers // and default scene settings. DrawInit( &SceneCtrl ); // Get current texture from the texture palette GetTplTexture( MyTplObj, &SceneCtrl.texture, 0 ); while(!( DEMOPadGetButton(0) & PAD_BUTTON_MENU )) { DEMOBeforeRender(); // Draw the model. DrawTick( &SceneCtrl ); DEMODoneRender(); // Read controller DEMOPadRead(); // Do animation AnimTick( &MyTplObj, &SceneCtrl); } // free buffer MEMFreeToAllocator( &DemoAllocator1, MyTplObj ); // make sure to release CNTHandle CNTReleaseHandle( &Cnt ); // for safety device shutdown CNTShutdown(); OSHalt( "End of demo" ); } /*---------------------------------------------------------------------------* Functions *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Name: LoadSIContentData Description: Load strap image content data elf file: load from {Dvdroot}/content3 wad file: load from content(index 3) in wad Arguments: buffer : pointer to strap image content data language : language of strap image Returns: none *---------------------------------------------------------------------------*/ static void LoadSIContentData( TPLPalettePtr *buffer, u32 language ) { CNTFileInfo fileInfo; u32 fileSize; // Load content OSReport( " File : %s \n", strapImage[ language ] ); // Open the strapImage to fileInfo1 CNTOpen(&Cnt, strapImage[ language ], &fileInfo); // Get the size of the files fileSize = CNTGetLength( &fileInfo ); OSReport( " Size : %d \n", fileSize ); // Allocate buffers to read the files. // Note that pointers returned by Allocator are all 32byte aligned. *buffer = (TPLPalettePtr)MEMAllocFromAllocator( &DemoAllocator1, OSRoundUp32B( fileSize ) ); // reads strapImage at one time CNTRead(&fileInfo, (void*)*buffer, (u32)OSRoundUp32B( fileSize )); // Close the files CNTClose( &fileInfo ); // load LZ compressed data *buffer = (TPLPalettePtr)LoadTexDataLZ( *buffer ); // bind tpl data TPLBind( *buffer ); GXInvalidateTexAll(); } /*---------------------------------------------------------------------------* Name: LoadTexDataLZ Description: Load LZ compressed tex data Returns: pointer of uncompressed tex data *---------------------------------------------------------------------------*/ static void* LoadTexDataLZ( void* buf ) { void* uncompData; u32 uncompSize; // allocated buffer uncompSize = CXGetUncompressedSize( buf ); uncompData = (u8*)MEMAllocFromAllocator( &DemoAllocator1, uncompSize ); // uncompress data CXUncompressLZ( buf, uncompData ); DCFlushRange( uncompData, uncompSize ); // free buffer MEMFreeToAllocator( &DemoAllocator1, buf ); return uncompData; } /*---------------------------------------------------------------------------* Name: GetTplTexture Description: get current texture from the texture palette Arguments: buffer : a pointer to tpl palette to : a pointer to MyTexObj structure to be set num : texture number in the tpl Returns: none *---------------------------------------------------------------------------*/ void GetTplTexture( TPLPalettePtr buffer, MyTexObj* to, u32 num ) { TPLDescriptorPtr tdp; u32 fmt; // Get a texture descriptor tdp = TPLGet( buffer, num ); // get texture format fmt = (u32)tdp->textureHeader->format; // get image size to->width = tdp->textureHeader->width; to->height = tdp->textureHeader->height; // init texture objects GXInitTexObj( &to->tobj, tdp->textureHeader->data, tdp->textureHeader->width, tdp->textureHeader->height, (GXTexFmt)fmt, tdp->textureHeader->wrapS, // s tdp->textureHeader->wrapT, // t GX_FALSE ); // Mipmap } /*---------------------------------------------------------------------------* Name: DrawInit Description: Initializes the vertex attribute format and sets up the array pointer for the indexed data. This function also initializes scene control parameters. Arguments: sc : pointer to the structure of scene control parameters Returns: none *---------------------------------------------------------------------------*/ static void DrawInit( MySceneCtrlObj* sc ) { const GXColor WHITE = { 0xff, 0xff, 0xff, 0xff }; const GXColor ALPHA0 = { 0xff, 0xff, 0xff, 0x00 }; // Crear EFB GXSetCopyClear( WHITE, GX_MAX_Z24 ); // Vertex Attribute GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0 ); GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0 ); GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_U16, 14 ); // Array Pointers and Strides GXInvalidateVtxCache(); // stride = 3 elements (x,y,z) each of type s16 GXSetArray( GX_VA_POS, TopVerts, 3*sizeof(s16) ); // backface culling off GXSetCullMode( GX_CULL_NONE ); // color channels GXSetNumChans(1); GXSetChanCtrl( GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE ); // channel color GXSetChanAmbColor( GX_COLOR0A0, WHITE ); GXSetChanMatColor( GX_COLOR0A0, ALPHA0 ); // texcoord gens GXSetNumTexGens(1); GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY); // Indirect Textures. GXSetNumIndStages(0); GXSetTevDirect( GX_TEVSTAGE0 ); // blend mode GXSetBlendMode( GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR ); // tev setting GXSetNumTevStages( 1 ); GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0); GXSetTevOp(GX_TEVSTAGE0, GX_MODULATE); // Default scene control parameter settings // camera sc->cam.cfg = DefaultCamera; SetCamera( &sc->cam ); // never changes in this test // Init tex info sc->texFileNumber = 0; sc->texNumber = 0; // model control parameters sc->modelScale = 1.0F; MTXIdentity( sc->modelCtrl ); // Init secTime sc->startTick = (u32)OSGetTick(); sc->switchTick = sc->startTick; } /*---------------------------------------------------------------------------* Name: DrawTick Description: Draw the model by using given scene parameters Arguments: sc : pointer to the structure of scene control parameters Returns: none *---------------------------------------------------------------------------*/ static void DrawTick( MySceneCtrlObj* sc ) { Mtx ms; // Model scale matrix. Mtx mv; // Modelview matrix. // Set modelview matrix MTXConcat( sc->cam.view, sc->modelCtrl, mv ); MTXScale( ms, (u32)( sc->modelScale * sc->texture.width / 2.0 / TEX_WSCALE ), (u32)( sc->modelScale * sc->texture.height / 2.0 ), (u32)( sc->modelScale * sc->texture.height / 2.0 ) ); MTXConcat( mv, ms, mv ); GXLoadPosMtxImm( mv, GX_PNMTX0 ); // load texture obj GXLoadTexObj( &sc->texture.tobj, GX_TEXMAP0 ); // draw a box DrawTexQuad(); } /*---------------------------------------------------------------------------* Name: AnimTick Description: Changes scene parameters according to the pad status. Arguments: buffer : pointer to strap image content data sc : pointer to the structure of scene control parameters Returns: none *---------------------------------------------------------------------------*/ static void AnimTick( TPLPalettePtr *buffer, MySceneCtrlObj* sc ) { f32 alpha; u32 currentTick = (u32)OSGetTick(); u32 diffCSTime = OSTicksToMilliseconds( OSDiffTick( currentTick, sc->startTick ) ); // end of strap image frame if ( diffCSTime >= END_MS_TIME + TEX_FADE_MS_TIME ) { alpha = 0.0f; sc->startTick = currentTick; } // fade in else if ( diffCSTime <= TEX_FADE_MS_TIME ) { alpha = (f32)( diffCSTime / (f32)TEX_FADE_MS_TIME * 255.9f ); } // fade out else if ( diffCSTime > END_MS_TIME ) { alpha = (f32)( 255.9 - ( diffCSTime - END_MS_TIME ) / (f32)TEX_FADE_MS_TIME * 255.9f ); } else { alpha = 255.0f; } // channel color GXSetChanMatColor( GX_COLOR0A0, ( GXColor ){ 0xff, 0xff, 0xff, (u8)alpha } ); // Change texture by Time count if ( OSTicksToMilliseconds( OSDiffTick( currentTick, sc->switchTick ) ) > TEX_CHANGE_MS_TIME ) { sc->texNumber = ( sc->texNumber + 1 ) % NUM_OF_FILES_IN_TPL; GetTplTexture( *buffer, &sc->texture, sc->texNumber ); sc->switchTick = currentTick; } // Change tpl file (Change Language) if ( DEMOPadGetButtonDown(0) & PAD_BUTTON_A ) { // free buffer MEMFreeToAllocator( &DemoAllocator1, *buffer ); // load content data sc->texFileNumber = ( sc->texFileNumber + 1 ) % NUM_OF_FILES_IN_CONTENT; LoadSIContentData( buffer, sc->texFileNumber ); sc->texNumber = 0; // get tpl texture GetTplTexture( *buffer, &sc->texture, sc->texNumber ); sc->startTick = currentTick; } } /*---------------------------------------------------------------------------* Name: VertexT Description: Draw a vertex with texture, direct data Arguments: v 8-bit position index s, t 16-bit tex coord Returns: none *---------------------------------------------------------------------------*/ static inline void VertexT( u8 v, u32 s, u32 t) { GXPosition1x8( v ); GXColor1x8( 0 ); GXTexCoord2u16( (u16)s, (u16)t ); } /*---------------------------------------------------------------------------* Name: DrawQuad Description: Draw a textured quad. Map extends to corners of the quad. MAX_SCOORD is the value of 1.0 in the fixed point format. Arguments: v0 8-bit position v1 8-bit position v2 8-bit position v3 8-bit position s0 s tex coord at v0 t0 t tex coord at v0 Returns: none *---------------------------------------------------------------------------*/ static inline void DrawQuad( u8 v0, u8 v1, u8 v2, u8 v3, u32 s0, u32 t0) { VertexT( v3, s0, t0+MAX_TCOORD ); VertexT( v2, s0+MAX_SCOORD, t0+MAX_TCOORD ); VertexT( v1, s0+MAX_SCOORD, t0 ); VertexT( v0, s0, t0 ); } /*---------------------------------------------------------------------------* Name: DrawTexQuad Description: Draw a tex quad model. Arguments: none Returns: none *---------------------------------------------------------------------------*/ static void DrawTexQuad( void ) { // set vertex descriptor GXClearVtxDesc(); GXSetVtxDesc( GX_VA_POS, GX_INDEX8 ); GXSetVtxDesc( GX_VA_CLR0, GX_INDEX8 ); GXSetVtxDesc( GX_VA_TEX0, GX_DIRECT ); // draw the QUAD GXBegin( GX_QUADS, GX_VTXFMT0, 4 ); DrawQuad( 0, 1, 5, 4, 0, 0 ); GXEnd(); } /*---------------------------------------------------------------------------* Name: SetCamera Description: set view matrix and load projection matrix into hardware Arguments: cam : pointer to the MyCameraObj structure Returns: none *---------------------------------------------------------------------------*/ static void SetCamera( MyCameraObj* cam ) { MTXLookAt( cam->view, &cam->cfg.location, &cam->cfg.up, &cam->cfg.target ); MTXOrtho( cam->proj, cam->cfg.top, - (cam->cfg.top), cam->cfg.left, - (cam->cfg.left), cam->cfg.znear, cam->cfg.zfar ); GXSetProjection(cam->proj, GX_ORTHOGRAPHIC); } /*---------------------------------------------------------------------------* Name: PrintIntro Description: Prints the directions on how to use this demo. Arguments: none Returns: none *---------------------------------------------------------------------------*/ void PrintIntro(void) { OSReport("******************************************************\n"); OSReport("Instructions:\n"); OSReport(" A Button : change the language of Strap Image\n"); OSReport(" Start : end.\n"); OSReport("******************************************************\n"); } /*============================================================================*/