.DrawPolygonLines LDA #1 \ Set polygonGoesRight = 1 as the default value, which STA polygonGoesRight \ we can change to a different value if the polygon \ extends to the right of the screen buffer STA polygonGoesLeft \ Set polygonGoesLeft = 1 as the default value, which \ we can change to a different value if the polygon \ extends to the left of the screen buffer LDA yPolygonTop \ Set X = (yPolygonTop + yPolygonBottom) / 2 CLC \ ADC yPolygonBottom \ So X is the pixel line number of the middle pixel line ROR A \ in the polygon TAX LDA xPolygonRight,X \ If the pixel x-coordinate of the right edge is less CMP xPolygonLeft,X \ than the pixel x-coordinate of the left edge for the BCC dpol2 \ middle line, then the polygon must be facing away from \ us, so jump to dpol2 to return from the subroutine \ without drawing the polygon LDA #240 \ Set T = 240 - yPolygonTop - 1 CLC \ SBC yPolygonTop \ So T contains the screen y-coordinate of polygon pixel STA T \ line number yPolygonTop (as the screen y-coordinates \ increase as you go down the screen, unlike the polygon \ pixel line numbers which follow the same direction as \ the normal 3D world y-axis, which increases as you go \ up the screen) LSR A \ Set A = T / 8 LSR A \ LSR A \ So A is the number of the character row that contains \ the screen y-coordinate in T CLC \ If we are configured to draw into the screen buffer ADC screenOrBuffer \ then screenOrBuffer will be 25, so this makes us fetch \ row addresses from bufferRowAddrLo and bufferRowAddrHi \ (as bufferRowAddrLo - screenRowAddrLo is 25) \ \ Otherwise we are configured to draw directly onto the \ screen and screenOrBuffer is zero, in which case this \ addition doesn't change anything TAX \ Set X to the offset within the screenRowAddr(Hi Lo) \ table of the character row containing y-coordinate \ yPolygonTop, for either the screen or screen buffer \ (as configured in screenOrBuffer) LDA T \ Set A = T mod 8 AND #%00000111 \ \ So T contains the offset within the character block at \ the start of the character row that contains screen \ y-coordinate T CLC \ Set (S R) = A + screenRowAddr(Hi Lo) for row X ADC screenRowAddrLo,X \ STA R \ So (S R) contains the screen address of the start of LDA screenRowAddrHi,X \ the pixel row containing the pixel line number in STA S \ yPolygonTop LDY screenBufferType \ If this is the right row screen buffer then Y = 1, so LDA R \ add the following to (S R): CLC \ ADC buffersOffsetLo,Y \ (S R) = (S R) + 96 STA R \ LDA S \ This addition is performed for all buffer types, but ADC buffersOffsetHi,Y \ the addition is only non-zero for the right row buffer STA S LDA polygonColours \ Set A to the polygon colour byte for the polygon we \ we are drawing BIT blendPolygonEdges \ If bit 7 of blendPolygonEdges is clear then polygon BPL dpol1 \ edges should be drawn in the edge colour, so jump to \ dpol1 to skip the following \ If we get here then bit 7 of blendPolygonEdges is set, \ so we draw the polygon edges in the same colour as the \ polygon body (i.e. the fill colour), so the edges \ blend into the body \ \ We typically do this for distant objects, as distinct \ edge colours can make those object look messy AND #%11001111 \ Copy the fill colour from bits 2-3 of the polygon STA T \ colour byte into the edge colour in bits 4-5, so the LDA polygonColours \ polygon edge colour merges into the polygon body ASL A ASL A AND #%00110000 ORA T .dpol1 STA dpol16+1 \ Modify the following instruction in part 3: \ \ ORA edgePixelsLeft+&3C,X -> \ ORA edgePixelsLeft + polygonColours,X \ \ so the ORA instruction uses the correct byte for the \ the specified polygon colours from the edgePixelsLeft \ table STA dpol10+1 \ Modify the following instruction in part 2: \ \ ORA edgePixelsLeft+&3C,X -> \ ORA edgePixelsLeft + polygonColours,X \ \ so the ORA instruction uses the correct byte for the \ the specified polygon colours from the edgePixelsLeft \ table ORA #&40 \ Modify the following instruction in part 2: STA dpol14+1 \ \ ORA edgePixelsRight+&3C,X -> \ ORA edgePixelsRight + polygonColours,X \ \ so the ORA instruction uses the correct byte for the \ the specified polygon colours from the edgePixelsRight \ table LSR A \ Set Y to the fill colour from bits 2-3 of the polygon LSR A \ colour byte AND #%00000011 TAY LDA colourPixels,Y \ Set polygonFillPixels to a pixel byte containing four STA polygonFillPixels \ pixels set to the fill colour for the polygon LDY yPolygonTop \ Set Y to the pixel line number of the top of the \ polygon (the highest y-coordinate in the polygon in \ terms of 3D world coordinates, so higher values are \ higher up the screen) STY yPolygonLine \ Store the top pixel line number in yPolygonLine CPY yPolygonBottom \ If yPolygonTop >= yPolygonBottom then there is at BCS dpol13 \ least one visible line to draw in this polygon, so \ jump to dpol13 to draw the polygon, starting from \ row Y .dpol2 RTS \ Return from the subroutineName: DrawPolygonLines (Part 1 of 4) [Show more] Type: Subroutine Category: Drawing polygons Summary: Draw an analysed polygon into the screen bufferContext: See this subroutine in context in the source code References: This subroutine is called as follows: * DrawPolygon calls DrawPolygonLines
Arguments: yPolygonTop The pixel line number of the top of the polygon (the highest y-coordinate in the polygon) yPolygonBottom The pixel line number of the bottom of the polygon (the lowest y-coordinate in the polygon)
Returns: polygonGoesRight A flag to record whether the polygon fitted into the screen buffer: * Not 1 = at least one line in the polygon we just drew extends past the right edge of the buffer * 1 = the default value polygonGoesLeft A flag to record whether the polygon fitted into the screen buffer: * Not 1 = at least one line in the polygon we just drew extends past the left edge of the buffer * 1 = the default value
[X]
Variable blendPolygonEdges in workspace Main variable workspace
A flag to determine whether polygon edges are drawn in the same colour as the polygon fill, thus blending the edges into the polygon body
[X]
Variable buffersOffsetHi (category: Screen buffer)
An offset to add to the address of the right column screen buffer (low byte)
[X]
Variable buffersOffsetLo (category: Screen buffer)
An offset to add to the address of the right column screen buffer (low byte)
[X]
Variable colourPixels (category: Graphics)
A table that maps logical colours 0 to 3 to a four-pixel byte in that colour
[X]
Label dpol1 is local to this routine
[X]
Label dpol10 in subroutine DrawPolygonLines (Part 2 of 4)
[X]
Label dpol13 in subroutine DrawPolygonLines (Part 3 of 4)
[X]
Label dpol14 in subroutine DrawPolygonLines (Part 3 of 4)
[X]
Label dpol16 in subroutine DrawPolygonLines (Part 3 of 4)
[X]
Label dpol2 is local to this routine
[X]
Variable polygonColours in workspace Zero page
The colours of the polygon that's being drawn in the DrawPolygon routine
[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 polygonGoesLeft in workspace Zero page
A flag to record whether the polygon we are drawing extends to the left of the screen buffer we are drawing in
[X]
Variable polygonGoesRight in workspace Zero page
A flag to record whether the polygon we are drawing extends to the right of the screen buffer we are drawing in
[X]
Variable screenBufferType in workspace Zero page
The type of screen buffer that is currently being used
[X]
Variable screenOrBuffer in workspace Zero page
Controls whether the graphics routines draw directly onto the screen, or into the screen buffer
[X]
Variable screenRowAddrHi (category: Graphics)
Address lookup table for character rows in screen memory (high byte)
[X]
Variable screenRowAddrLo (category: Screen buffer)
Address lookup table for character rows in screen memory (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 xPolygonRight (category: Drawing polygons)
The pixel x-coordinate of the right edge of each pixel line in the polygon being drawn
[X]
Variable yPolygonBottom in workspace Zero page
The y-coordinate of the bottom of the polygon being drawn (where higher y-coordinates are up the screen)
[X]
Variable yPolygonLine in workspace Zero page
The polygon y-coordinate of the pixel line being drawn in the DrawPolygonLines routine
[X]
Variable yPolygonTop in workspace Zero page
The y-coordinate of the top of the polygon being drawn (where higher y-coordinates are up the screen)