1 /*---------------------------------------------------------------------------*
2   Project:  THP Simple Player demo
3   File:     main.c
4 
5   Copyright (C)2002-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: main.c,v $
14   Revision 1.1  02/03/2006 10:01:13  aka
15   Imported from Dolphin tree.
16 
17 
18     4     03/11/25 11:24 Dante
19     Japanese to English translation of comments and text strings
20 
21     3     02/05/14 9:18a Suzuki
22     Support multi audio track
23 
24     2     02/02/28 6:38p Akagi
25     enabled to use with MusyX/AX by Suzuki-san (IRD).
26 
27     1     02/01/16 10:54a Akagi
28     Initial revision made by Suzuki-san (IRD).
29 
30   $NoKeywords: $
31 
32  *---------------------------------------------------------------------------*/
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <demo.h>
37 #ifdef HOLLYWOOD_REV
38 #include <revolution/mem.h>
39 #endif
40 
41 #include "THPSimple.h"
42 #include "axseq.h"
43 
44 #ifdef HOLLYWOOD_REV
45 MEMHeapHandle __ExpHeap;
46 #endif
47 
main(void)48 void main(void)
49 {
50     u32             size, x, y, count;
51     s32             frame, start, vol, audioTrack;
52     u16             buttonDown, button;
53     u8              *buffer;
54     GXRenderModeObj *rmode;
55     THPVideoInfo    videoInfo;
56     THPAudioInfo    audioInfo;
57 #ifdef HOLLYWOOD_REV
58     void            *arenaMem2Lo;
59     void            *arenaMem2Hi;
60 #endif
61 
62     DEMOInit(&GXNtsc480Int);
63 
64 #ifdef HOLLYWOOD_REV
65     arenaMem2Lo = OSGetMEM2ArenaLo();
66     arenaMem2Hi = OSGetMEM2ArenaHi();
67     __ExpHeap   = MEMCreateExpHeap(arenaMem2Lo, (u32)arenaMem2Hi - (u32) arenaMem2Lo);
68 #endif
69 
70     AIInit(NULL);
71 
72     AXSeqSetup();
73 
74     THPSimpleInit(THP_MODE_WITH_AX);
75 
76     GXSetDispCopyGamma(GX_GM_1_0);
77 
78     // Open THP file
79     if (THPSimpleOpen("thpdemo/fish_mlt.thp") == FALSE)
80     {
81         OSHalt("THPSimpleOpen fail");
82     }
83 
84     THPSimpleGetVideoInfo(&videoInfo);
85     THPSimpleGetAudioInfo(&audioInfo);
86 
87     rmode = DEMOGetRenderModeObj();
88 
89     x = (rmode->fbWidth   - videoInfo.xSize) / 2;
90     y = (rmode->efbHeight - videoInfo.ySize) / 2;
91 
92     // Set aside work area
93     size = THPSimpleCalcNeedMemory();
94 
95 #ifndef HOLLYWOOD_REV
96     buffer = (u8 *)OSAlloc(size);
97 #else
98     buffer = MEMAllocFromExpHeapEx(__ExpHeap, size, 32);
99 #endif
100 
101     if (!buffer)
102     {
103         OSHalt("Can't allocate the memory\n");
104     }
105 
106     THPSimpleSetBuffer(buffer);
107 
108     OSReport("\n#######################################\n");
109     OSReport("Push A button     : restart the movie, select audio track at random\n");
110     OSReport("Push B button     : play/stop midi file using AX\n");
111     OSReport("Push Start button : application end\n");
112     OSReport("Pad up            : volume up\n");
113     OSReport("Pad down          : volume down\n");
114     OSReport("#######################################\n");
115 
116 RESTART:
117     // Preload for playback
118     if (THPSimplePreLoad(THP_PLAY_LOOP) == FALSE)
119     {
120         OSHalt("THPSimplePreLoad fail");
121     }
122 
123     audioTrack = (s32)(OSGetTick() % audioInfo.sndNumTracks);
124 
125     start = 1;
126 
127     count = VIGetRetraceCount();
128 
129     while(1)
130     {
131         DEMOPadRead();
132 
133         buttonDown = DEMOPadGetButtonDown(PAD_CHAN0);
134         button     = DEMOPadGetButton(PAD_CHAN0);
135 
136         DEMOBeforeRender();
137 
138         // Decode 1 frame of THP data
139         if (THPSimpleDecode(audioTrack) == THP_VIDEO_DECODE_FAIL)
140         {
141             OSHalt("Failed to decode video data");
142         }
143 
144         // Draw decoded THP video data
145         frame = THPSimpleDrawCurrentFrame(rmode, x, y, videoInfo.xSize, videoInfo.ySize);
146 
147         while (VIGetRetraceCount() < count + 1)
148         {
149             VIWaitForRetrace();
150         }
151 
152         DEMODoneRender();
153 
154         count = VIGetRetraceCount();
155 
156         if (start)
157         {
158             // Allow audio output
159             THPSimpleAudioStart();
160             start = 0;
161         }
162 
163         if (buttonDown & PAD_BUTTON_A)
164         {
165             // Stop playback and then restart
166             THPSimpleAudioStop();
167             THPSimpleLoadStop();
168             goto RESTART;
169         }
170 
171         if (buttonDown & PAD_BUTTON_START)
172         {
173             // Stop playback and exit from main loop
174             THPSimpleAudioStop();
175             THPSimpleLoadStop();
176             THPSimpleClose();
177 
178 #ifndef HOLLYWOOD_REV
179             OSFree(buffer);
180 #else
181             MEMFreeToExpHeap(__ExpHeap, buffer);
182 #endif
183 
184             break;
185         }
186 
187         if (buttonDown & PAD_BUTTON_B)
188         {
189             if (GetSeqState())
190             {
191                 SeqStop();
192             }
193             else
194             {
195                 SeqPlay();
196             }
197         }
198 
199         if (button & PAD_BUTTON_UP)
200         {
201             vol = THPSimpleGetVolume() + 1;
202             if (vol > 127)
203             {
204                 vol = 127;
205             }
206             THPSimpleSetVolume(vol, 0);
207         }
208 
209         if (button & PAD_BUTTON_DOWN)
210         {
211             vol = THPSimpleGetVolume() - 1;
212             if (vol < 0)
213             {
214                 vol = 0;
215             }
216             THPSimpleSetVolume(vol, 0);
217         }
218     }
219 
220     THPSimpleQuit();
221 
222     VIWaitForRetrace();
223 
224     OSHalt("application end\n");
225 
226     return;
227 }
228