Aviator on the BBC Micro

# Drawing lines: ClipStartOfLine (Part 5 of 5)

```       Name: ClipStartOfLine (Part 5 of 5)                           [Show more]
Type: Subroutine
Category: Drawing lines
Summary: Move the start point to the clipped position and return it
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

If bit 7 of HH clear, (RR R) and (SS S) = (RR R) + (QQ G)
If bit 7 of HH set,   (RR R) and (SS S) = (RR R) - (QQ G)

If bits 6 and 7 of WW clear,        (SS S)  = 159 - (RR R)
If bit 6 of WW is set, bit 7 clear, (RR RR) = 159 - (SS S)

S = S - 4 to undo the bump in the first part

Confirm (R, S) is on-screen and return it as the new start point

.clip18

\ By the time we get here, (QQ G W) contains the delta
\ that we need to apply to the start point's x- or
\ y-coordinate, multiplied by 256 so the lowest byte is
\ the fractional part

\ If bit 7 of W is set, that means the fractional part
\ is at least 0.5, so we add 1 to the integral part in
\ (QQ G) to round it up when going from the fractional
\ (QQ G W) to the integer (QQ G)

LDA G                  \ We start with the low byte, rotating bit 7 of W into
ROL W                  \ the C flag which we then add with ADC #0
STA G

\ skip the following instruction

INC QQ                 \ And then we increment the high byte if the addition
\ overflowed

.clip19

LDA HH                 \ If bit 7 of HH is set, jump to clip20 to skip the
BMI clip20             \ following

\ Bit 7 of HH is clear, so we add the delta in (QQ G)
\ to both coordinates to move the whole line so the
\ start coordinate is on the edge of the screen

LDA R                  \ Set (RR R) and (S SS) = (RR R) + (QQ G)
CLC                    \
ADC G                  \ starting with the low bytes
STA R
STA S

LDA RR                 \ And then the high bytes
STA RR
STA SS

.clip20

\ Bit 7 of HH is set, so we subtract the delta in (QQ G)
\ from both coordinates to move the whole line so the
\ start coordinate is on the edge of the screen

LDA R                  \ Set (RR R) and (SS S) = (RR R) - (QQ G)
SEC                    \
SBC G                  \  starting with the low bytes
STA R
STA S

LDA RR                 \ And then the high bytes
SBC QQ
STA RR
STA SS

.clip21

BIT WW                 \ If bit 7 of WW is set, jump to clip23 to skip all of
BMI clip23             \ the following and return the final coordinates

BVS clip22             \ If bit 6 of WW is set, jump to clip22 to skip the
\ following

\ Bits 6 and 7 of WW are both clear, so we update the
\ y-coordinate

LDA #159               \ Set (SS S) = 159 - (RR R)
SEC                    \
SBC R                  \ starting with the low bytes
STA S

LDA #0                 \ And then the high bytes
SBC RR
STA SS

.clip22

\ Bit 6 of WW is set, so we update the x-coordinate

LDA #159               \ Set (RR RR) = 159 - (SS S)
SEC                    \
SBC S                  \ starting with the low bytes
STA R

LDA #0                 \ And then the high bytes
SBC SS
STA RR

.clip23

LDA RR                 \ If RR is non-zero, jump to clip24 to abort the line as
BNE clip24             \ the (RR R) x-coordinate is off-screen

LDA R                  \ If R >= 156, jump to clip24 to abort the line as the
CMP #156               \ (RR R) x-coordinate is off the right of the screen
BCS clip24

CMP #4                 \ If R < 4, jump to clip24 to abort the line as the
BCC clip24             \ (RR R) x-coordinate is off the left of the screen

LDA S                  \ Set S = S - 4
SEC                    \
SBC #4                 \ which subtracts the 4 that we added at the start of
STA S                  \ the routine

BCC clip24             \ If the subtraction just underflowed, jump to clip24 to
\ abort the line as the (SS S) y-coordinate is off the
\ bottom of the screen

CMP #152               \ If S > 152, jump to clip24 to abort the line as the
BCS clip24             \ (SS S) y-coordinate is off the top of the screen

\ If we get here then we have successfully clipped the
\ start point to the edge of the screen

LDA #1                 \ Set bit 0 of the line direction in V to indicate that
ORA V                  \ the line has been clipped
STA V

LDA R                  \ Copy the clipped (R, S) pixel coordinate into
STA xTemp1Lo           \ (xTemp1Lo, yTemp1Lo) so we can access it in the
LDA S                  \ DrawCanopyLine routine
STA yTemp1Lo

RTS                    \ Return from the subroutine

.clip24

JMP AbortLine          \ Jump to AbortLine to abort drawing this line and
\ return from the subroutine using a tail call
```