*******************************************************************************
* (Dual) Cross Playfield example
*******************************************************************************
* INFO	loads three 320x256x8 ILBMs and applies some (cross-)fading
*	effects in (Dual) Cross Playfield mode
* NOTE	- optionally you can pass as CLI argument the names of the
*	  320x256x8 IFF/ILBMs to load (if not one of those supplied in
*	  TCS/examples/pix/, make sure that yours is exactly 320x256x8
*	  and that it respects all the restrictions of TCS_LdILBM(),
*	  otherwise the program won't execute or the graphics will
*	  look bad - not dangerous, anyway!)
*	- press LMB to quickly skip ahead
*	- hold LMB down to exit anytime
*******************************************************************************

	machine	68020

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



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

DSPLWD	=	320	;our TCS display width and
DSPLHT	=	256	;height in 140 ns pixels
SCRWD	=	320	;TCS screen width and
SCRHT	=	256	;height in 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
CWBTM	=	0
CWTOP	=	0
CWRT	=	0
CWLF	=	0
VDOMODE	=	TCS_VM_RGBW | TCS_VMf_FullRes

ARGSNO	=	3	;command line arguments no.



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

	include	/includes/macros.i
	include	/includes/shl_strtup.i



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

_PrgInit	lea.l	DsplDef,a0	;use same DD structure to
	ori.l	#TCS_VMf_FPfld,(TCS_DD_VdoMode,a0) ;init another display
	CALLTCS	InitDspl	;for the front playfield
	move.l	d0,FPfldDI	;store DI structure address
	beq.s	.fail	;if error...

*******************************************************************************
* back playfield

	movea.l	CmdLnArgs,a0	;ILBM0 filename address
	tst.l	a0
	bne.s	.ld0	;if filename specified...
	lea.l	Pic0FlNm,a0	;default ILBM0
.ld0	movea.l	([DIAdr.l],TCS_DI_CSAdr.w),a1 ;directly to back
	move.l	#SCRWD*SCRHT,d0	;playfield screen buffer
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...

	move.b	(TCS_II_RGBxMode.w,d0.l),d2 ;get ILBM0's RGBx mode
	movea.l	d0,a0	;free all the memory allocated
	CALLTCS	UnLdILBM	;for the ILBMInfo structure
	movea.l	DIAdr,a0
	move.b	d2,d0	;RGBx mode
	move.w	#DSPLBRTNS,d1
	CALLTCS	SetRGBxMode	;set back playfield palette

*******************************************************************************
* front playfield

	movea.l	CmdLnArgs+4,a0	;ILBM1 filename address
	tst.l	a0
	bne.s	.ld1	;if filename specified...
	lea.l	Pic1FlNm,a0	;default ILBM1
.ld1	movea.l	([FPfldDI.l],TCS_DI_CSAdr.w),a1 ;directly to front
	move.l	#SCRWD*SCRHT,d0	;playfield screen buffer
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...

	move.b	(TCS_II_RGBxMode.w,d0.l),d2 ;get ILBM1's RGBx mode
	movea.l	d0,a0	;free all the memory allocated
	CALLTCS	UnLdILBM	;for the ILBMInfo structure
	movea.l	FPfldDI,a0
	move.b	d2,d0	;RGBx mode
	move.w	#DSPLBRTNS,d1
	CALLTCS	SetRGBxMode	;set front playfield palette

*******************************************************************************
* picture for Dual Cross Playfield

	movea.l	CmdLnArgs+8,a0	;ILBM2 filename address
	tst.l	a0
	bne.s	.ld2	;if filename specified...
	lea.l	Pic2FlNm,a0	;default ILBM2
.ld2	suba.l	a1,a1
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...
	move.l	d0,ILBM2	;store ILBMInfo structure pointer

*******************************************************************************
* Cross Playfield / exit

	movea.l	DIAdr,a0	;back playfield DI
	movea.l	FPfldDI,a1	;front playfield DI
	CALLTCS	EnbXPfld	;enable Cross Playfield

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



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

_PrgMain	movea.l	FPfldDI,a0
	CALLTCS	ShwDspl	;show front playfield, too
	movea.l	DIAdr,a0	;render back playfield
	CALLTCS	CPUFRPass0	;to make pixels visible
	movea.l	FPfldDI,a0	;render front playfield
	CALLTCS	CPUFRPass0	;to make pixels visible

*******************************************************************************
* first cross-fade: the front playfield disappears behind the back playfield

	movea.l	FPfldDI,a2
	move.w	#256,d2	;opacity at beginning (max)
.XFade0	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.w	d2,d0
	CALLTCS	SetFPfldOpct	;change opacity
	btst.b	#6,$bfe001	;exit if LMB
	dbeq	d2,.XFade0	;decrease opacity and repeat other 256 times

