\ We now draw the horizontal line between the left and \ right edges to complete the drawing of this polygon \ line, before looping back to draw the rest of the \ polygon TYA \ Set (Q P) = (S R) + Y CLC \ ADC R \ So (Q P) contains the screen address of the pixel byte STA P \ containing the left edge of the polygon line LDA S ADC #0 STA Q TYA \ Set A = Y \ \ So A contains the pixel byte offset within the screen \ character row of the left edge of the polygon line .dpol17 \ We now draw a horizontal line that starts in the left \ with the character column to the right of (Q P), as \ (Q P) contains the left edge \ \ And we want the line to end in the character column \ before the right edge, which we drew at pixel byte \ offset xPolygonRightEdge \ \ At this point, A contains the pixel byte offset of the \ left edge \ \ So we want to draw a line between pixel byte offset A \ and pixel byte offset xPolygonRightEdge \ \ Each character block contains eight bytes, so in terms \ of character columns, we want to draw a line that's \ this number of character columns wide: \ \ (xPolygonRightEdge - A) / 8 \ \ The following unrolled loop implements this, but in a \ reverse manner that means we have to negate the above \ value to calculate the correct entry point (as well as \ incorporating the fact that the jump points are spaced \ out by four bytes) SEC \ Set A = (A - xPolygonRightEdge) / 2 SBC xPolygonRightEdge \ LSR A \ So that's the above character column calculation, but \ negated and multiplied by 4 STA dpol18+1 \ Modify the following instruction below: \ \ BCC dpol19 -> BCC A \ \ so the BCC instruction jumps to the offset given in A, \ so the following routine draws a line of the length \ given in A, as follows: \ \ * When A = 0 * 4, draw from column 31 to column 1 \ \ * When A = 1 * 4, draw from column 30 to column 1 \ \ * When A = 2 * 4, draw from column 29 to column 1 \ \ ... \ \ * When A = 28 * 4, draw from column 3 to column 1 \ \ * When A = 29 * 4, draw from column 2 to column 1 \ \ * When A = 30 * 4, draw from column 1 to column 1 \ \ This works because there are four bytes in each of the \ LDY/STA instruction pairs below, and when the BCC is \ executed, the operand of the BCC is added to the \ address of the first instruction after the BCC \ \ In other words, this routine draws a line of length \ 31 - (A / 4) character columns LDA polygonFillPixels \ Set A to the contents of polygonFillPixels, which we \ set in part 1 to a pixel byte containing four pixels \ set to the fill colour for the polygon \ \ So the pixel byte we should use for drawing the line \ is in A CLC \ Clear the C flag so the BCC instruction below jumps to \ the correct entry to draw the specified number of \ character columns .dpol18 BCC dpol19 \ This instruction is modified in part 3 to jump to the \ correct entry in the following list for drawing a \ horizontal pixel line of the specified length \ \ The line length is specified in character columns (1 \ to 31) and the drawing starts with the pixel byte to \ the right of the pixel byte whose screen address is \ in (Q P), so (Q P) contains the left cap of the line \ and the line fills the polygon to the right for the \ specified number of character columns \ \ The line is drawn from right to left to enable the \ routine to be joined at the correct point for the \ required line length LDY #8 * 31 \ Draw a line from column 31 to column 1 STA (P),Y LDY #8 * 30 \ Draw a line from column 30 to column 1 STA (P),Y LDY #8 * 29 \ Draw a line from column 29 to column 1 STA (P),Y LDY #8 * 28 \ Draw a line from column 28 to column 1 STA (P),Y LDY #8 * 27 \ Draw a line from column 27 to column 1 STA (P),Y LDY #8 * 26 \ Draw a line from column 26 to column 1 STA (P),Y LDY #8 * 25 \ Draw a line from column 25 to column 1 STA (P),Y LDY #8 * 24 \ Draw a line from column 24 to column 1 STA (P),Y LDY #8 * 23 \ Draw a line from column 23 to column 1 STA (P),Y LDY #8 * 22 \ Draw a line from column 22 to column 1 STA (P),Y LDY #8 * 21 \ Draw a line from column 21 to column 1 STA (P),Y LDY #8 * 20 \ Draw a line from column 20 to column 1 STA (P),Y LDY #8 * 19 \ Draw a line from column 19 to column 1 STA (P),Y LDY #8 * 18 \ Draw a line from column 18 to column 1 STA (P),Y LDY #8 * 17 \ Draw a line from column 17 to column 1 STA (P),Y LDY #8 * 16 \ Draw a line from column 16 to column 1 STA (P),Y LDY #8 * 15 \ Draw a line from column 15 to column 1 STA (P),Y LDY #8 * 14 \ Draw a line from column 14 to column 1 STA (P),Y LDY #8 * 13 \ Draw a line from column 13 to column 1 STA (P),Y LDY #8 * 12 \ Draw a line from column 12 to column 1 STA (P),Y LDY #8 * 11 \ Draw a line from column 11 to column 1 STA (P),Y LDY #8 * 10 \ Draw a line from column 10 to column 1 STA (P),Y LDY #8 * 9 \ Draw a line from column 9 to column 1 STA (P),Y LDY #8 * 8 \ Draw a line from column 8 to column 1 STA (P),Y LDY #8 * 7 \ Draw a line from column 7 to column 1 STA (P),Y LDY #8 * 6 \ Draw a line from column 6 to column 1 STA (P),Y LDY #8 * 5 \ Draw a line from column 5 to column 1 STA (P),Y LDY #8 * 4 \ Draw a line from column 4 to column 1 STA (P),Y LDY #8 * 3 \ Draw a line from column 3 to column 1 STA (P),Y LDY #8 * 2 \ Draw a line from column 2 to column 1 STA (P),Y LDY #8 * 1 \ Draw a line from column 1 to column 1 STA (P),Y .dpol19 JMP dpol3 \ Loop back to part 2 to move on to the next pixel line \ in the polygonName: DrawPolygonLines (Part 4 of 4) [Show more] Type: Subroutine Category: Drawing polygons Summary: Draw a horizontal pixel line of a specific length in character columnsContext: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
[X]
Label dpol18 is local to this routine
[X]
Label dpol19 is local to this routine
[X]
Label dpol3 in subroutine DrawPolygonLines (Part 2 of 4)
[X]
Variable polygonFillPixels in workspace Zero page
A pixel byte containing four pixels set to the fill colour for the polygon we are drawing
[X]
Variable xPolygonRightEdge in workspace Zero page
The screen x-coordinate of the right edge of the polygon being drawn