Skip to content

Commit d01a70c

Browse files
calc84maniacmateoconlechuga
authored andcommitted
[graphx] Optimize clipping logic for gfx_Sprite and gfx_TransparentSprite
1 parent cd97828 commit d01a70c

File tree

1 file changed

+107
-128
lines changed

1 file changed

+107
-128
lines changed

src/graphx/graphx.asm

Lines changed: 107 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,24 +2640,12 @@ gfx_TransparentSprite:
26402640
; None
26412641
push ix ; save ix sp
26422642
call _ClipCoordinates
2643-
pop ix ; restore ix sp
2644-
ret nc
2643+
jr nc,.culled
26452644
ld (.amount),a
2646-
ld a,(iy + 0) ; _TmpWidth
2645+
ld a,c ; new width
26472646
ld (.next),a
2648-
ld a,(iy + 3) ; tmpHeight
2649-
ld h,ti.lcdWidth / 2
2650-
mlt hl
2651-
ld bc,0 ; zero ubc
2652-
add hl,hl
2653-
add hl,de
2654-
ld de,(CurrentBuffer)
2655-
add hl,de
2656-
push hl
2657-
ld hl,(iy+6) ; hl -> sprite data
2658-
pop iy
2659-
push ix
2660-
ld ixh,a
2647+
ld ixh,b ; new height
2648+
ld b,0
26612649
.transparent_color := $+1
26622650
ld a,TRASPARENT_COLOR
26632651
smcByte _TransparentColor
@@ -2674,6 +2662,7 @@ smcByte _TransparentColor
26742662
add iy,de
26752663
dec ixh
26762664
jr nz,.loop
2665+
.culled:
26772666
pop ix
26782667
ret
26792668

