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.

maps.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * maps.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * See <http://www.gnu.org/licenses/>.
  18. */
  19. #include <gbdk/platform.h>
  20. #include "maps.h"
  21. #include "title_map.h"
  22. #include "bg_map.h"
  23. #include "numbers.h"
  24. #define MAX_DIGITS 7
  25. // TODO inverted score color not visible on DMG
  26. const unsigned char num_attr_1[40] = {
  27. 0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  28. 0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  29. };
  30. const unsigned char num_attr_2[40] = {
  31. 0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,
  32. 0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,
  33. };
  34. const palette_color_t num_pal_inv[4] = {
  35. //RGB8( 0, 0, 0), RGB8(248,252,248), RGB8( 0, 0, 0), RGB8( 0, 0, 0)
  36. RGB8( 0, 0, 0), RGB8( 0, 0, 0), RGB8(248,252,248), RGB8( 0, 0, 0)
  37. };
  38. void map_title(void) {
  39. set_bkg_palette(OAMF_CGB_PAL0, title_map_PALETTE_COUNT, title_map_palettes);
  40. set_bkg_data(0, title_map_TILE_COUNT, title_map_tiles);
  41. set_bkg_attributes(0, 0, title_map_MAP_ATTRIBUTES_WIDTH, title_map_MAP_ATTRIBUTES_HEIGHT, title_map_MAP_ATTRIBUTES);
  42. set_bkg_tiles(0, 0, title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H, title_map_map);
  43. }
  44. void map_game(void) {
  45. set_bkg_palette(OAMF_CGB_PAL0, bg_map_PALETTE_COUNT, bg_map_palettes);
  46. set_bkg_data(0, bg_map_TILE_COUNT, bg_map_tiles);
  47. set_bkg_attributes(0, 0, bg_map_MAP_ATTRIBUTES_WIDTH, bg_map_MAP_ATTRIBUTES_HEIGHT, bg_map_MAP_ATTRIBUTES);
  48. set_bkg_tiles(0, 0, bg_map_WIDTH / bg_map_TILE_W, bg_map_HEIGHT / bg_map_TILE_H, bg_map_map);
  49. }
  50. inline void set_win_based(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles, uint8_t base_tile, const uint8_t *attributes)
  51. {
  52. VBK_REG = VBK_ATTRIBUTES;
  53. set_win_tiles(x, y, w, h, attributes);
  54. VBK_REG = VBK_TILES;
  55. set_win_based_tiles(x, y, w, h, tiles, base_tile);
  56. }
  57. static void digit(uint8_t val, uint8_t digit, uint8_t x_off, uint8_t is_black) {
  58. uint8_t off = val * numbers_WIDTH / numbers_TILE_W;
  59. set_win_based(x_off + (digit * numbers_WIDTH / numbers_TILE_W), 0,
  60. numbers_WIDTH / numbers_TILE_W, 1,
  61. numbers_map + off, bg_map_TILE_COUNT,
  62. (is_black ? num_attr_2 : num_attr_1) + off);
  63. set_win_based(x_off + (digit * numbers_WIDTH / numbers_TILE_W), 1,
  64. numbers_WIDTH / numbers_TILE_W, 1,
  65. numbers_map + off + (sizeof(numbers_map) / 2), bg_map_TILE_COUNT,
  66. (is_black ? num_attr_2 : num_attr_1) + off);
  67. }
  68. void win_game_load(void) {
  69. set_bkg_palette(OAMF_CGB_PAL0 + bg_map_PALETTE_COUNT, numbers_PALETTE_COUNT, numbers_palettes);
  70. set_bkg_palette(OAMF_CGB_PAL0 + bg_map_PALETTE_COUNT + 1, numbers_PALETTE_COUNT, num_pal_inv);
  71. set_win_data(bg_map_TILE_COUNT, numbers_TILE_COUNT, numbers_tiles);
  72. }
  73. void win_game_draw(int32_t score) {
  74. // TODO can not set numbers larger than int16 max?!
  75. //score = 32767 + 1; // wtf?!
  76. uint8_t is_black = 0;
  77. if (score < 0) {
  78. score = -score;
  79. is_black = 1;
  80. }
  81. uint8_t len = 0;
  82. uint8_t digits[MAX_DIGITS];
  83. do {
  84. digits[len++] = score % 10L;
  85. score = score / 10L;
  86. if (len >= MAX_DIGITS) {
  87. break;
  88. }
  89. } while (score > 0);
  90. // if the number was too large for our buffer don't draw anything
  91. if (score > 0) {
  92. return;
  93. }
  94. for (uint8_t i = 0; i < len; i++) {
  95. digit(digits[len - i - 1], i, 10 - len, is_black);
  96. }
  97. }