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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 "speed_table.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 = "music", .flag = DBG_NONE, .max = SND_COUNT }, // 5
  60. { .name = "sfx-test", .flag = DBG_NONE, .max = SFX_COUNT }, // 6
  61. { .name = "cl score", .flag = DBG_CLEAR_SCORE, .max = 1 }, // 7
  62. { .name = "0 scores", .flag = DBG_ZERO_SCORE, .max = 1 }, // 8
  63. };
  64. static void list_scores(uint8_t is_black) NONBANKED {
  65. for (uint8_t i = 0; i < SCORE_NUM; i++) {
  66. struct scores score;
  67. is_black ? score_lowest(i, &score) : score_highest(i, &score);
  68. win_score_draw(score, i, is_black);
  69. }
  70. }
  71. static void highscore(uint8_t is_black) NONBANKED {
  72. HIDE_WIN;
  73. move_win(MINWNDPOSX, MINWNDPOSY);
  74. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  75. win_score_clear(is_black ? 1 : 0, 0);
  76. SHOW_WIN;
  77. list_scores(is_black);
  78. while (1) {
  79. key_read();
  80. if (key_pressed(J_A) || key_pressed(J_B)) {
  81. break;
  82. } else if (key_pressed(J_SELECT)) {
  83. enum PRN_STATUS status = gbprinter_detect();
  84. if ((status & PRN_STATUS_MASK_ANY) == PRN_STATUS_OK) {
  85. win_score_clear(is_black, 1);
  86. list_scores(is_black);
  87. status = gbprinter_screenshot(1, is_black ? PRN_PALETTE_SC_B : PRN_PALETTE_SC_W);
  88. }
  89. if (status != PRN_STATUS_OK) {
  90. win_score_clear(2, 0);
  91. win_score_print(status);
  92. while (1) {
  93. key_read();
  94. if (key_pressed(0xFF)) break;
  95. vsync();
  96. }
  97. }
  98. break;
  99. }
  100. vsync();
  101. }
  102. }
  103. static void about_screen(void) NONBANKED {
  104. HIDE_WIN;
  105. move_win(MINWNDPOSX, MINWNDPOSY);
  106. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  107. win_about();
  108. SHOW_WIN;
  109. while (1) {
  110. key_read();
  111. if (mp_master_ready()) {
  112. mp_master_start();
  113. break;
  114. }
  115. win_about_mp();
  116. if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
  117. break;
  118. }
  119. vsync();
  120. }
  121. }
  122. static void conf_screen(void) NONBANKED {
  123. HIDE_WIN;
  124. debug_menu_index = 0;
  125. move_win(MINWNDPOSX, MINWNDPOSY);
  126. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  127. win_conf();
  128. SHOW_WIN;
  129. while (1) {
  130. key_read();
  131. if (key_pressed(J_SELECT)) {
  132. about_screen();
  133. break;
  134. } else if (key_pressed(J_UP)) {
  135. if (debug_menu_index > 0) {
  136. debug_menu_index--;
  137. } else {
  138. debug_menu_index = CONF_ENTRY_COUNT - 1;
  139. }
  140. win_conf();
  141. } else if (key_pressed(J_DOWN)) {
  142. if (debug_menu_index < (CONF_ENTRY_COUNT - 1)) {
  143. debug_menu_index++;
  144. } else {
  145. debug_menu_index = 0;
  146. }
  147. win_conf();
  148. } else if (key_pressed(J_LEFT)) {
  149. START_ROM_BANK(BANK(main)) {
  150. if (*conf_entries[debug_menu_index].var > 0) {
  151. (*conf_entries[debug_menu_index].var)--;
  152. } else {
  153. *conf_entries[debug_menu_index].var = conf_entries[debug_menu_index].max;
  154. }
  155. conf_write_crc();
  156. } END_ROM_BANK
  157. win_conf();
  158. } else if (key_pressed(J_RIGHT)) {
  159. START_ROM_BANK(BANK(main)) {
  160. if (*conf_entries[debug_menu_index].var < conf_entries[debug_menu_index].max) {
  161. (*conf_entries[debug_menu_index].var)++;
  162. } else {
  163. *conf_entries[debug_menu_index].var = 0;
  164. }
  165. conf_write_crc();
  166. } END_ROM_BANK
  167. win_conf();
  168. } else if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_START)) {
  169. break;
  170. }
  171. vsync();
  172. }
  173. debug_menu_index = 0;
  174. }
  175. static void splash_win(void) NONBANKED {
  176. HIDE_WIN;
  177. if (conf_get()->debug_flags & DBG_MENU) {
  178. win_debug();
  179. move_win(MINWNDPOSX, MINWNDPOSY);
  180. } else {
  181. // initially show the top 1 scores
  182. struct scores score;
  183. score_lowest(0, &score);
  184. int32_t low = score.score;
  185. score_highest(0, &score);
  186. int32_t high = score.score;
  187. win_splash_draw(-low, high);
  188. move_win(MINWNDPOSX, MINWNDPOSY);
  189. }
  190. SHOW_WIN;
  191. }
  192. static void splash_anim(uint8_t *hiwater) NONBANKED {
  193. if (++anim_frame >= 60) {
  194. anim_frame = 0;
  195. if (++anim_state >= 12) {
  196. anim_state = 0;
  197. }
  198. }
  199. int16_t spd_off_x = 0;
  200. int16_t spd_off_y = 0;
  201. int32_t score = 0;
  202. obj_do(&spd_off_x, &spd_off_y, &score, hiwater, 1);
  203. /*
  204. * 0: right
  205. * 1: right shoot
  206. * 2: right
  207. * 3: top-right (add)
  208. * 4: top
  209. * 5: top-left
  210. * 6: left
  211. * 7: left shoot
  212. * 8: left
  213. * 9: top-left (add)
  214. * 10: top
  215. * 11: top-right
  216. */
  217. switch (anim_state) {
  218. case 1:
  219. if (anim_frame == 0) {
  220. START_ROM_BANK(BANK(speed_table)) {
  221. obj_add(SPR_SHOT, SHIP_OFF, -42,
  222. speed_table[(ROT_90 * speed_table_WIDTH) + 0],
  223. -speed_table[(ROT_90 * speed_table_WIDTH) + 1]);
  224. } END_ROM_BANK;
  225. sample_play(SFX_SHOT);
  226. }
  227. case 0:
  228. case 2:
  229. spr_draw(SPR_SHIP, FLIP_NONE, -4, -42 - 1, 4, hiwater);
  230. break;
  231. case 3:
  232. if (anim_frame == 30) {
  233. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  234. }
  235. case 11:
  236. spr_draw(SPR_SHIP, FLIP_NONE, 1, -42 - 1, 2, hiwater);
  237. break;
  238. case 9:
  239. if (anim_frame == 30) {
  240. obj_add(SPR_DARK, -42, -42, 0, 0);
  241. }
  242. case 5:
  243. spr_draw(SPR_SHIP, FLIP_X, -1, -42 - 1, 2, hiwater);
  244. break;
  245. case 4:
  246. case 10:
  247. spr_draw(SPR_SHIP, FLIP_NONE, -1, -42 + 4, 0, hiwater);
  248. break;
  249. case 7:
  250. if (anim_frame == 0) {
  251. START_ROM_BANK(BANK(speed_table)) {
  252. obj_add(SPR_SHOT, -SHIP_OFF, -42,
  253. speed_table[(ROT_270 * speed_table_WIDTH) + 0],
  254. -speed_table[(ROT_270 * speed_table_WIDTH) + 1]);
  255. } END_ROM_BANK;
  256. sample_play(SFX_SHOT);
  257. }
  258. case 6:
  259. case 8:
  260. spr_draw(SPR_SHIP, FLIP_X, 4, -42, 4, hiwater);
  261. break;
  262. }
  263. }
  264. static void splash(void) NONBANKED {
  265. snd_music_off();
  266. snd_note_off();
  267. disable_interrupts();
  268. DISPLAY_OFF;
  269. map_load(1);
  270. map_fill(MAP_TITLE, 1);
  271. SHOW_BKG;
  272. spr_init_pal();
  273. SHOW_SPRITES;
  274. SPRITES_8x8;
  275. anim_frame = 0;
  276. anim_state = 0;
  277. obj_init();
  278. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  279. obj_add(SPR_DARK, -42, -42, 0, 0);
  280. splash_win();
  281. DISPLAY_ON;
  282. enable_interrupts();
  283. if (!(conf_get()->debug_flags & DBG_MENU)) {
  284. snd_music(SND_MENU);
  285. }
  286. while (1) {
  287. key_read();
  288. if (mp_slave_ready()) {
  289. mp_slave_start();
  290. splash_win();
  291. }
  292. win_splash_mp();
  293. if (key_pressed(J_LEFT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  294. highscore(1);
  295. splash_win();
  296. } else if (key_pressed(J_RIGHT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  297. highscore(0);
  298. splash_win();
  299. } else if (key_pressed(J_SELECT)) {
  300. conf_screen();
  301. splash_win();
  302. } else if (key_pressed(J_START)) {
  303. if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
  304. conf_get()->debug_flags |= DBG_MENU;
  305. snd_music_off();
  306. snd_note_off();
  307. conf_write_crc();
  308. splash_win();
  309. } else {
  310. break;
  311. }
  312. } else {
  313. if (conf_get()->debug_flags & DBG_MENU) {
  314. // do it here so you quickly see the flag going to 1 and back to 0
  315. if (conf_get()->debug_flags & DBG_CLEAR_SCORE) {
  316. score_reset();
  317. conf_get()->debug_flags &= ~DBG_CLEAR_SCORE;
  318. conf_write_crc();
  319. splash_win();
  320. }
  321. if (conf_get()->debug_flags & DBG_ZERO_SCORE) {
  322. score_zero();
  323. conf_get()->debug_flags &= ~DBG_ZERO_SCORE;
  324. conf_write_crc();
  325. splash_win();
  326. }
  327. uint8_t switch_special = 0;
  328. if (key_pressed(J_UP)) {
  329. if (debug_menu_index > 0) {
  330. debug_menu_index--;
  331. } else {
  332. debug_menu_index = DEBUG_ENTRY_COUNT - 1;
  333. }
  334. debug_special_value = 0;
  335. snd_music_off();
  336. snd_note_off();
  337. splash_win();
  338. } else if (key_pressed(J_DOWN)) {
  339. if (debug_menu_index < (DEBUG_ENTRY_COUNT - 1)) {
  340. debug_menu_index++;
  341. } else {
  342. debug_menu_index = 0;
  343. }
  344. debug_special_value = 0;
  345. snd_music_off();
  346. snd_note_off();
  347. splash_win();
  348. } else if (key_pressed(J_LEFT)) {
  349. START_ROM_BANK(BANK(main)) {
  350. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  351. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  352. conf_write_crc();
  353. } else {
  354. if (debug_special_value > 0) {
  355. debug_special_value--;
  356. } else {
  357. debug_special_value = debug_entries[debug_menu_index].max;
  358. }
  359. switch_special = 1;
  360. }
  361. } END_ROM_BANK
  362. splash_win();
  363. } else if (key_pressed(J_RIGHT)) {
  364. START_ROM_BANK(BANK(main)) {
  365. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  366. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  367. conf_write_crc();
  368. } else {
  369. if (debug_special_value < debug_entries[debug_menu_index].max) {
  370. debug_special_value++;
  371. } else {
  372. debug_special_value = 0;
  373. }
  374. switch_special = 1;
  375. }
  376. } END_ROM_BANK
  377. splash_win();
  378. } else if (key_pressed(J_A)) {
  379. START_ROM_BANK(BANK(main)) {
  380. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  381. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  382. conf_write_crc();
  383. } else {
  384. if (debug_special_value < debug_entries[debug_menu_index].max) {
  385. debug_special_value++;
  386. } else {
  387. debug_special_value = 0;
  388. }
  389. switch_special = 1;
  390. }
  391. } END_ROM_BANK
  392. splash_win();
  393. } else if (key_pressed(J_B)) {
  394. conf_get()->debug_flags &= ~DBG_MENU;
  395. debug_special_value = 0;
  396. conf_write_crc();
  397. splash_win();
  398. snd_music(SND_MENU);
  399. }
  400. if (switch_special && (debug_menu_index == 5)) {
  401. snd_music_off();
  402. if (debug_special_value > 0) {
  403. snd_music(debug_special_value - 1);
  404. }
  405. snd_note_off();
  406. } else if ((switch_special || (!sample_running())) && (debug_menu_index == 6)) {
  407. if (debug_special_value > 0) {
  408. sample_play(debug_special_value - 1);
  409. }
  410. }
  411. }
  412. }
  413. uint8_t hiwater = SPR_NUM_START;
  414. if (!(conf_get()->debug_flags & DBG_MENU)) {
  415. if (conf_get()->debug_flags & DBG_MARKER) {
  416. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -10, 0, &hiwater);
  417. spr_draw(SPR_SHOT_LIGHT, FLIP_NONE, 0, -10, 0, &hiwater);
  418. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 0, 0, &hiwater);
  419. spr_draw(SPR_SHOT, FLIP_NONE, 0, 0, 0, &hiwater);
  420. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 10, 0, &hiwater);
  421. spr_draw(SPR_SHOT_DARK, FLIP_NONE, 0, 10, 0, &hiwater);
  422. spr_draw(SPR_DEBUG, FLIP_NONE, 42, -42, 0, &hiwater);
  423. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -42, 0, &hiwater);
  424. spr_draw(SPR_DEBUG, FLIP_NONE, -42, -42, 0, &hiwater);
  425. }
  426. splash_anim(&hiwater);
  427. }
  428. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  429. vsync();
  430. }
  431. }
  432. static uint16_t ask_name(int32_t score) NONBANKED {
  433. snd_music_off();
  434. snd_note_off();
  435. disable_interrupts();
  436. DISPLAY_OFF;
  437. map_load(1);
  438. map_fill(MAP_TITLE, 0);
  439. SHOW_BKG;
  440. spr_init_pal();
  441. SHOW_SPRITES;
  442. SPRITES_8x8;
  443. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  444. win_name(score);
  445. move_win(MINWNDPOSX, MINWNDPOSY);
  446. SHOW_WIN;
  447. DISPLAY_ON;
  448. enable_interrupts();
  449. snd_music(SND_GAMEOVER);
  450. char name[3] = { 'a', 'a', 'a' };
  451. uint8_t pos = 0;
  452. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  453. while (1) {
  454. key_read();
  455. if (key_pressed(J_LEFT)) {
  456. if (pos > 0) {
  457. pos--;
  458. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  459. }
  460. } else if (key_pressed(J_RIGHT)) {
  461. if (pos < 3) {
  462. pos++;
  463. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  464. }
  465. } else if (key_pressed(J_UP)) {
  466. if (pos < 3) {
  467. name[pos]++;
  468. if (name[pos] > 'z') {
  469. name[pos] -= 'z' - 'a' + 1;
  470. }
  471. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  472. }
  473. } else if (key_pressed(J_DOWN)) {
  474. if (pos < 3) {
  475. name[pos]--;
  476. if (name[pos] < 'a') {
  477. name[pos] += 'z' - 'a' + 1;
  478. }
  479. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  480. }
  481. } else if (key_pressed(J_A)) {
  482. if (pos < 3) {
  483. pos++;
  484. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  485. } else {
  486. break;
  487. }
  488. } else if (key_pressed(J_START)) {
  489. break;
  490. }
  491. vsync();
  492. }
  493. return convert_name(name[0], name[1], name[2]);
  494. }
  495. static void sgb_init(void) NONBANKED {
  496. // Wait 4 frames
  497. // For SGB on PAL SNES this delay is required on startup, otherwise borders don't show up
  498. for (uint8_t i = 0; i < 4; i++) {
  499. vsync();
  500. }
  501. DISPLAY_ON;
  502. START_ROM_BANK(BANK(border_sgb)) {
  503. set_sgb_border((const uint8_t *)border_sgb_tiles, sizeof(border_sgb_tiles),
  504. (const uint8_t *)border_sgb_map, sizeof(border_sgb_map),
  505. (const uint8_t *)border_sgb_palettes, sizeof(border_sgb_palettes));
  506. } END_ROM_BANK
  507. DISPLAY_OFF;
  508. }
  509. void main(void) NONBANKED {
  510. // load sgb border
  511. sgb_init();
  512. // "cheat" and enable double-speed CPU mode on GBC
  513. if (_cpu == CGB_TYPE) {
  514. cpu_fast();
  515. }
  516. conf_init();
  517. timer_init();
  518. spr_init();
  519. snd_init();
  520. splash();
  521. uint16_t seed = DIV_REG;
  522. waitpadup();
  523. seed |= ((uint16_t)DIV_REG) << 8;
  524. initarand(seed);
  525. while (1) {
  526. int32_t score = game(GM_SINGLE);
  527. if ((!(conf_get()->debug_flags)) && (score != 0) && score_ranking(score)) {
  528. uint16_t name = ask_name(score);
  529. struct scores s = { .name = name, .score = score };
  530. score_add(s);
  531. }
  532. splash();
  533. }
  534. }