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.

127 lines
3.0 KiB

  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 = (MAP.spawn_y - MAP.camera_y) * 8;
  24. memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr,
  25. MAP.tile_size);
  26. int8_t x0 = MAP.camera_x - INIT_SCX / 8;
  27. int8_t y0 = MAP.camera_y - INIT_SCY / 8;
  28. uint16_t map_ptr = _SCRN0;
  29. for (uint8_t i = 0; i < SCRN_VY_B; ++i) {
  30. map_write_row(map_ptr, x0, y0);
  31. map_ptr += SCRN_VX_B;
  32. y0++;
  33. }
  34. pending_row_dest = 0;
  35. pending_col_dest = 0;
  36. }
  37. void map_update(void) {
  38. if (pending_row_dest != 0) {
  39. vram_enqueue_mem_xfer(XFER_TYPE_ROW, pending_row_dest, &pending_row[0],
  40. sizeof pending_row);
  41. pending_row_dest = 0;
  42. }
  43. if (pending_col_dest != 0) {
  44. vram_enqueue_mem_xfer(XFER_TYPE_COL, pending_col_dest, &pending_col[0],
  45. sizeof pending_col);
  46. pending_col_dest = 0;
  47. }
  48. }
  49. static void map_enqueue_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  50. pending_row_dest = vram_ptr;
  51. if (y0 < 0 || y0 >= MAP.map_height) {
  52. memset(&pending_row[0], TILE_INDEX_BACKGROUND, sizeof pending_row);
  53. return;
  54. }
  55. uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
  56. uint8_t left = sizeof pending_row;
  57. uint8_t *row = &pending_row[0];
  58. while (left > 0 && x0 < 0) {
  59. *row++ = TILE_INDEX_BACKGROUND;
  60. x0++;
  61. left--;
  62. }
  63. while (left > 0 && x0 < MAP.map_height) {
  64. *row++ = TILE_INDEX_BACKGROUND + *map_ptr++;
  65. x0++;
  66. left--;
  67. }
  68. while (left > 0) {
  69. *row++ = TILE_INDEX_BACKGROUND;
  70. left--;
  71. }
  72. }
  73. static void map_write_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  74. uint8_t *row = (uint8_t *)vram_ptr;
  75. if (y0 < 0 || y0 >= MAP.map_height) {
  76. memset(row, TILE_INDEX_BACKGROUND, SCRN_VX_B);
  77. return;
  78. }
  79. uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
  80. uint8_t left = SCRN_VX_B;
  81. while (left > 0 && x0 < 0) {
  82. *row++ = TILE_INDEX_BACKGROUND;
  83. x0++;
  84. left--;
  85. }
  86. while (left > 0 && x0 < MAP.map_height) {
  87. *row++ = TILE_INDEX_BACKGROUND + *map_ptr++;
  88. x0++;
  89. left--;
  90. }
  91. while (left > 0) {
  92. *row++ = TILE_INDEX_BACKGROUND;
  93. left--;
  94. }
  95. }
  96. static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0) {}