Prev: DB9E Up: Map Next: DC41
DBEB: Get next drawable itemstruct
This finds the next item to draw that is furthest behind (x,y).
Used by the routine at get_next_drawable.
Input
A' A value to leave in A' when nothing is found (e.g. 255)
BC' X position
DE' Y position
Output
A' Index of the greatest item with the item_FOUND flag set, if found
IY Pointer to an itemstruct, if found
get_next_drawable_itemstruct DBEB LD BC,$1007 Set B for 16 iterations (item__LIMIT) and set C for a seven byte stride simultaneously
DBEE LD HL,$76C9 Point HL at the first item_struct's room member
Start loop
ggi_loop DBF1 BIT 7,(HL) Is the itemstruct_ROOM_FLAG_ITEM_NEARBY_7 flag set? ($80)
DBF3 JR Z,ggi_next If not, jump to the next iteration
DBF5 BIT 6,(HL) Is the itemstruct_ROOM_FLAG_ITEM_NEARBY_6 flag set? ($40)
DBF7 JR Z,ggi_next If not, jump to the next iteration
DBF9 PUSH HL Preserve the item_struct pointer
DBFA PUSH BC Preserve the item counter and stride
Select an item if it's behind the point (x,y).
DBFB INC HL Advance HL to item_struct.pos.x
DBFC LD A,(HL) Fetch item_struct.pos.x
DBFD CALL multiply_by_8 Multiply it by 8 returning the result in BC
DC00 PUSH BC Preserve the result
DC01 EXX Flip to banked regs - so we can subtract X position
DC02 POP HL Restore the result into HL
DC03 AND A Clear the carry flag prior to SBC
DC04 SBC HL,BC Calculate (item_struct.pos.x * 8 - x_position)
DC06 EXX Flip to unbanked regs - now we have the result
DC07 JR Z,ggi_next_pop Was (item_struct.pos.x * 8 <= x_position)?
DC09 JR C,ggi_next_pop Jump if so
DC0B INC HL Advance HL to item_struct.pos.y
DC0C LD A,(HL) Fetch item_struct.pos.y
DC0D CALL multiply_by_8 Multiply it by 8 returning the result in BC
DC10 PUSH BC Preserve the result
DC11 EXX Flip to banked regs - so we can subtract Y position
DC12 POP HL Restore the result into HL
Q. Why are we not clearing the carry flag like at DC03?
DC13 SBC HL,DE Calculate (item_struct.pos.y * 8 - y_position)
DC15 EXX Flip to unbanked regs - now we have the result
DC16 JR Z,ggi_next_pop Was (item_struct.pos.y * 8 <= y_position)?
DC18 JR C,ggi_next_pop Jump if so
DC1A PUSH HL Preserve the item_struct pointer
DC1B EXX Flip to banked regs - so we can store new X,Y positions
DC1C POP HL Restore the item_struct pointer
Calculate (x,y) for the next iteration.
DC1D LD A,(HL) Fetch item_struct.pos.y
DC1E CALL multiply_by_8 Multiply it by 8 returning the result in BC
DC21 LD E,C DE = BC
DC22 LD D,B
DC23 DEC HL Rewind HL to point at item_struct.pos.x
DC24 LD A,(HL) Fetch item_struct.pos.x
DC25 CALL multiply_by_8 Multiply it by 8 returning the result in BC
DC28 DEC HL Rewind HL to point at item_struct (jump back over item & room bytes)
DC29 DEC HL
IY is used to return an item_struct here which is unusual compared to the rest of the code which maintains IY as a current vischar pointer.
DC2A PUSH HL Copy the item_struct pointer to IY
DC2B POP IY
DC2D EXX Flip to unbanked regs
DC2E POP BC Restore the item counter and stride
DC2F PUSH BC Preserve the item counter and stride
DC30 LD A,$10 Calculate the item index (item__LIMIT - item counter)
DC32 SUB B
DC33 OR $40 Set the item found flag
DC35 EX AF,AF' Return the value in A'
ggi_next_pop DC36 POP BC Restore item counter and stride
DC37 POP HL Restore item_struct pointer
ggi_next DC38 LD A,C Advance by stride to next item
DC39 ADD A,L
DC3A LD L,A
DC3B JR NC,ggi_loop_end
DC3D INC H
ggi_loop_end DC3E DJNZ ggi_loop ...loop
DC40 RET Return
Prev: DB9E Up: Map Next: DC41