;-------T-------T------------------------T----------------------------------;
;Bob example
;-----------
;This example blits a Worm from an IFF file onto a double buffered screen.
;The RESTORE mode is used to put the background back.

	INCDIR	"INCLUDES:"
	INCLUDE	"games/games_lib.i"
	INCLUDE	"games/games.i"

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	SECTION	"RamboWorm",CODE

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

	STARTGMS

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	GMSBase(pc),a6	;Activate user preferences.
	CALL	AllocBlitter
	tst.l	d0
	bne	.Error_Blitter

	CALL	AllocAudio
	tst.l	d0
	bne	.Error_Audio

	lea	ScreenTags(pc),a0	;Initialise the screen.
	CALL	AddScreen
	tst.l	d0
	beq	.Error_Screen

;---------------------------------------------------------------------------;
;Load a picture into the background of the screen.  We have to set the correct
;palette and copy it to our second buffer as part of this process.

	move.l	Screen(pc),a0
	lea	PIC_BackgroundTags(pc),a1
	move.l	GS_MemPtr2(a0),Data
	CALL	LoadPic
	tst.l	d0
	beq.s	.Error_Picture

	move.l	PIC_Background(pc),a1
	move.l	PIC_Palette(a1),GS_Palette(a0)
	CALL	UpdatePalette

	move.l	Screen(pc),a0
	CALL	SwapBuffers
	moveq	#BUFFER1,d0
	moveq	#BUFFER2,d1
	CALL	CopyBuffer

;---------------------------------------------------------------------------;
;Initialise the restorelist.

	moveq	#2,d0	;2 buffers.
	moveq	#1,d1	;1 BOB Entry.
	CALL	InitRestore
	move.l	d0,RestoreList
	beq.s	.Error_RestoreList

;---------------------------------------------------------------------------;
;Load the bob file in.  This contains the graphics data that we are
;going to draw to the screen.

	move.l	Screen(pc),a0	;Initialise the BOB so that it is
	lea	BBT_Rambo(pc),a1	;ready for drawing.
	CALL	InitBob
	tst.l	d0
	beq.s	.Error_Bob

	lea	SMT_Rambo(pc),a0
	CALL	InitSound
	tst.l	d0
	beq.s	.Error_Sound

	move.l	Screen(pc),a0
	CALL	ShowScreen

	bsr.s	Main

.ReturnToDOS
	move.l	GMSBase(pc),a6
	move.l	SND_Rambo(pc),a0
	CALL	FreeSound
.Error_Sound
	move.l	BOB_Rambo(pc),a1
	CALL	FreeBob
.Error_BOB
	move.l	RestoreList(pc),d0
	CALL	FreeRestore
.Error_RestoreList
	move.l	PIC_Background(pc),a1
	CALL	FreePic
.Error_Picture
	move.l	Screen(pc),a0
	CALL	DeleteS creen
.Error_Screen
	CALL	FreeAudio
.Error_Audio
	CALL	FreeBlitter
.Error_Blitter
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#ERR_OK,d0
	rts

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

SPEED	=	5
FIRESPEED =	0

Main:	move.l	GMSBase(pc),a6
	CALL	InitJoyPorts
	moveq	#$00,d7

.Loop	move.l	Screen(pc),a0
	move.l	RestoreList(pc),a1
	CALL	Restore

	move.l	BOB_Rambo(pc),a1
	move.l	RestoreList(pc),a2
	moveq	#BUFFER2,d0
	CALL	DrawBOB	;Blit the bob.
	CALL	WaitVBL	;Wait for VBL.
	CALL	SwapBuffers
	addq.w	#1,d7

	tst.b	FireState
	bne.s	.FireOn

	cmp.w	#SPEED,d7
	ble.s	.Move
	moveq	#$00,d7
	addq.w	#1,BOB_Frame(a1)
	cmp.w	#9,BOB_Frame(a1)
	blt.s	.Move
	clr.w	BOB_Frame(a1)
	bra.s	.Move

.FireOn	cmp.w	#FIRESPEED,d7
	ble.s	.Move
	moveq	#$00,d7
	cmp.w	#10,BOB_Frame(a1)
	bge.s	.On
	move.w	#9,BOB_Frame(a1)

