Prev: BE5F Up: Map Next: BF6A
BF22: Sort Draw List
Used by the routine at MainLoop.
Sorts the draw list, so that the sprites that are highest up the screen are drawn first. Sprites at the same height are drawn from left to right. It is a bubble sort, which is repeated until there is a pass without any swaps.
SortDrawList BF22 LD A,($D358) Return if there are fewer than two entries in the list.
BF25 AND A
BF26 RET Z
SortDrawList_Pass BF27 LD DE,$D357 Point DE at the height of the first entry.
BF2A LD HL,$D35A Point HL at DrawListEntries_SecondHeight.
BF2D XOR A Clear the flag that records that a swap was made.
BF2E EX AF,AF'
SortDrawList_Compare BF2F LD A,(DE) Fetch the height of the first entry...
BF30 CP (HL) ...and compare it with the height of the second.
BF31 DEC HL Point at the horizontal positions.
BF32 DEC DE
BF33 JR C,SortDrawList_Swap Jump to swap them if the first is lower.
BF35 JR NZ,SortDrawList_InOrder Jump if the first is higher, as they are in order.
BF37 LD A,(DE) Otherwise fetch the horizontal position of the first entry.
BF38 LD C,A
BF39 LD A,(HL) Compare it with the horizontal position of the second.
BF3A CP C
BF3B JR NC,SortDrawList_InOrder Jump if they are in order.
SortDrawList_Swap BF3D EX AF,AF' Record that a swap has been made.
BF3E OR $01
BF40 EX AF,AF'
BF41 DEC HL Point at the types.
BF42 DEC DE
BF43 LD A,(DE) Swap the types.
BF44 LD C,(HL)
BF45 LD (HL),A
BF46 LD A,C
BF47 LD (DE),A
BF48 INC DE Point at the horizontal positions.
BF49 INC HL
BF4A LD A,(DE) Swap the horizontal positions.
BF4B LD C,(HL)
BF4C LD (HL),A
BF4D LD A,C
BF4E LD (DE),A
BF4F INC DE Point at the heights.
BF50 INC HL
BF51 LD A,(DE) Swap the heights.
BF52 LD C,(HL)
BF53 LD (HL),A
BF54 LD A,C
BF55 LD (DE),A
SortDrawList_Advance BF56 INC DE Point at the type of the next entry.
BF57 INC HL
BF58 LD A,(HL) Jump if there are no more entries.
BF59 AND A
BF5A JR Z,SortDrawList_Finish
BF5C INC DE Point at the heights of the entries.
BF5D INC HL
BF5E INC DE
BF5F INC HL
BF60 JR SortDrawList_Compare Loop back to compare them.
SortDrawList_Finish BF62 EX AF,AF' Go round again if any swap was made.
BF63 JR NZ,SortDrawList_Pass
BF65 RET Return.
SortDrawList_InOrder BF66 INC DE Point at the heights and jump.
BF67 INC HL
BF68 JR SortDrawList_Advance
Prev: BE5F Up: Map Next: BF6A