1 /*---------------------------------------------------------------------------*
2   Project:  Channel jump demo
3   File:     ChannelJump.c
4 
5   Copyright (C) 2005-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   $Log: ChannelJump.c,v $
14   Revision 1.3  2007/08/01 09:36:29  kitase_hirotake
15   TAB -> SPACE
16 
17   Revision 1.2  2007/07/31 23:50:12  nishimoto_takashi
18   Revised slightly to avoid warnings.
19 
20   Revision 1.1  2007/07/30 01:17:38  nishimoto_takashi
21   Initial check in.
22 
23 
24   $NoKeywords: $
25 
26  *---------------------------------------------------------------------------*/
27 
28 
29 #include "ChannelJump.h"
30 
31 /*-----------------------------------------------------------------------------
32     Values
33   -----------------------------------------------------------------------------*/
34 static MEMAllocator  s_mem2Allocator ;
35 static MEMHeapHandle s_handle ;
36 
37 /*-----------------------------------------------------------------------------
38     Functions
39   -----------------------------------------------------------------------------*/
40 /*******************************************************************************
41     Initialize memory (MEM2)
42  *******************************************************************************/
init_memory2(void)43 static void init_memory2( void )
44 {
45     void *lo = OSGetMEM2ArenaLo() ;
46     void *hi = OSGetMEM2ArenaHi() ;
47     s_handle = MEMCreateFrmHeap( lo, (u32)hi - (u32)lo ) ;
48     if ( s_handle == MEM_HEAP_INVALID_HANDLE )
49     {
50         OSHalt("MEM2 heap allocation error.\n") ;
51     }
52     else
53     {
54         OSSetMEM2ArenaLo(hi) ;
55         MEMInitAllocatorForFrmHeap( &s_mem2Allocator, s_handle, 32 ) ;   // Buffer requires 32byte alignment.
56     }
57 }
58 
alloc32(u32 size)59 static void* alloc32( u32 size )
60 {
61     return MEMAllocFromAllocator( &s_mem2Allocator, size ) ;
62 
63 }
64 
free32(void * addr)65 static u8 free32( void *addr )
66 {
67     MEMFreeToAllocator( &s_mem2Allocator, addr ) ;
68     return 1 ;
69 }
70 
71 /*******************************************************************************
72     Camera Setting
73  *******************************************************************************/
SetupCamera(void)74 static void SetupCamera( void )
75 {
76     Mtx44 proj_mtx;
77     MTXPerspective( proj_mtx, 45.f, 640.f / 480.f * 0.908f, 1.f, 100.f );
78     GXSetProjection( proj_mtx, GX_PERSPECTIVE );
79 
80        MTXLookAt(View_Mtx, &(Vec){0.f, 3.f, 8.f}, &(Vec){0.f, 1.f, 0.f}, &(Vec){0.f, 0.f, 0.f});
81 }
82 
83 /*******************************************************************************
84     Draw scene.
85         DrawLines( void )        : Draw a grid pattern
86         DrawRectangles( void )    : Use textures
87         DrawBase( void )        : Use vertex color
88  *******************************************************************************/
89 static void DrawLines( void );
90 static void DrawRectangles( void );
91 static void DrawBase( void );
92 
DrawScene(void)93 static void DrawScene( void )
94 {
95     Mtx mv;
96     MTXIdentity(mv);
97     MTXRotDeg(mv, 'y', Rot_Deg);
98 
99     MTXConcat(View_Mtx, mv, mv);
100     GXLoadPosMtxImm(mv, GX_PNMTX0);
101 
102     switch(Scene_No) {
103         case 0:
104             DrawLines();
105             break;
106         case 1:
107             DrawRectangles();
108             break;
109         case 2:
110             DrawBase();
111             break;
112     }
113 }
114 
115 /*******************************************************************************
116     Set data ( set Scene_No as 8 bytes data)
117  *******************************************************************************/
SetAttachedData()118 static void SetAttachedData()
119 {
120     char data[5];
121 
122     switch(Scene_No) {
123         case 1:
124             (void)strcpy(data, "SCN1");
125             break;
126         case 2:
127             (void)strcpy(data, "SCN2");
128             break;
129         default:
130             (void)strcpy(data, "SCN0");
131             break;
132     }
133 
134     // Overwrite binary data in chjump.bin
135     (void) memcpy(Template_arc_begin + 0x00000060 + 0x00000020, data, 4);
136 
137     OSReport("set data successfully.\n");
138 }
139 
140 /*******************************************************************************
141     Send a test message. (Channnel Jump and data)
142  *******************************************************************************/
PostMessage()143 static void PostMessage()
144 {
145     NWC24Err        err;
146     NWC24MsgObj     msgObj;
147     NWC24UserId        testIdTo;
148 
149     // Initializes the object, specifies Wii-to-Wii message.
150     err = NWC24InitMsgObj(&msgObj, NWC24_MSGTYPE_WII_MENU);
151     if ( err != NWC24_OK )
152     {
153         OSReport("NWC24InitMsgObj(): error %d\n", err);
154         return;
155     }
156 
157     // To address (by user id.)
158     err = NWC24GetMyUserId(&testIdTo);
159     if( err != NWC24_OK )
160     {
161         OSReport("NWC24GetMyUserId(): error %d\n", err);
162         return;
163     }
164 
165     err = NWC24SetMsgToId(&msgObj, testIdTo);
166     if ( err != NWC24_OK )
167     {
168         OSReport("NWC24SetMsgToId(): error %d\n", err);
169         return;
170     }
171 
172     // Subject
173     err = NWC24SetMsgSubject(&msgObj, MsgSubject, (u32)strlen(MsgSubject));
174     if ( err != NWC24_OK )
175     {
176         OSReport("NWC24SetMsgSubject(): error %d\n", err);
177         return;
178     }
179 
180     // Message body (text)
181     err = NWC24SetMsgText(&msgObj, (const char*)MsgText, (u32)sizeof(MsgText) - 2,
182                           NWC24_UTF_16BE, NWC24_ENC_BASE64);
183     if ( err != NWC24_OK )
184     {
185         OSReport("NWC24SetMsgText(): error %d\n", err);
186         return;
187     }
188 
189     // Edit chjump.bin file.
190     SetAttachedData();
191 
192     // Attach a NWC24CHJumpObj.
193     err = NWC24SetMsgAttached(&msgObj, (const char*)Template_arc_begin,
194                              (u32)(Template_arc_end - Template_arc_begin), NWC24_APP_WII_MSGBOARD);
195     if ( err != NWC24_OK )
196     {
197         OSReport("NWC24SetMsgAttached(): error %d\n", err);
198         return;
199     }
200 
201     // Set alternate name.
202     MsgAltName[9] = (wchar_t)(L'0' + Scene_No);
203 
204     err = NWC24SetMsgAltName(&msgObj, (const u16*)MsgAltName, (u32)sizeof(MsgAltName) - 2);
205     if ( err != NWC24_OK )
206     {
207         OSReport("NWC24SetMsgAltName(): error %d\n", err);
208         return;
209     }
210 
211     // Commit settings and post the message into the send box.
212     err = NWC24CommitMsg(&msgObj);
213     if ( err != NWC24_OK )
214     {
215         OSReport("NWC24CommitMsg: error %d\n", err);
216         return;
217     }
218 
219     OSReport("Posted a test message successfully.\n");
220 
221     return;
222 }
223 
224 /*******************************************************************************
225     main
226  *******************************************************************************/
main(int argc,char * argv[])227 void main(int argc, char * argv[])
228 {
229     NWC24Err    err;
230     s32         tmp, count;
231     char*        libWorkMem;
232 
233     // Initialize (KPAD, NWC24)
234     DEMOInit(NULL);
235 
236     init_memory2();
237     WPADRegisterAllocator( alloc32, free32 );
238     KPADInit();
239 
240     VFInit();
241 
242     libWorkMem = MEMAllocFromAllocator(&s_mem2Allocator, NWC24_WORK_MEM_SIZE);
243 
244     if( NWC24_OK != ( err = NWC24OpenLib(libWorkMem)) )
245     {
246         OSReport("NWC24penLib() Error: %d\n", err);
247         OSHalt("Failed.\n");
248     }
249 
250     count = 0;
251     Rot_Deg = 0;
252 
253     // Output
254     OSReport("*******************************************************\n");
255     OSReport("Start Channel Jump demo.\n");
256     OSReport("*******************************************************\n");
257 
258 #ifdef _DEBUG
259     OSReport("This is a DEBUG build.\n");
260     OSReport("argc : %d\n", argc);
261 #else
262     OSReport("This is a NON-DEBUG/optimized build.\n");
263 #endif // _DEBUG
264 
265     // texture
266     GXInitTexObj( &TextureD, (void*)Default_tex, 8,  8, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE);
267     GXInitTexObj( &Texture1, (void*)No1_tex, 16, 16, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE);
268     GXInitTexObj( &Texture2, (void*)No2_tex, 16, 16, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE);
269 
270     GXInitTexObjLOD( &TextureD, GX_LINEAR, GX_LINEAR, 0.f, 0.f, 0.f, GX_FALSE, GX_FALSE, GX_ANISO_1);
271     GXInitTexObjLOD( &Texture1, GX_LINEAR, GX_LINEAR, 0.f, 0.f, 0.f, GX_FALSE, GX_FALSE, GX_ANISO_1);
272     GXInitTexObjLOD( &Texture2, GX_LINEAR, GX_LINEAR, 0.f, 0.f, 0.f, GX_FALSE, GX_FALSE, GX_ANISO_1);
273 
274     OSReport("Texture loaded.\n");
275 
276     if(argc > 1) {        // Set Scene_No by the string sent from a message.
277 #ifdef _DEBUG
278     int i;
279     for(i=1; i < argc; i++)
280     {
281         OSReport("argv[%d]: %s\n", i, argv[i]);
282     }
283 #endif // _DEBUG
284         if(strcmp(argv[1], "SCN1") == 0)    Scene_No = 1;
285         else if(strcmp(argv[1], "SCN2") == 0)    Scene_No = 2;
286         else    Scene_No = 0;
287     }
288     else {
289         Scene_No = 0;
290     }
291 
292     // Draw
293     while(1)
294     {
295         DEMOBeforeRender();
296             SetupCamera();
297             DrawScene();
298             GXDrawDone();
299 
300             // Text
301             DEMOInitCaption(DM_FT_OPQ, 320, 240);
302             DEMOPrintf(0, 0,0, "Demo Channnel - Scene %d", Scene_No);
303             DEMOPrintf(0,15,0, "HOME -> return to Menu");
304             DEMOPrintf(0,25,0, "+, -, RIGHT, LEFT -> change scene");
305             DEMOPrintf(0,35,0, "A -> post a message");
306 
307             if( argc != 0)
308                 DEMOPrintf(0,45,0, "ID: %s", argv[0]);
309 
310             if( count > 0 ){
311                 DEMOPrintf(0, 200, 0, "Posting......");
312             }
313             else if(Kpad_s[0][0].trig & KPAD_BUTTON_HOME ) {
314                 DEMOPrintf(0, 210, 0, "Return to Menu...");
315             }
316 
317             GXDrawDone();
318 
319         DEMODoneRender();
320 
321         // Input processing
322         if( count <= 0 ){
323             if( Kpad_s[0][0].trig & KPAD_BUTTON_HOME ) {
324                 OSReport("Return to Menu.\n");
325                 break;
326             }
327             else if(Kpad_s[0][0].trig & (KPAD_BUTTON_RIGHT | KPAD_BUTTON_PLUS)) {
328                 Scene_No += 1;
329                 if(Scene_No == 3)    Scene_No = 0;
330             }
331             else if(Kpad_s[0][0].trig & (KPAD_BUTTON_LEFT | KPAD_BUTTON_MINUS)) {
332                 if(Scene_No == 0)    Scene_No = 3;
333                 Scene_No -= 1;
334             }
335             else if( Kpad_s[0][0].trig & KPAD_BUTTON_A) {
336                 OSReport("Posted a Message.\n");
337                 PostMessage();
338                 count = 60;
339             }
340             else if( Kpad_s[0][0].hold & KPAD_BUTTON_B ) {
341                 if(Rot_Deg == 360)    Rot_Deg= 0;
342                 Rot_Deg += 1;
343             }
344         }
345         else
346             count--;
347 
348         tmp = KPADRead( 0, &Kpad_s[0][0], KPAD_MAX_READ_BUFS);
349     }
350 
351     if( NWC24_OK != (err = NWC24CloseLib()) )
352     {
353         OSReport("NWC24CloseLib() Error: %d\n", err);
354         OSHalt("Failed.\n");
355     }
356 
357     MEMFreeToAllocator(&s_mem2Allocator, libWorkMem);
358 
359     VFFinalize();
360 
361     OSReport("Sample complete\n");
362     OSReturnToMenu();
363 }
364 
365 /*******************************************************************************
366     Draw functions
367  *******************************************************************************/
DrawLines(void)368 static void DrawLines( void )
369 {
370     int i = -5;
371 
372     GXClearVtxDesc();
373     GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
374     GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
375 
376     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
377     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_CLR0, GX_CLR_RGB, GX_RGB8, 0);
378 
379     GXSetNumChans(1);
380     GXSetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
381     GXSetChanCtrl(GX_COLOR1A1, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
382     GXSetNumTexGens(0);
383     GXSetNumTevStages(1);
384     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
385     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
386 
387     for(i=-5 ; i<6 ; i++) {
388         GXBegin(GX_LINESTRIP, GX_VTXFMT1, 3);
389             GXPosition3f32(1.0f * i, 0.0f,  8.0f);
390             GXColor3u8(0, 255, 0);
391             GXPosition3f32(1.0f * i, 0.0f,  0.0f);
392             GXColor3u8(255, 0, 0);
393             GXPosition3f32(1.0f * i, 0.0f, -8.0f);
394             GXColor3u8(0, 255, 255);
395         GXEnd();
396 
397         GXBegin(GX_LINESTRIP, GX_VTXFMT1, 3);
398             GXPosition3f32(-5.5f, 0.0f, 7.7f - 1.4f * (i+5.f));
399             GXColor3u8(0, 0, 255);
400             GXPosition3f32( 0.0f, 0.0f, 7.7f - 1.4f * (i+5.f));
401             GXColor3u8(255, 255, 255);
402             GXPosition3f32( 5.5f, 0.0f, 7.7f - 1.4f * (i+5.f));
403             GXColor3u8(0, 255, 255);
404         GXEnd();
405     }
406 }
407 
DrawBase(void)408 static void DrawBase( void )
409 {
410     GXClearVtxDesc();
411     GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
412     GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
413 
414     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
415     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_CLR0, GX_CLR_RGB, GX_RGB8, 0);
416 
417     GXSetNumChans(1);
418     GXSetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
419     GXSetChanCtrl(GX_COLOR1A1, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
420     GXSetNumTexGens(0);
421     GXSetNumTevStages(1);
422     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
423     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
424 
425     GXBegin(GX_QUADS, GX_VTXFMT1, 4);
426         GXPosition3f32(-3.0f, 0.0f, -5.0f);
427         GXColor3u8(0, 255, 0);
428         GXPosition3f32( 3.0f, 0.0f, -5.0f);
429         GXColor3u8(0, 255, 255);
430         GXPosition3f32( 3.0f, 0.0f,  3.0f);
431         GXColor3u8(0, 0, 255);
432         GXPosition3f32(-3.0f, 0.0f,  3.0f);
433         GXColor3u8(255, 0, 0);
434     GXEnd();
435 }
436 
DrawRectangles(void)437 static void DrawRectangles( void )
438 {
439     GXClearVtxDesc();
440     GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
441     GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
442     GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
443 
444     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
445     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGB, GX_RGB8, 0);
446     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
447 
448     GXSetNumChans(1);
449     GXSetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
450     GXSetChanCtrl(GX_COLOR1A1, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
451     GXSetNumTexGens(1);
452     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
453 
454     GXSetNumTevStages(1);
455     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
456     GXSetTevDirect( GX_TEVSTAGE0 );
457     GXSetTevSwapMode( GX_TEVSTAGE0, GX_TEV_SWAP0, GX_TEV_SWAP0 );
458     GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
459 
460     GXSetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
461     GXSetZMode( GX_ENABLE, GX_LEQUAL, GX_ENABLE);
462 
463     //
464     GXLoadTexObj( &TextureD, GX_TEXMAP0);
465     GXBegin(GX_QUADS, GX_VTXFMT0, 4);
466         GXPosition3f32(-0.5f,  0.0f, -2.5f);
467         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 0.f);
468         GXPosition3f32( 0.5f,  0.0f, -2.5f);
469         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 0.f);
470         GXPosition3f32( 0.5f,  0.0f, -1.5f);
471         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 1.f);
472         GXPosition3f32(-0.5f,  0.0f, -1.5f);
473         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 1.f);
474     GXEnd();
475 
476     // 1
477     GXLoadTexObj( &Texture1, GX_TEXMAP0 );
478     GXBegin(GX_QUADS, GX_VTXFMT0, 4);
479         GXPosition3f32(-2.5f, 0.0f, -0.5f);
480         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 0.f);
481         GXPosition3f32(-1.5f, 0.0f, -0.5f);
482         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 1.f);
483         GXPosition3f32(-1.5f, 0.0f,  0.5f);
484         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 1.f);
485         GXPosition3f32(-2.5f, 0.0f,  0.5f);
486         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 0.f);
487     GXEnd();
488 
489     // 2
490     GXLoadTexObj( &Texture2, GX_TEXMAP0 );
491     GXBegin(GX_QUADS, GX_VTXFMT0, 4);
492         GXPosition3f32( 1.5f, 0.0f, -0.5f);
493         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 1.f);
494         GXPosition3f32( 2.5f, 0.0f, -0.5f);
495         GXColor3u8(255, 255, 255); GXTexCoord2f32(0.f, 0.f);
496         GXPosition3f32( 2.5f, 0.0f,  0.5f);
497         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 0.f);
498         GXPosition3f32( 1.5f, 0.0f,  0.5f);
499         GXColor3u8(255, 255, 255); GXTexCoord2f32(1.f, 1.f);
500     GXEnd();
501 }
502