;Sprite example
;--------------
;This example shows you how to set up a 16 colour OCS sprite with a doubled
;X axis (32 pixels width).  Those with hardware experience will know that it
;takes 4 sprite banks to do this successfully in OCS, which leaves you with
;another 4 banks to do with what you will.
;
;Notice that the screen is only 1 bitplane large, even though the sprite is
;16 colours.  This is because the sprite is independent of the screen
;bitmap.  You can access colours not in the screen by setting up a large
;palette and specifying the amount of colours in GS_AmtColours (in this
;case, we set 32 colours to access the sprite colour bank).
;
;Try moving the sprite around a bit with the mouse.  The LMB exits the demo.

	opt	o+,c+

	INCLUDE	"exec/exec_lib.i"
	INCLUDE	"games/games_lib.i"
	INCLUDE	"games/games.i"

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	SECTION	"Sprites",CODE

;===========================================================================;
;                             INITIALISE DEMO
;===========================================================================;

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	($4).w,a6
	lea	GMS_Name(pc),a1
	moveq	#$00,d0
	CALL	OpenLibrary
	move.l	d0,GMS_Base
	beq.s	.Error_GMS

	move.l	GMS_Base(pc),a6
	sub.l	a0,a0
	CALL	SetUserPrefs

	lea	Screen(pc),a0	;Initialise our screen for use.
	CALL	Add_Screen
	tst.l	d0
	bne.s	.Error_Screen
	CALL	Show_Screen	;Show our screen.

	lea	Sparkie(pc),a1
	CALL	Init_Sprite
	tst.l	d0
	bne.s	.Error_Sparkie

	bsr.s	Main

.ReturnToDOS
	move.l	GMS_Base(pc),a6
	lea	Sparkie(pc),a1
	CALL	Free_Sprite
.Error_Sparkie
	lea	Screen(pc),a0
	CALL	Delete_Screen	;Give back screen memory etc.
.Error_Screen
	move.l	GMS_Base(pc),a1
	move.l	($4).w,a6
	CALL	CloseLibrary
.Error_GMS
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#$00,d0
	rts

;===========================================================================;
;                                MAIN LOOP
;===========================================================================;

Main:	move.l	GMS_Base(pc),a6
	CALL	InitJoyPorts	;Initialise ports.
.loop	addq.w	#1,d7	;Animation/Frame delay, so that
	btst	#0,d7	; our anim does not run too fast!
	beq.s	.Mouse
	cmp.w	#5,SPR_Frame(a1)	;Do the animation for the sprite.
	beq.s	.reset	;(simply by increasing the frame
	addq.w	#1,SPR_Frame(a1)	; number).
	bra.s	.Mouse
.reset	clr.w	SPR_Frame(a1)

.Mouse	moveq	#JPORT1,d0	;Read from port 1
	CALL	Read_Mouse	;Go get mouse position.
	move.w	d0,d1
	asr.w	#8,d0
	add.w	d0,SPR_XPos(a1)
	ext.w	d1
	add.w	d1,SPR_YPos(a1)
	CALL	Wait_OSVBL	;Wait for a VBL.
	CALL	Update_Sprite	;Put Sparkie on the screen.
	btst	#MB_LMB,d0
	beq.s	.loop
	rts

;===========================================================================;
;                                  DATA
;===========================================================================;

GMS_Name:
	dc.b	"games.library",0
	even
GMS_Base:
	dc.l	0

Sparkie	dc.l	SPV1,0	;Structure version.
	dc.w	0	;Number 0.
	dc.l	Gfx_Sparkie	;Ptr to graphic.
	dc.w	100,100	;Beginning X/Y position
	dc.w	0	;Current frame.
	dc.w	16,21	;Width, Height.
	dc.w	16	;Amt of colours.
	dc.w	16	;Colour start in palette.
	dc.w	2	;Amt of planes.
	dc.w	LORES	;Resolution.
	dc.w	0	;PlayField position.
	dc.w	XLONG	;Special attributes.

AMT_PLANES =	1

Screen:	dc.l	GSV1,0
	dc.l	0,0,0	;Screen_Mem1/2/3
	dc.l	0	;Screen link.
	dc.l	.Palette	;Address of the screen palette.
	dc.l	0	;Address of a ColourList.
	dc.l	32	;Amt of colours in the palette.
	dc.w	320,256	;Screen Width and Height.
	dc.w	320/8,256	;Picture Width/8 and Height.
	dc.w	AMT_PLANES	;Amount of planes.
	dc.w	0,0	;X/Y screen offset.
	dc.w	0,0	;X/Y pixture offset.
	dc.l	SPRITES|NOSPRBDR	;Special attributes.
	dc.w	LORES|COL12BIT	;Screen mode.
	dc.b	INTERLEAVED	;Screen type
	dc.b	0	;Reserved.
	even

.Palette
	dc.w	$000,$000,$000,$000,$000,$000,$000,$000
	dc.w	$000,$688,$466,$344,$CC0,$980,$870,$650
	dc.w	$1C2,$050,$B0B,$606,$F20,$910,$BBB,$FFF
	dc.w	$0BF,$068,$568,$9BF,$FF0,$EE0,$BA0,$540

;===========================================================================;
;                         ALL CHIP RAM DATA HERE
;===========================================================================;

	SECTION	"Graphics",DATA_C

Gfx_Sparkie:
	INCBIN	"GAMESLIB:data/Sparkie.raw"
