Skip to content

Commit 2c12310

Browse files
author
Matt Waltz
authored
Merge pull request #48 from CE-Programming/opt/_AllocSprite
Optimized _AllocSprite
2 parents 60006ac + 9cee925 commit 2c12310

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/graphx/graphx.asm

+26-21
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,27 @@ _AllocSprite:
133133
; arg2 : pointer to malloc routine
134134
; Returns:
135135
; Pointer to allocated sprite, first byte width, second height
136-
ld iy,0
137-
add iy,sp
138-
ld l,(iy+3) ; l = width of sprite
139-
ld h,(iy+6) ; h = height of sprite
140-
ld iy,(iy+9) ; iy = pointer to some malloc
141-
push hl
142-
mlt hl ; width * height
143-
inc hl
144-
inc hl ; allocate space for width and height bytes
145-
push hl
146-
call __indcall ; call the malloc routine
147-
pop de
148-
pop de
149-
add hl,de
150-
or a,a
151-
sbc hl,de
152-
ret z ; check to make sure malloc did not fail
153-
ld (hl),e ; store width
154-
inc hl
155-
ld (hl),d ; store height
156-
dec hl ; return sprite pointer
136+
ld bc,3
137+
push bc
138+
pop hl
139+
add hl,sp
140+
ld e,(hl) ; e = width
141+
add hl,bc
142+
ld d,(hl) ; d = height
143+
add hl,bc
144+
ld hl,(hl) ; hl = malloc
145+
push de
146+
mlt de ; de = width * height
147+
inc de ; +2 to store width and height
148+
inc de ; de = width * height + 2
149+
push de
150+
call _indcallHL_ASM \.r ; hl = malloc(width * height + 2)
151+
pop de ; de = width * height + 2
152+
add hl,de ; this should never carry
153+
sbc hl,de ; check if malloc failed (hl == 0)
154+
pop de ; e = width, d = height
155+
ret z ; abort if malloc failed
156+
ld (hl),de ; store width and height
157157
ret
158158

159159
;-------------------------------------------------------------------------------
@@ -2689,6 +2689,11 @@ _SetTextXY:
26892689
ld (TextYPos_SMC),hl \.r
26902690
push hl ; xpos=don't care, sp=&xpos
26912691
ex de,hl ; hl=return address
2692+
;-------------------------------------------------------------------------------
2693+
_indcallHL_ASM:
2694+
; Calls HL
2695+
; Inputs:
2696+
; HL : Address to call
26922697
jp (hl)
26932698
26942699
;-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)