*******************************************************************************
* triple buffering & zoom example
*******************************************************************************
* INFO	loads a 320x256x8 ILBM and then zooms it in onto a triple
*	buffered FullRes screen
* 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 exit anytime
*******************************************************************************

	machine	68020

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



*******************************************************************************
* custom display & other 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 | TCS_VMf_TriBuf ;display video mode

ARGSNO	=	1	;command line arguments No.

STP	=	2	;zoom step (multiple of 2)
BRSHWD	=	DSPLWD	;source brush has the
BRSHHT	=	DSPLHT	;same dimensions of display
	ifgt	SCRWD-SCRHT
DF	=	SCRHT/2
	else
DF	=	SCRWD/2
	endif
LPS	=	DF/STP	;numbers of loops to perform



*******************************************************************************
* 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	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.l	([PicIIAdr.l],TCS_II_GfxAdr.w),a3 ;brush address
	lea.l	ZmStrc,a2	;info about the zoom areas
	move.w	#LPS-1,d6	;loops counter

.lp	subq.w	#STP,(8,a2)	;enlarge
	subq.w	#STP,(10,a2)	;the destination
	addq.w	#STP,(12,a2)	;area
	addq.w	#STP,(14,a2)	;on the screen

	movea.l	DIAdr,a0	;our display
	movea.l	a3,a1	;brush address
	move.w	#BRSHWD,d0	;brush width
	CALLTCS	FastFitBrshZn0	;zoom in!

	movea.l	DIAdr,a0	;render FullRes
	move.w	(8,a2),d0	;screen to make
	move.w	(10,a2),d1	;changes visible
	move.w	(12,a2),d2	;(for speed-up,
	move.w	(14,a2),d3	;only the area
	move.w	d0,d4	;written by
	move.w	d1,d5	;FastFitBrshZn0()
	CALLTCS	CPUFRPass2	;is updated)

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

	btst.b	#6,$bfe001
	dbeq.s	d6,.lp	;if not LMB...

	WTLMB
	rts



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

_PrgClnUp	movea.l	PicIIAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for the 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

	cnop	0,4
ZmStrc	dc.w	0,0	;source
	dc.w	BRSHWD-1,BRSHHT-1	;area
	dc.w	DF,DF	;destination
	dc.w	SCRWD-DF-1,SCRHT-DF-1	;area

PicIIAdr	dc.l	0	;picture ILBMInfo structure address
tmplt	dc.b	"PICTURE=P",0	;template for ReadArgs()
PicFlNm	dc.b	"/pix/Tiger Mask.rgb332.iff",0 ;default picture
