NEW
  AUTO 3,1
*--------------------------------------
*  CLIPPING Functions:
* In:
*  Graphic Context in OBJ.Cache
*  YA = Control Block
* Out:
*  CC : CB.Cache clipped & visible
*  CS : CB.Cache invisible
*--------------------------------------
CLIP.YA			jsr CB.GetInCache

CLIP.CB.Cache	lda CB.Cache+S.CB.CMD
				and #$7F				remove S.CB.CMD.OSD
				tax
				jmp (J.CLIP-2,x)
*--------------------------------------
CLIP.Point
				clc
				rts
*--------------------------------------
CLIP.HLine		>LDYA CB.Cache+S.CB.Y1
				>SCMPYA OBJ.Cache+S.OBJ.Y2
				bpl .9

				>LDYA CB.Cache+S.CB.Y2
				>SCMPYA OBJ.Cache+S.OBJ.Y1
				bmi .9

				>LDYA OBJ.Cache+S.OBJ.X1
				>STYAIFGTR CB.Cache+S.CB.X1
				
				>LDYA OBJ.Cache+S.OBJ.X2
				>STYAIFLWR CB.Cache+S.CB.X2

				clc
				rts
				
.9				sec
				rts
*--------------------------------------
CLIP.VLine		>LDYA CB.Cache+S.CB.X1
				>SCMPYA OBJ.Cache+S.OBJ.X2
				bpl .9

				>LDYA CB.Cache+S.CB.X2
				>SCMPYA OBJ.Cache+S.OBJ.X1
				bmi .9
				
				>LDYA OBJ.Cache+S.OBJ.Y1
				>STYAIFGTR CB.Cache+S.CB.Y1

				>LDYA OBJ.Cache+S.OBJ.Y2
				>STYAIFLWR CB.Cache+S.CB.Y2

				clc
				rts

.9				sec
				rts				
*--------------------------------------
CLIP.Rect		jsr CB.Cache.Overlap
				bcs .9

				>LDYA OBJ.Cache+S.OBJ.X1
				>STYAIFGTR CB.Cache+S.CB.X1

				>LDYA OBJ.Cache+S.OBJ.X2
				>STYAIFLWR CB.Cache+S.CB.X2

				>LDYA OBJ.Cache+S.OBJ.Y1
				>STYAIFGTR CB.Cache+S.CB.Y1

				>LDYA OBJ.Cache+S.OBJ.Y2
				>STYAIFLWR CB.Cache+S.CB.Y2

				clc
.9				rts
*--------------------------------------
CLIP.Text
				clc
				rts

CLIP.Text.9		sec
CLIP.Text.RTS	rts
*--------------------------------------
* BitBlt :
*--------------------------------------
CLIP.BitBlt		jsr CB.Cache.SetX2Y2	set X2=X1+W-1 Y2=X1+H-1

				jsr CB.Cache.Overlap
				bcs CLIP.Text.RTS

				lda OBJ.Cache+S.OBJ.X1
				sec
				sbc CB.Cache+S.CB.X1
				sta DX

				lda OBJ.Cache+S.OBJ.X1+1
				sbc CB.Cache+S.CB.X1+1
				sta DX+1
				bvc .1
				eor #$80
.1				bmi .2

				lda CB.Cache+S.CB.X1
				clc
				adc DX
				sta CB.Cache+S.CB.X1

				lda CB.Cache+S.CB.X1+1
				adc DX+1
				sta CB.Cache+S.CB.X1+1

				lda CB.Cache+S.CB.SrcX
				clc
				adc DX
				sta CB.Cache+S.CB.SrcX

				lda CB.Cache+S.CB.SrcX+1
				adc DX+1
				sta CB.Cache+S.CB.SrcX+1

				lda CB.Cache+S.CB.SrcW
				sec
				sbc DX
				sta CB.Cache+S.CB.SrcW

				lda CB.Cache+S.CB.SrcW+1
				sbc DX+1
				sta CB.Cache+S.CB.SrcW+1
				bcc CLIP.Text.9

