\ If we get here then xEdgeDelta < yEdgeDelta, so the \ y-axis delta is the biggest and the polygon edge has a \ steep gradient \ \ We therefore step along the y-axis one pixel at a time \ and cumulatively add the gradient to calculate the \ x-coordinate at each step \ \ We get here with the following: \ \ * (A Y) = (xPolygonAddrHi yEdgeStartLo) \ \ * X = the opcode for DEX or INX \ \ * T = the opcode for DEC zp or INC zp STA tred41+2 \ Modify the instruction at tred41 to use the address in \ (A Y), starting with the high byte STX tred40 \ Modify the instruction at tred40 to use the opcode \ specified in X, so we have: \ \ * DEX when xEdgeStart - xEdgeEnd is positive \ \ * INX when xEdgeStart - xEdgeEnd is negative \ \ We store the x-coordinate in X, so this ensures that: \ \ * We step left when required with DEX, for when the \ start point is to the right of the end point \ \ * We step right when required with INX, for when the \ start point is to the left of the end point \ \ We will run this instruction when we need to step \ along the x-axis, according to the cumulative value of \ the slope error LDA yEdgeStartHi \ If yEdgeStartHi = 0 then the start point is below the BEQ tred38 \ top edge of the screen, so skip the following \ instruction INY \ The start point is above the top edge of the screen, \ so increment the low byte of (A Y) .tred38 STY tred41+1 \ Modify the instruction at tred41 to use the address in \ (A Y), so it becomes: \ \ STX into address (xPolygonAddrHi yEdgeStartLo) \ \ with the low byte incremented by 1 when yEdgeStartHi \ is non-zero \ \ xPolygonAddrHi is set to the high byte of either \ xPolygonLeft or xPolygonRight, and both of these \ tables start on a page boundary, so this sets the \ address to offset yEdgeStartLo within the table \ specified by xPolygonAddrHi \ \ We will decrement this address for each step along the \ edge, so we step along the y-axis as we trace the edge LDA T \ Modify the instruction at tred45 to use the opcode STA tred45 \ specified in T, so we have: \ \ * DEC xEdgeStartHi when xEdgeStart - xEdgeEnd is \ positive \ \ * INC xEdgeStartHi when xEdgeStart - xEdgeEnd is \ negative \ \ So in either case this steps us along the x-axis and \ updates the high byte in xEdgeStartHi accordingly \ \ We will run this instruction when we need to step \ along the x-axis and update the high byte of the \ address in the process LDY yEdgeDeltaLo \ Set A = ~(yEdgeDeltaLo / 2) TYA \ LSR A \ We use A to keep track of the slope error, though I am EOR #&FF \ unsure of why we start with this value CLC \ Clear the C flag so the first addition we do in the \ following loop will work correctly INY \ Set U = yEdgeDeltaLo + 1 STY U \ \ We can use this as a pixel counter when stepping along \ the polygon edge's y-axis delta one pixel at a time, \ as there are yEdgeDeltaLo + 1 pixels along the edge \ (the additional one ensures we include the pixels at \ both ends) LDX xEdgeStartLo \ Set X to the x-coordinate of the starting point, so we \ can start tracing from the start of the edge JSR ModifyStoringCode \ Modify the instruction at tred41 and set drawPolygon \ and Y according to the values of xEdgeStartHi and \ yEdgeStartHi \ \ The instruction at tred41 is modified as follows: \ \ * BIT when yEdgeStartHi <> 0 \ \ * STY when yEdgeStartHi = 0 and xEdgeStartHi <> 0 \ \ * STX when yEdgeStartHi = 0 and xEdgeStartHi = 0 \ \ The drawPolygon flag is set as follows: \ \ * drawPolygon = 0 when yEdgeStartHi = 0 JMP tred41 \ Jump into the middle of the loop at tred41 .tred39 ADC xEdgeDelta \ Add the x-axis delta to the slope error in A, so we \ keep track of the slope error as we move along the \ y-axis BCC tred41 \ If the addition didn't overflow then we have not moved \ into a new x-coordinate with this step along the \ y-axis, so jump to tred41 so we do not move along the \ x-axis \ If we get here then the addition overflowed and the \ cumulative slope error along the x-axis has added up \ to a whole pixel, so we now need to step along the \ x-axis by a pixel SBC yEdgeDeltaLo \ Set A = A - yEdgeDeltaLo \ \ This updates the slope error in A \ \ This subtraction works as we just passed through a \ BCC, so we know the C flag is set .tred40 INX \ This instruction is modified above to: \ \ * INX (if we are stepping right along the edge) \ \ * DEX (if we are stepping left along the edge) \ \ So in either case this steps us along the x-axis by \ one pixel CPX yEdgeDeltaHi \ Set the status flags on the comparison of X and \ yEdgeDeltaHi CLC \ Clear the C flag so the comparison won't affect the \ arithmetic in this loop BEQ tred45 \ If X = yEdgeDeltaHi, jump to tred45 to step along the \ x-axis by updating the high byte in xEdgeStartHi .tred41 \ This is the entry point for the tracing loop and the \ point where we record the current pixel in the edge \ that we are tracing STX xPolygonLeft \ This instruction is modified above to the following \ address: \ \ Store into address (xPolygonAddrHi yEdgeStartLo) \ \ with the low byte incremented by 1 when yEdgeStartHi \ is non-zero \ \ So this stores the current x-coordinate as we step \ along the edge, storing the coordinate in either the \ xPolygonRight or xPolygonLeft table, and storing the \ coordinate in the offset given by the y-coordinate \ of the current position along the edge we are tracing \ \ The ModifyStoringCode routine modifies the instruction \ further, as follows: \ \ * BIT when yEdgeStartHi <> 0 \ \ * STY when yEdgeStartHi = 0 and xEdgeStartHi <> 0 \ \ * STX when yEdgeStartHi = 0 and xEdgeStartHi = 0 DEC tred41+1 \ Decrement the low byte of the address in the \ instruction above so that it points to the table \ entry for the next y-coordinate down BEQ tred43 \ If we just decremented the low byte of the address to \ zero then we have finished stepping along the y-axis, \ so jump to tred43 to finish off .tred42 DEC U \ Decrement the pixel counter in U BNE tred39 \ Loop back to keep tracing the polygon edge until we \ have worked our way through all the pixels along the \ y-axis JMP tred16 \ Jump to tred16 to return from the subroutine and stop \ the trace .tred43 DEC yEdgeStartHi \ Decrement the high byte of the current y-coordinate \ to keep moving along the y-axis BPL tred44 \ If yEdgeStartHi is positive then we haven't gone off \ the left edge of the screen, so jump to tred44 to \ keep going JMP tred16 \ Jump to tred16 to return from the subroutine and stop \ the trace .tred44 BNE tred42 \ If yEdgeStartHi is non-zero, jump to tred42 to keep \ working along the edge with the same y-coordinate DEC tred41+1 \ Decrement the low byte of the address in the \ instruction above so that it points to the table \ entry for the next y-coordinate down JSR ModifyStoringCode \ Modify the instruction at tred41 and set drawPolygon \ and Y according to the values of xEdgeStartHi and \ yEdgeStartHi \ \ The instruction at tred41 is modified as follows: \ \ * BIT when yEdgeStartHi <> 0 \ \ * STY when yEdgeStartHi = 0 and xEdgeStartHi <> 0 \ \ * STX when yEdgeStartHi = 0 and xEdgeStartHi = 0 \ \ The drawPolygon flag is set as follows: \ \ * drawPolygon = 0 when yEdgeStartHi = 0 JMP tred42 \ Jump to tred42 to keep working along the edge .tred45 INC xEdgeStartHi \ This instruction is modified above to: \ \ * DEC xEdgeStartHi when xEdgeStart - xEdgeEnd is \ positive \ \ * INC xEdgeStartHi when xEdgeStart - xEdgeEnd is \ negative \ \ So in either case this steps us along the x-axis and \ updates the high byte in xEdgeStartHi accordingly JSR ModifyStoringCode \ Modify the instruction at tred41 and set drawPolygon \ and Y according to the values of xEdgeStartHi and \ yEdgeStartHi \ \ The instruction at tred41 is modified as follows: \ \ * BIT when yEdgeStartHi <> 0 \ \ * STY when yEdgeStartHi = 0 and xEdgeStartHi <> 0 \ \ * STX when yEdgeStartHi = 0 and xEdgeStartHi = 0 \ \ The drawPolygon flag is set as follows: \ \ * drawPolygon = 0 when yEdgeStartHi = 0 JMP tred41 \ Jump to tred41 to restart the loopName: TracePolygonEdge (Part 7 of 8) [Show more] Type: Subroutine Category: Drawing polygons Summary: Trace a polygon edge with a steep gradient by stepping along the y-axis (for two-byte x-coordinates)Context: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
[X]
Subroutine ModifyStoringCode (category: Drawing polygons)
Modify the code in TracePolygonEdge that stores the coordinates of the polygon edge that is being traced
[X]
Label tred16 in subroutine TracePolygonEdge (Part 2 of 8)
[X]
Label tred38 is local to this routine
[X]
Label tred39 is local to this routine
[X]
Label tred40 is local to this routine
[X]
Label tred41 is local to this routine
[X]
Label tred42 is local to this routine
[X]
Label tred43 is local to this routine
[X]
Label tred44 is local to this routine
[X]
Label tred45 is local to this routine
[X]
Variable xEdgeDelta in workspace Zero page
The scaled and signed x-axis delta between two polygon points when tracing the polygon edges (single byte)
[X]
Variable xEdgeStartHi in workspace Zero page
The x-coordinate of the start point in the polygon edge being processed (high byte)
[X]
Variable xEdgeStartLo in workspace Zero page
The x-coordinate of the start point in the polygon edge being processed (low byte)
[X]
Variable xPolygonLeft (category: Drawing polygons)
The pixel x-coordinate of the left edge of each pixel line in the polygon being drawn
[X]
Variable yEdgeDeltaHi in workspace Zero page
The difference in pitch angle between two polygon points, i.e. the delta along the y-axis (high byte)
[X]
Variable yEdgeDeltaLo in workspace Zero page
The difference in pitch angle between two polygon points, i.e. the delta along the y-axis (low byte)
[X]
Variable yEdgeStartHi in workspace Zero page
The y-coordinate of the start point in the polygon edge being processed (high byte)