Skip to navigation

Drawing polygons: DrawPolygonLines (Part 3 of 4)

Name: DrawPolygonLines (Part 3 of 4) [Show more] Type: Subroutine Category: Drawing polygons Summary: Draw the left and right edges of the polygon line and fall into part 4 to draw the line in between
Context: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
.dpol13 \ This is the entry point for the loop that spans parts \ 2, 3 and 4 \ \ We jump here from part 1 with Y set to the number of \ the polygon pixel line at the top of the polygon \ \ We also get here on later iterations with Y set to the \ polygon pixel to draw next \ \ In either case we draw polygon pixel line Y and steps \ down the polygon one pixel line at a time until we \ reach the bottom LDA xPolygonRight,Y \ Set A to the pixel x-coordinate of the right edge for \ the line we are drawing CMP xPolygonLeft,Y \ If the pixel x-coordinate of the right edge is less BCC dpol2 \ than the pixel x-coordinate of the left edge for the \ line we are drawing, then the polygon must be facing \ away from us, so jump to dpol2 to return from the \ subroutine without drawing the polygon TAX \ Set X to the pixel x-coordinate of the right edge SBC xBufferLeft \ Set A = A - xBufferLeft \ \ So A is the distance between the right edge of the \ polygon and the left edge of the buffer \ \ This subtraction works because we just passed through \ a BCC, so we know the C flag is set BCC dpol5 \ If the subtraction underflowed then the right edge of \ the polygon is before the left edge of the buffer, so \ jump to dpol5 in part 2 to process this CMP xBufferWidth \ If A >= xBufferWidth then the right edge of the BCS dpol7 \ polygon is beyond the right edge of the buffer, so \ jump to dpol7 in part 2 to process this \ If we get here then the left and right edges of the \ polygon are both within the screen buffer ASL A \ Set Y to the offset within the character row of the AND #%11111000 \ pixel byte containing the right edge of the polygon TAY TXA \ Set X to the pixel number within the character block AND #%00000011 \ of the left edge of the polygon (as each character TAX \ block contains four pixels) STY xPolygonRightEdge \ Store the pixel byte offset within the character row \ of the right edge of the polygon in xPolygonRightEdge LDA (R),Y \ Set A to the current screen contents of the pixel byte \ where we want to draw the right edge of the polygon STA currentPixelByte \ Store the current screen contents for the right edge \ of the polygon in currentPixelByte, in case we need \ to draw the left and right edges in the same pixel \ byte AND pixelsToRight,X \ Clear the pixels to the left of the right edge by \ AND'ing the current screen contents with a pixel byte \ with all the pixels to the right of position X set \ \ So A contains the existing screen contents for the \ pixels outside the polygon line, and with the polygon \ line pixels cleared, so now we can OR the polygon line \ pixels into the space we just cleared .dpol14 ORA edgePixelsRight+&3C,X \ This instruction is modified in part 1 to point to \ the correct entry for the specified polygon \ colour: \ \ ORA edgePixelsRight + polygonColours,X \ \ So this inserts the right edge of the polygon into \ the left part of the pixel byte in the correct \ colours for the currently configured fill and edge \ colours, with the edge pixels starting at the left \ end of the pixel byte and ending at pixel number X \ within the byte \ \ The original value of edgePixelsRight+&3C is just \ workspace noise and has no meaning, it just sets \ the high byte to HI(edgePixelsRight) STA (R),Y \ Poke the pixel byte containing the right edge into \ screen memory, so we have now drawn the right edge of \ the polygon line .dpol15 LDY yPolygonLine \ Set A to the pixel x-coordinate of the left edge for LDA xPolygonLeft,Y \ the line we are drawing TAX \ Set X to the pixel x-coordinate of the left edge CMP xBufferRight \ If A >= xBufferRight then the left edge of the BCS dpol6 \ polygon is beyond the right edge of the buffer, so \ jump to dpol6 in part 2 to process this SEC \ Set A = A - xBufferLeft SBC xBufferLeft \ \ So A is the distance between the left edge of the \ polygon and the left edge of the buffer BCC dpol8 \ If the subtraction underflowed then the left edge of \ the polygon is before the left edge of the buffer, so \ jump to dpol8 in part 2 to process this ASL A \ Set Y to the offset within the character row of the AND #%11111000 \ pixel byte containing the left edge of the polygon TAY CPY xPolygonRightEdge \ If Y >= xPolygonRightEdge then the pixel byte offset BCS dpol9 \ for the left edge is the same as the pixel byte offset \ for the right edge, so jump to dpol9 to draw both \ edges within the same pixel byte TXA \ Set X to the pixel number within the character block AND #%00000011 \ of the left edge of the polygon (as each character TAX \ block contains four pixels) LDA (R),Y \ Set A to the current screen contents of the pixel byte \ where we want to draw the left edge of the polygon AND pixelsToLeft,X \ Clear the pixels to the right of the left edge by \ AND'ing the current screen contents with a pixel byte \ with all the pixels to the left of position X set \ \ So A contains the existing screen contents for the \ pixels outside the polygon line, and with the polygon \ line pixels cleared, so now we can OR the polygon line \ pixels into the space we just cleared .dpol16 ORA edgePixelsLeft+&3C,X \ This instruction is modified in part 1 to point to \ the correct entry for the specified polygon \ colour: \ \ ORA edgePixelsLeft + polygonColours,X \ \ So this inserts the left edge of the polygon into \ the right part of the pixel byte in the correct \ colours for the currently configured fill and edge \ colours, with the edge pixels starting at pixel \ number X within the byte and ending at the right \ end of the pixel byte \ \ The original value of edgePixelsLeft+&3C is just \ workspace noise and has no meaning, it just sets \ the high byte to HI(edgePixelsLeft) STA (R),Y \ Poke the pixel byte containing the left edge into \ screen memory, so we have now drawn the left edge of \ the polygon line \ Fall through into part 4 to draw a line between the \ left and right edges to finish off this line of the \ polygon