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
83 /*---------------------------------------------------------------------------*
84 Data for views and parameters
85 *---------------------------------------------------------------------------*/
86
87 /*---------------------------------------------------------------------------*
88 Global variables
89 *---------------------------------------------------------------------------*/
90 static TPLPalettePtr sMyTplObj[ 4 ] = { NULL, NULL, NULL, NULL }; // A pointer to the texture/palette data.
91
92
93 /*---------------------------------------------------------------------------*
94 Application main loop
95 *---------------------------------------------------------------------------*/
main(void)96 void main( void )
97 {
98 MySceneCtrlObj sceneCtrl;
99
100 DEMOInit( NULL ); // OS, Pad, GX and VI initialization
101
102 PrintIntro();
103
104 DrawInit( &sceneCtrl ); // camera initialization and loading of texture pattern from DVD
105 // compressed data is decompressed after loading
106
107 while ( !(DEMOPadGetButton( 0 ) & PAD_BUTTON_MENU) )
108 {
109 DEMOBeforeRender();
110 DrawTick( &sceneCtrl ); // render process for each frame
111 DEMODoneRender();
112 DEMOPadRead();
113 AnimTick( &sceneCtrl ); // pad input process for each frame
114 }
115
116 OSHalt("End of demo");
117 }
118
119
120 /*---------------------------------------------------------------------------*
121 Name: SetCamera
122
123 Description: Calculates and sets the view and projection matrices.
124
125 Arguments: cam : a pointer to MyCameraObj
126
127 Returns: None.
128 *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)129 static void SetCamera( MyCameraObj* cam )
130 {
131 MTXLookAt( cam->view,
132 &cam->cfg.location,
133 &cam->cfg.up,
134 &cam->cfg.target );
135
136 MTXOrtho( cam->proj,
137 cam->cfg.top,
138 -(cam->cfg.top),
139 cam->cfg.left,
140 -(cam->cfg.left),
141 cam->cfg.znear,
142 cam->cfg.zfar );
143
144 GXSetProjection( cam->proj, GX_ORTHOGRAPHIC );
145 }
146
147
148
149
150 /*---------------------------------------------------------------------------*
151 Name: LoadTexData
152
153 Description: loads non-compressed texture/palette data from the DVD
154
155 Arguments: path: the path to the texture/palette file on the DVD
156
157 Returns: Returns the pointer to the loaded data.
158 *---------------------------------------------------------------------------*/
LoadTexData(const char * path)159 static void* LoadTexData( const char* path )
160 {
161 DVDFileInfo fileInfo;
162 void* buf;
163
164 DVDOpen( path, &fileInfo );
165
166 buf = MEMAllocFromAllocator( &DemoAllocator1, OSRoundUp32B( fileInfo.length ) );
167 DVDRead( &fileInfo, buf, (s32)OSRoundUp32B( fileInfo.length ), 0 );
168 DVDClose( &fileInfo );
169
170 return buf;
171 }
172
173
174 /*---------------------------------------------------------------------------*
175 Name: LoadTexDataRL
176
177 Description: Loads texture/palette data from the DVD that is run-length compressed and then decompresses it.
178
179
180 Arguments: path: the path to the texture/palette file on the DVD
181
182 Returns: Returns the pointer to the decompressed data.
183 *---------------------------------------------------------------------------*/
LoadTexDataRL(const char * path)184 static void* LoadTexDataRL( const char* path )
185 {
186 void* buf;
187 void* uncompData;
188 u32 uncompSize;
189
190 // load data from the DVD
191 buf = LoadTexData( path );
192
193 // allocate the buffer for decompression
194 uncompSize = CXGetUncompressedSize( buf );
195 uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize );
196
197 // decompress the data
198 CXUncompressRL( buf, uncompData );
199 DCFlushRange( uncompData, uncompSize );
200
201 // deallocate the region ahead of decompression
202 MEMFreeToAllocator( &DemoAllocator1, buf );
203
204 return uncompData;
205 }
206
207
208 /*---------------------------------------------------------------------------*
209 Name: LoadTexDataLZ
210
211 Description: Loads texture/palette data from the DVD that is LZ77 compressed and then decompresses it.
212
213
214 Arguments: path: the path to the texture/palette file on the DVD
215
216 Returns: Returns the pointer to the decompressed data.
217 *---------------------------------------------------------------------------*/
LoadTexDataLZ(const char * path)218 static void* LoadTexDataLZ( const char* path )
219 {
220 void* buf;
221 void* uncompData;
222 u32 uncompSize;
223
224 // load data from the DVD
225 buf = LoadTexData( path );
226
227 // allocate the buffer for decompression
228 uncompSize = CXGetUncompressedSize( buf );
229 uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize );
230
231 // decompress the data
232 CXUncompressLZ( buf, uncompData );
233 DCFlushRange( uncompData, uncompSize );
234
235 // deallocate the region ahead of decompression
236 MEMFreeToAllocator( &DemoAllocator1, buf );
237
238 return uncompData;
239 }
240
241
242 /*---------------------------------------------------------------------------*
243 Name: LoadTexDataHuff
244
245 Description: Loads texture/palette data from the DVD that is Huffman compressed and then decompresses it.
246
247
248 Arguments: path: the path to the texture/palette file on the DVD
249
250 Returns: Returns the pointer to the decompressed data.
251 *---------------------------------------------------------------------------*/
LoadTexDataHuff(const char * path)252 static void* LoadTexDataHuff( const char* path )
253 {
254 void* buf;
255 void* uncompData;
256 u32 uncompSize;
257
258 // load data from the DVD
259 buf = LoadTexData( path );
260
261 // allocate the buffer for decompression
262 uncompSize = CXGetUncompressedSize( buf );
263 uncompData = MEMAllocFromAllocator( &DemoAllocator1, uncompSize );
264
265 // decompress the data
266 CXUncompressHuffman( buf, uncompData );
267 DCFlushRange( uncompData, uncompSize );
268
269 // deallocate the region ahead of decompression
270 MEMFreeToAllocator( &DemoAllocator1, buf );
271
272 return uncompData;
273 }
274
275
276 /*---------------------------------------------------------------------------*
277 Functions
278 *---------------------------------------------------------------------------*/
279
280 /*---------------------------------------------------------------------------*
281 Name: DrawInit
282
283 Description: camera initialization and loading and decompressing of texture and palette data from the DVD
284
285
286 Arguments: sc : a pointer to the scene parameter
287
288 Returns: None.
289 *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)290 static void DrawInit( MySceneCtrlObj* sc )
291 {
292 u32 i;
293 for ( i = 0; i < 4; i++ )
294 {
295 sc->texNum[ i ] = 0;
296 }
297
298 // Load TPL file
299 sMyTplObj[ 0 ] = (TPLPalettePtr)LoadTexData ( "/cxdemo/tex-01.tpl" );
300 sMyTplObj[ 1 ] = (TPLPalettePtr)LoadTexDataRL ( "/cxdemo/tex-01_RL.bin" );
301 sMyTplObj[ 2 ] = (TPLPalettePtr)LoadTexDataLZ ( "/cxdemo/tex-01_LZ.bin" );
302 sMyTplObj[ 3 ] = (TPLPalettePtr)LoadTexDataHuff( "/cxdemo/tex-01_Huff.bin" );
303
304 // bind the TPL object
305 TPLBind( sMyTplObj[ 0 ] );
306 TPLBind( sMyTplObj[ 1 ] );
307 TPLBind( sMyTplObj[ 2 ] );
308 TPLBind( sMyTplObj[ 3 ] );
309
310 // camera initialization
311 sc->cam.cfg = DEFAULT_CAMERA;
312 SetCamera( &sc->cam ); // never changes in this test
313
314 // Sets the view matrix.
315 GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 );
316 }
317
318
319 /*---------------------------------------------------------------------------*
320 Name: DrawTick
321
322 Description: render process for each frame
323
324 Arguments: sc : a pointer to the scene parameter
325
326 Returns: None.
327 *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)328 static void DrawTick( MySceneCtrlObj* sc )
329 {
330 TPLDescriptorPtr tdp;
331 GXTexObj texObj;
332 s32 i;
333
334 SetCamera( &sc->cam );
335 GXLoadPosMtxImm( sc->cam.view, GX_PNMTX0 );
336
337 for ( i = 0; i < 4; i++ )
338 {
339 {
340 // sets the texture index specified from the TPL object
341 tdp = TPLGet( sMyTplObj[ i ], sc->texNum[ i ] );
342
343 GXInitTexObj(
344 &texObj,
345 tdp->textureHeader->data,
346 tdp->textureHeader->width,
347 tdp->textureHeader->height,
348 (GXTexFmt)tdp->textureHeader->format,
349 tdp->textureHeader->wrapS,
350 tdp->textureHeader->wrapT,
351 GX_FALSE );
352
353 // Tev, TexGen and Zmode
354 GXSetTevOp ( GX_TEVSTAGE0, GX_REPLACE );
355 GXSetTevOrder ( GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL );
356 GXSetTexCoordGen( GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY );
357 GXSetZMode ( GX_ENABLE, GX_LEQUAL, GX_ENABLE );
358 GXSetNumTexGens( 1 );
359
360 GXSetCurrentMtx( GX_PNMTX0 );
361 GXLoadTexObj( &texObj, GX_TEXMAP0 );
362 }
363
364 {
365 // renders after applying Scale and Trans
366 Mtx mv;
367 Mtx m;
368
369 f32 transX = (i % 2)? -112 : 112;
370 f32 transY = (i / 2)? -112 : 112;
371
372 MTXScale( m, 0.5f, 0.5f, 0.5f );
373 MTXConcat( m, sc->cam.view, mv );
374 MTXTrans( m, transX, transY, 0 );
375 MTXConcat( m, mv, mv );
376
377 GXLoadPosMtxImm( mv, GX_PNMTX0 );
378 DrawRect();
379 }
380 }
381
382
383 DEMOInitCaption(
384 DM_FT_OPQ,
385 320,
386 224 );
387
388 DEMOPrintf(48, 0, 0, "Raw");
389 DEMOPrintf(160, 0, 0, "RunLength");
390 DEMOPrintf(48, 112, 0, "LZ77");
391 DEMOPrintf(160, 112, 0, "Huffman");
392 }
393
394
395
396 /*---------------------------------------------------------------------------*
397 Name: AnimTick
398
399 Description: pad process for each frame
400
401 Arguments: sc : a pointer to the scene parameter
402
403 Returns: none
404 *---------------------------------------------------------------------------*/
AnimTick(MySceneCtrlObj * sc)405 static void AnimTick ( MySceneCtrlObj* sc )
406 {
407 if ( DEMOPadGetButtonDown( 0 ) & PAD_BUTTON_A )
408 {
409 s32 i;
410 for ( i = 0; i < 4; i++ )
411 {
412 if ( ++sc->texNum[ i ] >= sMyTplObj[ i ]->numDescriptors )
413 {
414 sc->texNum[ i ] = 0;
415 }
416 }
417 }
418 }
419
420
421 /*---------------------------------------------------------------------------*
422 Name: DrawRect
423
424 Description: Renders a textured rectangle.
425
426 Arguments: none
427
428 Returns: none
429 *---------------------------------------------------------------------------*/
DrawRect(void)430 static void DrawRect( void )
431 {
432 const s16 VERTEX_POS = 224;
433
434 // Vertex Attribute
435 // Vertex Attribute ( VTXFMT0 is used by DEMOPuts lib.)
436 GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_POS, GX_POS_XY, GX_S16, 0 );
437 GXSetVtxAttrFmt( GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0 );
438
439 // Array Pointers and Strides
440 GXInvalidateVtxCache();
441
442 // set vertex descriptor
443 GXClearVtxDesc();
444 GXSetVtxDesc( GX_VA_POS, GX_DIRECT );
445 GXSetVtxDesc( GX_VA_TEX0, GX_DIRECT );
446
447 // draw a cube
448 GXBegin( GX_QUADS, GX_VTXFMT1, 4 );
449
450 GXPosition2s16( -VERTEX_POS, -VERTEX_POS );
451 GXTexCoord2s16( 0, 0 );
452
453 GXPosition2s16( VERTEX_POS, -VERTEX_POS );
454 GXTexCoord2s16( 1, 0 );
455
456 GXPosition2s16( VERTEX_POS, VERTEX_POS );
457 GXTexCoord2s16( 1, 1 );
458
459 GXPosition2s16( -VERTEX_POS, VERTEX_POS );
460 GXTexCoord2s16( 0, 1 );
461
462 GXEnd();
463 }
464
465
466 /*---------------------------------------------------------------------------*
467 Name: PrintIntro
468
469 Description: Prints the directions on how to use this demo.
470
471 Arguments: none
472
473 Returns: none
474 *---------------------------------------------------------------------------*/
PrintIntro(void)475 static void PrintIntro( void )
476 {
477 OSReport("\n\n");
478 OSReport("************************************************\n");
479 OSReport("cx_uncompress_stream: Uncompression demo\n");
480 OSReport("************************************************\n");
481 OSReport("to quit hit the start button\n");
482 OSReport("\n");
483 OSReport("A button : change texture index to next\n");
484 OSReport("************************************************\n\n");
485 }
486
487 /*============================================================================*/
488