*******************************************************************************
* ILBM loading and remapping example
*******************************************************************************
* INFO	loads a 320x256x8 ILBM with a non-RGBx palette and remaps it
*	to a 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 quit
*******************************************************************************

	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	;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	=	2	;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,a0
	movea.l	PicIIAdr,a3	;picture ILBMInfo structure address

	movea.l	CmdLnArgs+4,a1
	tst.l	a1
	beq.s	.SuggRGBxMode	;if user didn't specify a mode...
	move.l	(a1),d3	;RGBx mode wanted by user
	bra.s	.SetRGBxMode
.SuggRGBxMode	move.b	(TCS_II_RGBxMode,a3),d3	;RGBx mode suggested for the picture

.SetRGBxMode	move.b	d3,d0	;RGBx mode
	move.w	#256,d1
	CALLTCS	SetRGBxMode	;set mode for display

	movea.l	(TCS_II_GfxAdr,a3),a1	;address of picture graphics
	movea.l	(TCS_II_PalAdr,a3),a2	;address of picture palette (CLUT)
	move.b	d3,d0	;RGBx mode
	move.w	#SCRWD,d1
	move.w	#SCRHT,d2
	movea.l	a1,a0	;source & destination are the same!
	CALLTCS	CLUTPicToRGBx	;convert to RGBx

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

	WTLMB
	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,RGBxMODE=R/N",0 ;template for ReadArgs()
PicFlNm	dc.b	"/pix/Tiger Mask.iff",0	;default picture