*******************************************************************************
* second cross-fade: a new picture is written to the front playfield (still
* hidden) and then shown with a cross-fade-in (Dual mode)

	movea.l	([ILBM2.l],TCS_II_GfxAdr.w),a0 ;ILBM2 gfx data adr
	movea.l	(TCS_DI_CSAdr,a2),a1	;front playfield screen address
	move.w	#SCRWD*SCRHT/4-1,d0	;copy ILBM2
.CpyPxl	move.l	(a0)+,(a1)+	;to front playfield
	dbra	d0,.CpyPxl	;screen

	movea.l	a2,a0	;render front playfield
	CALLTCS	CPUFRPass0	;to make pixels visible

	movea.l	a2,a0	;our display
	moveq.l	#15,d0	;RGBx transparent color
	CALLTCS	EnbDXPfld	;enable Dual modality

	move.w	#256,d2	;counter
.XFade1	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.w	#256,d0	;calculate new
	sub.w	d2,d0	;opacity
	CALLTCS	SetFPfldOpct	;change opacity
	btst.b	#6,$bfe001	;exit if LMB
	dbeq	d2,.XFade1	;repeat other 256 times

*******************************************************************************
* first fade: the front playfield is faded out, while the back playfield
* remains visible

	move.b	(TCS_DI_VdoMode+3,a2),d3	;get RGBx mode
	move.w	#256,d2	;brightness at beginning (max)
.FadeOut0	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,.FadeOut0	;decrease brightness and repeat other 256 times

*******************************************************************************
* second fade: the front playfield is faded in, while the back playfield is
* still visible

	move.b	(TCS_DI_VdoMode+3,a2),d3	;get RGBx mode
	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	.FadeOut	;if LMB...
	cmpi.w	#256,d2
	bls.s	.FadeIn	;repeat other 256 times

*******************************************************************************
* third cross-fade: the front playfield is half-cross-faded-out

	movea.l	FPfldDI,a2	;front playfield DI structure pointer
	move.w	#128,d2	;counter
.XFade3	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.w	d2,d0	;calculate
	addi.w	#128,d0	;new opacity
	CALLTCS	SetFPfldOpct	;change opacity
	btst.b	#6,$bfe001	;exit if LMB
	dbeq	d2,.XFade3	;decrease opacity and repeat other 128 times

*******************************************************************************
* third fade: the back playfield is faded out, while the front playfield
* remains visible

.FadeOut	movea.l	DIAdr,a2	;back playfield DI structure pointer
	move.b	(TCS_DI_VdoMode+3,a2),d3	;get RGBx mode
	move.w	#256,d2	;brightness at beginning (max)
.FadeOut1	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,.FadeOut1	;decrease brightness and repeat other 256 times

*******************************************************************************
* fourth cross-fade: the front playfield is half-cross-faded-in

	movea.l	FPfldDI,a2	;front playfield DI structure pointer
	move.w	#128,d2	;counter
.XFade4	WTRSTLN	DSPLY0	;wait for Copper to write the palette
	movea.l	a2,a0	;our display
	move.w	#256,d0	;calculate
	sub.w	d2,d0	;new opacity
	CALLTCS	SetFPfldOpct	;change opacity
	btst.b	#6,$bfe001	;exit if LMB
	dbeq	d2,.XFade4	;decrease opacity and repeat other 128 times

*******************************************************************************
* final fade: the front playfield is faded out, too

	movea.l	FPfldDI,a2	;front playfield DI structure pointer
	move.b	(TCS_DI_VdoMode+3,a2),d3	;get RGBx mode
	move.w	#256,d2	;brightness at beginning (max)
.FadeOut2	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,.FadeOut2	;decrease brightness and repeat other 256 times

*******************************************************************************
* end

.exit	movea.l	FPfldDI,a0	;hide front playfield,
	CALLTCS	HideDspl	;otherwise it can't be freed!
	rts



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

_PrgClnUp	movea.l	ILBM2,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for ILBM2
	movea.l	DIAdr,a0
	CALLTCS	DsbXPfld	;disable Cross Playfield
	movea.l	FPfldDI,a0	;free all the memory allocated
	CALLTCS	FreeDspl	;for the front playfield
	rts



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

	include	/includes/dat.i

	cnop	0,4
FPfldDI	dc.l	0	;front playfield DI structure address
ILBM2	dc.l	0	;ILMBInfo structure pointer
tmplt	dc.b	"ILBM0/K,ILBM1/K,ILBM2/K",0 ;template for ReadArgs()
Pic0FlNm	dc.b	"/pix/TM_rgbm.iff",0	;default ILBM #0 (back playfield)
Pic1FlNm	dc.b	"/pix/Alphonse_rgbh.iff",0 ;default ILBM #1 (front playfield)
Pic2FlNm	dc.b	"/pix/DXP_rgbh.iff",0	;default ILBM #2 (for Dual Cross Playfield)
