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.

95 lines
2.3 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. uint8_t pending_row[SCRN_VX_B];
  10. uint16_t pending_row_dest;
  11. uint8_t pending_col[SCRN_VY_B];
  12. uint16_t pending_col_dest;
  13. static void map_write_row(uint16_t map_ptr, int8_t x0, int8_t y0);
  14. static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0);
  15. void map_load(map_t *map) {
  16. MAP = *map;
  17. rSCX = INIT_SCX;
  18. rSCY = INIT_SCY;
  19. // TODO: Update player state
  20. memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr,
  21. MAP.tile_count);
  22. int8_t x0 = MAP.camera_x - INIT_SCX;
  23. int8_t y0 = MAP.camera_y - INIT_SCY;
  24. uint16_t map_ptr = _SCRN0;
  25. for (uint8_t i = 0; i < SCRN_VY_B; ++i) {
  26. map_write_row(map_ptr, x0, y0);
  27. map_ptr += SCRN_VX_B;
  28. y0++;
  29. }
  30. }
  31. void map_update(void) {
  32. if (pending_row_dest != 0) {
  33. vram_enqueue_mem_xfer((uint8_t *)pending_row_dest, &pending_row[0],
  34. sizeof pending_row);
  35. pending_row_dest = 0;
  36. }
  37. // TODO: Make it work
  38. /* if (pending_col_dest != 0) {
  39. vram_enqueue_mem_xfer(pending_col_dest, &pending_col[0],
  40. sizeof pending_col);
  41. pending_col_dest = 0;
  42. } */
  43. }
  44. static void map_enqueue_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  45. pending_row_dest = vram_ptr;
  46. if (y0 < 0 || y0 > MAP.map_height) {
  47. memset(&pending_row[0], 0, sizeof pending_row);
  48. return;
  49. }
  50. uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
  51. uint8_t left = sizeof pending_row;
  52. uint8_t *row = &pending_row[0];
  53. while (left > 0 && x0 < 0) {
  54. *row++ = TILE_INDEX_BACKGROUND;
  55. left--;
  56. }
  57. while (left > 0 && x0 < MAP.map_height) {
  58. *row++ = *map_ptr++;
  59. left--;
  60. }
  61. while (left > 0) {
  62. *row++ = TILE_INDEX_BACKGROUND;
  63. left--;
  64. }
  65. }
  66. static void map_write_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
  67. map_enqueue_row(vram_ptr, x0, y0);
  68. memcpy((uint8_t *)pending_row_dest, &pending_row[0], sizeof pending_row);
  69. pending_row_dest = 0;
  70. }
  71. static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0) {}