NEW
  AUTO 3,1
*--------------------------------------
CLIP.GetRect	>STYA .1+1

				ldy #S.OBJ.Y2+1
				ldx #7

.1				lda $ffff,y
				sta CLIP.Rect,x
				dey
				dex
				bpl .1

				rts
*--------------------------------------
*  CLIPPING Functions:
* In:
*  Target Clipping Region in CLIP.Rect
*  YA = Control Block
* Out:
*  CC : CLIP.Cache clipped & visible
*  CS : CLIP.Cache invisible
*--------------------------------------
CLIP.CB.Cache	ldx #S.CB

.1				lda CB.Cache-1,x
				sta CLIP.Cache-1,x
				dex
				bne .1

*				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 CLIP.Cache+S.CB.Y1
				>SCMPYA CLIP.Rect+S.RECT.Y2
				bpl .9

				>LDYA CLIP.Cache+S.CB.Y1
				>SCMPYA CLIP.Rect+S.RECT.Y1
				bmi .9

				>LDYA CLIP.Rect+S.RECT.X1
				>STYAIFGTR CLIP.Cache+S.CB.X1

				>LDYA CLIP.Rect+S.RECT.X2
				>STYAIFLWR CLIP.Cache+S.CB.X2

				clc
				rts

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

				>LDYA CLIP.Cache+S.CB.X1
				>SCMPYA CLIP.Rect+S.RECT.X1
				
				bpl CLIP.Y1Y2
				
*				bmi .9

*				>LDYA CLIP.Rect+S.RECT.Y1
*				>STYAIFGTR CLIP.Cache+S.CB.Y1

*				>LDYA CLIP.Rect+S.RECT.Y2
*				>STYAIFLWR CLIP.Cache+S.CB.Y2

*				clc
*				rts

.9				sec
				rts
*--------------------------------------
CLIP.Rectangle	jsr CLIP.Rect.Overlap
				bcs CLIP.Text.RTS

				>LDYA CLIP.Rect+S.RECT.X1
				>STYAIFGTR CLIP.Cache+S.CB.X1

				>LDYA CLIP.Rect+S.RECT.X2
				>STYAIFLWR CLIP.Cache+S.CB.X2

CLIP.Y1Y2		>LDYA CLIP.Rect+S.RECT.Y1
				>STYAIFGTR CLIP.Cache+S.CB.Y1

				>LDYA CLIP.Rect+S.RECT.Y2
				>STYAIFLWR CLIP.Cache+S.CB.Y2

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

CLIP.Text.9		sec
CLIP.Text.RTS	rts
*--------------------------------------
* BitBlt :
*--------------------------------------
CLIP.BitBlt		jsr CLIP.SetX2Y2
				jsr CLIP.Rect.Overlap
				bcs CLIP.Text.RTS

				ldx #S.RECT				select CLIP.Rect

CLIP.BitBltX	lda CLIP.Screen+S.RECT.X1,x
				sec
				sbc CLIP.Cache+S.CB.X1
				sta TmpW

				lda CLIP.Screen+S.RECT.X1+1,x
				sbc CLIP.Cache+S.CB.X1+1
				sta TmpW+1
				bvc .1

				eor #$80

.1				bmi .2

				ldy #S.CB.X1
				jsr CLIP.AddTmpW2ClipCacheY

				ldy #S.CB.SrcX
				jsr CLIP.AddTmpW2ClipCacheY

				ldy #S.CB.SrcW
				jsr CLIP.SubTmpW2ClipCacheY
				bcc CLIP.Text.9
*--------------------------------------
.2				lda CLIP.Cache+S.CB.X2
				sec
				sbc CLIP.Screen+S.RECT.X2,x
				sta TmpW

				lda CLIP.Cache+S.CB.X2+1
				sbc CLIP.Screen+S.RECT.X2+1,x
				sta TmpW+1
				bvc .3

				eor #$80

.3				bmi .4

				ldy #S.CB.SrcW
				jsr CLIP.SubTmpW2ClipCacheY
				bcc .9
*--------------------------------------
.4				lda CLIP.Screen+S.RECT.Y1,x
				sec
				sbc CLIP.Cache+S.CB.Y1
				sta TmpW

				lda CLIP.Screen+S.RECT.Y1+1,x
				sbc CLIP.Cache+S.CB.Y1+1
				sta TmpW+1
				bvc .5

				eor #$80

.5				bmi .6

				ldy #S.CB.Y1
				jsr CLIP.AddTmpW2ClipCacheY

				ldy #S.CB.SrcY
				jsr CLIP.AddTmpW2ClipCacheY
				
				ldy #S.CB.SrcH
				jsr CLIP.SubTmpW2ClipCacheY
				bcc .9