.2				lda CB.Cache+S.CB.X2
				sec
				sbc OBJ.Cache+S.OBJ.X2
				sta DX

				lda CB.Cache+S.CB.X2+1
				sbc OBJ.Cache+S.OBJ.X2+1
				sta DX+1
				bvc .3
				eor #$80
.3				bmi .4

				lda CB.Cache+S.CB.SrcW
				sec
				sbc DX
				sta CB.Cache+S.CB.SrcW

				lda CB.Cache+S.CB.SrcW+1
				sbc DX+1
				sta CB.Cache+S.CB.SrcW+1
				bcc .9

.4				lda OBJ.Cache+S.OBJ.Y1
				sec
				sbc CB.Cache+S.CB.Y1
				sta DX

				lda OBJ.Cache+S.OBJ.Y1+1
				sbc CB.Cache+S.CB.Y1+1
				sta DX+1
				bvc .5
				eor #$80
.5				bmi .6

				lda CB.Cache+S.CB.Y1
				clc
				adc DX
				sta CB.Cache+S.CB.Y1

				lda CB.Cache+S.CB.Y1+1
				adc DX+1
				sta CB.Cache+S.CB.Y1+1

				lda CB.Cache+S.CB.SrcY
				clc
				adc DX
				sta CB.Cache+S.CB.SrcY

				lda CB.Cache+S.CB.SrcY+1
				adc DX+1
				sta CB.Cache+S.CB.SrcY+1

				lda CB.Cache+S.CB.SrcH
				sec
				sbc DX
				sta CB.Cache+S.CB.SrcH

				lda CB.Cache+S.CB.SrcH+1
				sbc DX+1
				sta CB.Cache+S.CB.SrcH+1
				bcc .9

.6				lda CB.Cache+S.CB.Y2
				sec
				sbc OBJ.Cache+S.OBJ.Y2
				sta DX

				lda CB.Cache+S.CB.Y2+1
				sbc OBJ.Cache+S.OBJ.Y2+1
				sta DX+1
				bvc .7
				eor #$80
.7				bmi .8

				lda CB.Cache+S.CB.SrcH
				sec
				sbc DX
				sta CB.Cache+S.CB.SrcH

				lda CB.Cache+S.CB.SrcH+1
				sbc DX+1
				sta CB.Cache+S.CB.SrcH+1
				bcc .9

.8				clc
.99				rts

.9				sec
				rts
*--------------------------------------
CLIP.Line.LEFT	.EQ 1
CLIP.Line.RIGHT	.EQ 2
CLIP.Line.BOTTOM	.EQ 4
CLIP.Line.TOP	.EQ 8
*--------------------------------------
CLIP.Line		jsr CLIP.Line.EncodeP1
				jsr CLIP.Line.EncodeP2

.1				lda CLIP.Line.P1
				ora CLIP.Line.P2
				beq .88

				lda CLIP.Line.P1
				and CLIP.Line.P2
				bne .99

				lda CLIP.Line.P1
				beq .5

				bit #CLIP.Line.TOP
				beq .2

* Y1 is above S.CB.Y1....
* DY=Y2-Y1;
				jsr CLIP.Line.EncodeP1
				bra .1

.2				bit #CLIP.Line.BOTTOM
				beq .3

* Y1 is below S.CB.Y2....

				jsr CLIP.Line.EncodeP1
				bra .1

.3				bit #CLIP.Line.RIGHT
				beq .4

* X1 is after S.CB.X2....

				jsr CLIP.Line.EncodeP1
				bra .1

* X1 is before S.CB.X1....

.4				jsr CLIP.Line.EncodeP1
				bra .1

* P2

.5				lda CLIP.Line.P2

				bit #CLIP.Line.TOP
				beq .6

