*******************************************************************************
* double buffering and FullRes scrolling example
*******************************************************************************
* INFO	shows how to scroll a double buffered FullRes screen
* NOTE	- optionally you can pass as CLI argument the name of the ILBM
*	  to load (its palette must be one of those in TCS/pal/ and
*	  must respect all the restrictions of TCS_LdILBM(), otherwise
*	  the program won't execute; no restriction on size)
*	- LMB to exit anytime
*******************************************************************************

	machine	68020

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



*******************************************************************************
* custom display definitions
*******************************************************************************

DSPLWD	=	320	;our TCS display width and
DSPLHT	=	256	;height (LORES pixels)
SCRWD	=	656	;TCS screen width and
SCRHT	=	414	;height (HalfRes 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_DubBuf ;display video mode

ARGSNO	=	1	;command line arguments No.



*******************************************************************************
* 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	;allocate new buffer for data
	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,a4	;our display
	movea.l	PicIIAdr,a3	;picture ILBMInfo structure address

	movea.l	a4,a0
	move.b	(TCS_II_RGBxMode,a3),d0	;picture RGBx mode
	move.w	#DSPLBRTNS,d1
	CALLTCS	SetRGBxMode	;set display palette

	movea.l	a4,a0	;display
	movea.l	(TCS_II_GfxAdr,a3),a1	;picture graphics address
	lea.l	VtxsAdr,a2
	move.w	(TCS_II_wd,a3),d0	;picture width
	move.w	d0,d1
	subq.w	#1,d1
	move.w	d1,(4,a2)
	move.w	(TCS_II_ht,a3),d1	;resize the picture
	subq.w	#1,d1	;to fit perfectly
	move.w	d1,(6,a2)	;the chunky screen
	CALLTCS	FitBrshZn0	;of the display

	movea.l	a4,a0
	moveq.l	#0,d0	;render the
	moveq.l	#0,d1	;logical
	CALLTCS	CPUFRPass1	;video buffer
	movea.l	a4,a0	;update both the
	CALLTCS	EquVdoBufs	;video buffers

	moveq.l	#0,d2	;x position
	moveq.l	#0,d3	;y position
	moveq.l	#0,d4	;direction
	lea.l	shfs,a2	;shifts address
	lea.l	lmts,a3	;limits address
	
.scrl	add.w	(a2,d4*4),d2	;update x position
	add.w	(2,a2,d4*4),d3	;update y position
	move.w	d2,d0	;x position
	move.w	d3,d1	;y position
	movea.l	a4,a0	;display
	CALLTCS	CPUFRPass1	;scroll screen

	WTRSTLN	DSPLY1-1	;wait end of frame
	movea.l	a4,a0	;swap display
	CALLTCS	DubSwp	;video buffers

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

	cmp.w	(a3,d4*4),d2	;check x position
	beq.s	.ChgDrctn	;if limit reached...
	cmp.w	(2,a3,d4*4),d3	;check y position
	bne.s	.scrl	;if limit not reached...
.ChgDrctn	addq.w	#1,d4	;change direction
	andi.w	#3,d4
	bra.s	.scrl

.exit	rts



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

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



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

	include	/incl/dat.i

	cnop	0,4
PicIIAdr	dc.l	0	;picture ILBMInfo structure address
tmplt	dc.b	"PICTURE=P",0	;template for ReadArgs()
PicFlNm	dc.b	"/pix/my WB.rgbm.iff",0	;default picture

	even
VtxsAdr	dc.w	0,0,655,413	;coordinates for
	dc.w	0,0,SCRWD-1,SCRHT-1	;picture fitting

shfs	dc.w	1,0	;scrolling
	dc.w	0,1	;shift values
	dc.w	-1,0
	dc.w	0,-1

lmts	dc.w	(SCRWD-DSPLWD),-1	;scrolling
	dc.w	-1,SCRHT-DSPLHT	;limits
	dc.w	0,-1
	dc.w	-1,0
