;-------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.
;
;All structures defined externally.  Look at the end of the source and see
;BlitWormOBJData.s.

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

	SECTION	"Bob",CODE

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

	STARTGMS

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	GMSBase(pc),a6	;Activate user preferences.
	lea	OBJ_FileName(pc),a0	;a0 = Object FileName.
	CALL	LoadObjectFile	;>> = Go and load them.
	move.l	d0,OBJ_Base	;MA = Save the Object Base.
	beq	.Error_ObjectFile	;>> = If error.

	move.l	d0,a0	;a0 = Object Base.
	lea	Objects(pc),a1	;a1 = A list of objects to get.
	CALL	GetObjectList	;>> = Get our objects.
	tst.l	d0	;d0 = Check for error.
	bne	.Error_Objects	;>> = Quit if error.

	CALL	AllocBlitter	;Allocate the blitter.
	tst.l	d0
	bne	.Error_Blitter

	CALL	AllocAudio
	tst.l	d0
	bne	.Error_Audio

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

;---------------------------------------------------------------------------;
;Load a picture into the background of the screen.

	move.l	PIC_Background(pc),a1	;Load the picture.
	move.l	GS_MemPtr1(a0),PIC_Data(a1)
	CALL	LoadPic
	tst.l	d0
	beq.s	.Error_Picture

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

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

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

	move.l	Screen(pc),a0
	moveq	#$00,d0
	moveq	#1,d1	;1 BOB Entry.
	CALL	InitRestore
	tst.l	d0
	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
	move.l	BOB_Worm(pc),a1	;ready for drawing.
	CALL	InitBob
	tst.l	d0
	beq.s	.Error_Bob

	move.l	SND_Rambo(pc),a0
	CALL	InitSound
	tst.l	d0
	beq.s	.Error_Sound

	move.l	Screen(pc),a0
	CALL	ShowScreen

	CALL	InitJoyPorts

	bsr.s	Main

.ReturnToDOS
	move.l	GMSBase(pc),a6
	move.l	SND_Rambo(pc),a0
	CALL	FreeSound
.Error_Sound
	move.l	BOB_Worm(pc),a1
	CALL	FreeBob
.Error_Bob
	move.l	Screen(pc),a0
	move.l	GS_Bitmap(a0),a0
	CALL	FreeRestore
.Error_Restorelist
	move.l	PIC_Background(pc),a1
	CALL	FreePic
.Error_Picture
	move.l	Screen(pc),a0
	CALL	DeleteScreen
.Error_Screen
	CALL	FreeAudio
.Error_Audio
	CALL	FreeBlitter
.Error_Blitter
.Error_Objects
	move.l	OBJ_Base(pc),a0
	CALL	FreeObjectFile
.Error_ObjectFile
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#ERR_OK,d0
	rts

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

SPEED	=	5
FIRESPEED =	1

Main:	moveq	#$00,d7
.Loop	move.l	Screen(pc),a0
	move.l	GS_Bitmap(a0),a0
	CALL	Restore

	move.l	BOB_Worm(pc),a1
	CALL	DrawBob	;Blit the bob.

	CALL	WaitVBL	;Wait for VBL.

	move.l	Screen(pc),a0
	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
;===========================================================================;

OBJ_Base:	dc.l  0
OBJ_FileName:	dc.b  "GMS:demos/data/OBJ.Worm",0
		even

Objects:	dc.l  OBJECTLIST,0
		dc.l  TXT_Worm
BOB_Worm:	dc.l  0
		dc.l  TXT_SNDRambo
SND_Rambo:	dc.l  0
		dc.l  TXT_GameScreen
Screen:		dc.l  0
		dc.l  TXT_Background
PIC_Background: dc.l  0
		dc.l  LISTEND

TXT_Worm:	dc.b  "Worm",0
TXT_BOBPicture:	dc.b  "BOBPicture",0
TXT_SNDRambo:	dc.b  "SNDRambo",0
TXT_GameScreen: dc.b  "GameScreen",0
TXT_Background: dc.b  "Background",0
		even

