*******************************************************************************
* 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 other
*	  320x256x8 ILBMs 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!)
*	- 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
	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	=	256	;max 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	=	3	;command line arguments No.
TRNSPCOL	=	15	;Dual Cross Playfield transparent color



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

	include	/incl/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	;picture #0 filename address
	tst.l	a0
	bne.s	.ld0	;if filename specified...
	lea.l	Pic0FlNm,a0	;default picture #0
.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 picture'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	;picture #1 filename address
	tst.l	a0
	bne.s	.ld1	;if filename specified...
	lea.l	Pic1FlNm,a0	;default picture #1
.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 picture'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	;picture #2 filename address
	tst.l	a0
	bne.s	.ld2	;if filename specified...
	lea.l	Pic2FlNm,a0	;default picture #2
.ld2	suba.l	a1,a1
	CALLTCS	LdILBM	;load it
	cmpi.l	#TCS_PE_OK,d0	;success?
	blo.s	.fail	;if not...
	move.l	d0,pic2IIAdr	;store ILBMInfo structure address

*******************************************************************************
* 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)

	move.b	([pic2IIAdr.l],TCS_II_RGBxMode.w),d0 ;get picture #2 RGBx
	movea.l	a2,a0	;mode for front playfield
	move.w	#DSPLBRTNS,d1	;maximum brightness
	CALLTCS	SetRGBxMode	;set appropriate palette

	movea.l	([pic2IIAdr.l],TCS_II_GfxAdr.w),a0 ;picture #2 graphics address
	movea.l	(TCS_DI_CSAdr,a2),a1	;front playfield screen address
	move.w	#SCRWD*SCRHT/4-1,d0	;copy picture #2
.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
	move.b	#TRNSPCOL,d1
	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
	move.b	#TRNSPCOL,d1
	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
	move.b	#TRNSPCOL,d1
	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	pic2IIAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for picture #2
	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	/incl/dat.i

	cnop	0,4
FPfldDI	dc.l	0	;front playfield DI structure address
pic2IIAdr	dc.l	0	;picture #2 ILMBInfo structure address
tmplt	dc.b	"PICTURE0=P0,PICTURE1=P1,"
	dc.b	"PICTURE2=P2",0	;template for ReadArgs()
Pic0FlNm	dc.b	"/pix/Tiger Mask.rgbp.iff",0 ;default picture #0 (back playfield)
Pic1FlNm	dc.b	"/pix/Alphonse.rgbh.iff",0 ;default picture #1 (front playfield)
Pic2FlNm	dc.b	"/pix/DXP.rgbh_15.iff",0	;default picture #2 (for Dual Cross Playfield)
