Last active
August 21, 2018 20:54
-
-
Save tobiasvl/b23517412f7280d53dd9a603f3367a43 to your computer and use it in GitHub Desktop.
Game Boy snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_map_position: | |
; from a sprite's pixel position, get the BG map address. | |
; d: Y pixel position | |
; e: X pixel position | |
; hl: returned map address | |
ld h, HIGH(_SCRN0) >> 2 | |
; Y | |
ld a, [rSCY] ; account for scroll | |
sub a, 16 ; account for base sprite offset | |
add a, d | |
and $F8 ; snap to grid | |
add a, a | |
rl h | |
add a, a | |
rl h | |
ld l, a | |
; X | |
ld a, [rSCX] ; account for scroll | |
sub a, 8 ; account for base sprite offset | |
add a, e | |
and $F8 ; snap to grid | |
rrca | |
rrca | |
rrca | |
add a, l | |
ld l, a | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment