/*---------------------------------------------------------------------------* Project: Dolphin/Revolution cx demo File: cx_uncompress.c Copyright 1998-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. *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* cx_uncompress data uncompress demo *---------------------------------------------------------------------------*/ #include #include /*---------------------------------------------------------------------------* 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 entire scene control typedef struct { MyCameraObj cam; u32 texNum[ 4 ]; } MySceneCtrlObj; /*---------------------------------------------------------------------------* Forward references *---------------------------------------------------------------------------*/ void main ( void ); static void DrawInit ( MySceneCtrlObj* sc ); static void DrawTick ( MySceneCtrlObj* sc ); static void AnimTick ( MySceneCtrlObj* sc ); static void SetCamera ( MyCameraObj* cam ); static void PrintIntro( void ); static void DrawRect ( void ); static void* LoadTexData ( const char* path ); static void* LoadTexDataRL ( const char* path ); static void* LoadTexDataLZ ( const char* path ); static void* LoadTexDataHuff( const char* path ); /*---------------------------------------------------------------------------* Camera configuration *---------------------------------------------------------------------------*/ static CameraConfig DEFAULT_CAMERA = { { 0.0F, 0.0F, 100.0F }, // location { 0.0F, 1.0F, 0.0F }, // up : { 0.0F, 0.0F, 0.0F }, // target -320.0F, // left -224.0F, // top (note: was 240, now adjusted for over scan) 0.1F, // near 2000.0F // far }; /*---------------------------------------------------------------------------* Data for views and parameters *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Global variables *---------------------------------------------------------------------------*/ static TPLPalettePtr sMyTplObj[ 4 ] = { NULL, NULL, NULL, NULL }; // A pointer to the texture/palette data. /*---------------------------------------------------------------------------* Application main loop *---------------------------------------------------------------------------*/ void main( void ) { MySceneCtrlObj sceneCtrl; DEMOInit( NULL ); // OS, Pad, GX and VI initialization PrintIntro(); DrawInit( &sceneCtrl ); // camera initialization and loading of texture pattern from DVD // compressed data is decompressed after loading while ( !(DEMOPadGetButton( 0 ) & PAD_BUTTON_MENU) ) { DEMOBeforeRender(); DrawTick( &sceneCtrl ); // render process for each frame DEMODoneRender(); DEMOPadRead(); AnimTick( &sceneCtrl ); // pad input process for each frame } OSHalt("End of demo"); } /*---------------------------------------------------------------------------* Name: SetCamera Description: Calculates and sets the view and projection matrices. Arguments: cam : a pointer to MyCameraObj 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: LoadTexData Description: loads non-compressed texture/palette data from the DVD Arguments: path: the path to the texture/palette file on the DVD Returns: Returns the pointer to the loaded data. *---------------------------------------------------------------------------*/ static void* LoadTexData( const char* path ) { DVDFileInfo fileInfo; void* buf; DVDOpen( path, &fileInfo ); buf = MEMAllocFromAllocator( &DemoAllocator1, OSRoundUp32B( fileInfo.length ) ); DVDRead( &fileInfo, buf, (s32)OSRoundUp32B( fileInfo.length ), 0 ); DVDClose( &fileInfo ); return buf; } /*---------------------------------------------------------------------------* Name: LoadTexDataRL Description: Loads texture/palette data from the DVD that is run-length compressed and then decompresses it. Arguments: path: the path to the texture/palette file on the DVD Returns: Returns the pointer to the decompressed data. *---------------------------------------------------------------------------*/ static void* LoadTexDataRL( const char* path ) { void* buf; void* uncompData; u32 uncompSize; // load data from the DVD buf = LoadTexData( path ); // allocate the buffer for decompression uncompSize = CXGetUncompressedSize( buf ); uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize ); // decompress the data CXUncompressRL( buf, uncompData ); DCFlushRange( uncompData, uncompSize ); // deallocate the region ahead of decompression MEMFreeToAllocator( &DemoAllocator1, buf ); return uncompData; } /*---------------------------------------------------------------------------* Name: LoadTexDataLZ Description: Loads texture/palette data from the DVD that is LZ77 compressed and then decompresses it. Arguments: path: the path to the texture/palette file on the DVD Returns: Returns the pointer to the decompressed data. *---------------------------------------------------------------------------*/ static void* LoadTexDataLZ( const char* path ) { void* buf; void* uncompData; u32 uncompSize; // load data from the DVD buf = LoadTexData( path ); // allocate the buffer for decompression uncompSize = CXGetUncompressedSize( buf ); uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize ); // decompress the data CXUncompressLZ( buf, uncompData ); DCFlushRange( uncompData, uncompSize ); // deallocate the region ahead of decompression MEMFreeToAllocator( &DemoAllocator1, buf ); return uncompData; } /*---------------------------------------------------------------------------* Name: LoadTexDataHuff Description: Loads texture/palette data from the DVD that is Huffman compressed and then decompresses it. Arguments: path: the path to the texture/palette file on the DVD Returns: Returns the pointer to the decompressed data. *---------------------------------------------------------------------------*/ static void* LoadTexDataHuff( const char* path ) { void* buf; void* uncompData; u32 uncompSize; // load data from the DVD buf = LoadTexData( path ); // allocate the buffer for decompression uncompSize = CXGetUncompressedSize( buf ); uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize ); // decompress the data CXUncompressHuffman( buf, uncompData ); DCFlushRange( uncompData, uncompSize ); // deallocate the region ahead of decompression MEMFreeToAllocator( &DemoAllocator1, buf ); return uncompData; } /*---------------------------------------------------------------------------* Functions *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Name: DrawInit Description: camera initialization and loading and decompressing of texture and palette data from the DVD Arguments: sc : a pointer to the scene parameter Returns: None. *---------------------------------------------------------------------------*/ static void DrawInit( MySceneCtrlObj* sc ) { u32 i; for ( i = 0; i < 4; i++ ) { sc->texNum[ i ] = 0; } // Load TPL file sMyTplObj[ 0 ] = (TPLPalettePtr)LoadTexData ( "/cxdemo/tex-01.tpl" ); sMyTplObj[ 1 ] = (TPLPalettePtr)LoadTexDataRL ( "/cxdemo/tex-01_RL.bin" ); sMyTplObj[ 2 ] = (TPLPalettePtr)LoadTexDataLZ ( "/cxdemo/tex-01_LZ.bin" ); sMyTplObj[ 3 ] = (TPLPalettePtr)LoadTexDataHuff( "/cxdemo/tex-01_Huff.bin" ); // bind the TPL object TPLBind( sMyTplObj[ 0 ] ); TPLBind( sMyTplObj[ 1 ] ); TPLBind( sMyTplObj[ 2 ] ); TPLBind( sMyTplObj[ 3 ] ); // camera initialization sc->cam.cfg = DEFAULT_CAMERA; SetCamera( &sc->cam ); // never changes in this test // Sets the view matrix. GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 ); } /*---------------------------------------------------------------------------* Name: DrawTick Description: render process for each frame Arguments: sc : a pointer to the scene parameter Returns: None. *---------------------------------------------------------------------------*/ static void DrawTick( MySceneCtrlObj* sc ) { TPLDescriptorPtr tdp; GXTexObj texObj; s32 i; SetCamera( &sc->cam ); GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 ); for ( i = 0; i < 4; i++ ) { { // sets the texture index specified from the TPL object tdp = TPLGet( sMyTplObj[ i ], sc->texNum[ i ] ); GXInitTexObj( &texObj, tdp->textureHeader->data, tdp->textureHeader->width, tdp->textureHeader->height, (GXTexFmt)tdp->textureHeader->format, tdp->textureHeader->wrapS, tdp->textureHeader->wrapT, GX_FALSE ); // Tev, TexGen and Zmode GXSetTevOp ( GX_TEVSTAGE0, GX_REPLACE ); GXSetTevOrder ( GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL ); GXSetTexCoordGen( GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY ); GXSetZMode ( GX_ENABLE, GX_LEQUAL, GX_ENABLE ); GXSetNumTexGens( 1 ); GXSetCurrentMtx( GX_PNMTX0 ); GXLoadTexObj( &texObj, GX_TEXMAP0 ); } { // renders after applying Scale and Trans Mtx mv; Mtx m; f32 transX = (i % 2)? -112 : 112; f32 transY = (i / 2)? -112 : 112; MTXScale( m, 0.5f, 0.5f, 0.5f ); MTXConcat( m, sc->cam.view, mv ); MTXTrans( m, transX, transY, 0 ); MTXConcat( m, mv, mv ); GXLoadPosMtxImm( mv, GX_PNMTX0 ); DrawRect(); } } DEMOInitCaption( DM_FT_OPQ, 320, 224 ); DEMOPrintf(48, 0, 0, "Raw"); DEMOPrintf(160, 0, 0, "RunLength"); DEMOPrintf(48, 112, 0, "LZ77"); DEMOPrintf(160, 112, 0, "Huffman"); } /*---------------------------------------------------------------------------* Name: AnimTick Description: pad process for each frame Arguments: sc : a pointer to the scene parameter Returns: none *---------------------------------------------------------------------------*/ static void AnimTick ( MySceneCtrlObj* sc ) { if ( DEMOPadGetButtonDown( 0 ) & PAD_BUTTON_A ) { s32 i; for ( i = 0; i < 4; i++ ) { if ( ++sc->texNum[ i ] >= sMyTplObj[ i ]->numDescriptors ) { sc->texNum[ i ] = 0; } } } } /*---------------------------------------------------------------------------* Name: DrawRect Description: Renders a textured rectangle. Arguments: none Returns: none *---------------------------------------------------------------------------*/ static void DrawRect( void ) { const s16 VERTEX_POS = 224; // Vertex Attribute // Vertex Attribute ( VTXFMT0 is used by DEMOPuts lib.) GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_POS, GX_POS_XY, GX_S16, 0 ); GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0 ); // Array Pointers and Strides GXInvalidateVtxCache(); // set vertex descriptor GXClearVtxDesc(); GXSetVtxDesc( GX_VA_POS, GX_DIRECT ); GXSetVtxDesc( GX_VA_TEX0, GX_DIRECT ); // draw a cube GXBegin( GX_QUADS, GX_VTXFMT1, 4 ); GXPosition2s16( -VERTEX_POS, -VERTEX_POS ); GXTexCoord2s16( 0, 0 ); GXPosition2s16( VERTEX_POS, -VERTEX_POS ); GXTexCoord2s16( 1, 0 ); GXPosition2s16( VERTEX_POS, VERTEX_POS ); GXTexCoord2s16( 1, 1 ); GXPosition2s16( -VERTEX_POS, VERTEX_POS ); GXTexCoord2s16( 0, 1 ); GXEnd(); } /*---------------------------------------------------------------------------* Name: PrintIntro Description: Prints the directions on how to use this demo. Arguments: none Returns: none *---------------------------------------------------------------------------*/ static void PrintIntro( void ) { OSReport("\n\n"); OSReport("************************************************\n"); OSReport("cx_uncompress_stream: Uncompression demo\n"); OSReport("************************************************\n"); OSReport("to quit hit the start button\n"); OSReport("\n"); OSReport("A button : change texture index to next\n"); OSReport("************************************************\n\n"); } /*============================================================================*/