1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution cx demo
3   File:     cx_uncompress.c
4 
5   Copyright 1998- 2006 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 /*---------------------------------------------------------------------------*
14     cx_uncompress
15         Data uncompress demo
16  *---------------------------------------------------------------------------*/
17 
18 #include <demo.h>
19 #include <revolution/cx.h>
20 
21 /*---------------------------------------------------------------------------*
22    Structure definitions
23  *---------------------------------------------------------------------------*/
24 // For camera
25 typedef struct
26 {
27     Vec    location;
28     Vec    up;
29     Vec    target;
30     f32    left;
31     f32    top;
32     f32    znear;
33     f32    zfar;
34 } CameraConfig;
35 
36 typedef struct
37 {
38     CameraConfig  cfg;
39     Mtx           view;
40     Mtx44         proj;
41 } MyCameraObj;
42 
43 // For entire scene control
44 typedef struct
45 {
46     MyCameraObj   cam;
47     u32           texNum[ 4 ];
48 } MySceneCtrlObj;
49 
50 
51 /*---------------------------------------------------------------------------*
52    Forward references
53  *---------------------------------------------------------------------------*/
54 void        main      ( void  );
55 static void DrawInit  ( MySceneCtrlObj* sc );
56 static void DrawTick  ( MySceneCtrlObj* sc );
57 static void AnimTick  ( MySceneCtrlObj* sc );
58 static void SetCamera ( MyCameraObj*   cam );
59 static void PrintIntro( void );
60 static void DrawRect  ( void );
61 
62 static void* LoadTexData    ( const char* path );
63 static void* LoadTexDataRL  ( const char* path );
64 static void* LoadTexDataLZ  ( const char* path );
65 static void* LoadTexDataHuff( const char* path );
66 
67 
68 /*---------------------------------------------------------------------------*
69    Camera configuration
70  *---------------------------------------------------------------------------*/
71 static CameraConfig DEFAULT_CAMERA =
72 {
73     { 0.0F, 0.0F, 100.0F }, // location
74     { 0.0F, 1.0F,   0.0F }, // up
75     { 0.0F, 0.0F,   0.0F }, // target
76     -320.0F,                // left
77     -224.0F,                // top  (note: was 240, now adjusted for over scan)
78     0.1F,                   // near
79     2000.0F                 // far
80 };
81 
82 #if !defined(RVL_OS)
83 // Replacing Dolphin-SDK
TEXBind(TEXPalettePtr pal)84 static void TEXBind( TEXPalettePtr pal )
85 {
86     u16 i;
87 
88     if(pal->versionNumber != 2142000 )
89         OSHalt("invalid version number for texture palette");
90 
91     pal->descriptorArray = (TEXDescriptorPtr)(((u32)(pal->descriptorArray)) + ((u32)pal));
92 
93     for ( i = 0; i < pal->numDescriptors; i++ )
94     {
95         if(pal->descriptorArray[i].textureHeader)
96         {
97             pal->descriptorArray[i].textureHeader = (TEXHeaderPtr)(((u32)(pal->descriptorArray[i].textureHeader)) + ((u32)pal));
98             if(!(pal->descriptorArray[i].textureHeader->unpacked))
99             {
100                 pal->descriptorArray[i].textureHeader->data = (Ptr)((u32)(pal->descriptorArray[i].textureHeader->data) + (u32)pal);
101                 pal->descriptorArray[i].textureHeader->unpacked = 1;
102             }
103         }
104         if(pal->descriptorArray[i].CLUTHeader)
105         {
106             pal->descriptorArray[i].CLUTHeader = (CLUTHeaderPtr)((u32)(pal->descriptorArray[i].CLUTHeader) + (u32)pal);
107             if(!(pal->descriptorArray[i].CLUTHeader->unpacked))
108             {
109                 pal->descriptorArray[i].CLUTHeader->data = (Ptr)((u32)(pal->descriptorArray[i].CLUTHeader->data) + (u32)pal);
110                 pal->descriptorArray[i].CLUTHeader->unpacked = 1;
111             }
112         }
113     }
114 }
115 
116 #define TPLPalettePtr       TEXPalettePtr
117 #define TPLDescriptorPtr    TEXDescriptorPtr
118 #define TPLGet              TEXGet
119 #define TPLBind             TEXBind
120 
121 #endif /* !defined(RVL_OS) */
122 
DEMOAlloc(u32 size)123 static inline void* DEMOAlloc( u32 size )
124 {
125 #ifdef RVL_OS
126     return MEMAllocFromAllocator( &DemoAllocator1, size );
127 #else
128     return OSAlloc( size );
129 #endif
130 }
131 
DEMOFree(void * mem)132 static inline void DEMOFree( void* mem )
133 {
134 #ifdef RVL_OS
135     MEMFreeToAllocator( &DemoAllocator1, mem );
136 #else
137     OSFree( mem );
138 #endif
139 }
140 
141 
142 /*---------------------------------------------------------------------------*
143    Data for views and parameters
144  *---------------------------------------------------------------------------*/
145 
146 /*---------------------------------------------------------------------------*
147    Global variables
148  *---------------------------------------------------------------------------*/
149 static TPLPalettePtr    sMyTplObj[ 4 ] = { NULL, NULL, NULL, NULL }; // A pointer to the texture/palette data
150 
151 
152 /*---------------------------------------------------------------------------*
153    Application main loop
154  *---------------------------------------------------------------------------*/
main(void)155 void main( void )
156 {
157     MySceneCtrlObj  sceneCtrl;
158 
159     DEMOInit( NULL );       // OS, Pad, GX and VI initialization
160 
161     PrintIntro();
162 
163     DrawInit( &sceneCtrl ); // Camera initialization and loading of texture data from optical disc
164                             // Compressed data is decompressed after loading
165 
166     while ( !(DEMOPadGetButton( 0 ) & PAD_BUTTON_MENU) )
167     {
168         DEMOBeforeRender();
169         DrawTick( &sceneCtrl );  // Render process for every frame
170         DEMODoneRender();
171         DEMOPadRead();
172         AnimTick( &sceneCtrl );  // Pad input process for each frame
173     }
174 
175     OSHalt("End of demo");
176 }
177 
178 
179 /*---------------------------------------------------------------------------*
180     Name:           SetCamera
181 
182     Description:    Calculates and sets the view and projection matrices.
183 
184     Arguments:      cam : A pointer to MyCameraObj
185 
186     Returns:        None.
187  *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)188 static void SetCamera( MyCameraObj* cam )
189 {
190     MTXLookAt( cam->view,
191                &cam->cfg.location,
192                &cam->cfg.up,
193                &cam->cfg.target );
194 
195     MTXOrtho( cam->proj,
196               cam->cfg.top,
197               -(cam->cfg.top),
198               cam->cfg.left,
199               -(cam->cfg.left),
200               cam->cfg.znear,
201               cam->cfg.zfar );
202 
203     GXSetProjection( cam->proj, GX_ORTHOGRAPHIC );
204 }
205 
206 
207 
208 
209 /*---------------------------------------------------------------------------*
210   Name:         LoadTexData
211 
212   Description:  Loads non-compressed texture/palette data from the DVD
213 
214   Arguments:    path    the path to the texture/palette file on the DVD
215 
216   Returns:      Returns the pointer to the loaded data.
217  *---------------------------------------------------------------------------*/
LoadTexData(const char * path)218 static void* LoadTexData( const char* path )
219 {
220     DVDFileInfo fileInfo;
221     void* buf;
222 
223     DVDOpen( path, &fileInfo );
224 
225     buf = DEMOAlloc( OSRoundUp32B( fileInfo.length ) );
226     DVDRead( &fileInfo, buf, (s32)OSRoundUp32B( fileInfo.length ), 0 );
227     DVDClose( &fileInfo );
228 
229     return buf;
230 }
231 
232 
233 /*---------------------------------------------------------------------------*
234   Name:         LoadTexDataRL
235 
236   Description:  Loads run-length compressed texture/palette data from the DVD and decompresses it after loading.
237 
238 
239   Arguments:    path    the path to the texture/palette file on the DVD
240 
241   Returns:      Returns the pointer to the decompressed data.
242  *---------------------------------------------------------------------------*/
LoadTexDataRL(const char * path)243 static void* LoadTexDataRL( const char* path )
244 {
245     void* buf;
246     void* uncompData;
247     u32   uncompSize;
248 
249     // Load data from the DVD
250     buf = LoadTexData( path );
251 
252     // Allocate the buffer for decompression
253     uncompSize = CXGetUncompressedSize( buf );
254     uncompData = DEMOAlloc( uncompSize );
255 
256     // Decompress the data
257     CXUncompressRL( buf, uncompData );
258     DCFlushRange( uncompData, uncompSize );
259 
260     // Deallocate the region used before the decompression
261     DEMOFree( buf );
262 
263     return uncompData;
264 }
265 
266 
267 /*---------------------------------------------------------------------------*
268   Name:         LoadTexDataLZ
269 
270   Description:  Loads LZ77 compressed texture/palette data from the DVD and decompresses it after loading.
271 
272 
273   Arguments:    path    the path to the texture/palette file on the DVD
274 
275   Returns:      Returns the pointer to the decompressed data.
276  *---------------------------------------------------------------------------*/
LoadTexDataLZ(const char * path)277 static void* LoadTexDataLZ( const char* path )
278 {
279     void* buf;
280     void* uncompData;
281     u32   uncompSize;
282 
283     // Load data from the DVD
284     buf = LoadTexData( path );
285 
286     // Allocate the buffer for decompression
287     uncompSize = CXGetUncompressedSize( buf );
288     uncompData = DEMOAlloc( uncompSize );
289 
290     // Decompress the data
291     CXUncompressLZ( buf, uncompData );
292     DCFlushRange( uncompData, uncompSize );
293 
294     // Deallocate the region used before the decompression
295     DEMOFree( buf );
296 
297     return uncompData;
298 }
299 
300 
301 /*---------------------------------------------------------------------------*
302   Name:         LoadTexDataHuff
303 
304   Description:  Loads Huffman compressed texture/palette data from the DVD and decompresses it after it is loaded.
305 
306 
307   Arguments:    path    the path to the texture/palette file on the DVD
308 
309   Returns:      Returns the pointer to the decompressed data.
310  *---------------------------------------------------------------------------*/
LoadTexDataHuff(const char * path)311 static void* LoadTexDataHuff( const char* path )
312 {
313     void* buf;
314     void* uncompData;
315     u32   uncompSize;
316 
317     // Load data from the DVD
318     buf = LoadTexData( path );
319 
320     // Allocate the buffer for decompression
321     uncompSize = CXGetUncompressedSize( buf );
322     uncompData = DEMOAlloc( uncompSize );
323 
324     // Decompress the data
325     CXUncompressHuffman( buf, uncompData );
326     DCFlushRange( uncompData, uncompSize );
327 
328     // Deallocate the region used before the decompression
329     DEMOFree( buf );
330 
331     return uncompData;
332 }
333 
334 
335 /*---------------------------------------------------------------------------*
336    Functions
337  *---------------------------------------------------------------------------*/
338 
339 /*---------------------------------------------------------------------------*
340     Name:           DrawInit
341 
342     Description:    Initializes camera and loads and decompresses texture/palette data from DVD
343 
344 
345     Arguments:      sc : a pointer to the scene parameter
346 
347     Returns:        None.
348  *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)349 static void DrawInit( MySceneCtrlObj* sc )
350 {
351     u32 i;
352     for ( i = 0; i < 4; i++ )
353     {
354         sc->texNum[ i ] = 0;
355     }
356 
357     // Load TPL file
358     sMyTplObj[ 0 ] = (TPLPalettePtr)LoadTexData    ( "/cxdemo/tex-01.tpl"      );
359     sMyTplObj[ 1 ] = (TPLPalettePtr)LoadTexDataRL  ( "/cxdemo/tex-01_RL.bin"   );
360     sMyTplObj[ 2 ] = (TPLPalettePtr)LoadTexDataLZ  ( "/cxdemo/tex-01_LZ.bin"   );
361     sMyTplObj[ 3 ] = (TPLPalettePtr)LoadTexDataHuff( "/cxdemo/tex-01_Huff.bin" );
362 
363     // Bind the TPL object
364     TPLBind( sMyTplObj[ 0 ] );
365     TPLBind( sMyTplObj[ 1 ] );
366     TPLBind( sMyTplObj[ 2 ] );
367     TPLBind( sMyTplObj[ 3 ] );
368 
369     // Camera initialization
370     sc->cam.cfg = DEFAULT_CAMERA;
371     SetCamera( &sc->cam );   // Never changes in this test
372 
373     // Sets the view matrix
374     GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 );
375 }
376 
377 
378 /*---------------------------------------------------------------------------*
379     Name:           DrawTick
380 
381     Description:    Render process for every frame
382 
383     Arguments:      sc : a pointer to the scene parameter
384 
385     Returns:        None.
386  *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)387 static void DrawTick( MySceneCtrlObj* sc )
388 {
389     TPLDescriptorPtr tdp;
390     GXTexObj         texObj;
391     s32              i;
392 
393     SetCamera( &sc->cam );
394     GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 );
395 
396     for ( i = 0; i < 4; i++ )
397     {
398         {
399             // Sets the texture index specified from the TPL object
400             tdp = TPLGet( sMyTplObj[ i ], sc->texNum[ i ] );
401 
402             GXInitTexObj(
403                     &texObj,
404                     tdp->textureHeader->data,
405                     tdp->textureHeader->width,
406                     tdp->textureHeader->height,
407                     (GXTexFmt)tdp->textureHeader->format,
408                     tdp->textureHeader->wrapS,
409                     tdp->textureHeader->wrapT,
410                     GX_FALSE );
411 
412             // Tev, TexGen and Zmode
413             GXSetTevOp      ( GX_TEVSTAGE0, GX_REPLACE );
414             GXSetTevOrder   ( GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL );
415             GXSetTexCoordGen( GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY   );
416             GXSetZMode      ( GX_ENABLE, GX_LEQUAL, GX_ENABLE );
417             GXSetNumTexGens( 1 );
418 
419             GXSetCurrentMtx( GX_PNMTX0 );
420             GXLoadTexObj( &texObj, GX_TEXMAP0 );
421         }
422 
423         {
424             // Renders after applying Scale and Trans
425             Mtx mv;
426             Mtx m;
427 
428             f32 transX = (i % 2)? -112 : 112;
429             f32 transY = (i / 2)? -112 : 112;
430 
431             MTXScale( m, 0.5f, 0.5f, 0.5f );
432             MTXConcat( m, sc->cam.view, mv );
433             MTXTrans( m, transX, transY, 0 );
434             MTXConcat( m, mv, mv );
435 
436             GXLoadPosMtxImm( mv, GX_PNMTX0 );
437             DrawRect();
438         }
439     }
440 
441 
442     DEMOInitCaption(
443         DM_FT_OPQ,
444         320,
445         224 );
446 
447     DEMOPrintf(48,  0,   0, "Raw");
448     DEMOPrintf(160, 0,   0, "RunLength");
449     DEMOPrintf(48,  112, 0, "LZ77");
450     DEMOPrintf(160, 112, 0, "Huffman");
451 }
452 
453 
454 
455 /*---------------------------------------------------------------------------*
456     Name:           AnimTick
457 
458     Description:    Pad process for every frame
459 
460     Arguments:      sc : a pointer to the scene parameter
461 
462     Returns:        none
463  *---------------------------------------------------------------------------*/
AnimTick(MySceneCtrlObj * sc)464 static void AnimTick  ( MySceneCtrlObj* sc )
465 {
466     if ( DEMOPadGetButtonDown( 0 ) & PAD_BUTTON_A )
467     {
468         s32 i;
469         for ( i = 0; i < 4; i++ )
470         {
471             if ( ++sc->texNum[ i ] >= sMyTplObj[ i ]->numDescriptors )
472             {
473                 sc->texNum[ i ] = 0;
474             }
475         }
476     }
477 }
478 
479 
480 /*---------------------------------------------------------------------------*
481     Name:           DrawRect
482 
483     Description:    Renders a rectangle with a texture.
484 
485     Arguments:      none
486 
487     Returns:        none
488  *---------------------------------------------------------------------------*/
DrawRect(void)489 static void DrawRect( void )
490 {
491     const s16 VERTEX_POS = 224;
492 
493     //   Vertex Attribute
494     // Vertex Attribute ( VTXFMT0 is used by DEMOPuts lib.)
495     GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_POS,  GX_POS_XY,  GX_S16, 0 );
496     GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST,  GX_S16, 0 );
497 
498     //  Array Pointers and Strides
499     GXInvalidateVtxCache();
500 
501     // Set vertex descriptor
502     GXClearVtxDesc();
503     GXSetVtxDesc( GX_VA_POS,  GX_DIRECT );
504     GXSetVtxDesc( GX_VA_TEX0, GX_DIRECT );
505 
506     // Draw a cube
507     GXBegin( GX_QUADS, GX_VTXFMT1, 4 );
508 
509     GXPosition2s16( -VERTEX_POS, -VERTEX_POS  );
510     GXTexCoord2s16( 0, 0 );
511 
512     GXPosition2s16( VERTEX_POS, -VERTEX_POS  );
513     GXTexCoord2s16( 1, 0 );
514 
515     GXPosition2s16( VERTEX_POS, VERTEX_POS  );
516     GXTexCoord2s16( 1, 1 );
517 
518     GXPosition2s16( -VERTEX_POS, VERTEX_POS  );
519     GXTexCoord2s16( 0, 1 );
520 
521     GXEnd();
522 }
523 
524 
525 /*---------------------------------------------------------------------------*
526     Name:           PrintIntro
527 
528     Description:    Prints the directions on how to use this demo.
529 
530     Arguments:      none
531 
532     Returns:        none
533  *---------------------------------------------------------------------------*/
PrintIntro(void)534 static void PrintIntro( void )
535 {
536     OSReport("\n\n");
537     OSReport("************************************************\n");
538     OSReport("cx_uncompress_stream: Uncompression demo\n");
539     OSReport("************************************************\n");
540     OSReport("to quit hit the start button\n");
541     OSReport("\n");
542     OSReport("A button     : change texture index to next\n");
543     OSReport("************************************************\n\n");
544 }
545 
546 /*============================================================================*/
547