Revs on the BBC Micro

# Screen buffer: SetBackground

```       Name: SetBackground                                           [Show more]
Type: Subroutine
Category: Screen buffer
Summary: Set the background colour for any track lines that have not yet
Deep dive: Drawing the track view
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* MainDrivingLoop (Part 2 of 5) calls SetBackground

.SetBackground

\ We start by setting the colour of the track line that
\ contains the horizon

LDX horizonListIndex   \ Set X = horizonListIndex
\
\ So X is the index in the track verge buffer for the
\ horizon line's right verge

LDY horizonLine        \ Set Y to the track line number of the horizon

LDA xVergeLeftHi,X     \ Set A = X-th entry in xVergeLeftHi + 20
CLC

BPL bgnd1              \ If A is positive, then the left edge of the verge is
\ the horizon to the colour of grass

LDA xVergeRightHi,X    \ Set A = X-th entry in xVergeRightHi + 20
CLC

BMI bgnd1              \ If A is negative, then the right edge of the verge is
\ the horizon to the colour of grass

\ If we get to this point, then:
\
\   * xVergeRightHi + 20 is positive
\
\   * xVergeLeftHi + 20 is negative
\
\ Adding 20 degrees to the yaw angles will move them to
\ the right by half the screen width, so this is the
\ same as moving the angles from the left edge of the
\ screen to the middle
\
\ We then check whether moving the angles to the centre
\ pushes the rightmost verge edge in A past the centre
\ (i.e. positive), while still leaving the leftmost edge
\ in the left half (i.e. negative)
\
\ If so, then this means the verge at the horizon is
\ straddling the left edge of the screen, so we need to
\ set the background colour for the horizon track line
\ to black

LDA #%00100000         \ Set A = %00100000 (colour 0, black) for the horizon
\ track line, where:
\
\   * %00 in bits 0-1 is logical colour 0
\
\   * %001xx0xx denotes that this value was stored in
\     the backgroundColour table by the SetBackground
\     routine

BNE bgnd2              \ Jump to bgnd2 (this BNE is effectively a JMP as A is
\ never zero)

.bgnd1

LDA #%00100011         \ Set A = %00100011 (colour 3, green) for the horizon
\ track line, where:
\
\   * %11 in bits 0-1 is logical colour 3
\
\   * %001xx0xx denotes that this value was stored in
\     the backgroundColour table by the SetBackground
\     routine

.bgnd2

STA backgroundColour,Y \ Set the colour of the horizon line to A

\ We now work our way through the whole backgroundColour
\ table, filling in any entries that are zero (and which
\ have therefore not been set yet) with blue

LDA #%00100001         \ Set A = %00100001 (colour 1, blue) to use as the line
\ colour for lines above the horizon, i.e. the sky,
\ where:
\
\   * %01 in bits 0-1 is logical colour 1
\
\   * %001xx0xx denotes that this value was stored in
\     the backgroundColour table by the SetBackground
\     routine

LDY #79                \ We now loop through all the track lines, starting from
\ line 79 at the top of the track view down to 0 at the
\ bottom, so set a counter in Y
\
\ If any values are zero, then we set them to blue

.bgnd3

LDX backgroundColour,Y \ Set X to the background colour for track line Y

BEQ bgnd4              \ If X is zero, then this track line doesn't currently
\ have a background colour set, so jump to bgnd4 to set
\ the track line to blue

TXA                    \ If we get here then X is non-zero, so there is already
\ a backgroundColour value for this track line, so copy
\ this value to A so the following STA instruction
\ doesn't change anything

.bgnd4

STA backgroundColour,Y \ Set the background colour for track line Y to A

DEY                    \ Decrement the loop counter to move down to the next
\ track line

BPL bgnd3              \ Loop back until we have set the line colour for all
\ 80 track lines

RTS                    \ Return from the subroutine
```