Skip to content

Commit a3b2a38

Browse files
committed
Optimized _AllocSprite
1 parent 6643e90 commit a3b2a38

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,27 @@ _AllocSprite:
121121
; arg2 : pointer to malloc routine
122122
; Returns:
123123
; Pointer to allocated sprite, first byte width, second height
124-
ld iy,0
125-
add iy,sp
126-
ld l,(iy+3) ; l = width of sprite
127-
ld h,(iy+6) ; h = height of sprite
128-
ld iy,(iy+9) ; iy = pointer to some malloc
129-
push hl
130-
mlt hl ; width * height
131-
inc hl
132-
inc hl ; allocate space for width and height bytes
133-
push hl
134-
call __indcall ; call the malloc routine
135-
pop de
136-
pop de
137-
add hl,de
138-
or a,a
139-
sbc hl,de
140-
ret z ; check to make sure malloc did not fail
141-
ld (hl),e ; store width
142-
inc hl
143-
ld (hl),d ; store height
144-
dec hl ; return sprite pointer
124+
ld bc,3
125+
push bc
126+
pop hl
127+
add hl,sp
128+
ld e,(hl) ;; e = width
129+
add hl,bc
130+
ld d,(hl) ;; d = height
131+
add hl,bc
132+
ld hl,(hl) ;; hl = malloc
133+
push de
134+
mlt de ;; de = width * height
135+
inc de ; +2 to store width and height
136+
inc de ;; de = width * height + 2
137+
push de
138+
call _indcallHL_ASM ;; hl = malloc(width * height + 2)
139+
pop de ;; de = width * height + 2
140+
add hl,de ; this should never carry
141+
sbc hl,de ; check if malloc failed (hl == 0)
142+
pop de ;; e = width, d = height
143+
ret z ; abort if malloc failed
144+
ld (hl),de ; store width and height
145145
ret
146146

147147
;-------------------------------------------------------------------------------
@@ -4368,6 +4368,13 @@ _: rla
43684368
xor a,1
43694369
ret
43704370

4371+
;-------------------------------------------------------------------------------
4372+
_indcallHL_ASM:
4373+
; Calls HL
4374+
; Inputs:
4375+
; HL : Address to call
4376+
jp (hl)
4377+
43714378
;-------------------------------------------------------------------------------
43724379
CharSpacing_ASM:
43734380
.dl DefaultCharSpacing_ASM \.r

0 commit comments

Comments
 (0)