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.

126 lines
2.9 KiB

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