*******************************************************************************
* 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 the
*	  320x256x8 IFF/ILBM 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!)
*	- LMB to quit
*******************************************************************************

	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	;min brightness
GFXCTXT	=	0
CWBTM	=	0
CWTOP	=	0
CWRT	=	0
CWLF	=	0
VDOMODE	=	TCS_VM_RGBW | TCS_VMf_FullRes ;TCS display video mode

ARGSNO	=	1	;command line arguments no.



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

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



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

_PrgInit	movea.l	CmdLnArgs,a0	;ILBM filename address
	tst.l	a0
	bne.s	.ld	;if filename specified...
	lea.l	PicFlNm,a0	;default ILBM
.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,ILBMStrcAdr	;else store its ILBM structure address
	rts		;& clr ccr.Z to signal success
.fail	move.w	#4,ccr	;signal error
	rts



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

_PrgMain	movea.l	ILBMStrcAdr,a3	;get ILBM structure address

	movea.l	DIAdr,a0
;	move.b	(TCS_II_RGBxMode,a0),d0	;get suggested RGBx mode
	move.b	#TCS_VM_RGB332,d0	;(but TCS_CLUTPicToRGBx() doesn't support all modes)
	move.w	#256,d1
	CALLTCS	SetRGBxMode

	movea.l	(TCS_II_GfxAdr,a3),a1	;get address of ILBM's graphic data
	movea.l	(TCS_II_PalAdr,a3),a2	;get address of ILBM's palette (CLUT)
;	move.b	(TCS_II_RGBxMode,a3),d0	;get suggested RGBx mode
	move.b	#TCS_VM_RGB332,d0	;(but TCS_CLUTPicToRGBx() doesn't support all modes)
	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	ILBMStrcAdr,a0	;free all the memory
	CALLTCS	UnLdILBM	;allocated for the ILBM
	rts



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

	include	/includes/dat.i

	cnop	0,4
ILBMStrcAdr	dc.l	0	;ILBM structure pointer
tmplt	dc.b	"ILBM/K",0	;template for ReadArgs()
PicFlNm	dc.b	"/pix/Alphonse.iff",0	;default ILBM