@@ -2729,28 +2718,17 @@ gfx_Sprite:
27292718
; arg0 : Pointer to sprite
27302719
; arg1 : X coordinate
27312720
; arg2 : Y coordinate
2732-
; arg3 : Width -- 8bits
2733-
; arg4 : Height -- 8bits
27342721
; Returns:
27352722
; None
27362723
push ix ; save ix sp
27372724
call _ClipCoordinates
27382725
pop ix ; restore ix sp
27392726
ret nc
27402727
ld (.amount),a
2741-
ld a,(iy + 0) ; a = _TmpWidth
2728+
ld a,c ; new width
27422729
ld (.next),a
2743-
ld a,(iy + 3) ; a = tmpHeight
2744-
ld h,ti.lcdWidth / 2
2745-
mlt hl
2746-
add hl,hl
2747-
add hl,de
2748-
ld de,(CurrentBuffer)
2749-
add hl,de
2750-
push hl
2751-
ld hl,(iy + 6) ; hl -> sprite data
2752-
pop iy
2753-
ld bc,0
2730+
ld a,b ; new height
2731+
ld b,0
27542732
wait_quick
27552733
.loop:
27562734
ld c,0
@@ -2922,112 +2900,116 @@ _ClipCoordinates:
29222900
; arg2 : Y coordinate
29232901
; Returns:
29242902
; A : How much to add to the sprite per iteration
2925-
; L : New Y coordinate
2926-
; DE : New X coordinate
2903+
; BCU: 0
2904+
; B : New sprite height
2905+
; C : New sprite width
2906+
; HL : Sprite pixel pointer
2907+
; IY : Buffer pixel pointer
29272908
; NC : If offscreen
29282909
ld ix,6 ; get pointer to arguments
2929-
lea iy,ix-6
29302910
add ix,sp
2931-
ld hl,(ix+3)
2932-
ld a,(hl)
2933-
ld de,_TmpWidth
2934-
ld (de),a ; save _TmpWidth
2935-
ld (.width),a ; save tmpSpriteWidth
2936-
add iy,de
2937-
inc hl
2938-
ld a,(hl)
2939-
ld (iy+3),a ; save tmpHeight
2940-
inc hl
2941-
ld (iy+6),hl ; save a ptr to the sprite data to change offsets
2942-
ld bc,(ix+9)
2943-
ld hl,(_YMin)
2944-
mIsHLLessThanBC
2945-
jr c,.notop
2946-
ld hl,(iy+3)
2947-
add hl,bc
2948-
ex de,hl
2949-
ld hl,(_YMin)
2950-
mIsHLLessThanDE
2951-
ret nc ; bc = y location
2952-
ld hl,(_YMin) ; ymin
2953-
or a,a
2911+
ld hl,(ix+3) ; hl -> sprite data
2912+
ld iy,(hl) ; iyl = width, iyh = height
2913+
2914+
ld bc,(_YMin)
2915+
ld hl,(ix+9) ; hl = y coordinate
29542916
sbc hl,bc
2955-
ld a,(iy+3)
2956-
sub a,l
2957-
ld (iy+3),a
2958-
ld h,(iy+0) ; h = _TmpWidth
2959-
mlt hl ; hl = amount of lines clipped off
2960-
ld de,(iy+6) ; de -> sprite data
2961-
add hl,de
2962-
ld (iy+6),hl ; store new ptr
2963-
ld bc,(_YMin) ; new y location ymin
2964-
.notop:
2965-
push bc
2966-
pop hl ; hl = y coordinate
2967-
ld de,(_YMax)
2968-
mIsHLLessThanDE
2969-
ret nc ; return if offscreen on bottom
2970-
; bc = y coordinate
2971-
ld hl,(iy+3) ; hl = tmpHeight
2917+
ex de,hl ; de = y coordinate relative to min y
2918+
ld hl,(_YMax)
2919+
xor a,a
2920+
sbc hl,bc ; hl = clip_height
2921+
ld c,iyh ; bc = height
2922+
sbc hl,bc ; get difference between clip_height and height
2923+
dec c ; bc = height - 1
2924+
jr nc,.nottaller
2925+
or a,a
2926+
sbc hl,de ; is partially clipped both top and bottom?
2927+
jr nc,.yclip
2928+
sub a,e ; a = negated relative y
2929+
add hl,de ; use clip_height as the draw height, and clip top
2930+
jr .cliptop
2931+
.nottaller:
2932+
sbc hl,de ; is fully onscreen vertically?
2933+
jr nc,.yclipped
2934+
.yclip:
2935+
add hl,bc ; is partially clipped bottom?
2936+
ex de,hl ; e = new height - 1, hl = relative y
2937+
jr c,.clipbottom
2938+
sub a,l ; a = negated relative y
2939+
.cliptop:
2940+
add hl,bc ; is partially clipped top?
2941+
ret nc
2942+
ex de,hl ; e = new height - 1
2943+
ld c,a ; c = negated relative y
2944+
ld b,iyl ; b = width
2945+
mlt bc ; bc = amount of bytes clipped off
2946+
ld hl,(ix+3) ; hl -> sprite data
29722947
add hl,bc
2973-
ld de,(_YMax)
2974-
mIsHLLessThanDE
2975-
jr c,.notbottom ; is partially clipped bottom?
2976-
ex de,hl ; hl = ymax
2977-
; bc = y coordinate
2978-
sbc hl,bc
2979-
ld (iy+3),hl ; store new tmpHeight
2980-
.notbottom:
2981-
ld hl,(ix+6) ; hl = x coordinate
2982-
ld de,(_XMin)
2983-
mIsHLLessThanDE
2948+
ld (ix+3),hl ; store new ptr
2949+
ld a,(_YMin)
2950+
ld (ix+9),a ; save min y coordinate
2951+
.clipbottom:
2952+
inc e
2953+
ld iyh,e ; save new height
2954+
.yclipped:
2955+
2956+
ld bc,(_XMin)
29842957
ld hl,(ix+6) ; hl = x coordinate
2985-
jr nc,.noleft ; is partially clipped left?
2986-
ld de,(iy+0) ; de = _TmpWidth
2987-
add hl,de
2988-
ld de,(_XMin)
2989-
ex de,hl
2990-
mIsHLLessThanDE
2991-
ret nc ; return if offscreen
2992-
ld de,(ix+6) ; de = x coordinate
2993-
ld hl,(_XMin)
29942958
or a,a
2995-
sbc hl,de
2996-
ex de,hl ; calculate new offset
2997-
ld hl,(iy+6) ; hl -> sprite data
2998-
add hl,de
2999-
ld (iy+6),hl ; save new ptr
3000-
ld hl,(iy+0) ; hl = _TmpWidth
3001-
or a,a
3002-
sbc hl,de
3003-
ld (iy+0),hl ; save new width
2959+
sbc hl,bc
2960+
ex de,hl ; de = x coordinate relative to min x
2961+
ld hl,(_XMax)
2962+
xor a,a
2963+
sbc hl,bc ; hl = clip_width
2964+
ld b,a
2965+
ld c,iyl ; bc = width
2966+
sbc hl,bc ; get difference between clip_width and width
2967+
dec c ; bc = width - 1
2968+
jr nc,.notwider
2969+
or a,a
2970+
sbc hl,de ; is partially clipped both left and right?
2971+
jr nc,.xclip
2972+
sub a,e ; a = negated relative x
2973+
add hl,de ; use clip_width as the draw width, and clip left
2974+
jr .clipleft
2975+
.notwider:
2976+
sbc hl,de ; is fully onscreen horizontally?
2977+
jr nc,.xclipped ; a = 0 for bytes to add per iteration
2978+
.xclip:
2979+
add hl,bc ; is partially clipped right?
2980+
ex de,hl ; e = new width - 1, hl = relative x
2981+
jr c,.clipright
2982+
sub a,l ; a = negated relative x
2983+
.clipleft:
2984+
add hl,bc ; is partially clipped left?
2985+
ret nc ; return if offscreen
2986+
ex de,hl ; e = new width - 1
2987+
ld c,a ; bc = negated relative x
2988+
ld hl,(ix+3) ; hl -> sprite data
2989+
add hl,bc
2990+
ld (ix+3),hl
30042991
ld hl,(_XMin)
30052992
ld (ix+6),hl ; save min x coordinate
3006-
.noleft:
3007-
ld de,(_XMax) ; de = xmax
3008-
mIsHLLessThanDE
3009-
ret nc ; return if offscreen
3010-
ld hl,(ix+6) ; hl = x coordinate
3011-
ld de,(iy+0) ; de = _TmpWidth
2993+
.clipright:
2994+
inc e
2995+
ld a,iyl ; get old width
2996+
ld iyl,e ; save new width
2997+
sub a,e ; calculate bytes to add per iteration
2998+
.xclipped:
2999+
3000+
lea.s bc,iy
3001+
ld l,(ix+9) ; l = y coordinate
3002+
ld h,ti.lcdWidth / 2
3003+
mlt hl
3004+
add hl,hl
3005+
ld de,(ix+6) ; de = x coordinate
30123006
add hl,de
3013-
ld de,(_XMax)
30143007
ex de,hl
3015-
mIsHLLessThanDE
3016-
jr nc,.noright ; is partially clipped right?
3017-
ld hl,(_XMax) ; clip on the right
3018-
ld de,(ix+6)
3019-
ccf
3020-
sbc hl,de
3021-
ld (iy+0),hl ; save new _TmpWidth
3022-
.noright:
3023-
ld a,(iy+3)
3024-
or a,a
3025-
ret z ; quit if new tmpHeight is 0 (edge case)
3026-
ld a,0
3027-
.width := $-1
3028-
ld de,(ix+6) ; de = x coordinate
3029-
ld l,c ; l = y coordinate
3030-
sub a,(iy+0) ; compute new x width
3008+
ld iy,(CurrentBuffer)
3009+
add iy,de
3010+
ld hl,(ix+3) ; hl -> sprite data
3011+
inc hl
3012+
inc hl
30313013
scf ; set carry for success
30323014
ret
30333015

@@ -6745,9 +6727,6 @@ _ClipRegion_Full:
67456727
dl ti.lcdWidth
67466728
dl ti.lcdHeight
67476729

6748-
_TmpWidth:
6749-
dl 0,0,0
6750-
67516730
_TmpCharSprite:
67526731
db 8,8
67536732
_TmpCharData:

0 commit comments

Comments
 (0)