Prev: 40033 Up: Map Next: 40217
40057: Draw Line
Used by the routine at DrawRoomFrame.
Draws a line between two points with PlotPixel. The longer of the two distances is the number of steps, and the other changes by a fraction of a pixel at each step, which is worked out with Divide. The flags in LineFlags have bit 0 set if the line goes down, and bit 1 set if it goes right.
Before After
draw-line-before draw-line-after
Input
B The Y position of the first point
C The X position of the first point
D The Y position of the second point
E The X position of the second point
DrawLine 40057 LD H,B Keep the Y position of the first point in H.
40058 LD L,C Keep the X position of the first point in L.
40059 LD C,0 C=0 (the flags).
40061 LD A,H Fetch the Y position of the first point.
40062 SUB D Subtract the Y position of the second point.
40063 JR NC,DrawLine_Width Jump to DrawLine_Width if the line does not go down.
40065 NEG Negate it, to make the distance positive.
40067 SET 0,C Set bit 0 of the flags.
DrawLine_Width 40069 LD B,A Keep the distance up or down in B.
40070 LD A,L Fetch the X position of the first point.
40071 SUB E Subtract the X position of the second point.
40072 JR NC,DrawLine_Direction Jump to DrawLine_Direction if the line does not go right.
40074 NEG Negate it, to make the distance positive.
40076 SET 1,C Set bit 1 of the flags.
DrawLine_Direction 40078 CP B Compare the distance across with the distance up or down.
40079 EX AF,AF' Switch to the shadow AF register.
40080 LD A,C Fetch the flags.
40081 LD (24100),A Write the flags to LineFlags.
40084 EX AF,AF' Switch back to the main AF register.
40085 JP C,DrawLine_Height Jump to DrawLine_Height if the distance up or down is more.
The distance across is the most, so move one pixel across at each step.
40088 LD (24099),A Write the distance across to LineDistance.
40091 PUSH DE Stash the second point on the stack.
40092 PUSH HL Stash the first point on the stack.
40093 LD E,A Keep the distance across in E.
40094 LD D,0 Clear the high byte of the distance.
40096 LD L,D Clear L.
40097 LD H,B H=the distance up or down.
40098 CALL Divide Call Divide to work out the fraction of a pixel to move up or down.
40101 LD A,(24100) Fetch the line flags from LineFlags.
40104 BIT 0,A Jump to DrawLine_StepAcross if the line goes down.
40106 JR NZ,DrawLine_StepAcross
40108 CALL NegateHL Call NegateHL to negate the fraction.
DrawLine_StepAcross 40111 LD A,(24100) Fetch the line flags from LineFlags.
40114 BIT 1,A Test bit 1.
40116 LD C,1 C=1 (the step to the right).
40118 JR NZ,DrawLine_Across Jump to DrawLine_Across if the line goes right.
40120 LD C,255 C=255 (the step to the left).
DrawLine_Across 40122 EX DE,HL Switch DE and HL.
40123 POP HL Restore the first point from the stack.
40124 LD A,(24099) Fetch the line distance from LineDistance.
40127 INC A Add one, to get the number of steps.
40128 LD B,A Keep the number of steps in B.
40129 LD A,E A=E.
40130 EX AF,AF' Switch to the shadow AF register.
DrawLine_AcrossLoop 40131 PUSH BC Stash the number of steps and the step across on the stack.
40132 CALL PlotPixel Call PlotPixel to plot the pixel.
40135 LD A,L A=L.
40136 EX AF,AF' Switch A with the shadow A.
40137 LD L,A L=A.
40138 ADD HL,DE Add the fraction to the position.
40139 LD A,L A=L.
40140 EX AF,AF' Switch A with the shadow A.
40141 POP BC Restore the number of steps and the step across from the stack.
40142 ADD A,C Move to the next pixel across.
40143 LD L,A L=A.
40144 DJNZ DrawLine_AcrossLoop Decrease the number of steps by one and loop back to DrawLine_AcrossLoop until every pixel of the line has been plotted.
40146 POP HL Restore the second point from the stack.
40147 JP PlotPixel Jump to PlotPixel to plot the last pixel.
The distance up or down is the most, so move one pixel up or down at each step.
DrawLine_Height 40150 EX AF,AF' Switch to the shadow AF register.
40151 LD A,B Fetch the distance up or down.
40152 LD (24099),A Write the distance up or down to LineDistance.
40155 EX AF,AF' Switch back to the main AF register.
40156 PUSH DE Stash the second point on the stack.
40157 PUSH HL Stash the first point on the stack.
40158 LD E,B Keep the distance up or down in E.
40159 LD D,0 Clear the high byte of the distance.
40161 LD L,D Clear L.
40162 LD H,A H=the distance across.
40163 CALL Divide Call Divide to work out the fraction of a pixel to move across.
40166 LD A,(24100) Fetch the line flags from LineFlags.
40169 BIT 1,A Jump to DrawLine_StepDown if the line goes right.
40171 JR NZ,DrawLine_StepDown
40173 CALL NegateHL Call NegateHL to negate the fraction.
DrawLine_StepDown 40176 LD A,(24100) Fetch the line flags from LineFlags.
40179 BIT 0,A Test bit 0.
40181 LD C,1 C=1 (the step down).
40183 JR NZ,DrawLine_Down Jump to DrawLine_Down if the line goes down.
40185 LD C,255 C=255 (the step up).
DrawLine_Down 40187 EX DE,HL Switch DE and HL.
40188 POP HL Restore the first point from the stack.
40189 LD A,(24099) Fetch the line distance from LineDistance.
40192 INC A Add one, to get the number of steps.
40193 LD B,A Keep the number of steps in B.
40194 LD A,E A=E.
40195 EX AF,AF' Switch to the shadow AF register.
DrawLine_DownLoop 40196 PUSH BC Stash the number of steps and the step down on the stack.
40197 CALL PlotPixel Call PlotPixel to plot the pixel.
40200 LD A,H A=H.
40201 EX AF,AF' Switch A with the shadow A.
40202 LD H,L H=L.
40203 LD L,A L=A.
40204 ADD HL,DE Add the fraction to the position.
40205 LD A,L A=L.
40206 LD L,H L=H.
40207 EX AF,AF' Switch A with the shadow A.
40208 POP BC Restore the number of steps and the step down from the stack.
40209 ADD A,C Move to the next pixel down.
40210 LD H,A H=A.
40211 DJNZ DrawLine_DownLoop Decrease the number of steps by one and loop back to DrawLine_DownLoop until every pixel of the line has been plotted.
40213 POP HL Restore the second point from the stack.
40214 JP PlotPixel Jump to PlotPixel to plot the last pixel.
Prev: 40033 Up: Map Next: 40217