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

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