Browse Source

Generalize Y coordinate computation for wrapping

master
Forest Belton 3 years ago
parent
commit
40479b0795
2 changed files with 65 additions and 27 deletions
  1. +12
    -0
      inc/util.inc
  2. +53
    -27
      src/map.s

+ 12
- 0
inc/util.inc View File

@ -65,3 +65,15 @@ MACRO SLA16
sla LOW(\1) sla LOW(\1)
rl HIGH(\1) rl HIGH(\1)
ENDM ENDM
; Check \1 - \2
; \1 16-bit register
; \2 16-bit immediate/register
MACRO CP16
ld a, HIGH(\1)
cp HIGH(\2)
jr nz, .done\@
ld a, LOW(\1)
cp LOW(\1)
.done\@
ENDM

+ 53
- 27
src/map.s View File

@ -120,6 +120,46 @@ MACRO LOAD_MAPX
.done\@: .done\@:
ENDM ENDM
; Loads the map Y-coordinate into C
MACRO LOAD_MAPY
; if SCY <= 0x7f
ld a, [rSCY]
cp $80
jr nc, .wrap\@
; C = CAMERA_Y - SCY/8
ld a, [rSCY]
ld c, a
ld a, [CURRENT_CAMERA_Y]
srln c, 3
sub c
ld c, a
jr .done\@
; else SCY > 0x7f
.wrap\@:
; C = CAMERA_Y - PAGE_Y/8
ld a, [PAGEY]
ld c, a
ld a, [CURRENT_CAMERA_Y]
srln c, 3
sub c
ld c, a
; HL += 32 * PAGE_Y/8
ld a, [PAGEY]
srln a, 3
ld d, a
ld e, 32
.adjust_hl\@:
ld a, d
ADD16 hl
dec e
jr nz, .adjust_hl\@
.done\@:
ENDM
; Update map state based on SCX/SCY ; Update map state based on SCX/SCY
Map_Scroll:: Map_Scroll::
; If SCY = PAGEY, write map row ; If SCY = PAGEY, write map row
@ -149,7 +189,7 @@ Map_Scroll::
sub 8 sub 8
ld [PAGEY], a ld [PAGEY], a
jp .done
ret
.scroll_down_check: .scroll_down_check:
; Check SCY + (SCRN_Y + 16) = PAGEY ; Check SCY + (SCRN_Y + 16) = PAGEY
@ -160,9 +200,7 @@ Map_Scroll::
jr nz, .scroll_left_check jr nz, .scroll_left_check
ld a, [PAGEY] ld a, [PAGEY]
srl a
srl a
srl a
srln a, 3
dec a dec a
call get_row_ptr call get_row_ptr
@ -179,7 +217,7 @@ Map_Scroll::
add 8 add 8
ld [PAGEY], a ld [PAGEY], a
jr .done
ret
.scroll_left_check: .scroll_left_check:
; If SCX = PAGEX, write map col ; If SCX = PAGEX, write map col
@ -191,9 +229,7 @@ Map_Scroll::
; HL = VRAM + PAGEX/8 ; HL = VRAM + PAGEX/8
ld hl, _SCRN0 ld hl, _SCRN0
ld a, [PAGEX] ld a, [PAGEX]
srl a
srl a
srl a
srln a, 3
dec a dec a
ADD16 hl ADD16 hl
@ -203,14 +239,7 @@ Map_Scroll::
ld b, a ld b, a
; C = CAMERA_Y - SCY/8 ; C = CAMERA_Y - SCY/8
ld a, [rSCY]
ld c, a
ld a, [CURRENT_CAMERA_Y]
srl c
srl c
srl c
sub c
ld c, a
LOAD_MAPY
call enqueue_col_write call enqueue_col_write
@ -225,7 +254,7 @@ Map_Scroll::
sub SCRN_X + 16 sub SCRN_X + 16
ld hl, rSCX ld hl, rSCX
cp [hl] cp [hl]
jr nz, .done
ret nz
; HL = VRAM + PAGEX/8 ; HL = VRAM + PAGEX/8
ld hl, _SCRN0 ld hl, _SCRN0
@ -242,14 +271,7 @@ Map_Scroll::
ld b, a ld b, a
; C = CAMERA_Y - SCY/8 ; C = CAMERA_Y - SCY/8
ld a, [rSCY]
ld c, a
ld a, [CURRENT_CAMERA_Y]
srl c
srl c
srl c
sub c
ld c, a
LOAD_MAPY
call enqueue_col_write call enqueue_col_write
@ -257,8 +279,6 @@ Map_Scroll::
add 8 add 8
ld [PAGEX], a ld [PAGEX], a
.done:
; TODO: Turn jumps here into rets
ret ret
Map_Update:: Map_Update::
@ -312,6 +332,12 @@ Map_Update::
ld a, SCRN_VX_B ld a, SCRN_VX_B
ADD16 bc ADD16 bc
; If BC = 9c00, set BC = 9800
CP16 bc, _SCRN0 + SCRN_VY_B * SCRN_VX_B
jr nz, .update_col_loop_next
ld b, HIGH(_SCRN0)
.update_col_loop_next:
dec d dec d
jr nz, .update_col_loop jr nz, .update_col_loop

Loading…
Cancel
Save