
; Sprite Example 11:		Demonstrating Sprite/Sprite Collisions
;				Sprites 0,1 & 2. Sprite 1 collision enabled

		include		hardware.i
		include		HW_Macros.i
		include		HW_Start.i
		include		HW_Sprites.i

; Define limits on movement

MIN_X		=		128
MAX_X		=		128+320-16
MIN_Y		=		44
MAX_Y		=		44+256-20

; Define extension to sprite structure

spr_dX		=		spr_Ext
spr_dY		=		spr_dX+2

; Stuff bitplane pointers into Copper list using a macro

Main		COPBPL		CopPlanes,bitplane,(320/8)*256,1

; Call routine that sets all sprite pointers to a blank sprite

		lea		SprtBase,a0
		bsr		SpritesOff

; Position sprite

		lea		Sprite1,a0
		bsr		SetSprPos
		lea		Sprite2,a0
		bsr		SetSprPos
		lea		Sprite3,a0
		bsr		SetSprPos

; Set sprite pointer 0 in copper list. Not that by omitting 3rd parameter,
;macro will assume SprtBase!

		SPRITEON	#0,#Sprite1		Turn sprite on
		SPRITEON	#1,#Sprite2
		SPRITEON	#2,#Sprite3

; Enable bitplane, Copper and sprite DMA.

		move.w		#SETIT!DMAEN!COPEN!BPLEN!SPREN,DMACON(a5)

		move.w		#$1000,CLXCON(a5)	enable Sprite 1

; Install vert blank interrupt handler and enable it

		move.l		#Level3,$6c
		move.w		#SETIT!INTEN!VERTB,INTENA(a5)

; Now strobe the Copper list.

		move.l		#CopperList,COP1LCH(a5)	address of list
		move.w		#0,COPJMP1(a5)		strobe Copper

; Wait for user to press the left mouse button

mouse		btst		#6,CIAAPRA
		bne.s		mouse

; And exit.

		rts

;		***************************
;		*   Vert Blank Interrupt  *
;		***************************

Level3		lea		$dff000,a5		a5->hardware base

; Check and act on collisions

		bsr		CheckCollision

; Update sprite positions

		lea		Sprite1,a0
		bsr		SetSprPos
		lea		Sprite2,a0
		bsr		SetSprPos
		lea		Sprite3,a0
		bsr		SetSprPos

		SPRITEON	#0,#Sprite1
		SPRITEON	#1,#Sprite2
		SPRITEON	#2,#Sprite3

; Animate the sprite

		ANIMATESPRITE	#Sprite1
		ANIMATESPRITE	#Sprite2
		ANIMATESPRITE	#Sprite3

; Move and update sprite

		lea		Sprite1,a0
		bsr		Boing
		lea		Sprite2,a0
		bsr		Boing
		lea		Sprite3,a0
		bsr		Boing

; Clear the interrupt request

.done		move.w		INTREQR(a5),d0		get bits
		and.w		#VERTB!COPER!BLIT,d0	mask level 3 bits
		move.w		d0,INTREQ(a5)		clear request
		
		rte					back to user mode

;		***************************
;		*     Move The Sprite	  *
;		***************************

;Entry		a0->Sprite structure

Boing		move.w		spr_X(a0),d0
		add.w		spr_dX(a0),d0
		
		cmp.w		#MIN_X,d0
		blt.s		.BoingX
		cmp.w		#MAX_X,d0
		bge.s		.BoingX
		
		move.w		d0,spr_X(a0)
		bra.s		.DoY

.BoingX		neg.w		spr_dX(a0)

.DoY		move.w		spr_Y(a0),d0
		add.w		spr_dY(a0),d0
		
		cmp.w		#MIN_Y,d0
		blt.s		.BoingY
		cmp.w		#MAX_Y,d0
		bge.s		.BoingY
		
		move.w		d0,spr_Y(a0)
		bra.s		.Done

.BoingY		neg.w		spr_dY(a0)

