Prev: 40825 Up: Map Next: 41150
40857: Sort Draw List
Used by the routines at RunFrame and RunGame.
Sorts the draw list at DrawList into the order in which the items must be drawn, and builds the sorted list at SortedDrawList. This is a painter's algorithm: an item can only be drawn after every item which is behind it. Each item in the lists has three bytes: flags, and the address of its record. Bit 0 of the flags is set if the record is an entity, and reset if it is a compact record with the three coordinates at offsets 2 4 and 6 which is given the size 6 6 1. Bit 7 is set once the item has been moved to the sorted list. . The routine takes the first item which has not been moved, and compares it with each of the other items which have not been moved. The result for each pair is a number in the range 0-26 made from the relation along the first, second and third coordinates (0 for 'the other item is before this one', 1 for 'they overlap' and 2 for 'the other item is after this one'). The number selects a routine from the table at DrawOrderTable. If the other item has to be drawn first then it becomes the item under consideration (unless it has been tried already, which would be a loop). When an item has no other item that has to be drawn before it, it is moved to the sorted list and the whole process starts again.
SortDrawList 40857 LD HL,44943 Point HL at the sorted list.
40860 LD (41338),HL Store it as the address of the next free place in the sorted list.
This entry point is used by the routine at MoveItemToSortedList.
SortDrawList_Next 40863 LD DE,45051 Point DE at the start of the draw list.
SortDrawList_FirstLoop 40866 LD A,(DE) Fetch the flags of the next item.
40867 INC DE Move onto the address of the record.
40868 AND A Test the flags.
40869 JP Z,EndSortedList Jump to EndSortedList if this is the end of the list.
40872 BIT 7,A Test whether the item has been moved already.
40874 LD L,A Keep the flags in L.
40875 LD A,(DE) Fetch the first byte of the address of the record.
40876 LD C,A Keep it in C.
40877 INC DE Move onto the second byte of the address.
40878 LD A,(DE) Fetch it.
40879 LD B,A Keep it in B.
40880 INC DE Move onto the next item.
40881 JR NZ,SortDrawList_FirstLoop Jump back to look at the next item if this one has been moved already.
40883 LD (41390),DE Store the address of the next item as that of the item under consideration.
40887 PUSH BC Copy the address of the record into IX (using the stack).
40888 POP IX
40890 RR L Rotate bit 0 of the flags into the carry flag.
40892 JR C,SortDrawList_SecondLoop Jump to SortDrawList_SecondLoop if the record is an entity.
40894 LD L,(IX+2) Fetch the first byte of the first coordinate of the record.
40897 LD H,(IX+3) Fetch the second byte of the first coordinate of the record.
40900 LD (40827),HL Store the first coordinate in the first scratch item.
40903 LD L,(IX+4) Fetch the first byte of the second coordinate of the record.
40906 LD H,(IX+5) Fetch the second byte of the second coordinate of the record.
40909 LD (40830),HL Store the second coordinate in the first scratch item.
40912 LD L,(IX+6) Fetch the first byte of the third coordinate of the record.
40915 LD H,(IX+7) Fetch the second byte of the third coordinate of the record.
40918 LD (40833),HL Store the third coordinate in the first scratch item.
40921 LD IX,40825 Point IX at ScratchItems.
This entry point is used by the routines at IgnoreItem, IgnoreOverlap, DrawOtherItemFirst and ResolveFullOverlap.
SortDrawList_SecondLoop 40925 LD A,(DE) Fetch the flags of the next item to compare.
40926 INC DE Move onto the address of the record.
40927 AND A Test the flags.
40928 JP Z,MoveItemToSortedList Jump to MoveItemToSortedList if the end of the list has been reached, as no other item has to go first.
40931 BIT 7,A Test whether the item has been moved already.
40933 LD L,A Keep the flags in L.
40934 LD A,(DE) Fetch the first byte of the address of the record.
40935 LD C,A Keep it in C.
40936 INC DE Move onto the second byte of the address.
40937 LD A,(DE) Fetch it.
40938 LD B,A Keep it in B.
40939 INC DE Move onto the next item.
40940 JR NZ,SortDrawList_SecondLoop Jump back to look at the next item if this one has been moved already.
40942 LD (41392),DE Store the address of the next item as that of the other item.
40946 PUSH BC Copy the address of the record into IY (using the stack).
40947 POP IY
40949 RR L Rotate bit 0 of the flags into the carry flag.
40951 JR C,SortDrawList_Compare Jump to SortDrawList_Compare if the record is an entity.
40953 LD L,(IY+2) Fetch the first byte of the first coordinate of the record.
40956 LD H,(IY+3) Fetch the second byte of the first coordinate of the record.
40959 LD (40843),HL Store the first coordinate in the second scratch item.
40962 LD L,(IY+4) Fetch the first byte of the second coordinate of the record.
40965 LD H,(IY+5) Fetch the second byte of the second coordinate of the record.
40968 LD (40846),HL Store the second coordinate in the second scratch item.
40971 LD L,(IY+6) Fetch the first byte of the third coordinate of the record.
40974 LD H,(IY+7) Fetch the second byte of the third coordinate of the record.
40977 LD (40849),HL Store the third coordinate in the second scratch item.
40980 LD IY,40841 Point IY at ScratchItems_Second.
40984 LD BC,40841 Use it as the address to compare against.
Skip the pair if both are the same item.
SortDrawList_Compare 40987 PUSH IX Copy the address of the item under consideration into HL (using the stack).
40989 POP HL
40990 AND A Compare it with the address of the other item.
40991 SBC HL,BC
40993 JR Z,SortDrawList_SecondLoop Jump to SortDrawList_SecondLoop if they are the same item.
Compare the third coordinate of the two items, and put the result in C: 0 if the other item is entirely before this one, 1 if they overlap, and 2 if the other item is entirely after this one.
40995 EXX Switch to the alternate registers.
40996 LD C,0 Clear the result.
40998 LD H,(IY+8) Fetch the first byte of the third coordinate of the other item.
41001 LD L,(IY+9) Fetch the second byte of the third coordinate of the other item.
41004 LD D,0 Clear D.
41006 LD E,(IY+13) Fetch the third size of the other item.
41009 DEC E Subtract one to make the last position it covers.
41010 ADD HL,DE Find the last position that the other item covers.
41011 LD D,(IX+8) Fetch the first byte of the third coordinate of this item.
41014 LD E,(IX+9) Fetch the second byte of the third coordinate of this item.
41017 SBC HL,DE Subtract the position of this item.
41019 JR C,SortDrawList_Second Jump to SortDrawList_Second if the other item ends before this one begins.
41021 LD L,(IX+13) Fetch the third size of this item.
41024 LD H,0 Clear H.
41026 ADD HL,DE Find the position just beyond this item.
41027 EX DE,HL Keep it in DE.
41028 LD H,(IY+8) Fetch the first byte of the third coordinate of the other item.
41031 LD L,(IY+9) Fetch the second byte of the third coordinate of the other item.
41034 AND A Subtract the position beyond this item.
41035 SBC HL,DE
41037 JR C,SortDrawList_ThirdAdd Jump to SortDrawList_ThirdAdd if the other item starts before the end of this one.
41039 INC C Add one to the result.
SortDrawList_ThirdAdd 41040 INC C Add one to the result.
Do the same for the second coordinate, adding 3 to the result for an overlap and 6 for the other item being entirely after this one.
SortDrawList_Second 41041 LD H,(IY+5) Fetch the first byte of the second coordinate of the other item.
41044 LD L,(IY+6) Fetch the second byte of the second coordinate of the other item.
41047 LD D,0 Clear D.
41049 LD E,(IY+12) Fetch the second size of the other item.
41052 DEC E Subtract one to make the last position it covers.
41053 ADD HL,DE Find the last position that the other item covers.
41054 LD D,(IX+5) Fetch the first byte of the second coordinate of this item.
41057 LD E,(IX+6) Fetch the second byte of the second coordinate of this item.
41060 SBC HL,DE Subtract the position of this item.
41062 JR C,SortDrawList_First Jump to SortDrawList_First if the other item ends before this one begins.
41064 LD L,(IX+12) Fetch the second size of this item.
41067 LD H,0 Clear H.
41069 ADD HL,DE Find the position just beyond this item.
41070 EX DE,HL Keep it in DE.
41071 LD H,(IY+5) Fetch the first byte of the second coordinate of the other item.
41074 LD L,(IY+6) Fetch the second byte of the second coordinate of the other item.
41077 AND A Subtract the position beyond this item.
41078 SBC HL,DE
41080 LD A,C Fetch the result so far.
41081 JR C,SortDrawList_SecondAdd Jump to SortDrawList_SecondAdd if the other item starts before the end of this one.
41083 ADD A,3 Add 3 to the result.
SortDrawList_SecondAdd 41085 ADD A,3 Add 3 to the result.
41087 LD C,A Store the result.
Do the same for the first coordinate, adding 9 to the result for an overlap and 18 for the other item being entirely after this one.
SortDrawList_First 41088 LD H,(IY+2) Fetch the first byte of the first coordinate of the other item.
41091 LD L,(IY+3) Fetch the second byte of the first coordinate of the other item.
41094 LD D,0 Clear D.
41096 LD E,(IY+11) Fetch the first size of the other item.
41099 DEC E Subtract one to make the last position it covers.
41100 ADD HL,DE Find the last position that the other item covers.
41101 LD D,(IX+2) Fetch the first byte of the first coordinate of this item.
41104 LD E,(IX+3) Fetch the second byte of the first coordinate of this item.
41107 SBC HL,DE Subtract the position of this item.
41109 JR C,SortDrawList_Dispatch Jump to SortDrawList_Dispatch if the other item ends before this one begins.
41111 LD L,(IX+11) Fetch the first size of this item.
41114 LD H,0 Clear H.
41116 ADD HL,DE Find the position just beyond this item.
41117 EX DE,HL Keep it in DE.
41118 LD H,(IY+2) Fetch the first byte of the first coordinate of the other item.
41121 LD L,(IY+3) Fetch the second byte of the first coordinate of the other item.
41124 AND A Subtract the position beyond this item.
41125 SBC HL,DE
41127 LD A,C Fetch the result so far.
41128 JR C,SortDrawList_FirstAdd Jump to SortDrawList_FirstAdd if the other item starts before the end of this one.
41130 ADD A,9 Add 9 to the result.
SortDrawList_FirstAdd 41132 ADD A,9 Add 9 to the result.
41134 LD C,A Store the result.
Use the result to jump to the routine in the table at DrawOrderTable.
SortDrawList_Dispatch 41135 LD A,C Fetch the result.
41136 ADD A,A Double it to make the offset into the table.
41137 EXX Switch back to the normal registers.
41138 LD HL,41150 Point HL at DrawOrderTable.
41141 LD C,A Copy the offset into C.
41142 LD B,0 Clear B.
41144 ADD HL,BC Add the offset to find the entry.
41145 LD A,(HL) Fetch the first byte of the address.
41146 INC HL Move onto the second byte.
41147 LD H,(HL) Fetch it.
41148 LD L,A Combine them into HL.
41149 JP (HL) Jump to the routine.
Prev: 40825 Up: Map Next: 41150