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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 "maps.h"
  27. #include "obj.h"
  28. #include "sprites.h"
  29. #include "sound.h"
  30. #include "input.h"
  31. #include "game.h"
  32. #include "score.h"
  33. #include "sgb_border.h"
  34. #include "border_sgb.h"
  35. #include "timer.h"
  36. #include "sample.h"
  37. #include "main.h"
  38. uint8_t debug_menu_index = 0;
  39. uint8_t debug_special_value = 0;
  40. BANKREF(main)
  41. const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
  42. { .name = "marker", .flag = DBG_MARKER, .max = 1 }, // 0
  43. { .name = "invuln", .flag = DBG_GOD_MODE, .max = 1 }, // 1
  44. { .name = "music", .flag = DBG_NONE, .max = 3 }, // 2
  45. { .name = "sfx-test", .flag = DBG_NONE, .max = 3 }, // 3
  46. { .name = "cl score", .flag = DBG_CLEAR_SCORE, .max = 1 }, // 4
  47. { .name = "0 scores", .flag = DBG_ZERO_SCORE, .max = 1 }, // 5
  48. };
  49. static void highscore(uint8_t is_black) NONBANKED {
  50. HIDE_WIN;
  51. move_win(MINWNDPOSX, MINWNDPOSY);
  52. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  53. win_score_clear(is_black ? 1 : 0);
  54. SHOW_WIN;
  55. for (uint8_t i = 0; i < SCORE_NUM; i++) {
  56. struct scores score;
  57. is_black ? score_lowest(i, &score) : score_highest(i, &score);
  58. win_score_draw(score, i, is_black);
  59. }
  60. while (1) {
  61. key_read();
  62. if (key_pressed(J_A) || key_pressed(J_B)) {
  63. break;
  64. }
  65. vsync();
  66. }
  67. }
  68. static void about_screen(void) NONBANKED {
  69. HIDE_WIN;
  70. move_win(MINWNDPOSX, MINWNDPOSY);
  71. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  72. win_about();
  73. SHOW_WIN;
  74. while (1) {
  75. key_read();
  76. if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
  77. break;
  78. }
  79. vsync();
  80. }
  81. }
  82. static void splash_win(void) NONBANKED {
  83. HIDE_WIN;
  84. if (conf_get()->debug_flags & DBG_MENU) {
  85. win_debug();
  86. move_win(MINWNDPOSX, MINWNDPOSY);
  87. } else {
  88. // initially show the top 1 scores
  89. struct scores score;
  90. score_lowest(0, &score);
  91. int32_t low = score.score;
  92. score_highest(0, &score);
  93. int32_t high = score.score;
  94. win_splash_draw(-low, high);
  95. move_win(MINWNDPOSX, MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - (8 * 4));
  96. }
  97. SHOW_WIN;
  98. }
  99. static void splash_anim(uint8_t *hiwater) NONBANKED {
  100. static uint8_t frame = 0;
  101. static uint8_t state = 0;
  102. if (++frame >= 60) {
  103. frame = 0;
  104. if (++state >= 10) {
  105. state = 0;
  106. }
  107. }
  108. int16_t spd_off_x = 0;
  109. int16_t spd_off_y = 0;
  110. int32_t score = 0;
  111. obj_do(&spd_off_x, &spd_off_y, &score, hiwater, 1);
  112. switch (state) {
  113. case 0:
  114. case 2:
  115. spr_draw(SPR_SHIP, FLIP_NONE, -4, -42 - 1, 4, hiwater);
  116. break;
  117. case 1:
  118. spr_draw(SPR_SHIP, FLIP_NONE, -4, -42 - 1, 4, hiwater);
  119. if (frame == 0) {
  120. obj_add(SPR_SHOT, SHIP_OFF, -42, SHOT_SPEED, 0);
  121. sample_play_shoot();
  122. }
  123. break;
  124. case 3:
  125. if (frame == 30) {
  126. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  127. }
  128. spr_draw(SPR_SHIP, FLIP_NONE, -1, -42 + 4, 0, hiwater);
  129. break;
  130. case 8:
  131. if (frame == 30) {
  132. obj_add(SPR_DARK, -42, -42, 0, 0);
  133. }
  134. spr_draw(SPR_SHIP, FLIP_NONE, -1, -42 + 4, 0, hiwater);
  135. break;
  136. case 4:
  137. case 9:
  138. spr_draw(SPR_SHIP, FLIP_NONE, -1, -42 + 4, 0, hiwater);
  139. break;
  140. case 5:
  141. case 7:
  142. spr_draw(SPR_SHIP, FLIP_X, 4, -42, 4, hiwater);
  143. break;
  144. case 6:
  145. spr_draw(SPR_SHIP, FLIP_X, 4, -42, 4, hiwater);
  146. if (frame == 0) {
  147. obj_add(SPR_SHOT, -SHIP_OFF, -42, -SHOT_SPEED, 0);
  148. sample_play_shoot();
  149. }
  150. break;
  151. }
  152. }
  153. static void splash(void) NONBANKED {
  154. snd_music_off();
  155. snd_note_off();
  156. disable_interrupts();
  157. DISPLAY_OFF;
  158. map_title();
  159. move_bkg(0, 0);
  160. SHOW_BKG;
  161. spr_init_pal();
  162. SHOW_SPRITES;
  163. SPRITES_8x8;
  164. obj_init();
  165. obj_add(SPR_LIGHT, 42, -42, 0, 0);
  166. obj_add(SPR_DARK, -42, -42, 0, 0);
  167. win_init(1);
  168. splash_win();
  169. DISPLAY_ON;
  170. enable_interrupts();
  171. if (!(conf_get()->debug_flags & DBG_MENU)) {
  172. snd_menu_music();
  173. }
  174. while (1) {
  175. key_read();
  176. if (key_pressed(J_LEFT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  177. highscore(1);
  178. splash_win();
  179. } else if (key_pressed(J_RIGHT) && (!(conf_get()->debug_flags & DBG_MENU))) {
  180. highscore(0);
  181. splash_win();
  182. } else if (key_pressed(J_SELECT)) {
  183. about_screen();
  184. splash_win();
  185. } else if (key_pressed(J_START)) {
  186. if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
  187. conf_get()->debug_flags |= DBG_MENU;
  188. snd_music_off();
  189. snd_note_off();
  190. conf_write_crc();
  191. splash_win();
  192. } else {
  193. break;
  194. }
  195. } else {
  196. if (conf_get()->debug_flags & DBG_MENU) {
  197. // do it here so you quickly see the flag going to 1 and back to 0
  198. if (conf_get()->debug_flags & DBG_CLEAR_SCORE) {
  199. score_reset();
  200. conf_get()->debug_flags &= ~DBG_CLEAR_SCORE;
  201. conf_write_crc();
  202. splash_win();
  203. }
  204. if (conf_get()->debug_flags & DBG_ZERO_SCORE) {
  205. score_zero();
  206. conf_get()->debug_flags &= ~DBG_ZERO_SCORE;
  207. conf_write_crc();
  208. splash_win();
  209. }
  210. uint8_t switch_special = 0;
  211. if (key_pressed(J_UP)) {
  212. if (debug_menu_index > 0) {
  213. debug_menu_index--;
  214. } else {
  215. debug_menu_index = DEBUG_ENTRY_COUNT - 1;
  216. }
  217. debug_special_value = 0;
  218. snd_music_off();
  219. snd_note_off();
  220. splash_win();
  221. } else if (key_pressed(J_DOWN)) {
  222. if (debug_menu_index < (DEBUG_ENTRY_COUNT - 1)) {
  223. debug_menu_index++;
  224. } else {
  225. debug_menu_index = 0;
  226. }
  227. debug_special_value = 0;
  228. snd_music_off();
  229. snd_note_off();
  230. splash_win();
  231. } else if (key_pressed(J_LEFT)) {
  232. START_ROM_BANK(BANK(main));
  233. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  234. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  235. conf_write_crc();
  236. } else {
  237. if (debug_special_value > 0) {
  238. debug_special_value--;
  239. } else {
  240. debug_special_value = debug_entries[debug_menu_index].max;
  241. }
  242. switch_special = 1;
  243. }
  244. END_ROM_BANK();
  245. splash_win();
  246. } else if (key_pressed(J_RIGHT)) {
  247. START_ROM_BANK(BANK(main));
  248. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  249. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  250. conf_write_crc();
  251. } else {
  252. if (debug_special_value < debug_entries[debug_menu_index].max) {
  253. debug_special_value++;
  254. } else {
  255. debug_special_value = 0;
  256. }
  257. switch_special = 1;
  258. }
  259. END_ROM_BANK();
  260. splash_win();
  261. } else if (key_pressed(J_A)) {
  262. START_ROM_BANK(BANK(main));
  263. if (debug_entries[debug_menu_index].flag != DBG_NONE) {
  264. conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
  265. conf_write_crc();
  266. } else {
  267. if (debug_special_value < debug_entries[debug_menu_index].max) {
  268. debug_special_value++;
  269. } else {
  270. debug_special_value = 0;
  271. }
  272. switch_special = 1;
  273. }
  274. END_ROM_BANK();
  275. splash_win();
  276. } else if (key_pressed(J_B)) {
  277. conf_get()->debug_flags &= ~DBG_MENU;
  278. debug_special_value = 0;
  279. conf_write_crc();
  280. splash_win();
  281. snd_menu_music();
  282. }
  283. if (switch_special && (debug_menu_index == 2)) {
  284. switch (debug_special_value) {
  285. case 0:
  286. snd_music_off();
  287. snd_note_off();
  288. break;
  289. case 1:
  290. snd_menu_music();
  291. snd_note_off();
  292. break;
  293. case 2:
  294. snd_game_music();
  295. snd_note_off();
  296. break;
  297. case 3:
  298. snd_gameover_music();
  299. snd_note_off();
  300. break;
  301. default:
  302. break;
  303. }
  304. } else if (switch_special && (debug_menu_index == 3)) {
  305. switch (debug_special_value) {
  306. case 1:
  307. sample_play_shoot();
  308. break;
  309. case 2:
  310. sample_play_explosion_orbs();
  311. break;
  312. case 3:
  313. sample_play_explosion_ship();
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. uint8_t hiwater = SPR_NUM_START;
  322. if (!(conf_get()->debug_flags & DBG_MENU)) {
  323. if (conf_get()->debug_flags & DBG_MARKER) {
  324. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -10, 0, &hiwater);
  325. spr_draw(SPR_SHOT_LIGHT, FLIP_NONE, 0, -10, 0, &hiwater);
  326. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 0, 0, &hiwater);
  327. spr_draw(SPR_SHOT, FLIP_NONE, 0, 0, 0, &hiwater);
  328. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 10, 0, &hiwater);
  329. spr_draw(SPR_SHOT_DARK, FLIP_NONE, 0, 10, 0, &hiwater);
  330. spr_draw(SPR_DEBUG, FLIP_NONE, 42, -42, 0, &hiwater);
  331. spr_draw(SPR_DEBUG, FLIP_NONE, 0, -42, 0, &hiwater);
  332. spr_draw(SPR_DEBUG, FLIP_NONE, -42, -42, 0, &hiwater);
  333. }
  334. splash_anim(&hiwater);
  335. }
  336. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  337. vsync();
  338. }
  339. }
  340. static uint16_t ask_name(int32_t score) NONBANKED {
  341. snd_music_off();
  342. snd_note_off();
  343. disable_interrupts();
  344. DISPLAY_OFF;
  345. map_title();
  346. move_bkg(0, 0);
  347. SHOW_BKG;
  348. spr_init_pal();
  349. SHOW_SPRITES;
  350. SPRITES_8x8;
  351. hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
  352. win_init(1);
  353. win_name(score);
  354. move_win(MINWNDPOSX, MINWNDPOSY);
  355. SHOW_WIN;
  356. DISPLAY_ON;
  357. enable_interrupts();
  358. snd_gameover_music();
  359. char name[3] = { 'a', 'a', 'a' };
  360. uint8_t pos = 0;
  361. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  362. while (1) {
  363. key_read();
  364. if (key_pressed(J_LEFT)) {
  365. if (pos > 0) {
  366. pos--;
  367. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  368. }
  369. } else if (key_pressed(J_RIGHT)) {
  370. if (pos < 3) {
  371. pos++;
  372. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  373. }
  374. } else if (key_pressed(J_UP)) {
  375. if (pos < 3) {
  376. name[pos]++;
  377. if (name[pos] > 'z') {
  378. name[pos] -= 'z' - 'a' + 1;
  379. }
  380. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  381. }
  382. } else if (key_pressed(J_DOWN)) {
  383. if (pos < 3) {
  384. name[pos]--;
  385. if (name[pos] < 'a') {
  386. name[pos] += 'z' - 'a' + 1;
  387. }
  388. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  389. }
  390. } else if (key_pressed(J_A)) {
  391. if (pos < 3) {
  392. pos++;
  393. win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
  394. } else {
  395. break;
  396. }
  397. } else if (key_pressed(J_START)) {
  398. break;
  399. }
  400. vsync();
  401. }
  402. return convert_name(name[0], name[1], name[2]);
  403. }
  404. static void sgb_init(void) NONBANKED {
  405. // Wait 4 frames
  406. // For SGB on PAL SNES this delay is required on startup, otherwise borders don't show up
  407. for (uint8_t i = 0; i < 4; i++) {
  408. vsync();
  409. }
  410. DISPLAY_ON;
  411. START_ROM_BANK(BANK(border_sgb));
  412. set_sgb_border((const uint8_t *)border_sgb_tiles, sizeof(border_sgb_tiles),
  413. (const uint8_t *)border_sgb_map, sizeof(border_sgb_map),
  414. (const uint8_t *)border_sgb_palettes, sizeof(border_sgb_palettes));
  415. END_ROM_BANK();
  416. DISPLAY_OFF;
  417. }
  418. void main(void) NONBANKED {
  419. // load sgb border
  420. sgb_init();
  421. // "cheat" and enable double-speed CPU mode on GBC
  422. if (_cpu == CGB_TYPE) {
  423. cpu_fast();
  424. }
  425. conf_init();
  426. timer_init();
  427. spr_init();
  428. snd_init();
  429. splash();
  430. uint16_t seed = DIV_REG;
  431. waitpadup();
  432. seed |= ((uint16_t)DIV_REG) << 8;
  433. initarand(seed);
  434. while (1) {
  435. int32_t score = game();
  436. if ((!(conf_get()->debug_flags & DBG_GOD_MODE)) && (score != 0) && score_ranking(score)) {
  437. uint16_t name = ask_name(score);
  438. struct scores s = { .name = name, .score = score };
  439. score_add(s);
  440. }
  441. splash();
  442. }
  443. }