Prev: 49850 Up: Map Next: 50002
49861: 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 49861 LD A,(41576) Fetch the rotation of the view.
49864 AND %00000001 Test the lowest bit.
49866 JR NZ,CompareObjectDepths_Rotated Jump if the view is rotated.
Find the number for the second object, in DE.
49868 LD A,(IY+1) Fetch the horizontal position, add the horizontal size, halve it and add 64.
49871 ADD A,(IY+8)
49874 RRA
49875 ADD A,64
49877 LD C,A Keep it in BC.
49878 LD B,0
49880 LD A,(IY+3) Fetch the vertical position, subtract the vertical size, halve it and add 64.
49883 SUB (IY+9)
49886 RRA
49887 ADD A,64
49889 LD L,A Make the top edge a sixteen bit number in HL.
49890 LD H,0
49892 AND A Subtract the horizontal edge.
49893 SBC HL,BC
49895 LD BC,256 Add 256.
49898 ADD HL,BC
49899 SRL H Divide the result by two...
49901 RR L
49903 EX DE,HL ...and keep it in DE.
Find the number for the first object, in HL, in the same way.
49904 LD A,(IX+1) Fetch the horizontal position, add the horizontal size, halve it and add 64.
49907 ADD A,(IX+8)
49910 RRA
49911 ADD A,64
49913 LD C,A Keep it in BC.
49914 LD B,0
49916 LD A,(IX+3) Fetch the vertical position and subtract the vertical size.
49919 SUB (IX+9)
CompareObjectDepths_Finish 49922 RRA Halve it and add 64.
49923 ADD A,64
49925 LD L,A Make the top edge a sixteen bit number in HL.
49926 LD H,0
49928 AND A Subtract the horizontal edge.
49929 SBC HL,BC
49931 LD BC,256 Add 256.
49934 ADD HL,BC
49935 SRL H Divide the result by two.
49937 RR L
49939 AND A Subtract the number for the other object.
49940 SBC HL,DE
49942 RET NC Return if the first number is not the lower.
49943 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 49946 LD A,(IX+1) Fetch the horizontal position, subtract the horizontal size, halve it and add 64.
49949 SUB (IX+8)
49952 RRA
49953 ADD A,64
49955 LD C,A Keep it in BC.
49956 LD B,0
49958 LD A,(IX+3) Fetch the vertical position, add the vertical size, halve it and add 64.
49961 ADD A,(IX+9)
49964 RRA
49965 ADD A,64
49967 LD L,A Make the lower edge a sixteen bit number in HL.
49968 LD H,0
49970 AND A Subtract the horizontal edge.
49971 SBC HL,BC
49973 LD BC,256 Add 256.
49976 ADD HL,BC
49977 SRL H Divide the result by two...
49979 RR L
49981 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.
49982 LD A,(IY+1) Fetch the horizontal position, subtract the horizontal size, halve it and add 64.
49985 SUB (IY+8)
49988 RRA
49989 ADD A,64
49991 LD C,A Keep it in BC.
49992 LD B,0
49994 LD A,(IY+3) Fetch the vertical position and add the vertical size.
49997 ADD A,(IY+9)
Go to find the number.
50000 JR CompareObjectDepths_Finish Jump to CompareObjectDepths_Finish.
View the equivalent code in;
Prev: 49850 Up: Map Next: 50002