-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
575 lines (479 loc) · 13.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include <jo/jo.h>
#include "global_defines.h"
#include "analogPad.h"
#include "pcmsys.h"
#include "pcmstm.h"
#include "pcmcdda.h"
#include "musicHandler.h"
#include "model.h"
#include "font3D.h"
#include "score.h"
#include "background.h"
#include "ui.h"
#include "uiControls.h"
#include "bullet.h"
#include "bulletList.h"
#include "tools.h"
#include "explosions.h"
#include "player.h"
#include "npc.h"
#include "pickup.h"
#include "gamemode.h"
#include "gamemodeEndless.h"
#include "gamemodeStory.h"
#include "gamemodeManager.h"
#include "game.h"
#include "introCinematic.h"
void MenuCreateGame(bool backButton);
void MenuCreateMain(bool backButton);
void GameCreateGameOver();
void GameCreateDemoOver();
#define LWRAM_HEAP_SIZE 0x40000 // number of bytes to extend heap by
/* Music stuff */
void *loading_system_scratch_buffer = (void *)LWRAM;
int snd_adx = 0;
/* Indicates whether game assets have loaded */
static bool GameLoaded = false;
/* Logo mesh collection */
static SaturnMesh Logo;
/* Game over score label text */
static char GameOverScoreLabel[SCORE_TEXT_LEN + 1];
/* Game end score */
static int CurrentGameEndScore = 0;
/* Do game draw */
static bool DoGameDraw = false;
/* Is coop mode */
static bool IsCoop = false;
/* Is endless mode */
static bool IsEndless = false;
/** @brief Resume game button action
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuResumePauseGame(bool backButton)
{
if (!backButton)
{
for (jo_node *node = WidgetsGetAll()->first; node != JO_NULL; node = node->next)
{
((WidgetsWidget*)(node->data.ptr))->IsVisible = !((WidgetsWidget*)(node->data.ptr))->IsVisible;
}
if (!WidgetsById(0)->IsVisible)
{
// Game was not paused
WidgetsSetCurrent(WidgetsById(2));
MusicSetVolumeDirect(4);
}
else
{
// Game was paused
WidgetsSetCurrent(WidgetsById(0));
MusicSetVolumeDirect(7);
}
}
}
/** @brief Check if any controller got start button pressed
* @return Was any start button pressed
*/
bool AnyStartButtonPressed()
{
for(int input = 0; input < JO_INPUT_MAX_DEVICE; input++)
{
if (jo_is_input_available(input) && jo_is_input_key_down(input, JO_KEY_START))
{
return true;
}
}
return false;
}
/** @brief Game logic handler
* @param widget Game screen widget
* @param message Game logic message
*/
void GameHandler(WidgetsWidget *widget, WidgetMessages message)
{
switch (message)
{
case WIDGET_INIT:
GameInitializeMortal(IsCoop, IsEndless);
break;
case WIDGET_FREE:
GameDisposeMortal();
break;
case WIDGET_INPUT:
// Create pause menu
if (AnyStartButtonPressed())
{
jo_clear_screen();
MenuResumePauseGame(false);
return;
}
CurrentState state;
state.GameEnd = ResultNone;
GameUpdateTick(&state);
CurrentGameEndScore = state.Score;
if (state.GameEnd == ResultGameOver)
{
GameCreateGameOver();
}
if (state.GameEnd == ResultDemoOver)
{
GameCreateDemoOver();
}
break;
case WIDGET_DRAW:
GameScoreDraw();
DoGameDraw = true;
break;
default:
break;
}
}
/** @brief Exit game and go to main menu
* @param backButton Indicates whether back button (B) was pressed
*/
void GameExit(bool backButton)
{
if (!backButton)
{
WidgetsClearAll();
MenuCreateMain(backButton);
MusicSetVolumeDirect(7);
MusicSetCurrentDirect(MENU_MUSIC, true);
}
}
/** @brief Called when new game starts
* @param backButton Indicates whether back button (B) was pressed
*/
void GameCreateNew(bool backButton)
{
if (!backButton)
{
WidgetsClearAll();
WidgetsSetCurrent(WidgetsCreate(0, 0, GameHandler));
WidgetsCreateLabel(0, 15, "Game paused")->IsVisible = false;
WidgetsCreateButton(0, -10, "Resume game", MenuResumePauseGame)->IsVisible = false;
WidgetsCreateButton(0, -18, "Exit", GameExit)->IsVisible = false;
MusicSetCurrentDirect(LVL1_MUSIC, true);
}
else
{
MenuCreateGame(false);
}
}
/** @brief Create credits menu
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCredits(bool backButton)
{
if (!backButton)
{
WidgetsClearAll();
WidgetsCreateLabel(0, 18, "Coding and Art");
WidgetsCreateLabel(0, 10, "ReyeMe");
WidgetsCreateLabel(0, -2, "Music");
WidgetsCreateLabel(0, -10, "Random");
WidgetsCreateLabel(0, -22, "Art");
WidgetsCreateLabel(0, -30, "AnriFox");
WidgetsSetCurrent(WidgetsCreateButton(44, -40, "Exit", MenuCreateMain));
}
}
/* Draw logo process
* @widget Generic widget
* @message Control message
*/
void DrawLogo(WidgetsWidget *widget, WidgetMessages message)
{
static int rotation = 0;
if (message == WIDGET_DRAW)
{
jo_3d_push_matrix();
{
jo_3d_translate_matrix(widget->y, widget->x, -60);
jo_3d_rotate_matrix_x(rotation++);
TmfDraw(&Logo);
}
jo_3d_pop_matrix();
if (rotation > 359)
{
rotation = 0;
}
}
}
/** @brief Create coop game
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCreateCoop(bool backButton)
{
IsCoop = true;
GameCreateNew(backButton);
}
/** @brief Create game menu
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCreatePlayerMode(bool backButton)
{
if (!backButton)
{
IsCoop = false;
WidgetsClearAll();
WidgetsCreate(0, 17, DrawLogo)->IsSelectable = false;
WidgetsCreateLabel(0, -5, IsEndless ? "Endless play" : "Story mode");
WidgetsSetCurrent(WidgetsCreateButton(0, -22, "Single", GameCreateNew));
WidgetsCreateButton(0, -30, "Cooperative", MenuCreateCoop);
}
else
{
MenuCreateMain(false);
}
}
/** @brief Create coop game
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCreateEndless(bool backButton)
{
IsEndless = true;
MenuCreatePlayerMode(backButton);
}
/** @brief Create game menu
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCreateGame(bool backButton)
{
if (!backButton)
{
IsEndless = false;
WidgetsClearAll();
WidgetsCreate(0, 17, DrawLogo)->IsSelectable = false;
WidgetsCreateLabel(0, -5, "New game");
WidgetsSetCurrent(WidgetsCreateButton(0, -22, "Story demo", MenuCreatePlayerMode));
WidgetsCreateButton(0, -30, "Endless", MenuCreateEndless);
}
}
/** @brief Create main menu
* @param backButton Indicates whether back button (B) was pressed
*/
void MenuCreateMain(bool backButton)
{
if (!backButton)
{
WidgetsClearAll();
WidgetsCreate(0, 17, DrawLogo)->IsSelectable = false;
WidgetsCreateLabel(0, -5, "Sky Blaster");
WidgetsSetCurrent(WidgetsCreateButton(0, -22, "New game", MenuCreateGame));
WidgetsCreateButton(0, -30, "Credits", MenuCredits);
}
}
/** @brief Called when player runs out of lives
* @param backButton Indicates whether back button (B) was pressed
*/
void GameCreateGameOver()
{
WidgetsClearAll();
WidgetsCreateLabel(0, 15, "Game over");
sprintf(GameOverScoreLabel, "%d", CurrentGameEndScore);
WidgetsCreateLabel(0, 7, GameOverScoreLabel);
WidgetsSetCurrent(WidgetsCreateButton(0, -10, "Try again", GameCreateNew));
WidgetsCreateButton(0, -18, "Exit", GameExit);
MusicSetCurrentDirect(GAME_END_MUSIC, false);
}
/** @brief Called when player runs out of lives
* @param backButton Indicates whether back button (B) was pressed
*/
void GameCreateDemoOver()
{
WidgetsClearAll();
WidgetsCreateLabel(0, 15, "End of the demo");
WidgetsCreateLabel(0, 3, "Thanks");
WidgetsCreateLabel(0, -5, "for playing");
sprintf(GameOverScoreLabel, "%d", CurrentGameEndScore);
WidgetsCreateLabel(0, -18, GameOverScoreLabel);
WidgetsSetCurrent(WidgetsCreateButton(0, -28, "Exit", GameExit));
MusicSetCurrentDirect(CLEAR_MUSIC, false);
}
/** @brief Main gaim loop
*/
void MainLogic()
{
// Update player and background
BackgroundUdpate();
WidgetsInvokeInput();
}
/** @brief Start game fade in effect
*/
void StartGameFadeIn()
{
static int step = TOOLS_FADE_OFFSET;
if (step != 0)
{
if (step == TOOLS_FADE_OFFSET)
{
jo_core_tv_on();
}
slBack1ColSet((void *)BACK_CRAM, CD_Black);
slColOffsetB(-TOOLS_FADE_OFFSET, -TOOLS_FADE_OFFSET, -TOOLS_FADE_OFFSET);
slColOffsetOn(NBG0ON | NBG1ON | NBG2ON | NBG3ON | SPRON | RBG0ON);
slColOffsetBUse(NBG0ON | NBG1ON | NBG2ON | NBG3ON | SPRON | RBG0ON);
if (step > 0)
{
step -= TOOLS_FADE_SPEED;
slColOffsetB(-step, -step, -step);
}
else if (step < 0)
{
step = 0;
slColOffsetB(0, 0, 0);
}
}
}
/** @brief Redering loop
*/
void MainDraw()
{
StartGameFadeIn();
jo_3d_push_matrix();
{
jo_3d_rotate_matrix(0, 180, -90);
// Redraw active widgets
WidgetsRedraw();
}
jo_3d_pop_matrix();
// Game world
jo_3d_push_matrix();
{
jo_3d_translate_matrix_fixed(0, 0, (150) << 16);
// Draw world background
BackgroundDraw();
if (DoGameDraw)
{
GameDraw();
DoGameDraw = false;
}
}
jo_3d_pop_matrix();
}
/** @brief Loading screen
*/
void LoadingScreen()
{
static int rotation = 0;
static jo_fixed scale = JO_FIXED_1;
jo_printf(15, 20, "Loading...");
jo_3d_push_matrix();
{
jo_3d_rotate_matrix(0, 180, -90);
jo_3d_translate_matrix(0, 0, -60);
jo_3d_set_scale_fixed(scale, scale, scale);
jo_3d_rotate_matrix_x(rotation);
TmfDraw(&Logo);
}
jo_3d_pop_matrix();
if (GameLoaded)
{
rotation += 17;
scale -= 1700;
}
// Draw PoneSound logo
jo_printf(5, 27, "Audio powered by");
jo_printf(5, 28, "PoneSound");
jo_sprite_draw3D2(0, 0, 200, 90);
}
/** @brief Initialize game data
*/
void GameInitialize()
{
jo_core_tv_off();
// Load PoneSound logo
jo_sprite_add_tga(JO_ROOT_DIR, "PONE.TGA", JO_COLOR_Transparent);
// Load logo model
TmfLoadMesh(&Logo, "LOGO.TMF", JO_ROOT_DIR);
LoadingScreen();
// Fade in loading screen
ToolsFadeIn(SPRON | NBG2ON | RBG0ON | NBG0ON, LoadingScreen);
// Load background assets
BackgroundInitialize();
// Load bullets
BulletLoadAssets();
// Load font
FontInitialize();
// Load resources for controls
WidgetsControlsInitialize();
// Load NPCs and entities
ExplosionsInitialize();
NpcInitialize();
PickupInitialize();
BulletListInitialize();
// Load and initialize game
GameInitializeImmortal();
// All base assets are loaded now
GameLoaded = true;
// Show all other layers and turn off TV
ToolsFadeOut(SPRON | NBG2ON | RBG0ON | NBG0ON, LoadingScreen);
// Load ever present RGB0 background
BackgroundInitializeRGB0();
jo_clear_screen();
jo_core_set_screens_order(JO_NBG0_SCREEN, JO_SPRITE_SCREEN, JO_NBG2_SCREEN, JO_RBG0_SCREEN);
jo_set_displayed_screens(JO_SPRITE_SCREEN | JO_NBG0_SCREEN | JO_NBG2_SCREEN | JO_RBG0_SCREEN);
}
/** @brief game logic gets called here
*/
void Logic()
{
static bool startup = true;
slUnitMatrix(0);
MainLogic();
MainDraw();
CdcStat cdStatus;
CDC_GetCurStat(&cdStatus);
if (cdStatus.status == CDC_ST_OPEN)
{
jo_goto_boot_menu();
}
if (jo_is_pad1_key_pressed(JO_KEY_A) &&
jo_is_pad1_key_pressed(JO_KEY_B) &&
jo_is_pad1_key_pressed(JO_KEY_C) &&
jo_is_pad1_key_pressed(JO_KEY_START))
{
jo_goto_boot_menu();
}
if (startup)
{
MusicSetCurrentDirect(MENU_MUSIC, true);
startup = false;
}
}
/** @brief Initialize game
*/
void GameStart()
{
// Load sound driver
load_drv(ADX_MASTER_2304);
snd_adx = add_adx_front_buffer(23040);
add_adx_back_buffer((void *)LWRAM);
CDDAInitialize();
// V-Blank callback for audio handling
jo_core_add_vblank_callback(sdrv_stm_vblank_rq);
// Random seed
jo_random_seed = jo_time_get_frc();
/* Initialize game stuff */
WidgetsInitialize();
GameInitialize();
/* Make main menu */
MenuCreateMain(false);
// Start game
jo_core_add_callback(Logic);
}
/** @brief Application entry point
*/
void jo_main()
{
// Prepare scene
jo_core_init(JO_COLOR_Black);
*(volatile unsigned char *)0x060FFCD8 = 0x1F;
// extend the heap to use LWRAM as well otherwise our sprites won't fit in
// main memory
//jo_add_memory_zone((unsigned char *)LWRAM, LWRAM_HEAP_SIZE);
// Play intro cinematic
// Cinematics use their own sound driver, so we play it before loading pone sound
IntroCpkPlay(GameStart);
jo_core_run();
}