.Done		rts

;		***************************
;		* Check Sprite Collisions *
;		***************************

CheckCollision	move.w		CLXDAT(a5),d0		read & clear coll bits

		tst.w		ColFlag
		beq.s		TestForColl
		subq.w		#1,ColFlag
		bra.s		NoColl

TestForColl	and.w		#$0200,d0		Sprites 0&1 with 2&3?
		beq.s		NoColl			nope!, skip the rest!

; Disable collision checking for a while, gives sprites time to separate!

		move.w		#8,ColFlag		set timer

; Reverse dX, dY values for sprite 1


		lea		Sprite1,a0		a0->3rd Sprite
		neg.w		spr_dX(a0)
		neg.w		spr_dY(a0)

; Reverse dX, dY values for sprite 2


		lea		Sprite2,a0		a0->3rd Sprite
		neg.w		spr_dX(a0)
		neg.w		spr_dY(a0)

; Reverse dX, dY values for sprite 3


		lea		Sprite3,a0		a0->3rd Sprite
		neg.w		spr_dX(a0)
		neg.w		spr_dY(a0)

NoColl		rts

;		***************************
;		*    Blank All Sprites	  *
;		***************************

; Entry		a0->SprBase for relevant Copper list

SpritesOff	move.l		#BlankSprite,d0		blank sprite
		moveq.l		#7,d1			sprite counter

_sprLoop	swap		d0			get high part of addr
		move.w		d0,2(a0)		into Copper list
		swap		d0			get low part of addr
		move.w		d0,6(a0)		into Copper list
		addq.l		#8,a0			bump Copper list ptr
		dbra		d1,_sprLoop
		
		rts

;		***************************
;		*	   Data    	  *
;		***************************

ColFlag		dc.w		0

Sprite1		dc.w		130		X
		dc.w		50		Y
		dc.w		20		height
		dc.l		sprite1aData	address of initial data
		dc.l		Sprite1Anim	->Anim structure
		dc.w		0		current frame number
		dc.w		1		initial timer value (0 Secs)
		dc.w		1		dX -- our extension
		dc.w		1		dY -- our extension

Sprite1Anim	dc.w		5		animate after 5 VBLs
		dc.w		4		4 frames
		dc.l		sprite1aData	address of 1st frame
		dc.l		sprite1bData	address of 2nd frame
		dc.l		sprite1cData	address of 3rd frame
		dc.l		sprite1dData	address of 4th frame

Sprite2		dc.w		330		X
		dc.w		250		Y
		dc.w		20		height
		dc.l		sprite2bData	address of initial data
		dc.l		Sprite2Anim	->Anim structure
		dc.w		1		current frame number
		dc.w		50*5		initial timer value (5 Secs)
		dc.w		-1		dX -- our extension
		dc.w		-1		dY -- our extension

Sprite2Anim	dc.w		12		animate after 5 VBLs
		dc.w		4		4 frames
		dc.l		sprite2aData	address of 1st frame
		dc.l		sprite2bData	address of 2nd frame
		dc.l		sprite2cData	address of 3rd frame
		dc.l		sprite2dData	address of 4th frame

Sprite3		dc.w		230		X
		dc.w		150		Y
		dc.w		20		height
		dc.l		sprite3dData	address of initial data
		dc.l		Sprite3Anim	->Anim structure
		dc.w		3		current frame number
		dc.w		50*10		initial timer value (10 Secs)
		dc.w		-2		dX -- our extension
		dc.w		-2		dY -- our extension

Sprite3Anim	dc.w		2		animate after 5 VBLs
		dc.w		4		4 frames
		dc.l		sprite3aData	address of 1st frame
		dc.l		sprite3bData	address of 2nd frame
		dc.l		sprite3cData	address of 3rd frame
		dc.l		sprite3dData	address of 4th frame

;		***************************
;		*     CHIP Memory Data    *
;		***************************

section		data custom,chip		****  Use this for A68K  ****

