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

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