*******************************************************************************
* triple buffering & rotzoom example
*******************************************************************************
* INFO	loads a 512x512x8 ILBM (and another ILBM of the same size as
*	mask, if required) and then rotzooms it in onto a triple
*	buffered FullRes screen
* NOTE	- optionally you can pass as CLI argument the name of another
*	  512x512x8 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 exit anytime
*******************************************************************************

	machine	68020

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



*******************************************************************************
* custom display & other definitions
*******************************************************************************

DSPLWD	=	192	;our TCS display width and
DSPLHT	=	192	;height (LORES pixels)
SCRWD	=	192	;TCS screen width and
SCRHT	=	192	;height (FullRes pixels)
DSPLX0	=	$c1*4	;display start
DSPLY0	=	$49	;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 | TCS_VMf_TriBuf ;display video mode

ARGSNO	=	3	;command line arguments No.

ROTSPD	=	1	;rotation speed
SCLSTP	=	1	;scale step
ACCDLY	=	5	;acceleration delay
BRSHWDLOG	=	9	;brush width = 512
BRSHWD	=	1<<BRSHWDLOG	;source brush
BRSHHT	=	BRSHWD	;dimensions
WHEELWD	=	256	;calculate loops needed
STRTCTR	=	256*WHEELWD/(SCLSTP*DSPLWD) ;to fit wheel on screen
*STRTCTR	=	256	;counter to stop at scale 1:1



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

	include	/incl/shl_strtup.i



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

_PrgInit	tst.l	CmdLnArgs+8
	beq.s	.pic	;if mask not required...
	movea.l	CmdLnArgs+4,a0	;mask filename address
	tst.l	a0
	bne.s	.LdMsk	;if filename specified...
	lea.l	MskFlNm,a0	;default mask
.LdMsk	suba.l	a1,a1	;to separate buffer
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...
	move.l	d0,MskIIAdr	;else store its ILBMInfo structure address

	movea.l	DIAdr,a0	;our display
	lea.l	DsplDef+TCS_DD_GfxCtxt,a1 ;add masking
	ori.w	#TCS_GCf_msk,(a1)	;to its
	CALLTCS	SetGfxCtxt	;Graphic Context

.pic	movea.l	CmdLnArgs,a0	;picture filename address
	tst.l	a0
	bne.s	.LdPic	;if filename specified...
	lea.l	PicFlNm,a0	;default picture
.LdPic	suba.l	a1,a1	;to separate 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

	movea.l	DIAdr,a0	;our display
	move.b	(TCS_II_RGBxMode.w,d0.l),d0 ;picture RGBx mode
	move.w	#256,d1	;max brightness
	CALLTCS	SetRGBxMode	;set appropriate palette

	move.w	#0,ccr	;OK!
	rts

.fail	move.w	#4,ccr	;signal error
	rts



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

_PrgMain	move.l	#TriBufHndlr,([_VBR.l],$6c.w) ;set level3 autovector
	move.w	#$c020,$dff09a	;set INTENA.INTEN/VERTB

	movea.w	#ROTSPD,a3	;rotation step
	move.w	#SCLSTP,a5	;scaling step

	movea.l	([PicIIAdr.l],TCS_II_GfxAdr.w),a2 ;calculate brsh
	adda.l	#((BRSHHT/2)*BRSHWD)+BRSHWD/2,a2 ;centre address
	movea.l	([MskIIAdr.l],TCS_II_GfxAdr.w),a4 ;calculate mask
	adda.l	#((BRSHHT/2)*BRSHWD)+BRSHWD/2,a4 ;centre address
	move.w	#SCRWD-1,d2	;bottom-right corner
	move.w	#SCRHT-1,d3	;of destination area
	move.w	#0,d4	;start rotation angle
	move.w	#0,d5	;start scaling factor
	move.w	#BRSHWDLOG,d6	;brush width logarythm
	move.w	#STRTCTR-1,d7	;initialize counter

.lp	movea.l	DIAdr,a0	;our display
	movea.l	a2,a1	;brush centre address
	moveq.l	#0,d0	;top-left corner
	moveq.l	#0,d1	;of destination area
	CALLTCS	FastRotZmBrsh	;rotzoom!

	movea.l	DIAdr,a0	;render FullRes screen
	CALLTCS	CPUFRPass0	;to make changes visible

	movea.l	DIAdr,a0	;acknowledge screen
	CALLTCS	TriUpd	;rendering completion

	btst.b	#6,$bfe001
	beq.s	.exit	;if LMB...

	add.w	a3,d4	;new rotation angle
	add.w	a5,d5	;new scale factor
	dbra	d7,.lp	;repeat till next change of speed

	addq.l	#1,a3	;increase rotation speed
	suba.l	a5,a5	;stop zooming
	moveq.l	#ACCDLY-1,d7	;re-initialize counter
	bra.s	.lp	;repeat all

.exit	rts



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

_PrgClnUp	movea.l	MskIIAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for mask
	movea.l	PicIIAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for picture
	rts



*******************************************************************************
* triple buffer handler (level 3 interrupt handler)
*******************************************************************************

TriBufHndlr	btst.b	#5,$dff01f
	beq.s	.exit	;if not INTREQR.VERTB...
	movem.l	d0-d1/a0-a1/a6,-(sp)

	movea.l	DIAdr,a0
	CALLTCS	TriSwp	;swap screen buffers

	movem.l	(sp)+,d0-d1/a0-a1/a6
.exit	move.w	#$70,$dff09c	;clr INTREQ.BLIT/VERTB/COPER
	rte



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

	include	/incl/dat.i

PicIIAdr	dc.l	0	;picture ILBMInfo structure address
MskIIAdr	dc.l	0	;mask ILBMInfo structure address
tmplt	dc.b	"PICTURE=P,"
	dc.b	"MASK=M,USEMASK=U/S",0	;template for ReadArgs()
PicFlNm	dc.b	"/pix/wheel.rgbw.iff",0	;default picture
MskFlNm	dc.b	"/pix/Suppa.rgbs.iff",0	;default mask