;		Section		custom,data_C	**** Use this for Devpac ****

CopperList	CMOVE		COLOR00,$0000		Black background

		CWAIT		0,20			build in a pause!

SprtBase	CMOVE		SPR0PTH,0		Sprite defs
		CMOVE		SPR0PTL,0
		CMOVE		SPR1PTH,0
		CMOVE		SPR1PTL,0
		CMOVE		SPR2PTH,0
		CMOVE		SPR2PTL,0
		CMOVE		SPR3PTH,0
		CMOVE		SPR3PTL,0
		CMOVE		SPR4PTH,0
		CMOVE		SPR4PTL,0
		CMOVE		SPR5PTH,0
		CMOVE		SPR5PTL,0
		CMOVE		SPR6PTH,0
		CMOVE		SPR6PTL,0
		CMOVE		SPR7PTH,0
		CMOVE		SPR7PTL,0
		
		CMOVE		DIWSTRT,$2c81		PAL -- 256 lines
		CMOVE		DIWSTOP,$2cc1
		CMOVE		DDFSTRT,$0038		LoRes
		CMOVE		DDFSTOP,$00d0
		CMOVE		BPL1MOD,$0000		No modulos
		CMOVE		BPL2MOD,$0000
		CMOVE		BPLCON0,$1200		1 bitplane & colour
		CMOVE		BPLCON1,$0000		No scrolling
		CMOVE		BPLCON2,%0000000000001000		pf1=1
;                                                 P222111

CopPlanes	CMOVE		BPL1PTH,0		Bit plane pointer
		CMOVE		BPL1PTL,0

		CMOVE		COLOR00,$0000		black background
		CMOVE		COLOR01,$0fff		white foreground
		CMOVE		COLOR16,$0000		black
		CMOVE		COLOR17,$0fff		white
		CMOVE		COLOR18,$000d		blue
		CMOVE		COLOR19,$0e00		red
		
		CEND					end of list

bitplane	ds.b		40*256

BlankSprite	dc.w		0,0		non-exsistent sprite!

; Sprite Data for 4 colour sprite, width=16, height=20

sprite1aData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite1bData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$3FF8,$1FF0,$3FF8,$1FF0
		dc.w	$3FF8,$1FF0,$3FF8,$1FF0,$3FF8,$1FF0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite1cData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1F70,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite1dData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$0770,$03E0,$03F8,$01F0,$01F8,$00F0
		dc.w	$00F8,$0070,$01F8,$00F0,$03F8,$01F0,$07F0,$03E0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite2aData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite2bData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$3FF8,$1FF0,$3FF8,$1FF0
		dc.w	$3FF8,$1FF0,$3FF8,$1FF0,$3FF8,$1FF0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite2cData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1F70,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite2dData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$0770,$03E0,$03F8,$01F0,$01F8,$00F0
		dc.w	$00F8,$0070,$01F8,$00F0,$03F8,$01F0,$07F0,$03E0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite3aData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite3bData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1EF0,$0FE0,$3FF8,$1FF0,$3FF8,$1FF0
		dc.w	$3FF8,$1FF0,$3FF8,$1FF0,$3FF8,$1FF0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite3cData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$1F70,$0FE0,$0FF8,$07F0,$07F8,$03F0
		dc.w	$03F8,$01F0,$07F8,$03F0,$0FF8,$07F0,$1FF0,$0FE0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer

sprite3dData	dc.w		0 		1st control word
		dc.w		0 		2nd control word
	
		dc.w	$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$07C0,$0000
		dc.w	$0FE0,$07C0,$0770,$03E0,$03F8,$01F0,$01F8,$00F0
		dc.w	$00F8,$0070,$01F8,$00F0,$03F8,$01F0,$07F0,$03E0
		dc.w	$0FE0,$07C0,$07C0,$0000,$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000,$0000,$0000

		dc.l		0		next sprite pointer


		end

		
