;-------T-------T------------------------T----------------------------------;
;Name:      Resource Tracking Demonstration
;Author:    Paul Manias
;Copyright: DreamWorld Productions (c) 1996-1997.  Freely distributable.
;
;This is the same as the RamboWorm demo but it doesn't free any of its
;resources!  This would normally crash our machine but GMS can recover from
;such an instance and give back everything that was allocated.
;
;Run it and be amazed at the fact that your system is still safe :-)


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

	SECTION	"NastyCode",CODE

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

Start:	STARTGMS

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

	CALL	AllocAudio
	tst.l	d0
	bne	.Error_Audio

	lea	BackgroundFile(pc),a1
	moveq	#GETPALETTE|VIDEOMEM,d0
	CALL	LoadPicFile
	move.l	d0,PIC_Background
	beq	.Error_Picture

	CALL	GetScreen
	move.l	d0,Screen
	beq	.Error_GetScreen

	move.l	PIC_Background(pc),a0
	move.l	Screen(pc),a1
	move.l	#DBLBUFFER,GS_Attrib(a1)
	move.l	PIC_Data(a0),GS_MemPtr1(a1)
	CALL	CopyStructure

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

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

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

	move.l	Screen(pc),a0	;a0 = GameScreen.
	move.l	GS_Bitmap(a0),a0	;a0 = Bitmap for restore list.
	moveq	#2,d0	;d0 = 2 buffers.
	moveq	#1,d1	;d1 = 1 Bob Entry.
	CALL	InitRestore	;>> = Initialise the restore list.
	tst.l	d0	;d0 = Check for errors.
	beq.s	.Error_RestoreList	;>> = Error, exit.

;---------------------------------------------------------------------------;
;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		;Everything that should be freed
;	move.l	GMSBase(pc),a6	;has been commented out.  We won't
;	move.l	SND_Rambo(pc),a0	;crash though, thanks to resource
;	CALL	FreeSound	;tracking.

.Error_Sound
;	move.l	BOB_Rambo(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
.Error_AddScreen
;	move.l	Screen(pc),a0
;	CALL	DeleteScreen
.Error_GetScreen
;	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:	CALL	InitJoyPorts
	moveq	#$00,d7

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

	move.l	BOB_Rambo(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
	clr.w	BOB_Frame(a1)
	clr.b	FireState
	move.l	SND_Rambo(pc),a0
	CALL	PlaySound

.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
;===========================================================================;

Screen:		dc.l  0
PIC_Background:	dc.l  0

BackgroundFile:
	dc.b	"GMS:demos/data/PIC.Green",0
	even

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

BBT_Rambo:
	dc.l	TAGS_BOB
BOB_Rambo:
	dc.l	0
	dc.l	BBA_GfxCoords,.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,RESTORE|GENMASKS|CLIP
	dc.l	BBA_PictureTags,.picture
	dc.l	TAGEND

.frames	dc.w	  0,00   ;0 GraphicX / GraphicY
	dc.w	 32,00   ;1 ...
	dc.w	 64,00   ;2
	dc.w	 96,00   ;3
	dc.w	128,00   ;4
	dc.w	160,00   ;5
	dc.w	192,00   ;6
	dc.w	224,00   ;7
	dc.w	256,00   ;8
	dc.w	288,00   ;9
	dc.w	  0,48   ;10
	dc.w	 32,48   ;11
	dc.w	 64,48   ;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

