Skip to navigation

Drawing polygons: TracePolygonEdge (Part 5 of 8)

Name: TracePolygonEdge (Part 5 of 8) [Show more] Type: Subroutine Category: Drawing polygons Summary: Trace a shallow edge that starts off-screen, without storing the coordinates, until we reach the screen and return to part 3
Context: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
.tred31 INC tred24+1 \ Increment the low byte of the address in the STX \ instruction at tred24 to balance out the additional \ DEC tred24+1 that we do below when we rejoin the \ loop in part 3 LDX tred22 \ Modify the instruction at tred32 so it matches the STX tred32 \ modified instruction at tred22, so that's: \ \ * INX (if we are stepping right along the x-axis) \ \ * DEX (if we are stepping left along the x-axis) \ \ So in either case this steps us along the x-axis by \ one pixel LDX xEdgeStartLo \ Set X to the x-coordinate of the starting point, so we \ can start tracing from the start of the edge \ The loop below has the same structure as the loop in \ part 3 between tred22 and tred25, and which we enter \ via tred24, but in this version of the loop we do not \ store the coordinates of the edge, as the portion we \ are currently tracing is off-screen JMP tred33 \ Jump into the middle of the loop at tred33 to trace \ the edge but without storing the results .tred32 INX \ This instruction is modified above to: \ \ * INX (if we are stepping right along the x-axis) \ \ * DEX (if we are stepping left along the x-axis) \ \ So in either case this steps us along the x-axis by \ one pixel ADC yEdgeDeltaLo \ Add the y-axis delta to the slope error in A, so we \ keep track of the slope error as we move along the \ x-axis BCC tred33 \ If the addition didn't overflow then we have not \ moved into a new y-coordinate with this step along the \ x-axis, so jump to tred33 so we do not move along the \ y-axis SBC xEdgeDelta \ Set A = A - xEdgeDelta \ \ 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 DEC tred24+1 \ Decrement the low byte of the address in the STX \ instruction at tred24 so that it points to the table \ entry for the next y-coordinate down BEQ tred34 \ If we just decremented the low byte of the address to \ zero then we have finished stepping along the y-axis \ for this value of the high byte, so the next address \ we decrement it to will be on-screen \ \ We therefore jump to tred34 to set the address for the \ top of the screen and rejoin the edge-tracing loop in \ part 3 to keep tracing the edge .tred33 DEY \ Decrement the pixel counter in Y BNE tred32 \ Loop back to keep tracing the polygon edge until we \ have worked our way through all the pixels along the \ x-axis JMP tred16 \ If we get here then we have finished tracing the edge \ and we didn't reach an on-screen portion, so jump to \ tred16 to return from the subroutine without changing \ drawPolygon (as we didn't draw anything in this \ routine) .tred34 DEC tred24+1 \ Decrement the low byte of the address in the STX \ instruction at tred24 to &FF (as we only get here when \ we have just decremented it to zero), so it now points \ to the address for the top line of the screen at a \ y-coordinate of 255 JMP tred25 \ Jump to tred25 to rejoin the normal edge-tracing loop \ in part 3 so we trace the rest of the edge