1 /*---------------------------------------------------------------------------*
2   Project:  THP Player
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.4  2008/05/30 12:00:33  aka
15   Added switch of stereo <-> mono.
16 
17   Revision 1.3  2006/10/11 02:52:02  aka
18   Revised HOLLYWOOD_REV.
19 
20   Revision 1.2  2006/03/06 09:59:05  kawaset
21   Eliminated warnings.
22 
23   Revision 1.1  2006/02/03 10:01:41  aka
24   Imported from Dolphin tree.
25 
26 
27     4     2003/11/25 11:24 Dante
28     Japanese to English translation of comments and text strings
29 
30     3     2002/05/14 9:08a Suzuki
31     supported multi audio track
32 
33     2     2002/02/28 6:36p Akagi
34     enabled to use with MusyX/AX by Suzuki-san (IRD).
35 
36     1     2002/01/16 10:52a Akagi
37     Initial revision made by Suzuki-san (IRD).
38 
39   $NoKeywords: $
40 
41  *---------------------------------------------------------------------------*/
42 
43 #include <string.h>
44 #include <math.h>
45 #include <demo.h>
46 #include <revolution/mem.h>
47 
48 #include "axseq.h"
49 
50 #include "THPPlayer.h"
51 
52 static void MovieDoneRender(void);
53 
54 MEMHeapHandle   __ExpHeap;
55 
main(void)56 void main(void)
57 {
58     u32             size, x, y;
59     u8              *buffer;
60     GXRenderModeObj *rmode;
61     GXColor         black;
62     THPVideoInfo    videoInfo;
63     THPAudioInfo    audioInfo;
64     s32             frame, vol, audioTrack, smode;
65     u16             buttonDown, button, dir;
66     char            *fileName;
67     BOOL            open, play, onMemory;
68     void            *arenaMem2Lo;
69     void            *arenaMem2Hi;
70 
71     black.r = 0;
72     black.g = 0;
73     black.b = 0;
74     black.a = 255;
75 
76     DEMOInit(&GXNtsc480Int);
77 
78     arenaMem2Lo = OSGetMEM2ArenaLo();
79     arenaMem2Hi = OSGetMEM2ArenaHi();
80     __ExpHeap   = MEMCreateExpHeap(arenaMem2Lo, (u32)arenaMem2Hi - (u32) arenaMem2Lo);
81 
82     GXSetDispCopyGamma(GX_GM_1_0);
83     GXSetCopyFilter(FALSE, NULL, FALSE, NULL);
84     GXSetCopyClear(black, 0xffffff);
85 
86     rmode = DEMOGetRenderModeObj();
87 
88     AIInit(NULL);
89     AXSeqSetup();
90     THPPlayerInit(THP_MODE_WITH_AX);
91 
92     open     = FALSE;
93     play     = FALSE;
94     onMemory = FALSE;
95 
96     OSReport("\n");
97     OSReport("###########################################################\n");
98     OSReport("Push A Button     : Play the movie with audio\n");
99     OSReport("Push B Button     : Play the movie with audio on the memory\n");
100     OSReport("Push X Button     : Play the interlace movie\n");
101     OSReport("Push Y Button     : Play the movie with multiple audio track,\n");
102     OSReport("                  : select audio track at random\n");
103     OSReport("Push L Trigger    : Pause / Restart the movie\n");
104     OSReport("Push R Trigger    : Skip one frame if the movie is paused\n");
105     OSReport("Pad Up            : Volume up\n");
106     OSReport("Pad Down          : Volume down\n");
107     OSReport("Pad Right         : Play/stop midi file using AX\n");
108     OSReport("Push Start Button : Application end\n");
109     OSReport("###########################################################\n");
110 
111     while(1)
112     {
113         DEMOPadRead();
114 
115         buttonDown = DEMOPadGetButtonDown(PAD_CHAN0);
116         button     = DEMOPadGetButton(PAD_CHAN0);
117         dir        = DEMOPadGetDirs(PAD_CHAN0);
118 
119         DEMOBeforeRender();
120 
121         if (play)
122         {
123             frame = THPPlayerDrawCurrentFrame(rmode, x, y, videoInfo.xSize, videoInfo.ySize);
124         }
125 
126         // Movie Playback DoneRender
127         MovieDoneRender();
128 
129         // Check player state
130         if (THPPlayerGetState() == THP_PLAYER_ERROR)
131         {
132             THPPlayerStop();
133             THPPlayerClose();
134             MEMFreeToExpHeap(__ExpHeap, buffer);
135             OSHalt("Error happen");
136         }
137 
138         if (buttonDown & PAD_BUTTON_A)
139         {
140             fileName = "thpdemo/rebirth.thp";
141             open     = TRUE;
142             onMemory = FALSE;
143         }
144 
145         if (buttonDown & PAD_BUTTON_B)
146         {
147             fileName = "thpdemo/fish.thp";
148             open     = TRUE;
149             onMemory = TRUE;
150         }
151 
152         if (buttonDown & PAD_BUTTON_X)
153         {
154             fileName = "thpdemo/fish_int.thp";
155             open     = TRUE;
156             onMemory = FALSE;
157         }
158 
159         if (buttonDown & PAD_BUTTON_Y)
160         {
161             fileName = "thpdemo/fish_mlt.thp";
162             open     = TRUE;
163             onMemory = FALSE;
164         }
165 
166         if (buttonDown & PAD_TRIGGER_L)
167         {
168             if (THPPlayerGetState() == THP_PLAYER_PAUSE)
169             {
170                 THPPlayerPlay();
171             }
172             else
173             {
174                 THPPlayerPause();
175             }
176         }
177 
178         if (buttonDown & PAD_TRIGGER_R)
179         {
180             THPPlayerSkip();
181         }
182 
183         if (buttonDown & PAD_BUTTON_START)
184         {
185             if (play)
186             {
187                 THPPlayerStop();
188                 THPPlayerClose();
189                 MEMFreeToExpHeap(__ExpHeap, buffer);
190             }
191 
192             break;
193         }
194 
195         if (button & PAD_BUTTON_UP)
196         {
197             vol = THPPlayerGetVolume() + 1;
198             if (vol > 127)
199             {
200                 vol = 127;
201             }
202             THPPlayerSetVolume(vol, 0);
203         }
204 
205         if (button & PAD_BUTTON_DOWN)
206         {
207             vol = THPPlayerGetVolume() - 1;
208             if (vol < 0)
209             {
210                 vol = 0;
211             }
212             THPPlayerSetVolume(vol, 0);
213         }
214 
215         if (buttonDown & PAD_BUTTON_RIGHT)
216         {
217             if (GetSeqState())
218             {
219                 SeqStop();
220             }
221             else
222             {
223                 SeqPlay();
224             }
225         }
226 
227         if (buttonDown & PAD_TRIGGER_Z)
228         {
229             smode = THPPlayerGetSoundMode();
230             if (smode == THP_SOUND_MODE_MONO)
231             {
232                 THPPlayerSetSoundMode(THP_SOUND_MODE_STEREO);
233                 OSReport("Change sound mode : mono -> stereo.\n");
234             }
235             else if (smode == THP_SOUND_MODE_STEREO)
236             {
237                 THPPlayerSetSoundMode(THP_SOUND_MODE_MONO);
238                 OSReport("Change sound mode : stereo -> mono.\n");
239             }
240             else
241             {
242                 OSReport("Change sound mode : ???.\n");
243             }
244         }
245         if (open)
246         {
247             // If playing back, release Stop and work area
248             if (play)
249             {
250                 THPPlayerStop();
251                 THPPlayerClose();
252                 MEMFreeToExpHeap(__ExpHeap, buffer);
253                 play = FALSE;
254             }
255         }
256 
257         if (open && !play)
258         {
259             // Open THP file
260             if (THPPlayerOpen(fileName, onMemory) == FALSE)
261             {
262                 OSHalt("Fail to open the thp file\n");
263             }
264 
265             THPPlayerGetVideoInfo(&videoInfo);
266             THPPlayerGetAudioInfo(&audioInfo);
267 
268             x = (rmode->fbWidth   - videoInfo.xSize) / 2;
269             y = (rmode->efbHeight - videoInfo.ySize) / 2;
270 
271             // Set aside work area
272             size = THPPlayerCalcNeedMemory();
273             buffer = MEMAllocFromExpHeapEx(__ExpHeap, size, 32);
274             if (!buffer)
275             {
276                 OSHalt("Can't allocate the memory");
277             }
278 
279             // Set work area
280             THPPlayerSetBuffer(buffer);
281 
282             // Select randomly, if there are multiple audio tracks.
283             if (audioInfo.sndNumTracks != 1)
284             {
285                 audioTrack = (s32)(OSGetTick() % audioInfo.sndNumTracks);
286             }
287             else
288             {
289                 audioTrack = 0;
290             }
291 
292             // Preparation before playback
293             if (THPPlayerPrepare(0, THP_PLAY_LOOP, audioTrack) == FALSE)
294             {
295                 OSHalt("Fail to prepare\n");
296             }
297 
298             // Start playback
299             THPPlayerPlay();
300 
301             open = FALSE;
302             play = TRUE;
303         }
304     }
305 
306     THPPlayerQuit();
307 
308     VIWaitForRetrace();
309 
310     OSHalt("application end\n");
311 
312     return;
313 }
314 
MovieDoneRender(void)315 static void MovieDoneRender(void)
316 {
317     // Release used THP video data at same time as graphics processor
318     THPPlayerDrawDone();
319 
320     DEMODoneRender();
321 }
322