|
|
- #include "map.h"
-
- #include <string.h>
-
- #include "game.h"
- #include "sdk/hardware.h"
- #include "vram.h"
-
- #define INIT_SCX ((SCRN_VX - SCRN_X) / 2)
- #define INIT_SCY ((SCRN_VY - SCRN_Y) / 2)
-
- map_t MAP;
-
- uint8_t pending_row[SCRN_VX_B];
- uint16_t pending_row_dest;
-
- uint8_t pending_col[SCRN_VY_B];
- uint16_t pending_col_dest;
-
- static void map_write_row(uint16_t map_ptr, int8_t x0, int8_t y0);
-
- static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0);
-
- void map_load(map_t *map) {
- MAP = *map;
-
- rSCX = INIT_SCX;
- rSCY = INIT_SCY;
-
- // TODO: Update player state
-
- memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr,
- MAP.tile_count);
-
- int8_t x0 = MAP.camera_x - INIT_SCX;
- int8_t y0 = MAP.camera_y - INIT_SCY;
- uint16_t map_ptr = _SCRN0;
-
- for (uint8_t i = 0; i < SCRN_VY_B; ++i) {
- map_write_row(map_ptr, x0, y0);
- map_ptr += SCRN_VX_B;
- y0++;
- }
- }
-
- void map_update(void) {
- if (pending_row_dest != 0) {
- vram_enqueue_mem_xfer((uint8_t *)pending_row_dest, &pending_row[0],
- sizeof pending_row);
- pending_row_dest = 0;
- }
-
- // TODO: Make it work
- /* if (pending_col_dest != 0) {
- vram_enqueue_mem_xfer(pending_col_dest, &pending_col[0],
- sizeof pending_col);
- pending_col_dest = 0;
- } */
- }
-
- static void map_enqueue_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
- pending_row_dest = vram_ptr;
-
- if (y0 < 0 || y0 > MAP.map_height) {
- memset(&pending_row[0], 0, sizeof pending_row);
- return;
- }
-
- uint8_t *map_ptr = (uint8_t *)(MAP.map_ptr + y0 * MAP.map_width);
- uint8_t left = sizeof pending_row;
- uint8_t *row = &pending_row[0];
-
- while (left > 0 && x0 < 0) {
- *row++ = TILE_INDEX_BACKGROUND;
- left--;
- }
-
- while (left > 0 && x0 < MAP.map_height) {
- *row++ = *map_ptr++;
- left--;
- }
-
- while (left > 0) {
- *row++ = TILE_INDEX_BACKGROUND;
- left--;
- }
- }
-
- static void map_write_row(uint16_t vram_ptr, int8_t x0, int8_t y0) {
- map_enqueue_row(vram_ptr, x0, y0);
- memcpy((uint8_t *)pending_row_dest, &pending_row[0], sizeof pending_row);
- pending_row_dest = 0;
- }
-
- static void map_write_col(uint16_t map_ptr, int8_t x0, int8_t y0) {}
|