*******************************************************************************
* brush handling example
*******************************************************************************
* INFO	loads a 320x256x8 ILBM to a FullRes screen and then performs
*	various brush operations on it
* NOTE	- optionally you can pass as CLI argument the name of another
*	  320x256x8 ILBM to load (yours must have the same size and
*	  must respect all the restrictions of TCS_LdILBM(), otherwise
*	  the program won't execute or the graphics will look bad -
*	  not dangerous, anyway!)
*	- LMB to quit
*******************************************************************************

	machine	68020

	include	INCLUDES:libraries/tcs.i
	include	INCLUDES:libraries/tcs_lib.i
	include	/incl/macros.i



*******************************************************************************
* definitions
*******************************************************************************

DSPLWD	=	320	;our TCS display width and
DSPLHT	=	256	;height (LORES pixels)
SCRWD	=	320	;TCS screen width and
SCRHT	=	256	;height (FullRes pixels)
DSPLX0	=	$81*4	;display start
DSPLY0	=	$2c	;position (SHRES pixels)
DSPLX1	=	DSPLX0+DSPLWD*4	;display end
DSPLY1	=	DSPLY0+DSPLHT	;position (SHRES pixels)
DSPLBRTNS	=	256	;max brightness
GFXCTXT	=	0	;graphic context
CWBTM	=	0	;clipping
CWTOP	=	0	;window
CWRT	=	0	;borders
CWLF	=	0	;coordinates
VDOMODE	=	TCS_VMf_FullRes	;display video mode

ARGSNO	=	1	;command line arguments No.
BRSHSZ	=	32	;width & height of a brush
TRNSPCOL	=	153



*******************************************************************************
* code start
*******************************************************************************

	include	/incl/shl_strtup.i



*******************************************************************************
* init
*******************************************************************************

_PrgInit	movea.l	CmdLnArgs,a0	;picture filename address
	tst.l	a0
	bne.s	.ld	;if filename specified...
	lea.l	PicFlNm,a0	;default picture
.ld	movea.l	([DIAdr.l],TCS_DI_CSAdr.w),a1 ;directly to
	move.l	#SCRWD*SCRHT,d0	;screen buffer
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...
	move.l	d0,PicIIAdr	;else store its ILBMInfo structure address
	rts		;& clr ccr.Z to signal success
.fail	move.w	#4,ccr	;signal error
	rts



*******************************************************************************
* main
*******************************************************************************

_PrgMain	movea.l	DIAdr,a0
	move.b	([PicIIAdr.l],TCS_II_RGBxMode.w),d0 ;picture RGBx mode
	move.w	#DSPLBRTNS,d1
	CALLTCS	SetRGBxMode	;set display palette

*******************************************************************************
* normal masks

	movea.l	DIAdr,a0	;cut the first
	suba.l	a1,a1	;brush from the
	moveq.l	#0,d0	;screen
	moveq.l	#64,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;allocated by
	CALLTCS	GetBrsh	;the library
	move.l	d0,brsh0	;store the brush address
	beq.s	.exit	;if error...

	movea.l	DIAdr,a0	;cut the second
	suba.l	a1,a1	;brush from the
	moveq.l	#0,d0	;screen
	moveq.l	#96,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;allocated by
	CALLTCS	GetBrsh	;the library
	move.l	d0,brsh1	;store the brush address
	beq.s	.exit	;if error...

	movea.l	brsh0,a0	;make a normal
	suba.l	a1,a1	;mask for the
	move.b	#$ff,d0	;first brush
	move.b	#TRNSPCOL,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;allocated by
	CALLTCS	MkBrshMsk	;the library
	move.l	d0,msk0	;store the mask address
	beq.s	.exit	;if error...

	movea.l	brsh1,a0	;make a normal
	suba.l	a1,a1	;mask for the
	move.b	#$ff,d0	;second brush
	move.b	#TRNSPCOL,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;allocated by
	CALLTCS	MkBrshMsk	;the library
	move.l	d0,msk1	;store the mask address
	beq.s	.exit	;if error...

	movea.l	DIAdr,a0	;print
	movea.l	msk0,a1	;the
	move.w	#64,d0	;first
	move.w	#64,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	msk1,a1	;the
	move.w	#64,d0	;second
	move.w	#96,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	movea.l	msk0,a2	;first
	move.w	#96,d0	;brush
	move.w	#64,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh1,a1	;the
	movea.l	msk1,a2	;second
	move.w	#96,d0	;brush
	move.w	#96,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

	movea.l	brsh0,a0	;merge
	movea.l	brsh1,a1	;the two
	movea.l	msk0,a2	;brushes
	movea.l	a0,a3	;together
	move.w	#BRSHSZ,d2	;using the
	move.w	#BRSHSZ,d3	;first brush
	CALLTCS	MrgBrshs	;as destination

	movea.l	msk0,a0	;merge
	movea.l	msk1,a1	;the two
	movea.l	a0,a2	;masks
	movea.l	a0,a3	;together
	move.w	#BRSHSZ,d2	;using the
	move.w	#BRSHSZ,d3	;first mask
	CALLTCS	MrgBrshs	;as destination

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	move.w	#160,d0	;mixed
	move.w	#64,d1	;brush
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	msk0,a1	;the
	move.w	#192,d0	;mixed
	move.w	#64,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	movea.l	msk0,a2	;mixed
	move.w	#224,d0	;brush
	move.w	#64,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

