*******************************************************************************
* ILBM handling and fade in/out example
*******************************************************************************
* INFO	loads a 320x256x8 ILBM to a FullRes screen and then fades it
*	in and out
* 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



*******************************************************************************
* 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	=	0	;min 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.



*******************************************************************************
* 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,a2	;our display
	movea.l	PicIIAdr,a0	;picture ILBMInfo structure address
	movea.l	(TCS_II_GfxAdr,a0),a1	;address of picture graphics
	move.b	(TCS_II_RGBxMode,a0),d3	;picture RGBx mode

	movea.l	a2,a0	;render FullRes screen
	CALLTCS	CPUFRPass0	;to make pixels visible

	moveq.l	#0,d2	;brightness at beginning (min)
.FadeIn	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.b	d3,d0	;RGBx mode
	move.w	d2,d1	;brightness
	CALLTCS	SetRGBxMode	;change palette
	addq.w	#1,d2	;increase brightness
	btst.b	#6,$bfe001
	beq.s	.exit	;if LMB...
	cmpi.w	#256,d2
	bls.s	.FadeIn	;repeat other 256 times

	move.w	#256,d2	;brightness at beginning (max)
.FadeOut	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.b	d3,d0	;RGBx mode
	move.w	d2,d1	;brightness
	CALLTCS	SetRGBxMode	;change palette
	btst.b	#6,$bfe001	;exit if LMB
	dbeq	d2,.FadeOut	;decrease brightness and repeat other 256 times
.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/Tiger Mask.rgbw.iff",0 ;default picture
