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.
 
 
 
 

403 lines
7.1 KiB

INCLUDE "hardware.inc"
INCLUDE "util.inc"
SECTION "Map Data", WRAM0
PAGEX:: DB ; X coordinate to enqueue map column at
PAGEY:: DB ; Y coordinate to enqueue map row at
LAST_SCX:: DB ; Value of SCX last frame
LAST_SCY:: DB ; Value of SCY last frame
PENDING_ROW_PTR:: DW ; Where to write pending row data (0 = no write)
PENDING_ROW_DATA:: DS SCRN_VX_B ; Row to be written
PENDING_COL_PTR:: DW ; Where to write pending column data (0 = no write)
PENDING_COL_DATA: DS SCRN_VY_B ; Column to be written
CURRENT_DATA_START::
CURRENT_TILE_PTR:: DW ; Location of tile data
CURRENT_TILE_SIZE:: DB ; Length of tile data (num_tiles * 8)
CURRENT_MAP_PTR:: DW ; Location of map data
CURRENT_MAP_COLLISION:: DW ; Location of map collision data
CURRENT_MAP_WIDTH:: DB ; Width of map in tiles
CURRENT_MAP_HEIGHT:: DB ; Height of map in tiles
CURRENT_SPAWN_X:: DB ; X coordinate to spawn player at
CURRENT_SPAWN_Y:: DB ; Y coordinate to spawn player at
CURRENT_CAMERA_X:: DB ; X coordinate of camera (top left of viewport)
CURRENT_CAMERA_Y:: DB ; Y coordinate of camera (top left of viewport)
CURRENT_DATA_END::
SECTION "Map Code", ROM0
DEF INIT_SCX EQUS "((SCRN_VX - SCRN_X) / 2)"
DEF INIT_SCY EQUS "((SCRN_VY - SCRN_Y) / 2)"
; Loads a map
; @param hl Pointer to map metadata
Map_Load::
; Initialize scroll state
ld a, INIT_SCX
ld [rSCX], a
ld [LAST_SCX], a
ld a, INIT_SCY
ld [rSCY], a
ld [LAST_SCY], a
ld a, 8
ld [PAGEX], a
ld [PAGEY], a
; Store metadata
ld bc, CURRENT_DATA_START
ld d, CURRENT_DATA_END - CURRENT_DATA_START
call memcpy
; Write tiles to VRAM
ld hl, CURRENT_TILE_PTR
ld a, [hl+]
ld c, a
ld a, [hl+]
ld b, a
ld a, [CURRENT_TILE_SIZE]
ld d, a
ld hl, _VRAM
MEMCPY hl, bc, d
; Write initial map data
ld hl, _SCRN0
ld a, [CURRENT_CAMERA_X]
sub INIT_SCX / 8
ld b, a
ld a, [CURRENT_CAMERA_Y]
sub INIT_SCY / 8
ld c, a
ld d, SCRN_VY_B
.write_rows:
call write_map_row
inc c
ld a, SCRN_VX_B
ADD16 hl
dec d
jr nz, .write_rows
ret
; Update map state based on SCX/SCY
Map_Scroll::
; If SCY = PAGEY, write map row
ld hl, PAGEY
ld a, [rSCY]
cp [hl]
jr nz, .scroll_down_check
; B = CAMERA_X - SCX/8
ld a, [rSCX]
ld b, a
ld a, [CURRENT_CAMERA_X]
srl b
srl b
srl b
sub b
ld b, a
; TODO: WTF does this shit even do...
ld a, [CURRENT_CAMERA_Y]
cp 2
jr c, .done
sub 2
ld c, a
; a = scy/8 + (scy/8 < 2 ? 30 : -2)
ld a, [rSCY]
srl a
srl a
srl a
cp 2
jr nc, .loop0
add 32
.loop0:
sub 2
; HL = _SCRN0 + 32 * (32 - page_y/8 - 2)
; HL = _SCRN0 + 32 * A
ld e, a
ld hl, _SCRN0
.loop:
ld a, e
or a
jr z, .write_up
ld a, 32
ADD16 hl
dec e
jr .loop
.write_up:
call enqueue_row_write
ld a, [PAGEY]
sub 8
ld [PAGEY], a
jr .done
.scroll_down_check:
; Check SCY + (SCRN_Y + 16) = PAGEY
ld a, [PAGEY]
sub SCRN_Y + 16
ld hl, rSCY
cp [hl]
jr nz, .done
ld a, [PAGEY]
srl a
srl a
srl a
dec a
ld e, a
call compute_vram_ptr
; B = CAMERA_X - SCX/8
ld a, [rSCX]
ld b, a
ld a, [CURRENT_CAMERA_X]
srl b
srl b
srl b
sub b
ld b, a
; C = CAMERA_Y + 18 + 1
; TODO: Figure out if a similar method to this one is sufficient for
; whatever the fuck the scroll up check is doing
ld a, [CURRENT_CAMERA_Y]
add SCRN_Y_B + 1
ld c, a
call enqueue_row_write
ld a, [PAGEY]
add 8
ld [PAGEY], a
jr .done
; If SCX = PAGEX, write map col
; If SCX = SCRN_X - PAGEX, write map col
.done:
ld hl, LAST_SCY
ld a, [rSCY]
ld [hl], a
ret
Map_Update::
; Skip row update if PENDING_ROW_PTR is 0
ld a, [PENDING_ROW_PTR]
ld c, a
ld a, [PENDING_ROW_PTR + 1]
ld b, a
or c
jr z, .update_col
ld hl, PENDING_ROW_DATA
ld d, SCRN_VX_B
MEMCPY bc, hl, d
.update_col:
; Skip update if PENDING_COL_PTR is 0
ld a, [PENDING_COL_PTR]
ld c, a
ld a, [PENDING_COL_PTR + 1]
ld b, a
or c
ret z
ld d, SCRN_VX_B
ld hl, PENDING_COL_DATA
.update_col_loop:
ld a, [hl+]
ld [bc], a
ld a, SCRN_VX_B
ADD16 bc
dec d
jr nz, .update_col_loop
ret
; Computes the offset into map RAM
; @param e The map RAM y-coordinate
; @return hl Pointer into map RAM
compute_vram_ptr:
push bc
ld hl, _SCRN0
ld b, 0
ld c, SCRN_VX_B
ld a, e
.loop:
or a
jr z, .done
add hl, bc
dec a
jr .loop
.done:
pop bc
ret
; Write a row of map data into row buffer
; @param b Map X coordinate (signed)
; @param c Map Y coordinate (signed)
; @param hl Where to write the row in map VRAM
; @destroy All registers
enqueue_row_write:
; PENDING_ROW_PTR = HL
ld a, l
ld [PENDING_ROW_PTR], a
ld a, h
ld [PENDING_ROW_PTR + 1], a
; If Y < 0, write 0s
bit 7, c
jr nz, .zero_row
; If Y >= MAP_HEIGHT, write 0s
ld a, [CURRENT_MAP_HEIGHT]
dec a
cp c
jr c, .zero_row
; HL = CURRENT_MAP_PTR
ld a, [CURRENT_MAP_PTR]
ld l, a
ld a, [CURRENT_MAP_PTR + 1]
ld h, a
; HL = CURRENT_MAP_PTR + Y * MAP_WIDTH
ld d, 0
ld a, [CURRENT_MAP_WIDTH]
ld e, a
ld a, c
.get_map_row_ptr:
or a
jr z, .copy_map_row
add hl, de
dec a
jr .get_map_row_ptr
.copy_map_row:
; C = BYTES_LEFT
ld c, SCRN_VX_B
ld de, PENDING_ROW_DATA
; Note: Can skip checking BYTES_LEFT > 0 in this loop. If there were
; SCRN_VX_B zeros to write, then Y would be 1 greater and we would have
; jumped into .zero_row before reaching this code
.pad_left:
; Check X < 0
bit 7, b
jr z, .copy_middle
; *ROW++ = 0
ld [de], a
inc de
; X++, BYTES_LEFT--
inc b
dec c
jr .pad_left
.copy_middle:
; Check X < MAP_WIDTH
ld a, [CURRENT_MAP_WIDTH]
dec a
cp b
jr c, .pad_right
; Check BYTES_LEFT > 0
ld a, c
or a
ret z
; *ROW++ = *MAP++
ld a, [hl+]
ld [de], a
inc de
; X++, BYTES_LEFT--
inc b
dec c
jr .copy_middle
.pad_right:
; Check BYTES_LEFT > 0
ld a, c
or a
ret z
; *ROW++ = 0
xor a
ld [de], a
inc de
; X++, BYTES_LEFT--
inc b
dec c
jr .pad_left
.zero_row:
ld hl, PENDING_ROW_DATA
xor a
ld c, SCRN_VX_B
.zero_row_loop:
ld [hl+], a
dec c
jr nz, .zero_row_loop
ret
; NOTE: This works by enqueueing a row to write, and then immediately flushing
; the queue. The subroutine could be sped up by writing to map RAM directly, but
; this is simpler to write and saves on code size.
;
; Write a row of map data into map RAM.
; @param b Map X coordinate (signed)
; @param c Map Y coordinate (signed)
; @param hl Pointer into map VRAM
write_map_row:
push bc
push de
push hl
call enqueue_row_write
ld a, [PENDING_ROW_PTR]
ld c, a
ld a, [PENDING_ROW_PTR + 1]
ld b, a
ld hl, PENDING_ROW_DATA
ld d, SCRN_VX_B
MEMCPY bc, hl, d
pop hl
pop de
pop bc
ret