* Y2 is above S.CB.Y1....

				jsr CLIP.Line.EncodeP2
				bra .1

.6				bit #CLIP.Line.BOTTOM
				beq .7

* Y2 is below S.CB.Y2....

				jsr CLIP.Line.EncodeP2
				bra .1

.7				bit #CLIP.Line.RIGHT
				beq .8

* X2 is after S.CB.X2....

				jsr CLIP.Line.EncodeP2
				bra .1

* X2 is before S.CB.X1....

.8				jsr CLIP.Line.EncodeP2
				bra .1

.88				clc
				rts

.99				sec
				rts
*--------------------------------------
CLIP.Line.EncodeP1
				ldy #0

				lda CB.Cache+S.CB.X1
				ldx CB.Cache+S.CB.X1+1
				jsr CLIP.Line.EncodeX.AX
				tay
				lda CB.Cache+S.CB.Y1
				ldx CB.Cache+S.CB.Y1+1
				jsr CLIP.Line.EncodeY.AX
				sta CLIP.Line.P1
				rts
*--------------------------------------
CLIP.Line.EncodeP2
				ldy #0

				lda CB.Cache+S.CB.X2
				ldx CB.Cache+S.CB.X2+1
				jsr CLIP.Line.EncodeX.AX
				tay
				lda CB.Cache+S.CB.Y2
				ldx CB.Cache+S.CB.Y2+1
				jsr CLIP.Line.EncodeY.AX
				sta CLIP.Line.P2
				rts
*--------------------------------------
CLIP.Line.EncodeX.AX
				pha
				>SCMPAX OBJ.Cache+S.OBJ.X1
				bpl .1

				tya
				ora #CLIP.Line.LEFT
				plx
				rts

.1				pla
				>SCMPAX OBJ.Cache+S.OBJ.X2
				bmi .8

				tya
				ora #CLIP.Line.RIGHT

.8				tya
				rts
*--------------------------------------
CLIP.Line.EncodeY.AX
				pha
				>SCMPAX OBJ.Cache+S.OBJ.Y1
				bpl .1

				tya
				ora #CLIP.Line.TOP
				plx
				rts

.1				pla
				>SCMPAX OBJ.Cache+S.OBJ.Y2
				bmi .8

				tya
				ora #CLIP.Line.BOTTOM

.8				tya
				rts
*--------------------------------------
CB.Cache.SetX2Y2
				lda CB.Cache+S.CB.SrcW
				clc
				adc CB.Cache+S.CB.X1
				pha

				lda CB.Cache+S.CB.SrcW+1
				adc CB.Cache+S.CB.X1+1
				tax
				pla
				bne .2

				dex

.2				dec
				sta CB.Cache+S.CB.X2
				stx CB.Cache+S.CB.X2+1

				lda CB.Cache+S.CB.SrcH

				clc
				adc CB.Cache+S.CB.Y1
				pha

				lda CB.Cache+S.CB.SrcH+1
				adc CB.Cache+S.CB.Y1+1
				tax
				pla
				bne .3

				dex

.3				dec
				sta CB.Cache+S.CB.Y2
				stx CB.Cache+S.CB.Y2+1
				rts
*--------------------------------------
CB.Cache.Overlap
				>LDYA CB.Cache+S.CB.X1
				>SCMPYA OBJ.Cache+S.OBJ.X2
				bpl .9

				>LDYA CB.Cache+S.CB.X2
				>SCMPYA OBJ.Cache+S.OBJ.X1
				bmi .9

				>LDYA CB.Cache+S.CB.Y1
				>SCMPYA OBJ.Cache+S.OBJ.Y2
				bpl .9

				>LDYA CB.Cache+S.CB.Y2
				>SCMPYA OBJ.Cache+S.OBJ.Y1
				bmi .9

				clc
				rts

.9				sec
				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libgui.s.clip
LOAD usr/src/lib/libgui.s
ASM
