From: Lukas Krickl Date: Wed, 1 Oct 2025 16:09:29 +0000 (+0200) Subject: rectangle: optimized collision test by removing calls and pushes and calculating... X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=4b7fa5d7e0d2de1dcb2212c5280f7a763512c0a7;p=gbrg%2F.git rectangle: optimized collision test by removing calls and pushes and calculating the points more directly --- diff --git a/src/rectangle.s b/src/rectangle.s index e8c8b77..99d3a10 100644 --- a/src/rectangle.s +++ b/src/rectangle.s @@ -159,20 +159,16 @@ rect_br: ; a == 1: inside ; a == 0: not inside rect_point_test: - ; TODO: remove store hl - ld a, h - ld [h2], a - ld a, l - ld [l2], a - inc hl ; skip mask ; bottom left ; load bottom left point ld a, [hl+] + ; TODO: remove use of d2 + ld [d2], a ; save y in d2 ld d, a ; d = y - ld a, [hl] + ld a, [hl+] ld e, a ; e = x ld a, b ; compare y @@ -182,15 +178,17 @@ rect_point_test: cp a, e jr c, @no_collision REL - ; TODO: remove restore hl - ld a, [h2] - ld h, a - ld a, [l2] - ld l, a ; top left + + ; get top left y point + ld a, [hl+] push hl - call rect_tl + ld l, a ; TODO: remove use of l to avoid push + ld a, d + sub a, l ; y-height + capunderflow + ld d, a ; d = top left y pop hl ld a, b ; compare y points @@ -203,9 +201,12 @@ rect_point_test: ; top right - push hl - call rect_tr - pop hl + + ; get top right x + ld a, [hl] + add a, e ; e = x + width + capoverflow + ld e, a ld a, b ; compare y cp a, d @@ -216,12 +217,15 @@ rect_point_test: ; bottom right - push hl - call rect_br - pop hl + + ; remove height from y + ; to get bottom right point + ld a, [d2] ; d2 = original y point + ld d, a ld a, b ; compare y - jr c, @no_collision REL + cp a, d + jr nc, @no_collision REL ld a, c ; compare x cp a, e jr nc, @no_collision REL