Skip to content

Commit 0def17a

Browse files
committed
optimized sprite flip/rotate90
1 parent 0ac2683 commit 0def17a

File tree

1 file changed

+70
-66
lines changed

1 file changed

+70
-66
lines changed

src/graphx/graphx.asm

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4584,98 +4584,98 @@ d_18: ld bc,(ix-17)
45844584

45854585
;-------------------------------------------------------------------------------
45864586
gfx_FlipSpriteY:
4587-
; Flips an array horizontally about the center vertical axis
4587+
; Flips a sprite horizontally about the center vertical axis
45884588
; Arguments:
45894589
; arg0 : Pointer to sprite struct input
45904590
; arg1 : Pointer to sprite struct output
45914591
; Returns:
45924592
; arg1 : Pointer to sprite struct output
45934593
ld iy,0
45944594
add iy,sp
4595-
push ix
4596-
ld ix,(iy+3)
4597-
ld a,(ix+0) ; a = width of sprite
4595+
ld de,(iy+3)
4596+
ld a,(de) ; a = width of sprite
4597+
inc.s bc ; clear UBC
4598+
45984599
sbc hl,hl
45994600
ld l,a
46004601
ld c,a
4601-
push hl
4602-
ld (.width),a
4603-
add hl,hl
4604-
ld (.delta),hl ; width*2
4605-
ld a,(ix+1) ; a = height of sprite
4606-
pop hl
4607-
lea de,ix+2
4602+
4603+
inc de
4604+
ld a,(de) ; a = height of sprite
4605+
4606+
inc de
46084607
add hl,de
4609-
ld ix,(iy+6)
4610-
ld (ix+1),a ; store height to width
4611-
ld (ix+0),c ; store width to height
4612-
lea de,ix+2 ; de -> sprite data
4613-
ex (sp),ix ; restore stack frame
4608+
ld de,(iy+6) ; de -> sprite data
4609+
push de
4610+
inc de
4611+
ld (de),a ; store height
4612+
ld iyh, a
4613+
; inc de ; use the inc de inside the loop instead
46144614
.loop:
4615-
ld b,0
4616-
.width := $-1
4617-
ld c,a
4615+
ld b,c ; width
46184616
.pixelloop:
46194617
dec hl
4618+
inc de
46204619
ld a,(hl)
46214620
ld (de),a ; store the new pixel data
4622-
inc de
46234621
djnz .pixelloop
4624-
ld a,c
4625-
ld bc,0
4626-
.delta := $-3
4622+
; hl += delta * 2
46274623
add hl,bc
4628-
dec a
4624+
add hl,bc
4625+
dec iyh
46294626
jr nz,.loop
46304627
pop hl
4628+
ld (hl),c ; store width (the loop preserves c)
46314629
ret
46324630

46334631
;-------------------------------------------------------------------------------
46344632
gfx_FlipSpriteX:
4635-
; Flip a sprite vertically about the center horizontal axis
4633+
; Flips a sprite vertically about the center horizontal axis
46364634
; Arguments:
46374635
; arg0 : Pointer to sprite struct input
46384636
; arg1 : Pointer to sprite struct output
46394637
; Returns:
46404638
; arg1 : Pointer to sprite struct output
4641-
ld iy,0
4642-
add iy,sp
4643-
push ix
4644-
ld ix,(iy+3)
4645-
xor a,a
4646-
sub a,(ix+0)
4647-
ld (.delta),a
4648-
neg
4649-
ld (.width),a
4650-
ld l,(ix+1)
4651-
ld c,l
4652-
dec l
4653-
ld h,a
4654-
mlt hl
4655-
lea de,ix+2
4656-
add hl,de
4657-
ld ix,(iy+6)
4658-
ld (ix+0),a
4659-
ld (ix+1),c
4660-
lea de,ix+2
4661-
push ix
4639+
ld iy, 6
4640+
lea hl, iy
4641+
ld c, l ; prevent underflow from ldi
4642+
add hl, sp
4643+
ld de, (hl)
4644+
push de ; return value
4645+
dec hl
4646+
dec hl
4647+
dec hl
4648+
ld hl, (hl)
4649+
4650+
xor a, a
4651+
ld b, (hl)
4652+
sub a, b
4653+
ld (.delta), a
4654+
ld iyl, b ; (.width)
4655+
ldi ; copy width
4656+
4657+
ld a, (hl)
4658+
ld c, a
4659+
ldi ; copy height (and decrement C)
4660+
4661+
mlt bc
4662+
add hl, bc
46624663
.loop:
4663-
ld bc,0
4664-
.width := $-3
4664+
lea bc, iy ; (.width)
46654665
ldir
4666-
ld bc,-1
4667-
.delta := $-3
4668-
add hl,bc
4669-
add hl,bc
4666+
dec bc ; ld bc, -1
4667+
ld c, -1
4668+
.delta := $-1
4669+
add hl, bc
4670+
add hl, bc
46704671
dec a
4671-
jr nz,.loop
4672+
jr nz, .loop
46724673
pop hl
4673-
pop ix
46744674
ret
46754675

46764676
;-------------------------------------------------------------------------------
46774677
gfx_RotateSpriteC:
4678-
; Rotates an array 90 degress clockwise
4678+
; Rotates a sprite 90 degress clockwise
46794679
; Arguments:
46804680
; arg0 : Pointer to sprite struct input
46814681
; arg1 : Pointer to sprite struct output
@@ -4756,29 +4756,33 @@ gfx_RotateSpriteCC:
47564756

47574757
;-------------------------------------------------------------------------------
47584758
gfx_RotateSpriteHalf:
4759-
; Rotates an array 180 degrees
4759+
; Rotates a sprite 180 degrees
47604760
; Arguments:
47614761
; arg0 : Pointer to sprite struct input
47624762
; arg1 : Pointer to sprite struct output
47634763
; Returns:
47644764
; arg1 : Pointer to sprite struct output
4765-
ld iy,0
4766-
add iy,sp
4767-
ld hl,(iy+3)
4768-
ld c,(hl) ; c = width
4765+
ld hl, 6
4766+
add hl, sp
4767+
ld de, (hl) ; output sprite
4768+
dec hl
4769+
dec hl
4770+
dec hl
4771+
ld hl, (hl) ; input sprite
4772+
ld c, (hl) ; c = width
47694773
inc hl
4770-
ld b,(hl) ; b = height
4771-
ld iy,(iy+6)
4772-
ld (iy+0),bc
4774+
ld b, (hl) ; b = height
4775+
ex de, hl
4776+
ld (hl), bc
4777+
ex de, hl
47734778
mlt bc
4774-
add hl,bc
4775-
lea de,iy
4779+
add hl, bc
47764780
push de
47774781
.loop:
47784782
inc de
47794783
inc de
47804784
ldd
4781-
jp pe,.loop
4785+
jp pe, .loop
47824786
pop hl
47834787
ret
47844788

0 commit comments

Comments
 (0)