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.Get
				ldx CB.Cache+S.CB.CMD
CLIP.X			jmp (J.CLIP,x)
*--------------------------------------
CLIP.Point
CLIP.HLine
CLIP.VLine

CLIP.Rect		
CLIP.Text
				clc
				rts
*--------------------------------------
CLIP.BitBlt.9	pla
CLIP.BitBlt.99	sec
				rts
CLIP.BitBlt		lda CB.Cache+S.CB.DstX
				ldx CB.Cache+S.CB.DstX+1
				pha

				>SCMPAX OBJ.Cache+S.OBJ.X2
				bpl CLIP.BitBlt.9			DestX after X2....exit
				pla
				>SCMPAX OBJ.Cache+S.OBJ.X1
				bpl .1						DestX Inside X1-X2, go check if X1+W fit in  
* S.CB.DstX before S.OBJ.X1: D=S.OBJ.X1-S.CB.DstX, S.CB.SrcW-=D, S.CB.DstX=+=D, S.CB.X1+=D

				lda OBJ.Cache+S.OBJ.X1		Compute D....
				sec
				sbc CB.Cache+S.CB.DstX
				sta D
				lda OBJ.Cache+S.OBJ.X1+1
				sbc CB.Cache+S.CB.DstX+1
				sta D+1						....always positive
				lda CB.Cache+S.CB.SrcW		S.CB.SrcW-=D
				sec
				sbc D
				sta CB.Cache+S.CB.SrcW
				lda CB.Cache+S.CB.SrcW+1
				sbc D+1
				sta CB.Cache+S.CB.SrcW+1
				bcc CLIP.BitBlt.99			Negative!!!! nothing to do,exit...
				lda CB.Cache+S.CB.DstX		S.CB.DstX+=D
				clc
				adc D
				sta CB.Cache+S.CB.DstX
				lda CB.Cache+S.CB.DstX+1
				adc D+1
				sta CB.Cache+S.CB.DstX+1
				lda CB.Cache+S.CB.X1		S.CB.X1+=D
				clc
				adc D
				sta CB.Cache+S.CB.X1
				lda CB.Cache+S.CB.X1+1
				adc D+1
				sta CB.Cache+S.CB.X1+1		S.CB.DstX=0, new between S.OBJ.X1 & S.OBJ.X2....

* S.CB.DstX between S.OBJ.X1 & S.OBJ.X2: D=(S.OBJ.X2+1)-S.CB.DstX

.1				lda OBJ.Cache+S.OBJ.X2		Compute D in X,Y
				sec
				sbc CB.Cache+S.CB.DstX
				tax
				lda OBJ.Cache+S.OBJ.X2+1
				sbc CB.Cache+S.CB.DstX+1
				inx
				bne .2
				inc
.2				tay
				cpx CB.Cache+S.CB.SrcW
				sbc CB.Cache+S.CB.SrcW+1
				bcs CLIP.BitBlt.Y			D >= SrcW....nothing to clip

				stx CB.Cache+S.CB.SrcW		SrcW < D, make SrcW=D
				sty CB.Cache+S.CB.SrcW+1
CLIP.BitBlt.Y	lda CB.Cache+S.CB.DstY
				ldx CB.Cache+S.CB.DstY+1
				pha
				>SCMPAX OBJ.Cache+S.OBJ.Y2	DestY after Y2....exit
				bmi .10
				pla
				sec
				rts
.10				pla
				>SCMPAX OBJ.Cache+S.OBJ.Y1
				bpl .1						DestY Inside Y1-Y2, go check if Y1+H fit in  
* S.CB.DstY above S.OBJ.Y1: D=S.OBJ.Y1-S.CB.DstY, S.CB.SrcH-=D, S.CB.DstY=0, S.CB.Y1+=D
				lda OBJ.Cache+S.OBJ.Y1		Compute D....
				sec
				sbc CB.Cache+S.CB.DstY
				sta D
				lda OBJ.Cache+S.OBJ.Y1+1
				sbc CB.Cache+S.CB.DstY+1
				sta D+1						....always positive
				lda CB.Cache+S.CB.SrcH		S.CB.SrcH-=D
				sec
				sbc D
				sta CB.Cache+S.CB.SrcH
				lda CB.Cache+S.CB.SrcH+1
				sbc D+1
				sta CB.Cache+S.CB.SrcH+1
				bcc .99						Negative!!!! nothing to do,exit...
				lda CB.Cache+S.CB.DstY		Dst.Y+=D
				clc
				adc D
				sta CB.Cache+S.CB.DstY
				lda CB.Cache+S.CB.DstY+1
				adc D+1
				sta CB.Cache+S.CB.DstY+1
				lda CB.Cache+S.CB.Y1		S.CB.Y1+=D
				clc
				adc D
				sta CB.Cache+S.CB.Y1
				lda CB.Cache+S.CB.Y1+1
				adc D+1
				sta CB.Cache+S.CB.Y1+1		S.CB.DstY=0, new between S.OBJ.Y1 & S.OBJ.Y2....

* S.CB.DstY between S.OBJ.Y1 & S.OBJ.Y2: D=(S.OBJ.Y2+1)-S.CB.DstY

.1				lda OBJ.Cache+S.OBJ.Y2		Compute D in X,Y
				sec
				sbc CB.Cache+S.CB.DstY
				tax
				lda OBJ.Cache+S.OBJ.Y2+1
				sbc CB.Cache+S.CB.DstY+1

				inx
				bne .2
				inc

.2				tay
				cpx CB.Cache+S.CB.SrcH
				sbc CB.Cache+S.CB.SrcH+1
				bcs .8						D >= SrcH....nothing to clip

				stx CB.Cache+S.CB.SrcH		SrcH < D, make SrcH=D
				sty CB.Cache+S.CB.SrcH+1
.8				clc
				rts
.9				pla
.99				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
				bmi .1
				tya
				ora #CLIP.Line.LEFT
				plx
				rts
.1				pla
				>SCMPAX OBJ.Cache+S.OBJ.X1
				bpl .8
				tya
				ora #CLIP.Line.RIGHT
.8				rts	
*--------------------------------------
CLIP.Line.EncodeY.AX		
				pha
				>SCMPAX OBJ.Cache+S.OBJ.Y1
				bmi .1
				tya
				ora #CLIP.Line.TOP
				plx
				rts
.1				pla
				>SCMPAX OBJ.Cache+S.OBJ.Y1
				bpl .8
				tya
				ora #CLIP.Line.BOTTOM
.8				rts
*--------------------------------------
MAN
SAVE USR/SRC/LIB/LIBGUI.S.CLIP
LOAD USR/SRC/LIB/LIBGUI.S
ASM
