Skip to content

Commit 82e5d5a

Browse files
Optimize ClipDraw by ~350cc (~220 worst case)
1 parent 8592704 commit 82e5d5a

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

CEdev/lib/src/graphics/graphx/graphics_lib.asm

+35-25
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ DEFAULT_TEXT_BG_COLOR equ 255
117117
DEFAULT_TEXT_TP_COLOR equ 255
118118
;-------------------------------------------------------------------------------
119119

120+
;-------------------------------------------------------------------------------
121+
; Useful Macros
122+
#define mSignedCompareDE() or a,a \ sbc hl,de \ add hl,hl \ jp po,$+5 \.r \ ccf \
123+
#define mSignedCompareBC() or a,a \ sbc hl,bc \ add hl,hl \ jp po,$+5 \.r \ ccf \
124+
;-------------------------------------------------------------------------------
125+
120126
;-------------------------------------------------------------------------------
121127
_AllocSprite:
122128
; Allocates space for a new sprite
@@ -663,12 +669,12 @@ _HorizLine:
663669
add iy,sp
664670
ld de,(_ymin) \.r
665671
ld hl,(iy+6)
666-
call _SignedCompare_ASM \.r ; compare y coordinate <-> ymin
672+
mSignedCompareDE() ; compare y coordinate <-> ymin
667673
ret c
668674
ld hl,(_ymax) \.r
669675
dec hl ; inclusive
670676
ld de,(iy+6)
671-
call _SignedCompare_ASM \.r ; compare y coordinate <-> ymax
677+
mSignedCompareDE() ; compare y coordinate <-> ymax
672678
ret c
673679
ld hl,(iy+9)
674680
ld de,(iy+3)
@@ -682,7 +688,7 @@ _HorizLine:
682688
call _Min_ASM \.r
683689
ld (iy+9),hl ; save minimum x value
684690
ld de,(iy+3)
685-
call _SignedCompare_ASM \.r
691+
mSignedCompareDE()
686692
ret c
687693
ld hl,(iy+9)
688694
sbc hl,de
@@ -742,11 +748,11 @@ _VertLine:
742748
ld hl,(_xmax) \.r
743749
dec hl ; inclusive
744750
ld de,(iy+3)
745-
call _SignedCompare_ASM \.r
751+
mSignedCompareDE()
746752
ret c ; return if x > xmax
747-
ld hl,(iy+3)
753+
ex de,hl
748754
ld de,(_xmin) \.r
749-
call _SignedCompare_ASM \.r
755+
mSignedCompareDE()
750756
ret c ; return if x < xmin
751757
ld hl,(iy+9)
752758
ld de,(iy+6)
@@ -760,7 +766,7 @@ _VertLine:
760766
call _Min_ASM \.r ; get maximum y
761767
ld (iy+9),hl
762768
ld de,(iy+6)
763-
call _SignedCompare_ASM \.r
769+
mSignedCompareDE()
764770
ret c ; return if not within y bounds
765771
ld hl,(iy+9)
766772
sbc hl,de
@@ -2010,6 +2016,10 @@ _Sprite:
20102016
; arg4 : Height -- 8bits
20112017
; Returns:
20122018
; None
2019+
scf
2020+
sbc hl,hl
2021+
ld (hl),2
2022+
20132023
push ix ; save ix sp
20142024
call _ClipDraw_ASM \.r
20152025
pop ix ; restore ix sp
@@ -2214,13 +2224,13 @@ _ClipDraw_ASM:
22142224
ld (iy+6),hl ; save a ptr to the sprite data to change offsets
22152225
ld bc,(ix+9)
22162226
ld hl,(_ymin) \.r
2217-
call _SignedCompareBC_ASM \.r
2227+
mSignedCompareBC()
22182228
jr c,NoTopClipNeeded_ASM
22192229
ld hl,(iy+3)
22202230
add hl,bc
22212231
ex de,hl
22222232
ld hl,(_ymin) \.r
2223-
call _SignedCompare_ASM \.r
2233+
mSignedCompareDE()
22242234
ret nc ; bc = y location
22252235
ld hl,(_ymin) \.r ; ymin
22262236
or a,a
@@ -2239,13 +2249,13 @@ NoTopClipNeeded_ASM:
22392249
push bc
22402250
pop hl ; hl = y coordinate
22412251
ld de,(_ymax) \.r
2242-
call _SignedCompare_ASM \.r
2252+
mSignedCompareDE()
22432253
ret nc ; return if offscreen on bottom
22442254
; bc = y coordinate
22452255
ld hl,(iy+3) ; hl = tmpHeight
22462256
add hl,bc
22472257
ld de,(_ymax) \.r
2248-
call _SignedCompare_ASM \.r
2258+
mSignedCompareDE()
22492259
jr c,NoBottomClipNeeded_ASM ; is partially clipped bottom?
22502260
ex de,hl ; hl = ymax
22512261
; bc = y coordinate
@@ -2254,14 +2264,14 @@ NoTopClipNeeded_ASM:
22542264
NoBottomClipNeeded_ASM:
22552265
ld hl,(ix+6) ; hl = x coordinate
22562266
ld de,(_xmin) \.r
2257-
call _SignedCompare_ASM \.r
2267+
mSignedCompareDE()
22582268
ld hl,(ix+6) ; hl = x coordinate
22592269
jr nc,NoLeftClip_ASM ; is partially clipped left?
22602270
ld de,(iy+0) ; de = tmpWidth
22612271
add hl,de
22622272
ld de,(_xmin) \.r
22632273
ex de,hl
2264-
call _SignedCompare_ASM \.r
2274+
mSignedCompareDE()
22652275
ret nc ; return if offscreen
22662276
ld de,(ix+6) ; de = x coordinate
22672277
ld hl,(_xmin) \.r
@@ -2279,15 +2289,15 @@ NoBottomClipNeeded_ASM:
22792289
ld (ix+6),hl ; save min x coordinate
22802290
NoLeftClip_ASM:
22812291
ld de,(_xmax) \.r ; de = xmax
2282-
call _SignedCompare_ASM \.r
2292+
mSignedCompareDE()
22832293
ret nc ; return if offscreen
22842294
ld hl,(ix+6) ; hl = x coordinate
22852295
ld de,(iy+0) ; de = tmpWidth
22862296
add hl,de
22872297
ld de,(_xmax) \.r
22882298
ex de,hl
2289-
call _SignedCompare_ASM \.r ; is partially clipped right?
2290-
jr nc,NoRightClip_ASM
2299+
mSignedCompareDE()
2300+
jr nc,NoRightClip_ASM ; is partially clipped right?
22912301
ld hl,(_xmax) \.r ; clip on the right
22922302
ld de,(ix+6)
22932303
ccf
@@ -4231,7 +4241,7 @@ _: ld bc,(ix+6)
42314241
ld a,(ix+9)
42324242
ld (iy+6),a
42334243
ld (iy+9),1
4234-
lea iy,iy+10 ; push(x, x, y, 1);
4244+
lea iy,iy+10 ; push(x, x, y, 1)
42354245
InvalidPush_1:
42364246
add hl,de
42374247
dec hl
@@ -4248,7 +4258,7 @@ _: ld bc,(ix+6)
42484258
inc a
42494259
ld (iy+6),a
42504260
ld (iy+9),-1
4251-
lea iy,iy+10 ; push(x, x, y+1, -1);
4261+
lea iy,iy+10 ; push(x, x, y+1, -1)
42524262
InvalidPush_2:
42534263
42544264
jp WhileStackNotEmptyBegin \.r ; while ( sp > stack )
@@ -4267,7 +4277,7 @@ WhileStackNotEmpty:
42674277
ld de,(iy+6)
42684278
add hl,de
42694279
ld (ix+9),l ; pop (xl, xr, y, dy);
4270-
; for ( x = xl; x >= xmin && gfx_getpixel(x, y) == old_color; --x )
4280+
; for ( x = xl; x >= xmin && getpixel(x, y) == old_color; --x )
42714281
ld e,l
42724282
ld hl,(currDrawBuffer)
42734283
add hl,bc
@@ -4281,7 +4291,7 @@ WhileStackNotEmpty:
42814291
Loop_For_1:
42824292
NewColor_SMC_1 =$+1
42834293
ld a,0
4284-
ld (de),a ; setpixel(x, y);
4294+
ld (de),a ; setpixel(x, y)
42854295
dec de
42864296
dec bc
42874297
Loop_For_Begin_1:
@@ -4303,7 +4313,7 @@ Loop_For_Done:
43034313
jp nc,SkipChecks \.r
43044314
add hl,bc
43054315
inc hl
4306-
ld (ix-19),hl ; left = x+1;
4316+
ld (ix-19),hl ; left = x+1
43074317
or a,a
43084318
sbc hl,bc ; if( left < xl )
43094319
jp p,+_ \.r
@@ -4348,7 +4358,7 @@ _: ld (iy+0),bc
43484358
NoPush_5:
43494359
InvalidPush_5:
43504360
ld bc,(ix-16)
4351-
inc bc ; x = xl+1;
4361+
inc bc ; x = xl+1
43524362
43534363
DoWhileLoop:
43544364
ld e,(ix+9)
@@ -4464,10 +4474,10 @@ _: ld bc,(ix-43)
44644474
ld (iy+9),a ; a = -dy
44654475
ld a,(ix+9)
44664476
ld (iy+6),a
4467-
lea iy,iy+10 ; push(xr+1, x-1, y, -dy);
4477+
lea iy,iy+10 ; push(xr+1, x-1, y, -dy)
44684478
SkipChecks:
44694479
InvalidPush_4:
4470-
ld bc,(ix+6) ; for( ++x; x <= xr && getpixel(x, y) != old_color; ++x );
4480+
ld bc,(ix+6) ; for( ++x; x <= xr && getpixel(x, y) != old_color; ++x )
44714481
inc bc
44724482
ld e,(ix+9)
44734483
ld hl,(currDrawBuffer)
@@ -4487,7 +4497,7 @@ Loop_For_Begin_3:
44874497
sbc hl,bc
44884498
jr c,Loop_For_Done_3
44894499
add hl,bc
4490-
ld a,(de)
4500+
ld a,(de) ; getpixel(x, y)
44914501
OldColor_SMC_3 =$+1
44924502
cp a,0
44934503
jr nz,Loop_For_3

0 commit comments

Comments
 (0)