;Wide Scroll & Sprite
;--------------------
;This opens an extra wide picture of 640x256 pixels and sticks an animated
;sprite on top of it.  Extremely large pictures are *totally* inadequate for
;games such as platformers and shoot'em-ups, because the picture size takes
;up  huge  amounts  of  memory.   However  for games like Skidmarks,
;Lemmings, Monkey Island, etc, such large screens can be a necessity.
;
;Moving the sprite to the edges of the screen will result in a sound.
;
;Press LMB to exit.

	opt	o+

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

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	SECTION	"WideScroll&Sprite",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	Quit

	move.l	GMS_Base(pc),a6          ;Set task at the user-specified
	CALL	SetUserPri               ;priority.

	CALL	AllocAudio
	tst.w	d0
	bne	DeleteScr

	lea	Sound_Gong(pc),a0
	CALL	InitSound
	tst.w	d0
	bne	ReturnToDOS

	lea	ScreenStruct(pc),a0
	CALL	Add_Screen
	tst.l	d0
	bne	Error

	lea	Picture(pc),a1           ;a1 = Picture struct.
	move.l	SS_MemPtr1(a0),PIC_Data(a1)
	lea	PicFile(pc),a0           ;a0 = Picture file.
	CALL	LoadPic
	tst.w	d0
	bne	ReturnToDOS

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

	CALL	Show_Screen

	moveq	#JPORT1,d0               ;Initialise the mouse port.
	CALL	Read_Mouse

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

	moveq	#$00,d6                  ;d6 = 00.
Loop:	moveq	#JPORT1,d0               ;Read from port 1
	CALL	Read_Mouse               ;Go get mouse movements.
	move.w	d0,d1
	asr.w	#8,d0
	add.w	d0,SPR_XPos(a1)
	ext.w	d1
	add.w	d1,SPR_YPos(a1)
	btst	#MB_LMB,d0
	bne.s	ReturnToDOS

	addq.w	#1,d7                    ;Animation/Frame delay, so that
	btst	#0,d7                    ; our anim does not run too fast!
	beq.s	Scroller
	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	Scroller
.reset	clr.w	SPR_Frame(a1)

Scroller:
	lea	ScreenStruct(pc),a0
	tst.w	d6
	beq.s	.Right
.Left	tst.w	SS_PicXOffset(a0)
	ble.s	.Right
	st	d6
	subq.w	#2,SS_PicXOffset(a0)
	bra.s	.scroll
.Right	cmp.w	#320,SS_PicXOffset(a0)
	bge.s	.Left
	moveq	#$00,d6
	addq.w	#2,SS_PicXOffset(a0)

.scroll	CALL	Move_Picture
	CALL	Wait_OSVBL
	CALL	Update_Sprite            ;Put Sparkie on the screen.

	cmp.w	#320-16,SPR_XPos(a1)
	bge.s	.play
	tst.w	SPR_XPos(a1)
	ble.s	.play
	tst.w	SPR_YPos(a1)
	ble.s	.play
	cmp.w	#256-16,SPR_YPos(a1)
	bge.s	.play
	bra	Loop

.play	lea	Sound_Gong(pc),a0
	CALL	PlaySoundPri
	bra	Loop

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

ReturnToDOS:
	move.l	GMS_Base(pc),a6
	lea	Sound_Gong(pc),a0
	CALL	FreeSound
	CALL	FreeAudio
DeleteScr:
	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

AMT_PLANES =	4

SparkieStruct:
	dc.l	"SPV1",0          ;Structure version.
	dc.w	0                 ;Number 0.
	dc.l	Gfx_Sparkie       ;Ptr to graphic.
	dc.w	150,150           ;Beginning X/Y position
	dc.w	0                 ;Current frame.
	dc.w	16,21             ;Width (16/32/64), Height.
	dc.w	16                ;Amt of colours.
	dc.w	16                ;Colour start in palette.
	dc.w	2                 ;Amt of planes.
	dc.w	HIRES|XLONG       ;Attributes.
	dc.w	0                 ;PlayField position.
	dc.l	0,0               ;Private.

ScreenStruct:
	dc.l	"GSV1",0
	dc.l	0,0,0             ;Screen_Mem1/2/3
	dc.l	0                 ;Screen link.
	dc.l	Palette           ;Address of palette
	dc.l	0                 ;Address of rasterlist.
	dc.l	32                ;Amt of colours in palette.
	dc.w	320,256,640,256   ;Screen & Pic Height/Width. 
	dc.w	AMT_PLANES        ;Amt_Planes
	dc.w	0,0               ;X/Y screen offset.
	dc.w	0,0               ;X/Y picture offset.
	dc.l	HSCROLL           ;Special attributes.
	dc.w	LORES             ;Screen mode.
	dc.b	INTERLEAVED       ;Screen type
	dc.b	0                 ;Reserved.
	even

Palette	dc.w	$0000,$0400,$0501,$0501,$0601,$0701,$0701,$0801
	dc.w	$0901,$0A01,$0B02,$0432,$0CC0,$0F00,$0211,$0880
	dc.w	$01C2,$0050,$0B0B,$0606,$0F20,$0910,$0BBB,$0FFF
	dc.w	$00BF,$0068,$0568,$09BF,$0FF0,$0EE0,$0BA0,$0540

PicFile	dc.b	"GAMESLIB:data/IFF.Pic640x256",0
	even

Picture	dc.l	"PCV1",0           ;Version header.
	dc.l	0                  ;Source data.
	dc.w	640,256            ;Width, Height.
	dc.w	AMT_PLANES         ;Amount of Planes.
	dc.l	32                 ;Amount of colours.
	dc.l	Palette            ;Source palette (remap).
	dc.w	LORES              ;Screen mode.
	dc.w	INTERLEAVED        ;Destination
	dc.l	0                  ;Parameters.

Sound_Gong:
	dc.l	"SMV1",0           ;Structure version.
	dc.w	CHANNEL1           ;Channel to play through.
	dc.w	0                  ;Priority.
	dc.l	0                  ;Sample header
	dc.l	0                  ;Sample address.
	dc.l	0                  ;Sample length.
	dc.w	OCT_C2             ;Sample period.
	dc.w	100                ;Sample volume.
	dc.l	SBIT8|SEMPTY       ;Sound is 8 bit.
	dc.l	Gong               ;Sound file.

Gong:	dc.b	"GAMESLIB:data/IFF.Lightning",0
	even

;===========================================================================;
;                              CHIP RAM DATA
;===========================================================================;

	SECTION	"Graphics",DATA_C

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