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