Prev: C2BA Up: Map Next: C352
C2C5: Compare Object Depths
Works out which of two objects which overlap should be drawn first. For each object it finds a number from the difference between a vertical and a horizontal edge, and it swaps the objects if the numbers are the wrong way round. The edges which it uses are different when the view is rotated.
Input
IX The address of the first object
IY The address of the second object
CompareObjectDepths C2C5 LD A,($A268) Fetch the rotation of the view.
C2C8 AND %00000001 Test the lowest bit.
C2CA JR NZ,CompareObjectDepths_Rotated Jump if the view is rotated.
Find the number for the second object, in DE.
C2CC LD A,(IY+$01) Fetch the horizontal position, add the horizontal size, halve it and add 40.
C2CF ADD A,(IY+$08)
C2D2 RRA
C2D3 ADD A,$40
C2D5 LD C,A Keep it in BC.
C2D6 LD B,$00
C2D8 LD A,(IY+$03) Fetch the vertical position, subtract the vertical size, halve it and add 40.
C2DB SUB (IY+$09)
C2DE RRA
C2DF ADD A,$40
C2E1 LD L,A Make the top edge a sixteen bit number in HL.
C2E2 LD H,$00
C2E4 AND A Subtract the horizontal edge.
C2E5 SBC HL,BC
C2E7 LD BC,$0100 Add 0100.
C2EA ADD HL,BC
C2EB SRL H Divide the result by two...
C2ED RR L
C2EF EX DE,HL ...and keep it in DE.
Find the number for the first object, in HL, in the same way.
C2F0 LD A,(IX+$01) Fetch the horizontal position, add the horizontal size, halve it and add 40.
C2F3 ADD A,(IX+$08)
C2F6 RRA
C2F7 ADD A,$40
C2F9 LD C,A Keep it in BC.
C2FA LD B,$00
C2FC LD A,(IX+$03) Fetch the vertical position and subtract the vertical size.
C2FF SUB (IX+$09)
CompareObjectDepths_Finish C302 RRA Halve it and add 40.
C303 ADD A,$40
C305 LD L,A Make the top edge a sixteen bit number in HL.
C306 LD H,$00
C308 AND A Subtract the horizontal edge.
C309 SBC HL,BC
C30B LD BC,$0100 Add 0100.
C30E ADD HL,BC
C30F SRL H Divide the result by two.
C311 RR L
C313 AND A Subtract the number for the other object.
C314 SBC HL,DE
C316 RET NC Return if the first number is not the lower.
C317 JP ChooseOtherObject Otherwise swap the objects.
Find the numbers when the view is rotated. The number for the first object goes in DE...
CompareObjectDepths_Rotated C31A LD A,(IX+$01) Fetch the horizontal position, subtract the horizontal size, halve it and add 40.
C31D SUB (IX+$08)
C320 RRA
C321 ADD A,$40
C323 LD C,A Keep it in BC.
C324 LD B,$00
C326 LD A,(IX+$03) Fetch the vertical position, add the vertical size, halve it and add 40.
C329 ADD A,(IX+$09)
C32C RRA
C32D ADD A,$40
C32F LD L,A Make the lower edge a sixteen bit number in HL.
C330 LD H,$00
C332 AND A Subtract the horizontal edge.
C333 SBC HL,BC
C335 LD BC,$0100 Add 0100.
C338 ADD HL,BC
C339 SRL H Divide the result by two...
C33B RR L
C33D EX DE,HL ...and keep it in DE.
...and the number for the second object is found in HL by the end of the routine above.
C33E LD A,(IY+$01) Fetch the horizontal position, subtract the horizontal size, halve it and add 40.
C341 SUB (IY+$08)
C344 RRA
C345 ADD A,$40
C347 LD C,A Keep it in BC.
C348 LD B,$00
C34A LD A,(IY+$03) Fetch the vertical position and add the vertical size.
C34D ADD A,(IY+$09)
Go to find the number.
C350 JR CompareObjectDepths_Finish Jump to CompareObjectDepths_Finish.
View the equivalent code in;
Prev: C2BA Up: Map Next: C352