lol its in c
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.

128 lines
3.0 KiB

2 years ago
  1. #include "map.h"
  2. #include <string.h>
  3. #include "game.h"
  4. #include "player.h"
  5. #include "sdk/hardware.h"
  6. #include "vram.h"
  7. #define INIT_SCX ((SCRN_VX - SCRN_X) / 2)
  8. #define INIT_SCY ((SCRN_VY - SCRN_Y) / 2)
  9. map_t MAP;
  10. #define ROW_BUFFER_SIZE (SCRN_X_B + 4)
  11. #define COL_BUFFER_SIZE (SCRN_Y_B + 4)
  12. uint8_t pending_row[ROW_BUFFER_SIZE];
  13. uint16_t pending_row_dest;
  14. uint8_t pending_col[COL_BUFFER_SIZE];
  15. uint16_t pending_col_dest;
  16. static void map_write_row(uint16_t map_ptr, int8_t x0, int8_t y0);
  17. static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0);
  18. void map_load(map_t *map) {
  19. MAP = *map;
  20. rSCX = INIT_SCX;
  21. rSCY = INIT_SCY;
  22. PLAYER.x = (MAP.spawn_x - MAP.camera_x) * 8;
  23. PLAYER.y_hi = (MAP.spawn_y - MAP.camera_y) * 8;
  24. PLAYER.y_lo = 0;
  25. memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr,
  26. MAP.tile_size);
  27. int8_t x0 = MAP.camera_x - INIT_SCX / 8;
  28. int8_t y0 = MAP.camera_y - INIT_SCY / 8;
  29. uint16_t map_ptr = _SCRN0;
  30. for (uint8_t i = 0; i < SCRN_VY_B; ++i) {
  31. map_write_row(map_ptr, x0, y0);
  32. map_ptr += SCRN_VX_B;
  33. y0++;
  34. }
  35. pending_row_dest = 0;
  36. pending_col_dest = 0;
  37. }
  38. void map_update(void) {
  39. if (pending_row_dest != 0) {
  40. vram_enqueue_mem_xfer(XFER_TYPE_ROW, pending_row_dest, &pending_row[0],
  41. sizeof pending_row);
  42. pending_row_dest = 0;
  43. }
  44. if (pending_col_dest != 0) {
  45. vram_enqueue_mem_xfer(XFER_TYPE_COL, pending_col_dest, &pending_col[0],
  46. sizeof pending_col);
  47. pending_col_dest = 0;
  48. }
  49. }
  50. static void map_enqueue_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  51. pending_row_dest = vram_ptr;
  52. if (y0 < 0 || y0 >= MAP.map_height) {
  53. memset(&pending_row[0], TILE_INDEX_BACKGROUND, sizeof pending_row);
  54. return;
  55. }
  56. uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
  57. uint8_t left = sizeof pending_row;
  58. uint8_t *row = &pending_row[0];
  59. while (left > 0 && x0 < 0) {
  60. *row++ = TILE_INDEX_BACKGROUND;
  61. x0++;
  62. left--;
  63. }
  64. while (left > 0 && x0 < MAP.map_height) {
  65. *row++ = TILE_INDEX_BACKGROUND + *map_ptr++;
  66. x0++;
  67. left--;
  68. }
  69. while (left > 0) {
  70. *row++ = TILE_INDEX_BACKGROUND;
  71. left--;
  72. }
  73. }
  74. static void map_write_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  75. uint8_t *row = (uint8_t *)vram_ptr;
  76. if (y0 < 0 || y0 >= MAP.map_height) {
  77. memset(row, TILE_INDEX_BACKGROUND, SCRN_VX_B);
  78. return;
  79. }
  80. uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
  81. uint8_t left = SCRN_VX_B;
  82. while (left > 0 && x0 < 0) {
  83. *row++ = TILE_INDEX_BACKGROUND;
  84. x0++;
  85. left--;
  86. }
  87. while (left > 0 && x0 < MAP.map_height) {
  88. *row++ = TILE_INDEX_BACKGROUND + *map_ptr++;
  89. x0++;
  90. left--;
  91. }
  92. while (left > 0) {
  93. *row++ = TILE_INDEX_BACKGROUND;
  94. left--;
  95. }
  96. }
  97. static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0) {}