1 /*---------------------------------------------------------------------------*
2   Project:  Revolution strapimage demo
3   File:     strapcntdemo.c
4 
5   Copyright 1998 - 2008 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   $Log: strapcntdemo.c,v $
14   Revision 1.2.6.1  2009/12/08 02:44:49  abe_takehiro
15   Erased warning message
16 
17   Revision 1.2  2008/07/23 06:44:39  nakano_yoshinobu
18   Moved from build/demos/strapimagedemo.
19 
20 
21  *---------------------------------------------------------------------------*/
22 
23 /*---------------------------------------------------------------------------*
24    Header files
25  *---------------------------------------------------------------------------*/
26 #include <demo.h>
27 #include <revolution/cnt.h>
28 #include <revolution/cx.h>
29 #include <revolution/tpl.h>
30 
31 /*---------------------------------------------------------------------------*
32    Macro definitions
33  *---------------------------------------------------------------------------*/
34 #define MAX_SCOORD         0x4000
35 #define MAX_TCOORD         0x4000
36 #define TEX_WSCALE         1.0
37 
38 #define TEX_FADE_MS_TIME   500
39 #define TEX_CHANGE_MS_TIME 5000
40 #define END_MS_TIME        20000
41 
42 /*---------------------------------------------------------------------------*
43    Structure definitions
44  *---------------------------------------------------------------------------*/
45 // for camera
46 typedef struct
47 {
48     Vec    location;
49     Vec    up;
50     Vec    target;
51     f32    left;
52     f32    top;
53     f32    znear;
54     f32    zfar;
55 } CameraConfig;
56 
57 typedef struct
58 {
59     CameraConfig  cfg;
60     Mtx           view;
61     Mtx44         proj;
62 } MyCameraObj;
63 
64 // for texture
65 typedef struct
66 {
67     GXTexObj      tobj;
68     u32           width;
69     u32           height;
70     u32           alpha0, alpha1;
71     GXColor       color;
72 } MyTexObj;
73 
74 // for entire scene control
75 typedef struct
76 {
77     MyCameraObj    cam;
78     MyTexObj       texture;
79     Mtx            modelCtrl;
80     f32            modelScale;
81     u32            texNumber;
82     u32            texFileNumber;
83     u32            startTick;
84     u32            switchTick;
85 } MySceneCtrlObj;
86 
87 /*---------------------------------------------------------------------------*
88    Forward references
89  *---------------------------------------------------------------------------*/
90 void        main             ( void );
91 static void LoadSIContentData( TPLPalettePtr *buffer, u32 language );
92 static void *LoadTexDataLZ   ( void* buf );
93 static void GetTplTexture    ( TPLPalettePtr buffer, MyTexObj* to, u32 num );
94 static void DrawInit         ( MySceneCtrlObj* sc );
95 static void DrawTick         ( MySceneCtrlObj* sc );
96 static void AnimTick         ( TPLPalettePtr *buffer, MySceneCtrlObj* sc );
97 static void DrawTexQuad      ( void );
98 static void SetCamera        ( MyCameraObj* cam );
99 static void PrintIntro       ( void );
100 
101 /*---------------------------------------------------------------------------*
102    Model Data
103  *---------------------------------------------------------------------------*/
104 /*---------------------------------------------------------------------------*
105    Data arrays for indexed primitives must be 32B aligned.  Normally, memory
106    for the arrays would be OSAlloc'd (which returns 32B aligned pointers) and
107    the data would be loaded from ROM.  The pragma variable_align provides a
108    convenient way to align initialized arrays.
109  *---------------------------------------------------------------------------*/
110 static s16 TopVerts[] ATTRIBUTE_ALIGN(32) =
111 {
112     -1, -1,  1, // 0
113      1, -1,  1, // 1
114      1,  1,  1, // 2
115     -1,  1,  1, // 3
116     -1, -1, -1, // 4
117      1, -1, -1, // 5
118      1,  1, -1, // 6
119     -1,  1, -1  // 7
120 };
121 
122 /*---------------------------------------------------------------------------*
123    Camera configuration
124  *---------------------------------------------------------------------------*/
125 static CameraConfig DefaultCamera =
126 {
127     { 0.0F, -5000.0F, 0.0F }, // location
128     { 0.0F,     0.0F, 1.0F }, // up
129     { 0.0F,     0.0F, 0.0F }, // tatget
130     -320.0F, // left
131     240.0F,  // top
132     0.0F,    // near
133     10000.0F // far
134 };
135 
136 /*---------------------------------------------------------------------------*
137   File List in Content
138  *---------------------------------------------------------------------------*/
139 
140 // the number of content to be used in this program
141 #define TARGET_CONTENT 3
142 
143 // The names of the files we are going to read.
144 static char* strapImage[] =
145 {
146     "strapImage_jp_LZ.bin",
147     "strapImage_En_LZ.bin",
148     "strapImage_Fr_LZ.bin",
149     "strapImage_Ge_LZ.bin",
150     "strapImage_It_LZ.bin",
151     "strapImage_Sp_LZ.bin",
152     "strapImage_Du_LZ.bin"
153 };
154 
155 enum
156 {
157     JP,
158     EN,
159     FR,
160     GE,
161     IT,
162     SP,
163     DU
164 };
165 
166 #define NUM_OF_FILES_IN_CONTENT 7
167 #define NUM_OF_FILES_IN_TPL  2
168 
169 CNTHandle   Cnt;
170 
171 /*---------------------------------------------------------------------------*
172    Application main loop
173  *---------------------------------------------------------------------------*/
main(void)174 void main ( void )
175 {
176     static MySceneCtrlObj    SceneCtrl;  // scene control parameters
177     static TPLPalettePtr     MyTplObj;   // texure palette
178 
179     DEMOInit( NULL );
180 
181     CNTInit();
182     CNTInitHandle( TARGET_CONTENT, &Cnt, &DemoAllocator1 );
183 
184     // load content data
185     LoadSIContentData( &MyTplObj, JP );
186 
187     // Print demo directions
188     PrintIntro();
189 
190     // Initialize vertex formats, array pointers
191     // and default scene settings.
192     DrawInit( &SceneCtrl );
193 
194     // Get current texture from the texture palette
195     GetTplTexture( MyTplObj, &SceneCtrl.texture, 0 );
196 
197     while(!( DEMOPadGetButton(0) & PAD_BUTTON_MENU ))
198     {
199         DEMOBeforeRender();
200         // Draw the model.
201         DrawTick( &SceneCtrl );
202         DEMODoneRender();
203         // Read controller
204         DEMOPadRead();
205         // Do animation
206         AnimTick( &MyTplObj, &SceneCtrl);
207     }
208 
209     // free buffer
210     MEMFreeToAllocator( &DemoAllocator1, MyTplObj );
211 
212     // make sure to release CNTHandle
213     CNTReleaseHandle( &Cnt );
214 
215     // for safety device shutdown
216     CNTShutdown();
217 
218     OSHalt( "End of demo" );
219 }
220 
221 /*---------------------------------------------------------------------------*
222    Functions
223  *---------------------------------------------------------------------------*/
224 
225 /*---------------------------------------------------------------------------*
226   Name:         LoadSIContentData
227 
228   Description:  Load strap image content data
229                 elf file:
230                       load from {Dvdroot}/content3
231                 wad file:
232                       load from content(index 3) in wad
233 
234   Arguments:    buffer    : pointer to strap image content data
235                 language  : language of strap image
236 
237   Returns:      none
238 *---------------------------------------------------------------------------*/
LoadSIContentData(TPLPalettePtr * buffer,u32 language)239 static void LoadSIContentData( TPLPalettePtr *buffer, u32 language )
240 {
241     CNTFileInfo fileInfo;
242     u32         fileSize;
243 
244     // Load content
245     OSReport( " File    : %s \n", strapImage[ language ] );
246     // Open the strapImage to fileInfo1
247     CNTOpen(&Cnt, strapImage[ language ], &fileInfo);
248 
249     // Get the size of the files
250     fileSize = CNTGetLength( &fileInfo );
251     OSReport( " Size    : %d \n", fileSize );
252 
253     // Allocate buffers to read the files.
254     // Note that pointers returned by Allocator are all 32byte aligned.
255     *buffer = (TPLPalettePtr)MEMAllocFromAllocator( &DemoAllocator1, OSRoundUp32B( fileSize ) );
256 
257     // reads strapImage at one time
258     CNTRead(&fileInfo, (void*)*buffer, (u32)OSRoundUp32B( fileSize ));
259 
260     // Close the files
261     CNTClose( &fileInfo );
262 
263     // load LZ compressed data
264     *buffer = (TPLPalettePtr)LoadTexDataLZ( *buffer );
265 
266     // bind tpl data
267     TPLBind( *buffer );
268     GXInvalidateTexAll();
269 
270 }
271 
272 /*---------------------------------------------------------------------------*
273   Name:         LoadTexDataLZ
274 
275   Description:  Load LZ compressed tex data
276 
277   Returns:      pointer of uncompressed tex data
278 *---------------------------------------------------------------------------*/
LoadTexDataLZ(void * buf)279 static void* LoadTexDataLZ( void* buf )
280 {
281     void* uncompData;
282     u32   uncompSize;
283 
284     // allocated buffer
285     uncompSize = CXGetUncompressedSize( buf );
286     uncompData = (u8*)MEMAllocFromAllocator( &DemoAllocator1, uncompSize );
287 
288     // uncompress data
289     CXUncompressLZ( buf, uncompData );
290     DCFlushRange( uncompData, uncompSize );
291 
292     // free buffer
293     MEMFreeToAllocator( &DemoAllocator1, buf );
294 
295     return uncompData;
296 }
297 
298 /*---------------------------------------------------------------------------*
299     Name:           GetTplTexture
300 
301     Description:    get current texture from the texture palette
302 
303     Arguments:      buffer : a pointer to tpl palette
304                     to     : a pointer to MyTexObj structure to be set
305                     num    : texture number in the tpl
306 
307     Returns:        none
308  *---------------------------------------------------------------------------*/
GetTplTexture(TPLPalettePtr buffer,MyTexObj * to,u32 num)309 void GetTplTexture( TPLPalettePtr buffer, MyTexObj* to, u32 num )
310 {
311     TPLDescriptorPtr tdp;
312     u32              fmt;
313 
314     // Get a texture descriptor
315     tdp = TPLGet( buffer, num );
316 
317     // get texture format
318     fmt = (u32)tdp->textureHeader->format;
319 
320     // get image size
321     to->width  = tdp->textureHeader->width;
322     to->height = tdp->textureHeader->height;
323 
324     // init texture objects
325     GXInitTexObj(
326         &to->tobj,
327         tdp->textureHeader->data,
328         tdp->textureHeader->width,
329         tdp->textureHeader->height,
330         (GXTexFmt)fmt,
331         tdp->textureHeader->wrapS, // s
332         tdp->textureHeader->wrapT, // t
333         GX_FALSE ); // Mipmap
334 }
335 
336 /*---------------------------------------------------------------------------*
337     Name:           DrawInit
338 
339     Description:    Initializes the vertex attribute format and sets up
340                     the array pointer for the indexed data.
341                     This function also initializes scene control parameters.
342 
343     Arguments:      sc : pointer to the structure of scene control parameters
344 
345     Returns:        none
346  *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)347 static void DrawInit( MySceneCtrlObj* sc )
348 {
349     const GXColor WHITE = { 0xff, 0xff, 0xff, 0xff };
350     const GXColor ALPHA0 = { 0xff, 0xff, 0xff, 0x00 };
351 
352     // Crear EFB
353     GXSetCopyClear( WHITE, GX_MAX_Z24 );
354 
355     // Vertex Attribute
356     GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0 );
357     GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0 );
358     GXSetVtxAttrFmt( GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_U16, 14 );
359 
360     // Array Pointers and Strides
361     GXInvalidateVtxCache();
362     // stride = 3 elements (x,y,z) each of type s16
363     GXSetArray( GX_VA_POS, TopVerts, 3*sizeof(s16) );
364 
365     // backface culling off
366     GXSetCullMode( GX_CULL_NONE );
367 
368     // color channels
369     GXSetNumChans(1);
370     GXSetChanCtrl( GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE );
371 
372     // channel color
373     GXSetChanAmbColor( GX_COLOR0A0, WHITE );
374     GXSetChanMatColor( GX_COLOR0A0, ALPHA0 );
375 
376     // texcoord gens
377     GXSetNumTexGens(1);
378     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
379 
380     // Indirect Textures.
381     GXSetNumIndStages(0);
382     GXSetTevDirect( GX_TEVSTAGE0 );
383 
384     // blend mode
385     GXSetBlendMode( GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR );
386 
387     // tev setting
388     GXSetNumTevStages( 1 );
389     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
390     GXSetTevOp(GX_TEVSTAGE0, GX_MODULATE);
391 
392     // Default scene control parameter settings
393     // camera
394     sc->cam.cfg = DefaultCamera;
395     SetCamera( &sc->cam );   // never changes in this test
396 
397     // Init tex info
398     sc->texFileNumber = 0;
399     sc->texNumber     = 0;
400 
401     // model control parameters
402     sc->modelScale = 1.0F;
403     MTXIdentity( sc->modelCtrl );
404 
405     // Init secTime
406     sc->startTick       = (u32)OSGetTick();
407     sc->switchTick         = sc->startTick;
408 
409 }
410 
411 /*---------------------------------------------------------------------------*
412     Name:           DrawTick
413 
414     Description:    Draw the model by using given scene parameters
415 
416     Arguments:      sc : pointer to the structure of scene control parameters
417 
418     Returns:        none
419  *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)420 static void DrawTick( MySceneCtrlObj* sc )
421 {
422 
423     Mtx  ms; // Model scale matrix.
424     Mtx  mv; // Modelview matrix.
425 
426     // Set modelview matrix
427     MTXConcat( sc->cam.view, sc->modelCtrl, mv );
428     MTXScale(
429         ms,
430         (f32)( sc->modelScale * sc->texture.width / 2.0 / TEX_WSCALE ),
431         (f32)( sc->modelScale * sc->texture.height / 2.0 ),
432         (f32)( sc->modelScale * sc->texture.height / 2.0 ) );
433     MTXConcat( mv, ms, mv );
434     GXLoadPosMtxImm( mv, GX_PNMTX0 );
435 
436     // load texture obj
437     GXLoadTexObj( &sc->texture.tobj, GX_TEXMAP0 );
438 
439     // draw a box
440     DrawTexQuad();
441 
442 }
443 
444 
445 /*---------------------------------------------------------------------------*
446     Name:           AnimTick
447 
448     Description:    Changes scene parameters according to the pad status.
449 
450     Arguments:      buffer    : pointer to strap image content data
451                     sc  : pointer to the structure of scene control parameters
452 
453     Returns:        none
454  *---------------------------------------------------------------------------*/
AnimTick(TPLPalettePtr * buffer,MySceneCtrlObj * sc)455 static void AnimTick( TPLPalettePtr *buffer, MySceneCtrlObj* sc )
456 {
457     f32 alpha;
458     u32 currentTick = (u32)OSGetTick();
459     u32 diffCSTime = OSTicksToMilliseconds( OSDiffTick( currentTick, sc->startTick ) );
460 
461     // end of strap image frame
462     if ( diffCSTime >= END_MS_TIME + TEX_FADE_MS_TIME )
463     {
464         alpha = 0.0f;
465         sc->startTick = currentTick;
466     }
467     // fade in
468     else if ( diffCSTime <= TEX_FADE_MS_TIME )
469     {
470        alpha = (f32)( diffCSTime / (f32)TEX_FADE_MS_TIME * 255.9f );
471     }
472     // fade out
473     else if ( diffCSTime > END_MS_TIME )
474     {
475         alpha = (f32)( 255.9 - ( diffCSTime - END_MS_TIME ) / (f32)TEX_FADE_MS_TIME * 255.9f );
476     }
477     else
478     {
479        alpha = 255.0f;
480     }
481 
482     // channel color
483     GXSetChanMatColor( GX_COLOR0A0, ( GXColor ){ 0xff, 0xff, 0xff, (u8)alpha } );
484 
485     // Change texture by Time count
486     if ( OSTicksToMilliseconds( OSDiffTick( currentTick, sc->switchTick ) ) > TEX_CHANGE_MS_TIME )
487     {
488         sc->texNumber = ( sc->texNumber + 1 ) % NUM_OF_FILES_IN_TPL;
489         GetTplTexture( *buffer, &sc->texture, sc->texNumber );
490         sc->switchTick = currentTick;
491     }
492 
493     // Change tpl file (Change Language)
494     if ( DEMOPadGetButtonDown(0) & PAD_BUTTON_A )
495     {
496         // free buffer
497         MEMFreeToAllocator( &DemoAllocator1, *buffer );
498 
499         // load content data
500         sc->texFileNumber = ( sc->texFileNumber + 1 ) % NUM_OF_FILES_IN_CONTENT;
501         LoadSIContentData( buffer, sc->texFileNumber );
502         sc->texNumber = 0;
503 
504         // get tpl texture
505         GetTplTexture( *buffer, &sc->texture, sc->texNumber );
506 
507         sc->startTick = currentTick;
508     }
509 
510 }
511 
512 /*---------------------------------------------------------------------------*
513     Name:           VertexT
514 
515     Description:    Draw a vertex with texture, direct data
516 
517     Arguments:      v        8-bit position index
518                     s, t     16-bit tex coord
519 
520     Returns:        none
521  *---------------------------------------------------------------------------*/
VertexT(u8 v,u32 s,u32 t)522 static inline void VertexT( u8 v, u32 s, u32 t)
523 {
524     GXPosition1x8( v );
525     GXColor1x8( 0 );
526     GXTexCoord2u16( (u16)s, (u16)t );
527 }
528 
529 /*---------------------------------------------------------------------------*
530     Name:           DrawQuad
531 
532     Description:    Draw a textured quad.  Map extends to corners of the quad.
533                     MAX_SCOORD is the value of 1.0 in the fixed point format.
534 
535     Arguments:      v0        8-bit position
536                     v1        8-bit position
537                     v2        8-bit position
538                     v3        8-bit position
539                     s0        s tex coord at v0
540                     t0        t tex coord at v0
541 
542     Returns:        none
543  *---------------------------------------------------------------------------*/
DrawQuad(u8 v0,u8 v1,u8 v2,u8 v3,u32 s0,u32 t0)544 static inline void DrawQuad(
545     u8  v0,
546     u8  v1,
547     u8  v2,
548     u8  v3,
549     u32 s0,
550     u32 t0)
551 {
552     VertexT( v3, s0, t0+MAX_TCOORD );
553     VertexT( v2, s0+MAX_SCOORD, t0+MAX_TCOORD );
554     VertexT( v1, s0+MAX_SCOORD, t0 );
555     VertexT( v0, s0, t0 );
556 }
557 
558 /*---------------------------------------------------------------------------*
559     Name:           DrawTexQuad
560 
561     Description:    Draw a tex quad model.
562 
563     Arguments:      none
564 
565     Returns:        none
566  *---------------------------------------------------------------------------*/
DrawTexQuad(void)567 static void DrawTexQuad( void )
568 {
569     // set vertex descriptor
570     GXClearVtxDesc();
571     GXSetVtxDesc( GX_VA_POS, GX_INDEX8 );
572     GXSetVtxDesc( GX_VA_CLR0, GX_INDEX8 );
573     GXSetVtxDesc( GX_VA_TEX0, GX_DIRECT );
574 
575     // draw the QUAD
576     GXBegin( GX_QUADS, GX_VTXFMT0, 4 );
577         DrawQuad( 0, 1, 5, 4, 0, 0 );
578     GXEnd();
579 }
580 
581 /*---------------------------------------------------------------------------*
582     Name:           SetCamera
583 
584     Description:    set view matrix and load projection matrix into hardware
585 
586     Arguments:      cam : pointer to the MyCameraObj structure
587 
588     Returns:        none
589  *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)590 static void SetCamera( MyCameraObj* cam )
591 {
592     MTXLookAt(
593         cam->view,
594         &cam->cfg.location,
595         &cam->cfg.up,
596         &cam->cfg.target );
597 
598     MTXOrtho(
599         cam->proj,
600         cam->cfg.top,
601         - (cam->cfg.top),
602         cam->cfg.left,
603         - (cam->cfg.left),
604         cam->cfg.znear,
605         cam->cfg.zfar );
606     GXSetProjection(cam->proj, GX_ORTHOGRAPHIC);
607 }
608 
609  /*---------------------------------------------------------------------------*
610     Name:           PrintIntro
611 
612     Description:    Prints the directions on how to use this demo.
613 
614     Arguments:      none
615 
616     Returns:        none
617  *---------------------------------------------------------------------------*/
PrintIntro(void)618 void PrintIntro(void)
619 {
620     OSReport("******************************************************\n");
621     OSReport("Instructions:\n");
622     OSReport("    A Button    : change the language of Strap Image\n");
623     OSReport("    Start       : end.\n");
624     OSReport("******************************************************\n");
625 }
626 
627 /*============================================================================*/
628