-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
380 lines (319 loc) · 10.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*===========================================
Mahjongg Solitaire Wii
Code : Justin Wood
============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <ogcsys.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <unistd.h>
#include <fat.h>
#include <aesndlib.h>
#include <gcmodplay.h> // modplayer
#include "GRRLIB/GRRLIB.h"
#include "commons.h"
#include "menu.h"
#include "game.h"
#include "disk.h"
#include "sushi_mod.h"
#include "dojo_dan_oriental_mod.h"
#include "graveyard_mod.h"
#include "egypt_crap_mod.h"
#include "childhood_mod.h"
#include "nebulos_mod.h"
#include "gfx/bigmenuback_jpg.h"
#include "gfx/credits_png.h"
#include "gfx/handpointerred_png.h"
#include "gfx/handpointergreen_png.h"
Mtx GXmodelView2D;
static GXRModeObj *rmode = NULL;
static MODPlay mod_track;
#define STARTUP 2
#define GAME 1
#define MENU 0
#define FILE_CFG "rpmjg.cfg"
static int main_mode=STARTUP;
static int alpha=0; //,xm=64,xs=255;
bool opt_rumble=true, opt_hoverhint=false, opt_widescreen=false;
int opt_sound=255, opt_music=64, opt_layout=0, opt_lang=-1, opt_tileset=0;
static int evctr = 0;
u8 *layouts[LAYOUTS]; //extern
unsigned long g_scores[LAYOUTS * 2];//extern
static const int languages[] = {JAPANESE,ENGLISH,GERMAN,FRENCH,SPANISH,ITALIAN,DUTCH};
#define MAX_SOUND_SLOT 2
AESNDPB *sound[MAX_SOUND_SLOT];
static int next_voice_idx = 0;
void Init_Voice() {
for (int i=0; i<MAX_SOUND_SLOT; i++) {
sound[i] = NULL;
sound[i] = AESND_AllocateVoice(NULL);
}
}
void Free_Voice() {
for (int i=0; i<MAX_SOUND_SLOT; i++) {
AESND_FreeVoice(sound[i]);
sound[i] = NULL;
}
}
static void processMenuOption(int menuopt)
{
switch(menuopt) {
case EXIT :
killMenuLanguages();
MODPlay_Unload (&mod_track);
Free_Voice();
WPAD_Shutdown();
GRRLIB_Stop();
saveConfig(FILE_CFG);
// if we have been launched from a channel then reset to menu
if (!*((u32*) 0x80001800)) {
WII_Initialize();
WII_ReturnToMenu();
}
else { // else the homebrew is launched from a loader so exit back to the loader
exit(0);
}
break;
case ONE_PLAYER_GAME :
case TWO_PLAYER_COOP :
case TWO_PLAYER_VERSUS :
main_mode=GAME;
initGame(menuopt);
break;
}
if(menuopt>=MUSIC_BAR && menuopt<=(MUSIC_BAR+64)) {
MODPlay_SetVolume( &mod_track, opt_music, opt_music);
}
}
static void countevs(long int chan, const WPADData *data)
{
evctr++;
}
static void setupLayouts()
{
layouts[0]=(u8 *)default_mtl;
layouts[1]=(u8 *)cross_mtl;
layouts[2]=(u8 *)butterfly_mtl;
layouts[3]=(u8 *)fortress_mtl;
layouts[4]=(u8 *)crab_mtl;
layouts[5]=(u8 *)spider_mtl;
layouts[6]=(u8 *)fourbridges_mtl;
layouts[7]=(u8 *)cloud_mtl;
layouts[8]=(u8 *)arena_mtl;
layouts[9]=(u8 *)tictactoe_mtl;
layouts[10]=(u8 *)tale_mtl;
layouts[11]=(u8 *)tikal_mtl;
}
static u8 HWButton = 0;
/**
* Callback for the reset button on the Wii.
*/
static void WiiResetPressed()
{
HWButton = SYS_RETURNTOMENU;
}
/**
* Callback for the power button on the Wii.
*/
static void WiiPowerPressed()
{
HWButton = SYS_POWEROFF_STANDBY;
}
/**
* Callback for the power button on the Wiimote.
* @param[in] chan The Wiimote that pressed the button
*/
static void WiimotePowerPressed(s32 chan)
{
HWButton = SYS_POWEROFF_STANDBY;
}
static void initMain()
{
// try to load a saved config, if none then get Wii language menu
if(!loadConfig(FILE_CFG)) {
// get the language of the Wii menu and map this to the Mahjongg Wii languages
if(CONF_GetLanguage()<7) opt_lang = languages[CONF_GetLanguage()];
// dirty hack to force English if not German as default
if (opt_lang != 2) {
opt_lang = 1;
}
}
GRRLIB_Widescreen(opt_widescreen);
// setup the layouts array for use in menu and game
setupLayouts();
//init scores with 0
int i;
for( i = 0; i < LAYOUTS * 2; ++i) {
g_scores[i] = 0;
}
//load scores
loadHighscores( FILE_HIGHSCORE, g_scores);
}
int main(int argc, char* argv[])
{
u8 *tex_ptrone=GRRLIB_LoadTexture(handpointerred_png);
u8 *tex_ptrtwo=GRRLIB_LoadTexture(handpointergreen_png);
u8 *tex_back=GRRLIB_LoadJPG(bigmenuback_jpg, bigmenuback_jpg_size);
u8 *tex_fore=GRRLIB_LoadTexture(credits_png);
fatInitDefault();
GRRLIB_InitVideo();
WPAD_Init();
SYS_SetResetCallback(WiiResetPressed);
SYS_SetPowerCallback(WiiPowerPressed);
WPAD_SetPowerButtonCallback(WiimotePowerPressed);
rmode = VIDEO_GetPreferredMode(NULL);
AESND_Init(NULL);
MODPlay_Init(&mod_track);
Init_Voice();
AESND_Pause(0); // the sound loop is running now
// set the MOD song
if (MODPlay_SetMOD (&mod_track, dojo_dan_oriental_mod) < 0 ) {
MODPlay_Unload (&mod_track);
} else {
// set the music volume to the minimum so we don't hear the music before saved volume is known
MODPlay_SetVolume( &mod_track, 0,0);
MODPlay_Start (&mod_track);// Play the MOD
}
WPAD_SetDataFormat(WPAD_CHAN_ALL, WPAD_FMT_BTNS_ACC_IR);
WPAD_SetVRes(WPAD_CHAN_ALL, rmode->fbWidth, rmode->xfbHeight);
initMain();
MODPlay_SetVolume( &mod_track, opt_music, opt_music);
while( HWButton == 0) {
WPAD_ScanPads();
u32 wpad_one_down = WPAD_ButtonsDown(0);
u32 wpad_two_down = WPAD_ButtonsDown(1);
u32 type;
WPADData *wd_one, *wd_two;
WPAD_ReadPending(WPAD_CHAN_ALL, countevs);
WPAD_Probe(WPAD_CHAN_ALL, &type);
wd_one = WPAD_Data(0);
wd_two = WPAD_Data(1);
switch(main_mode) {
case STARTUP :
GRRLIB_FillScreen(0xFF000000);
GRRLIB_DrawImg(0, 0, 640, 480, tex_back, 0, 1, 1, alpha>255?255:alpha);
GRRLIB_DrawImg(68,308, 256, 80, tex_fore, 0, 1, 1, alpha>255?255:alpha);
if(alpha++>394 || wpad_one_down > 0) {
main_mode=MENU;
if(tex_back) free(tex_back);
if(tex_fore) free(tex_fore);
initMenu();
}
break;
case MENU :
GRRLIB_FillScreen(0xFF000000);
int menuopt = menuWiimote(wd_one,wpad_one_down);
if(menuopt==EXIT) {
if(tex_ptrone) free(tex_ptrone);
if(tex_ptrtwo) free(tex_ptrtwo);
}
if(menuopt>NOTHING) {
processMenuOption(menuopt);
if(main_mode==GAME) {
MODPlay_Unload (&mod_track);
if(opt_tileset==SPOOKY)
MODPlay_SetMOD (&mod_track, graveyard_mod);
else
if(opt_tileset==EGYPTIAN)
MODPlay_SetMOD (&mod_track, egypt_crap_mod);
else
if(opt_tileset==SIMPLE)
MODPlay_SetMOD (&mod_track, childhood_mod);
else
if(opt_tileset==SPACE)
MODPlay_SetMOD (&mod_track, nebulos_mod);
else
MODPlay_SetMOD (&mod_track, sushi_mod);
MODPlay_SetVolume( &mod_track, opt_music, opt_music);
MODPlay_Start (&mod_track);
}
else
drawMenu(wd_one);
}
else
drawMenu(wd_one);
break;
case GAME :
if(gameWiimote(wd_one,wpad_one_down,wd_two,wpad_two_down)) {
// we are exiting the game back to the menu
main_mode=MENU;
MODPlay_Unload (&mod_track);
MODPlay_SetMOD (&mod_track, dojo_dan_oriental_mod);
MODPlay_SetVolume( &mod_track, opt_music, opt_music);
MODPlay_Start (&mod_track);
killGame();
initMenu();
}
else {
drawGame();
}
break;
}
// alternate which pointer is on top every frame to not give the advantage to player one in two player mode
static int ticktock=0;
ticktock++;
if(wd_two->ir.valid && ticktock%2==0) {
if(main_mode==GAME && whatGameMode()==ONE_PLAYER_GAME) {
// don't display second pointer in one player mode
}
else
GRRLIB_DrawColImg(wd_two->ir.x - 9,wd_two->ir.y - 7,68,80,tex_ptrtwo,0,1,1,0xEEFFFFFF);
}
if(wd_one->ir.valid) {
GRRLIB_DrawColImg(wd_one->ir.x - 9,wd_one->ir.y - 7,68,80,tex_ptrone,0,1,1,0xEEFFFFFF);
}
if(wd_two->ir.valid && ticktock%2!=0) {
if(main_mode==GAME && whatGameMode()==ONE_PLAYER_GAME) {
// don't display second pointer in one player mode
}
else
GRRLIB_DrawColImg(wd_two->ir.x - 9,wd_two->ir.y - 7,68,80,tex_ptrtwo,0,1,1,0xEEFFFFFF);
}
if(wd_one->btns_h & WPAD_BUTTON_1) {
GRRLIB_ScrShot("MahjonggWii_Screen_%y%m%d_%H%M%S.png",time(NULL));
}
GRRLIB_Render();
}
// we are exiting, free the mallocs
switch( main_mode) {
case GAME:
killGame();
}
if(tex_ptrone) free(tex_ptrone);
if(tex_ptrtwo) free(tex_ptrtwo);
killMenuLanguages();
MODPlay_Unload (&mod_track);
Free_Voice();
WPAD_Shutdown();
GRRLIB_Stop();
saveConfig(FILE_CFG);
SYS_ResetSystem(HWButton, 0, 0);
return 0;
}
#define VOICE_RUNNING 0x40000000 // from aesndlib.c
void playRaw(const void *buf, const u32 len, u32 freq, u16 vol_left, u16 vol_right, u32 delay) {
int i=next_voice_idx;
/*
for (i=0; sound[i] && i < MAX_SOUND_SLOT; i++) {
u32 *pFlag = (u32 *)&(((char *)(sound[i]))[40]); // offset 40 is flags
if (!(*pFlag & VOICE_RUNNING))
break;
}
*/
if (i == MAX_SOUND_SLOT)
i = 0;
if (i > 0 && sound[i] == NULL)
i = 0;
if (sound[i]) {
AESND_PlayVoice(sound[i], VOICE_MONO16, buf, len, freq, delay, 0);
AESND_SetVoiceVolume(sound[i], vol_left, vol_right);
next_voice_idx = i+1;
} else {
next_voice_idx = 0;
}
}
// vim:et sw=4 ts=4 ai