Skip to navigation

Elite on the BBC Micro and NES

Drawing suns: PLFL

[NES version, Bank 1]

Name: PLFL [Show more] Type: Subroutine Category: Drawing suns Summary: Calculate the sun's width on a given pixel row Deep dive: Drawing the sun
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * SUN (Part 2 of 2) calls PLFL * CIRCLE calls via RTS2

This part calculate the sun's width on a given pixel row.
Arguments: V As we draw lines for the new sun, V contains the vertical distance between the line we're drawing and the centre of the new sun. As we draw lines and move up the screen, we either decrement (bottom half) or increment (top half) this value. See the deep dive on "Drawing the sun" to see a diagram that shows V in action V+1 This determines which half of the new sun we are drawing as we work our way up the screen, line by line: * 0 means we are drawing the bottom half, so the lines get wider as we work our way up towards the centre, at which point we will move into the top half, and V+1 will switch to $FF * $FF means we are drawing the top half, so the lines get smaller as we work our way up, away from the centre TGT The maximum y-coordinate of the new sun on-screen (i.e. the screen y-coordinate of the bottom row of the new sun) CNT The fringe size of the new sun K2(1 0) The new sun's radius squared, i.e. K^2 Y The y-coordinate of the bottom row of the new sun
Returns: A The half-width of the sun on the line specified in V
Other entry points: RTS2 Contains an RTS
.PLFL SETUP_PPU_FOR_ICON_BAR ; If the PPU has started drawing the icon bar, configure ; the PPU to use nametable 0 and pattern table 0 STY Y1 ; Store Y in Y1, so we can restore it after the call to ; LL5 LDA V ; Set (T P) = V * V JSR SQUA2 ; = V^2 STA T LDA K2 ; Set (R Q) = K^2 - V^2 SEC ; SBC P ; First calculating the low bytes STA Q LDA K2+1 ; And then doing the high bytes SBC T STA R JSR LL5 ; Set Q = SQRT(R Q) ; = SQRT(K^2 - V^2) ; ; So Q contains the half-width of the new sun's line at ; height V from the sun's centre - in other words, it ; contains the half-width of the sun's line on the ; current pixel row Y LDY Y1 ; Restore Y from Y1 SETUP_PPU_FOR_ICON_BAR ; If the PPU has started drawing the icon bar, configure ; the PPU to use nametable 0 and pattern table 0 JSR DORND ; Set A and X to random numbers AND CNT ; Reduce A to a random number in the range 0 to CNT, ; where CNT is the fringe size of the new sun LDY Y1 ; Restore Y from Y1 CLC ; Set A = A + Q ADC Q ; ; So A now contains the half-width of the sun on row ; V, plus a random variation based on the fringe size BCC RTS2 ; If the above addition did not overflow then LDA #255 ; The above overflowed, so set the value of A to 255 ; So A contains the half-width of the new sun on pixel ; line Y, changed by a random amount within the size of ; the sun's fringe .RTS2 RTS ; Return from the subroutine