GameBoy (Color) port of the GTA San Andreas arcade game Duality
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * main.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * Based on examples from gbdk-2020:
  8. * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/rand/rand.c
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * See <http://www.gnu.org/licenses/>.
  21. */
  22. #include <gbdk/metasprites.h>
  23. #include <rand.h>
  24. #include "banks.h"
  25. #include "config.h"
  26. #include "gb/hardware.h"
  27. #include "maps.h"
  28. #include "obj.h"
  29. #include "sprites.h"
  30. #include "sound.h"
  31. #include "input.h"
  32. #include "game.h"
  33. #include "score.h"
  34. #include "sgb_border.h"
  35. #include "border_sgb.h"
  36. #include "timer.h"
  37. #include "sample.h"
  38. #include "window.h"
  39. #include "gbprinter.h"
  40. #include "multiplayer.h"
  41. #include "table_speed_shot.h"
  42. #include "main.h"
  43. uint8_t debug_menu_index = 0;
  44. uint8_t debug_special_value = 0;
  45. static uint8_t anim_frame = 0;
  46. static uint8_t anim_state = 0;
  47. BANKREF(main)
  48. const struct conf_entry conf_entries[CONF_ENTRY_COUNT] = {
  49. //{ .name = "sfx-vol", .var = &mem.config.sfx_vol, .max = 3 },
  50. { .name = "musi-vol", .var = &mem.config.music_vol, .max = 15 },
  51. { .name = "game-map", .var = &mem.config.game_bg, .max = 1 },
  52. };
  53. const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
  54. { .name = "marker", .flag = DBG_MARKER, .max = 1 }, // 0
  55. { .name = "invuln", .flag = DBG_GOD_MODE, .max = 1 }, // 1
  56. { .name = "no-spawn", .flag = DBG_NO_OBJ, .max = 1 }, // 2
  57. { .name = "no-fuel", .flag = DBG_NO_FUEL, .max = 1 }, // 3
  58. { .name = "fastmove", .flag = DBG_FAST, .max = 1 }, // 4
  59. { .name = "s-frames", .flag = DBG_SHOW_FRAMES, .max = 1 }, // 5
  60. { .name = "s-timer", .flag = DBG_SHOW_TIMER, .max = 1 }, // 6
  61. { .name = "s-stack", .flag = DBG_SHOW_STACK, .max = 1 }, // 7
  62. // keep at end
  63. { .name = "music", .flag = DBG_NONE, .max = SND_COUNT }, // 8
  64. { .name = "sfx-test", .flag = DBG_NONE, .max = SFX_COUNT }, // 9
  65. { .name = "cl score", .flag = DBG_NONE, .max = 1 }, // 10
  66. { .name = "0 scores", .flag = DBG_NONE, .max = 1 }, // 11
  67. };
  68. #define DEBUG_MENU_MUSIC_INDEX (DEBUG_ENTRY_COUNT - 4)
  69. #define DEBUG_MENU_SFX_INDEX (DEBUG_ENTRY_COUNT - 3)
  70. #define DEBUG_MENU_CLEAR_INDEX (DEBUG_ENTRY_COUNT - 2)
  71. #define DEBUG_MENU_ZERO_INDEX (DEBUG_ENTRY_COUNT - 1)
  72. static void list_scores(uint8_t is_black) {
  73. for (uint8_t i = 0; i < SCORE_NUM; i++) {
  74. struct scores score;
  75. is_black ? score_lowest(i, &score) : score_highest(i, &score);
  76. win_score_draw(score, i, is_black);
  77. }
  78. }
  79. static void highscore(uint8_t is_black) {
  80. HIDE_WIN;
  81. move_win(MINWNDPOSX, MINWNDPOSY);
  82. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  83. win_score_clear(is_black ? 1 : 0, 0);
  84. SHOW_WIN;
  85. list_scores(is_black);
  86. while (1) {
  87. key_read();
  88. if (key_pressed(J_A) || key_pressed(J_B)) {
  89. break;
  90. } else if (key_pressed(J_SELECT)) {
  91. enum PRN_STATUS status = gbprinter_detect();
  92. if ((status & PRN_STATUS_MASK_ANY) == PRN_STATUS_OK) {
  93. win_score_clear(is_black, 1);
  94. list_scores(is_black);
  95. status = gbprinter_screenshot(1, is_black ? PRN_PALETTE_SC_B : PRN_PALETTE_SC_W);
  96. }
  97. if (status != PRN_STATUS_OK) {
  98. win_score_clear(2, 0);
  99. win_score_print(status);
  100. while (1) {
  101. key_read();
  102. if (key_pressed(0xFF)) break;
  103. vsync();
  104. }
  105. }
  106. break;
  107. }
  108. vsync();
  109. }
  110. }
  111. static void about_screen(void) {
  112. HIDE_WIN;
  113. move_win(MINWNDPOSX, MINWNDPOSY);
  114. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  115. win_about();
  116. SHOW_WIN;
  117. while (1) {
  118. key_read();
  119. if (mp_master_ready()) {
  120. mp_master_start();
  121. break;
  122. }
  123. win_about_mp();
  124. if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
  125. break;
  126. }
  127. vsync();
  128. }
  129. }
  130. static void conf_screen(void) {
  131. HIDE_WIN;
  132. debug_menu_index = 0;
  133. move_win(MINWNDPOSX, MINWNDPOSY);
  134. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  135. win_conf();
  136. SHOW_WIN;
  137. while (1) {
  138. key_read();
  139. if (key_pressed(J_SELECT)) {
  140. about_screen();
  141. break;
  142. } else if (key_pressed(J_UP)) {
  143. if (debug_menu_index > 0) {
  144. debug_menu_index--;
  145. } else {
  146. debug_menu_index = CONF_ENTRY_COUNT - 1;
  147. }
  148. win_conf();
  149. } else if (key_pressed(J_DOWN)) {
  150. if (debug_menu_index < (CONF_ENTRY_COUNT - 1)) {
  151. debug_menu_index++;
  152. } else {
  153. debug_menu_index = 0;
  154. }
  155. win_conf();
  156. } else if (key_pressed(J_LEFT)) {
  157. if (*conf_entries[debug_menu_index].var > 0) {
  158. (*conf_entries[debug_menu_index].var)--;
  159. } else {
  160. *conf_entries[debug_menu_index].var = conf_entries[debug_menu_index].max;
  161. }
  162. conf_write_crc();
  163. win_conf();
  164. } else if (key_pressed(J_RIGHT)) {
  165. if (*conf_entries[debug_menu_index].var < conf_entries[debug_menu_index].max) {
  166. (*conf_entries[debug_menu_index].var)++;
  167. } else {
  168. *conf_entries[debug_menu_index].var = 0;
  169. }
  170. conf_write_crc();
  171. win_conf();
  172. } else if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_START)) {
  173. break;
  174. }
  175. vsync();
  176. }
  177. debug_menu_index = 0;
  178. }
  179. static void splash_win(void) {
  180. HIDE_WIN;
  181. if (conf_get()->debug_flags & DBG_MENU) {
  182. win_debug();
  183. move_win(MINWNDPOSX, MINWNDPOSY);
  184. } else {
  185. // initially show the top 1 scores
  186. struct scores score;
  187. score_lowest(0, &score);
  188. int32_t low = score.score;
  189. score_highest(0, &score);
  190. int32_t high = score.score;
  191. win_splash_draw(-low, high);
  192. move_win(MINWNDPOSX, MINWNDPOSY);
  193. }
  194. SHOW_WIN;
  195. }
  196. static void splash_anim(uint8_t *hiwater) NONBANKED {
  197. if (++anim_frame >= 60) {
  198. anim_frame = 0;
  199. if (++anim_state >= 12) {
  200. anim_state = 0;
  201. }
  202. }
  203. int16_t spd_off_x = 0;
  204. int16_t spd_off_y = 0;
  205. int32_t score = 0;
  206. obj_do(&spd_off_x, &spd_off_y, &score, hiwater, 1);
  207. /*
  208. * 0: right
  209. * 1: right shoot
  210. * 2: right
  211. * 3: top-right (add)
  212. * 4: top
  213. * 5: top-left
  214. * 6: left
  215. * 7: left shoot
  216. * 8: left
  217. * 9: top-left (add)
  218. * 10: top
  219. * 11: top-right
  220. */
  221. switch (anim_state) {
  222. case 1:
  223. if (anim_frame == 0) {
  224. START_ROM_BANK(BANK(table_speed_shot)) {
  225. obj_add(SPR_SHOT, SHIP_OFF, -42,
  226. table_speed_shot[(ROT_90 * table_speed_shot_WIDTH) + 0],
  227. -table_speed_shot[(ROT_90 * table_speed_shot_WIDTH) + 1]);
  228. } END_ROM_BANK;
  229. sample_play(SFX_SHOT);
  230. }
  231. case 0:
  232. case 2:
  233. spr_draw(SPR_SHIP, FLIP_NONE, -4, -42 - 1, 4, hiwater);
  234. break;
  235. case 3:
  236. if (anim_frame == 30) {
  237. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  238. }
  239. case 11:
  240. spr_draw(SPR_SHIP, FLIP_NONE, 1, -42 - 1, 2, hiwater);
  241. break;
  242. case 9:
  243. if (anim_frame == 30) {
  244. obj_add(SPR_DARK, -42, -42, 0, 0);
  245. }
  246. case 5:
  247. spr_draw(SPR_SHIP, FLIP_X, -1, -42 - 1, 2, hiwater);
  248. break;
  249. case 4:
  250. case 10:
  251. spr_draw(SPR_SHIP, FLIP_NONE, -1, -42 + 4, 0, hiwater);
  252. break;
  253. case 7:
  254. if (anim_frame == 0) {
  255. START_ROM_BANK(BANK(table_speed_shot)) {
  256. obj_add(SPR_SHOT, -SHIP_OFF, -42,
  257. table_speed_shot[(ROT_270 * table_speed_shot_WIDTH) + 0],
  258. -table_speed_shot[(ROT_270 * table_speed_shot_WIDTH) + 1]);
  259. } END_ROM_BANK;
  260. sample_play(SFX_SHOT);
  261. }
  262. case 6:
  263. case 8:
  264. spr_draw(SPR_SHIP, FLIP_X, 4, -42, 4, hiwater);
  265. break;
  266. }
  267. }
  268. void splash(void) BANKED {
  269. snd_music_off();
  270. snd_note_off();
  271. disable_interrupts();
  272. DISPLAY_OFF;
  273. map_load(1);
  274. map_fill(MAP_TITLE, 1);
  275. SHOW_BKG;
  276. spr_init_pal();
  277. SHOW_SPRITES;
  278. SPRITES_8x8;
  279. anim_frame = 0;
  280. anim_state = 0;
  281. obj_init();
  282. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  283. obj_add(SPR_DARK, -42, -42, 0, 0);
  284. splash_win();
  285. DISPLAY_ON;
  286. enable_interrupts();
  287. if (!(conf_get()->debug_flags & DBG_MENU)) {
  288. snd_music(SND_MENU);
  289. }
  290. while (1) {
  291. key_read();
  292. if (mp_slave_ready()) {
  293. mp_slave_start();
  294. splash_win();
  295. }
  296. win_splash_mp();
  297. if (key_pressed(J_LEFT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  298. highscore(1);
  299. splash_win();
  300. } else if (key_pressed(J_RIGHT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  301. highscore(0);
  302. splash_win();
  303. } else if (key_pressed(J_SELECT)) {
  304. conf_screen();
  305. splash_win();
  306. } else if (key_pressed(J_START)) {
  307. if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
  308. conf_get()->debug_flags |= DBG_MENU;
  309. snd_music_off();
  310. snd_note_off();
  311. conf_write_crc();
  312. splash_win();
  313. } else {
  314. break;
  315. }
  316. } else {
  317. if (conf_get()->debug_flags & DBG_MENU) {
  318. uint8_t switch_special = 0;
  319. if (key_pressed(J_UP)) {
  320. if (debug_menu_index > 0) {
  321. debug_menu_index--;
  322. } else {
  323. debug_menu_index = DEBUG_ENTRY_COUNT - 1;
  324. }
  325. debug_special_value = 0;
  326. snd_music_off();
  327. snd_note_off();
  328. splash_win();
  329. } else if (key_pressed(J_DOWN)) {
  330. if (debug_menu_index < (DEBUG_ENTRY_COUNT - 1)) {
  331. debug_menu_index++;
  332. } else {
  333. debug_menu_index = 0;
  334. }
  335. debug_special_value = 0;
  336. snd_music_off();
  337. snd_note_off();
  338. splash_win();
  339. } else if (key_pressed(J_LEFT)) {
  340. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  341. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  342. conf_write_crc();
  343. } else {
  344. if (debug_special_value > 0) {
  345. debug_special_value--;
  346. } else {
  347. debug_special_value = debug_entries[debug_menu_index].max;
  348. }
  349. switch_special = 1;
  350. }
  351. splash_win();
  352. } else if (key_pressed(J_RIGHT)) {
  353. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  354. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  355. conf_write_crc();
  356. } else {
  357. if (debug_special_value < debug_entries[debug_menu_index].max) {
  358. debug_special_value++;
  359. } else {
  360. debug_special_value = 0;
  361. }
  362. switch_special = 1;
  363. }
  364. splash_win();
  365. } else if (key_pressed(J_A)) {
  366. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  367. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  368. conf_write_crc();
  369. } else {
  370. if (debug_special_value < debug_entries[debug_menu_index].max) {
  371. debug_special_value++;
  372. } else {
  373. debug_special_value = 0;
  374. }
  375. switch_special = 1;
  376. }
  377. splash_win();
  378. } else if (key_pressed(J_B)) {
  379. conf_get()->debug_flags &= ~DBG_MENU;
  380. debug_special_value = 0;
  381. conf_write_crc();
  382. splash_win();
  383. snd_music(SND_MENU);
  384. }
  385. if (switch_special && (debug_menu_index == DEBUG_MENU_MUSIC_INDEX)) {
  386. snd_music_off();
  387. if (debug_special_value > 0) {
  388. snd_music(debug_special_value - 1);
  389. }
  390. snd_note_off();
  391. } else if ((switch_special || (!sample_running()))
  392. && (debug_menu_index == DEBUG_MENU_SFX_INDEX)) {
  393. if (debug_special_value > 0) {
  394. sample_play(debug_special_value - 1);
  395. }
  396. } else if (switch_special && debug_special_value
  397. && (debug_menu_index == DEBUG_MENU_CLEAR_INDEX)) {
  398. debug_special_value = 0;
  399. score_reset();
  400. conf_write_crc();
  401. splash_win();
  402. } else if (switch_special && debug_special_value
  403. && (debug_menu_index == DEBUG_MENU_ZERO_INDEX)) {
  404. debug_special_value = 0;
  405. score_zero();
  406. conf_write_crc();
  407. splash_win();
  408. }
  409. }
  410. }
  411. uint8_t hiwater = SPR_NUM_START;
  412. if (!(conf_get()->debug_flags & DBG_MENU)) {
  413. if (conf_get()->debug_flags & DBG_MARKER) {
  414. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -10, 0, &hiwater);
  415. spr_draw(SPR_SHOT_LIGHT, FLIP_NONE, 0, -10, 0, &hiwater);
  416. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 0, 0, &hiwater);
  417. spr_draw(SPR_SHOT, FLIP_NONE, 0, 0, 0, &hiwater);
  418. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 10, 0, &hiwater);
  419. spr_draw(SPR_SHOT_DARK, FLIP_NONE, 0, 10, 0, &hiwater);
  420. spr_draw(SPR_DEBUG, FLIP_NONE, 42, -42, 0, &hiwater);
  421. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -42, 0, &hiwater);
  422. spr_draw(SPR_DEBUG, FLIP_NONE, -42, -42, 0, &hiwater);
  423. }
  424. splash_anim(&hiwater);
  425. }
  426. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  427. vsync();
  428. }
  429. }
  430. uint16_t ask_name(int32_t score) BANKED {
  431. snd_music_off();
  432. snd_note_off();
  433. disable_interrupts();
  434. DISPLAY_OFF;
  435. map_load(1);
  436. map_fill(MAP_TITLE, 0);
  437. SHOW_BKG;
  438. spr_init_pal();
  439. SHOW_SPRITES;
  440. SPRITES_8x8;
  441. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  442. win_name(score);
  443. move_win(MINWNDPOSX, MINWNDPOSY);
  444. SHOW_WIN;
  445. DISPLAY_ON;
  446. enable_interrupts();
  447. snd_music(SND_GAMEOVER);
  448. char name[3] = { 'a', 'a', 'a' };
  449. uint8_t pos = 0;
  450. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  451. while (1) {
  452. key_read();
  453. if (key_pressed(J_LEFT)) {
  454. if (pos > 0) {
  455. pos--;
  456. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  457. }
  458. } else if (key_pressed(J_RIGHT)) {
  459. if (pos < 3) {
  460. pos++;
  461. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  462. }
  463. } else if (key_pressed(J_UP)) {
  464. if (pos < 3) {
  465. name[pos]++;
  466. if (name[pos] > 'z') {
  467. name[pos] -= 'z' - 'a' + 1;
  468. }
  469. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  470. }
  471. } else if (key_pressed(J_DOWN)) {
  472. if (pos < 3) {
  473. name[pos]--;
  474. if (name[pos] < 'a') {
  475. name[pos] += 'z' - 'a' + 1;
  476. }
  477. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  478. }
  479. } else if (key_pressed(J_A)) {
  480. if (pos < 3) {
  481. pos++;
  482. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  483. } else {
  484. break;
  485. }
  486. } else if (key_pressed(J_START)) {
  487. break;
  488. }
  489. vsync();
  490. }
  491. return convert_name(name[0], name[1], name[2]);
  492. }
  493. static void sgb_init(void) NONBANKED {
  494. // Wait 4 frames
  495. // For SGB on PAL SNES this delay is required on startup, otherwise borders don't show up
  496. for (uint8_t i = 0; i < 4; i++) {
  497. vsync();
  498. }
  499. DISPLAY_ON;
  500. START_ROM_BANK(BANK(border_sgb)) {
  501. set_sgb_border((const uint8_t *)border_sgb_tiles, sizeof(border_sgb_tiles),
  502. (const uint8_t *)border_sgb_map, sizeof(border_sgb_map),
  503. (const uint8_t *)border_sgb_palettes, sizeof(border_sgb_palettes));
  504. } END_ROM_BANK
  505. DISPLAY_OFF;
  506. }
  507. void main(void) NONBANKED {
  508. // load sgb border
  509. sgb_init();
  510. // "cheat" and enable double-speed CPU mode on GBC
  511. if (_cpu == CGB_TYPE) {
  512. cpu_fast();
  513. }
  514. conf_init();
  515. timer_init();
  516. spr_init();
  517. snd_init();
  518. splash();
  519. uint16_t seed = DIV_REG;
  520. waitpadup();
  521. seed |= ((uint16_t)DIV_REG) << 8;
  522. initarand(seed);
  523. while (1) {
  524. int32_t score = game(GM_SINGLE);
  525. if ((!(conf_get()->debug_flags)) && (score != 0) && score_ranking(score)) {
  526. uint16_t name = ask_name(score);
  527. struct scores s = { .name = name, .score = score };
  528. score_add(s);
  529. }
  530. splash();
  531. }
  532. }