Aviator on the BBC Micro

# Drawing lines: ClipStartOfLine (Part 1 of 5)

```       Name: ClipStartOfLine (Part 1 of 5)                           [Show more]
Type: Subroutine
Category: Drawing lines
Summary: Check whether the line is completely off-screen
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ClipBestEndOfLine calls ClipStartOfLine
* DrawClippedLine (Part 4 of 6) calls ClipStartOfLine

The commentary in this routine is a work in progress.

Arguments:

(RR R)               The x-coordinate of the line's start point

(SS S)               The y-coordinate of the line's start point

(I T)                The line's |x-delta|

(J U)                The line's |y-delta|

TT                   The clipping requirements for the start point

Bit 4     Bit 5
Off top of screen           0         1
On-screen                   0         0
Off bottom of screen        1         0

Bit 6     Bit 7
Off right of screen         0         1
On-screen                   0         0
Off left of screen          1         0

V                    Direction of the line:

* Bit 7 is the direction of the x-delta

* Bit 6 is the direction of the y-delta

Direction is like a clock, so positive (clear) is up and
right

Returns:

R                    The x-coordinate of the line's start point, clipped to
fit on-screen

S                    The y-coordinate of the line's start point, clipped to
fit on-screen

V                    Bit 0 is set to indicate the line has been clipped

xTemp1Lo             If the line has been clipped, contains a copy of R

yTemp1Lo             If the line has been clipped, contains a copy of S

.ClipStartOfLine

LDA S                  \ Set (SS S) = (SS S) + 4
CLC                    \
ADC #4                 \ starting with the low bytes
STA S

\ skip the following instruction

INC SS                 \ And then we increment the high byte if the addition
\ overflowed
\
\ So we have now added an extra 4 to the start
\ y-coordinate:
\
\   (SS S) = (SS S) + 4
\
\ We add this to make the x- and y-coordinate consistent
\ in terms of range, as the canopy's x-coordinates start
\ at 4, but the y-coordinates start at 0. We remove this
\ additional 4 at the end, after clipping the line

.clip1

\ We now do some simple checks to weed out lines that
\ are entirely off to one side of the screen

LDA TT                 \ Set A = the clipping requirements for the start point

BIT V                  \ If bit 7 of V is clear, jump to clip2 as the x-delta
BPL clip2              \ of the line is positive, or to the right

\ Bit 7 of V is set, so the x-delta of the line is
\ negative, or to the left

AND #%01000000         \ If bit 6 of TT is set, then the start point is off the
BNE AbortLine          \ left of the screen and the line direction is also to
\ line as it must be entirely off-screen

BEQ clip3              \ Jump to clip3 to move on to the next check (this BEQ
\ is effectively a JMP, as A is always zero)

.clip2

\ If we get here then the x-delta of the line is
\ positive, or to the right

AND #%10000000         \ If bit 7 of TT is set, then the start point is off the
BNE AbortLine          \ right of the screen and the line direction is also to
\ line as it must be entirely off-screen

BEQ clip3              \ Jump to clip3 to move on to the next check (this BEQ
\ is effectively a JMP, as A is always zero)

.clip3

LDA TT                 \ Set A = the clipping requirements for the start point

BVC clip4              \ If bit 6 of V is clear, jump to clip4 as the y-delta
\ of the line is positive, or up

\ Bit 6 of V is set, so the y-delta of the line is
\ negative, or down

AND #%00010000         \ If bit 4 of TT is set, then the start point is off the
BNE AbortLine          \ bottom of the screen and the line direction is also
\ line as it must be entirely off-screen

BEQ clip5              \ Jump to clip5 to move on to the next check (this BEQ
\ is effectively a JMP, as A is always zero)

.clip4

\ If we get here then the y-delta of the line is
\ positive, or up

AND #%00100000         \ If bit 5 of TT is set, then the start point is off the
BNE AbortLine          \ top of the screen and the line direction is also up,
\ so jump to AbortLine to stop drawing the line as it
\ must be entirely off-screen

\ If we get here then the line might have an on-screen
\ element, so we now move on to the actual clipping
```