;-------T-------T------------------------T----------------------------------;
;Circle Example
;--------------
;This an example of 3 circles blitted onto a planar screen at different
;levels.  Move the green circle over the bouncing circles to get an idea of
;the effect you can achieve with a planar screen.
;
;The screen is double buffered and interlaced, just to be a bit different
;from the other demos.  The circles are removed from the screen using the
;CLEAR mode and a Restorelist.

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

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	SECTION	"Circles",CODE

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

	STARTGMS

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

	lea	ScreenTags(pc),a0
	CALL	AddScreen
	tst.l	d0
	beq.s	.Error_Screen

	move.l	Screen(pc),a0
	lea	BOB_List(pc),a1
	CALL	InitBob
	tst.l	d0
	beq.s	.Error_BOB

	move.l	BOB_Circle1(pc),BLC1
	move.l	BOB_Circle2(pc),BLC2
	move.l	BOB_Circle3(pc),BLC3

	moveq	#2,d0	;d0 = 2 buffers.
	moveq	#3,d1	;d1 = 3 BOB Entries.
	CALL	InitRestore
	move.l	d0,Restorelist
	beq.s	.Error_Restorelist

	CALL	ShowScreen

	CALL	InitJoyPorts

	bsr.s	Main

.ReturnToDOS
	move.l	GMSBase(pc),a6
	move.l	Restorelist(pc),d0
	CALL	FreeRestore
.Error_Restorelist
	lea	BOB_List(pc),a1
	CALL	FreeBob
.Error_BOB
	move.l	GMSBase(pc),a6
	move.l	Screen(pc),a0
	CALL	DeleteScreen
.Error_Screen
	CALL	FreeBlitter
.Error_Blitter
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#ERR_OK,d0
	rts

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

Main:	moveq	#$00,d7
.loop	move.l	BOB_Circle1(pc),a1
	moveq	#JPORT1,d0
	moveq	#JT_ZBXY,d1
	CALL	ReadJoyPort	;Go get port status.
	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
	bne.s	.done

	move.l	Screen(pc),a0
	move.l	BOB_Circle2(pc),a1
	movem.w	Circle2XV(pc),d0/d1
	bsr.s	MoveBob
	movem.w	d0/d1,Circle2XV

	move.l	BOB_Circle3(pc),a1
	movem.w	Circle3XV(pc),d0/d1
	bsr.s	MoveBOB
	movem.w	d0/d1,Circle3XV

.draw	move.l	Screen(pc),a0
	move.l	Restorelist(pc),a1
	CALL	Restore	;Clear all buffered bob's.

	lea	BOB_List(pc),a1
	move.l	Restorelist(pc),a2
	move.w	#BUFFER2,d0
	CALL	DrawBobList	;Go and draw all circles.
	CALL	WaitVBL
	CALL	SwapBuffers
	bra.s	.loop
.done	rts

;===========================================================================;
;                              MOVE A BOB
;===========================================================================;
;Function: Moves a bob, bouncing it across the screen.
;Requires: a0 = GameScreen
;	   a1 = Bob to move.
;	   d0 = X Velocity
;	   d1 = Y Velocity
;Returns:  d0 = New X Velocity.
;	   d1 = New Y Velocity.

MoveBob:
	add.w	d0,BOB_XCoord(a1)
	add.w	d1,BOB_YCoord(a1)

.ChkLX	tst.w	BOB_XCoord(a1)
	bgt.s	.ChkTY
	neg.w	BOB_XCoord(a1)
	neg.w	d0

.ChkTY	tst.w	BOB_YCoord(a1)
	bgt.s	.ChkRX
	neg.w	BOB_YCoord(a1)
	neg.w	d1

.ChkRX	move.w	GS_PicWidth(a0),d2
	sub.w	BOB_Width(a1),d2
	cmp.w	BOB_XCoord(a1),d2
	bgt.s	.ChkBY
	move.w	d2,BOB_XCoord(a1)
	neg.w	d0

.ChkBY	move.w	GS_PicHeight(a0),d2
	sub.w	BOB_Height(a1),d2
	cmp.w	BOB_YCoord(a1),d2
	bgt.s	.done
	move.w	d2,BOB_YCoord(a1)
	neg.w	d1
.done	rts

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

Circle2XV: dc.w 3
Circle2YV: dc.w 2
Circle3XV: dc.w -2
Circle3YV: dc.w -4

Restorelist:
	dc.l	0

ScreenTags:
	dc.l	TAGS_GAMESCREEN
Screen:	dc.l	0
	dc.l	GSA_Palette,.palette
	dc.l	GSA_AmtColours,8
	dc.l	GSA_ScrWidth,640
	dc.l	GSA_ScrHeight,256
	dc.l	GSA_Attrib,DBLBUFFER
	dc.l	GSA_ScrMode,HIRES|LACED
	dc.l	GSA_ScrType,PLANAR
	dc.l	TAGEND
.palette
	dc.l	$000000,$f00000,$00f000,$f0f000,$0000f0,$f000f0,$00f0f0,$f0f0f0

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

BOB_List:
	dc.l	LIST
BLC1:	dc.l	TAGS_Circle1
BLC2:	dc.l	TAGS_Circle2
BLC3:	dc.l	TAGS_Circle3
	dc.l	LISTEND

TAGS_Circle1:
	dc.l	TAGS_BOB
BOB_Circle1:
	dc.l	0
	dc.l	BBA_Graphics,CircleGfx
	dc.l	BBA_Masks,CircleGfx
	dc.l	BBA_FrameList,.frame
	dc.l	BBA_Width,96
	dc.l	BBA_Height,83
	dc.l	BBA_XCoord,100
	dc.l	BBA_YCoord,40
	dc.l	BBA_FPlane,1
	dc.l	BBA_AmtPlanes,1
	dc.l	BBA_Attrib,CLIP|MASK|CLEAR|CLRNOMASK
	dc.l	TAGEND

.frame	dc.w	0,0,0,0
	dc.l	-1

TAGS_Circle2:
	dc.l	TAGS_BOB
BOB_Circle2:
	dc.l	0
	dc.l	BBA_Graphics,CircleGfx
	dc.l	BBA_Masks,CircleGfx
	dc.l	BBA_FrameList,.frame
	dc.l	BBA_Width,96
	dc.l	BBA_Height,83
	dc.l	BBA_XCoord,278
	dc.l	BBA_YCoord,86
	dc.l	BBA_FPlane,0
	dc.l	BBA_AmtPlanes,1
	dc.l	BBA_Attrib,CLEAR|CLRNOMASK
	dc.l	TAGEND

.frame	dc.w	0,0,0,0
	dc.l	-1

TAGS_Circle3:
	dc.l	TAGS_BOB
BOB_Circle3:
	dc.l	0
	dc.l	BBA_Graphics,CircleGfx
	dc.l	BBA_Masks,CircleGfx
	dc.l	BBA_FrameList,.frame
	dc.l	BBA_Width,96
	dc.l	BBA_Height,83
	dc.l	BBA_XCoord,450
	dc.l	BBA_YCoord,150
	dc.l	BBA_FPlane,2
	dc.l	BBA_AmtPlanes,1
	dc.l	BBA_Attrib,CLEAR|CLRNOMASK
	dc.l	TAGEND

.frame	dc.w	0,0,0,0
	dc.l	-1

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

	SECTION	"Graphics",DATA_C

CircleGfx:
	INCBIN	"GMS:demos/data/BOB_Circle.raw"