*******************************************************************************
* partial masks

	movea.l	DIAdr,a0	;cut a brush
	movea.l	brsh0,a1	;brush from the
	moveq.l	#0,d0	;screen
	move.w	#160,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;previously allocated
	CALLTCS	GetBrsh	;by the library

	movea.l	brsh0,a0	;make a red-only
	movea.l	msk0,a1	;mask for the
	move.b	#$88,d0	;brush
	move.b	#TRNSPCOL,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;previously allocated
	CALLTCS	MkBrshMsk	;by the library

	movea.l	DIAdr,a0	;print
	movea.l	msk0,a1	;the
	move.w	#64,d0	;newly created
	move.w	#160,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	movea.l	msk0,a2	;newly created
	move.w	#96,d0	;brush
	move.w	#160,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

	movea.l	brsh0,a0	;make a green-only
	movea.l	msk0,a1	;mask for the
	move.b	#$44,d0	;brush
	move.b	#TRNSPCOL,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;previously allocated
	CALLTCS	MkBrshMsk	;by the library

	movea.l	DIAdr,a0	;print
	movea.l	msk0,a1	;the
	move.w	#64,d0	;newly created
	move.w	#192,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	movea.l	msk0,a2	;newly created
	move.w	#96,d0	;brush
	move.w	#192,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

	movea.l	brsh0,a0	;make a blue-only
	movea.l	msk0,a1	;mask for the
	move.b	#$22,d0	;brush
	move.b	#TRNSPCOL,d1	;and keep it
	move.w	#BRSHSZ,d2	;in a buffer
	move.w	#BRSHSZ,d3	;previously allocated
	CALLTCS	MkBrshMsk	;by the library

	movea.l	DIAdr,a0	;print
	movea.l	msk0,a1	;the
	move.w	#64,d0	;newly created
	move.w	#224,d1	;mask
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh0	;screen

	movea.l	DIAdr,a0	;print
	movea.l	brsh0,a1	;the
	movea.l	msk0,a2	;newly created
	move.w	#96,d0	;brush
	move.w	#224,d1	;(masked)
	move.w	#BRSHSZ,d2	;on
	move.w	#BRSHSZ,d3	;the
	CALLTCS	WrtBrsh8	;screen

*******************************************************************************

	movea.l	DIAdr,a0	;make screen
	CALLTCS	CPUFRPass0	;buffer visible

	WTLMB
.exit	rts



*******************************************************************************
* cleanup
*******************************************************************************

_PrgClnUp	movea.l	msk1,a0	;free all the memory
	CALLTCS	FreeBrsh	;allocated for
	movea.l	msk0,a0	;the masks
	CALLTCS	FreeBrsh	;created here
	movea.l	brsh1,a0	;free all the memory
	CALLTCS	FreeBrsh	;allocated for
	movea.l	brsh0,a0	;the brushes
	CALLTCS	FreeBrsh	;created here
	movea.l	PicIIAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for the picture
	rts



*******************************************************************************
* data
*******************************************************************************

	include	/incl/dat.i

	cnop	0,4

brsh0	dc.l	0	;pointers to brushes
brsh1	dc.l	0	;created here
msk0	dc.l	0	;pointers to masks
msk1	dc.l	0	;created here
PicIIAdr	dc.l	0	;picture ILBMInfo structure address
tmplt	dc.b	"PICTURE=P",0	;template for ReadArgs()
PicFlNm	dc.b	"/pix/brshs.rgbh.iff",0	;default picture