*--------------------------------------
.6				lda CLIP.Cache+S.CB.Y2
				sec
				sbc CLIP.Screen+S.RECT.Y2,x
				sta TmpW

				lda CLIP.Cache+S.CB.Y2+1
				sbc CLIP.Screen+S.RECT.Y2+1,x
				sta TmpW+1
				bvc .7

				eor #$80

.7				bmi .8

				ldy #S.CB.SrcH
				jsr CLIP.SubTmpW2ClipCacheY
				bcc .9

.8				clc
				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 CLIP.Cache+S.CB.X1
				ldx CLIP.Cache+S.CB.X1+1
				jsr CLIP.Line.EncodeX.AX

				lda CLIP.Cache+S.CB.Y1
				ldx CLIP.Cache+S.CB.Y1+1
				jsr CLIP.Line.EncodeY.AX

				sty CLIP.Line.P1
				rts
*--------------------------------------
CLIP.Line.EncodeP2
				ldy #0

				lda CLIP.Cache+S.CB.X2
				ldx CLIP.Cache+S.CB.X2+1
				jsr CLIP.Line.EncodeX.AX

				lda CLIP.Cache+S.CB.Y2
				ldx CLIP.Cache+S.CB.Y2+1
				jsr CLIP.Line.EncodeY.AX

				sty CLIP.Line.P2
				rts
*--------------------------------------
CLIP.Line.EncodeX.AX
				pha
				>SCMPAX CLIP.Rect+S.RECT.X1
				bpl .1

				pla

				tya
				ora #CLIP.Line.LEFT
				tay

				rts

.1				pla
				>SCMPAX CLIP.Rect+S.RECT.X2
				bmi .8

				tya
				ora #CLIP.Line.RIGHT
				tay

.8				rts
*--------------------------------------
CLIP.Line.EncodeY.AX
				pha
				>SCMPAX CLIP.Rect+S.RECT.Y1
				bpl .1

				pla

				tya
				ora #CLIP.Line.TOP
				tya
				rts

.1				pla
				>SCMPAX CLIP.Rect+S.RECT.Y2
				bmi .8

				tya
				ora #CLIP.Line.BOTTOM
				tay

.8				rts
*--------------------------------------
CLIP.SetX2Y2	ldx CLIP.Cache+S.CB.SrcW+1
				lda CLIP.Cache+S.CB.SrcW
				bne .3

				dex

.3				dec
				clc
				adc CLIP.Cache+S.CB.X1
				sta CLIP.Cache+S.CB.X2

				txa
				adc CLIP.Cache+S.CB.X1+1
				sta CLIP.Cache+S.CB.X2+1

				ldx CLIP.Cache+S.CB.SrcH+1
				lda CLIP.Cache+S.CB.SrcH
				bne .4

				dex

.4				dec
				clc
				adc CLIP.Cache+S.CB.Y1
				sta CLIP.Cache+S.CB.Y2

				txa
				adc CLIP.Cache+S.CB.Y1+1
				sta CLIP.Cache+S.CB.Y2+1

				rts
*--------------------------------------
CLIP.Rect.Overlap
				>LDYA CLIP.Cache+S.CB.X1
				>SCMPYA CLIP.Rect+S.RECT.X2
				bpl .9

				>LDYA CLIP.Cache+S.CB.X2
				>SCMPYA CLIP.Rect+S.RECT.X1
				bmi .9

				>LDYA CLIP.Cache+S.CB.Y1
				>SCMPYA CLIP.Rect+S.RECT.Y2
				bpl .9

				>LDYA CLIP.Cache+S.CB.Y2
				>SCMPYA CLIP.Rect+S.RECT.Y1
				bmi .9

				clc
				rts

.9				sec
				rts
*--------------------------------------
CLIP.AddTmpW2ClipCacheY
				clc
				
				lda CLIP.Cache,y
				adc TmpW
				sta CLIP.Cache,y

				lda CLIP.Cache+1,y
				adc TmpW+1
				sta CLIP.Cache+1,y
				rts
*--------------------------------------
CLIP.SubTmpW2ClipCacheY
				sec
				
				lda CLIP.Cache,y
				sbc TmpW
				sta CLIP.Cache,y

				lda CLIP.Cache+1,y
				sbc TmpW+1
				sta CLIP.Cache+1,y
				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libgui.s.clip
LOAD usr/src/lib/libgui.s
ASM