.On	addq.w	#1,BOB_Frame(a1)
	cmp.w	#13,BOB_Frame(a1)
	blt.s	.Move

	move.l	SND_Rambo(pc),a0
	CALL	PlaySound

	btst	#MB_LMB,d0
	beq.s	.Off
	move.w	#11,BOB_Frame(a1)
	bra.s	.Move

.Off	clr.w	BOB_Frame(a1)
	clr.b	FireState

.Move	moveq	#JPORT1,d0	;Read the mouse, then update the
	moveq	#JT_ZBXY,d1	;BOB's X and Y co-ordinates.
	CALL	ReadJoyPort
	move.w	d0,d1
	ext.w	d0
	add.w	d0,BOB_YPos(a1)
	asr.w	#8,d1
	add.w	d1,BOB_XPos(a1)

	btst	#MB_LMB,d0
	beq.s	.chkRMB
	st	FireState	;Set fire to on.
.chkRMB	btst	#MB_RMB,d0
	beq	.Loop
	rts

FireState:
	dc.b	0
	even

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

RestoreList:	dc.l  0

ScreenTags:
	dc.l	TAGS_GAMESCREEN
Screen:	dc.l	0
	dc.l	GSA_ScrWidth,320
	dc.l	GSA_ScrHeight,256
	dc.l	GSA_Planes,5
	dc.l	GSA_Attrib,DBLBUFFER
	dc.l	TAGEND

PIC_BackgroundTags:
	dc.l	TAGS_PICTURE
PIC_Background:
	dc.l	0
	dc.l	PCA_Data
Data:	dc.l	0
	dc.l	PCA_Width,320
	dc.l	PCA_Height,256
	dc.l	PCA_Planes,5
	dc.l	PCA_Options,GETPALETTE
	dc.l	PCA_File,.file
	dc.l	TAGEND
.file	dc.b	"GMS:demos/data/PIC.Green",0
	even

;---------------------------------------------------------------------------;

BBT_Rambo:
	dc.l	TAGS_BOB
BOB_Rambo:
	dc.l	0
	dc.l	BBA_Framelist,.frames
	dc.l	BBA_Width,32
	dc.l	BBA_Height,24
	dc.l	BBA_XCoord,150
	dc.l	BBA_YCoord,150
	dc.l	BBA_ClipLX,32
	dc.l	BBA_ClipRX,320-32
	dc.l	BBA_ClipTY,32
	dc.l	BBA_ClipBY,256-32
	dc.l	BBA_Attrib,GENMASKS|CLIP|RESTORE
	dc.l	BBA_PictureTags,.picture
	dc.l	TAGEND

.frames	dc.w	000,00,0,0   ;0 X/Y Graphic, X/Y Mask
	dc.w	032,00,0,0   ;1 ...
	dc.w	064,00,0,0   ;2
	dc.w	096,00,0,0   ;3
	dc.w	128,00,0,0   ;4
	dc.w	160,00,0,0   ;5
	dc.w	192,00,0,0   ;6
	dc.w	224,00,0,0   ;7
	dc.w	256,00,0,0   ;8
	dc.w	288,00,0,0   ;9
	dc.w	000,48,0,0   ;10
	dc.w	032,48,0,0   ;11
	dc.w	064,48,0,0   ;12
	dc.l	-1
.picture
	dc.l	TAGS_PICTURE,0
	dc.l	PCA_Planes,5
	dc.l	PCA_Options,BLITMEM
	dc.l	PCA_FILE,.file
	dc.l	TAGEND

.file	dc.b	"GMS:demos/data/PIC.Rambo",0
	even

SMT_Rambo:
	dc.l	TAGS_SOUND
SND_Rambo:
	dc.l	0
	dc.l	SA_Channel,CHANNEL1
	dc.l	SA_Octave,OCT_C2S
	dc.l	SA_Volume,100
	dc.l	SA_File,.file
	dc.l	TAGEND

.file	dc.b	"GMS:demos/data/SND.Rambo",0
	even

