Skip to content

Instantly share code, notes, and snippets.

@tobiasvl
Last active August 21, 2018 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasvl/b23517412f7280d53dd9a603f3367a43 to your computer and use it in GitHub Desktop.
Save tobiasvl/b23517412f7280d53dd9a603f3367a43 to your computer and use it in GitHub Desktop.
Game Boy snippet
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