;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 SS_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+

	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

	move.l	GMS_Base(pc),a6          ;Run this demo at a user-selected
	CALL	SetUserPri               ;priority.

	lea	ScreenStruct(pc),a0      ;Initialise our screen for use.
	CALL	Add_Screen
	tst.l	d0
	bne.s	Error

	CALL	Show_Screen              ;Show our screen.

	lea	SparkieStruct(pc),a1
	CALL	Init_Sprite
	CALL	Update_Sprite            ;First get him onto the screen.

	moveq	#JPORT1,d0               ;Read from port 1
	CALL	Read_Mouse               ;Initialise the port.

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

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               ;Allow screen-switching.
	CALL	Update_Sprite            ;Put Sparkie on the screen.

	btst	#MB_LMB,d0
	beq.s	Loop

;===========================================================================;
;                              RETURN TO DOS
;===========================================================================;

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

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

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

SparkieStruct:
	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|XLONG       ;Attributes.
	dc.w	0                 ;PlayField position.
	dc.l	0,0               ;Private.

AMT_PLANES =	1

ScreenStruct:
	dc.l	"GSV1",0
	dc.l	0,0,0             ;Screen_Mem1/2/3
	dc.l	0                 ;Screen link.
	dc.l	ScreenPalette     ;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,320,256   ;Screen & Pic Height/Width
	dc.w	AMT_PLANES        ;Amt_Planes
	dc.w	0,0               ;X/Y Top Of Screen
	dc.w	0,0               ;X/Y Pic Offsets (for scrolling).
	dc.l	SPRITES|NOSPRBDR  ;Special attributes.
	dc.w	LORES             ;Screen mode.
	dc.b	INTERLEAVED       ;Screen type
	dc.b	0                 ;Reserved.
	even

ScreenPalette:
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0688,$0466,$0344,$0CC0,$0980,$0870,$0650
	dc.w	$01C2,$0050,$0B0B,$0606,$0F20,$0910,$0BBB,$0FFF
	dc.w	$00BF,$0068,$0568,$09BF,$0FF0,$0EE0,$0BA0,$0540

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

	SECTION	"Graphics",DATA_C

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