*******************************************************************************
*                                                                             *
* VacBench.asm  Version 1.5 By Randy Jouett  02/8/88 (MetaCompost).           *
* This program is released into the pubic/public domain :-)                   *
* If you notice any bugs, please e-mail me at one of the following addresses: *
*                                                                             *
*               {ihnp4,cbosgd}!killer!usl!usl-pc!jpdres11                     *
*                      ut-sally!usl!usl-pc!jpdres11                           *
*           	        jpdres11%usl-pc@usl.CSNET                             *
*                                                                             *
*******************************************************************************

	TTL	VacBench.asm

	include	"macros.i"
	include	"intuition.i"
	include	"view.i"
	include	"gfx.i"
	include	"ibase.i"
	include	"rastport.i"
	include	"screens.i"
	include "memory.i"

	XLVO	OpenLibrary
	XLVO	CloseLibrary
	XLVO	OpenWindow
	XLVO	CloseWindow
	XLVO	ActivateWindow
	XLVO	ViewPortAddress
	XLVO	ReadPixel
	XLVO	WritePixel
	XLVO	BltBitMapRastPort
	XLVO	BltBitMap
	XLVO	SetAPen
	XLVO	GetMsg
	XLVO	ReplyMsg
	XLVO	SetPointer
	XLVO	ClearPointer
	XLVO	SetRGB4
	XLVO	AllocRaster
	XLVO	FreeRaster
	XLVO	AllocMem
	XLVO	FreeMem
	XLVO	InitBitMap
	XLVO	LoadRGB4

VACWIDTH	equ	16
VACHEIGHT	equ	46

* Open IntuitionBase and GfxBase
main:
	moveq	#0,d0			;Library version.
	lea.l	GfxName,a1		;Put address of graphics.library in a1.
	SYS	OpenLibrary,4		;Open graphics.library.
	move.l	d0,GBase		;Save library ptr.

	moveq	#0,d0			;Now do same for Intuition library
	lea.l	IntuiName,a1
	SYS	OpenLibrary
	move.l	d0,IBase

	move.l	IBase,a2
	move.l	ib_ActiveScreen(a2),a2		;Get ActiveScreen.
	lea.l	sc_ViewPort(a2),a4
	move.l	a4,svp				;The ViewPort.
	lea.l	sc_RastPort(a2),a2		;The RastPort.
	move.l	vp_ColorMap(a4),a4		;The ColorMap.

	move.l	cm_ColorTable(a4),a0		;The ColorTable.
	lea.l	ctable,a1
	moveq	#31,d0
1$:
	move.w	(a0)+,(a1)+
	dbf	d0,1$

	move.l	#32,count
	move.l	rp_BitMap(a2),a2		;The Bitmap.
	move.l	a2,bm1				;Save BitMap ptr.
	moveq	#0,d0
	move.b	bm_Depth(a2),d0			;Get Depth.
	move.l	d0,depth
	move.w	bm_BytesPerRow(a2),d0		;Calculate width.
	lsl.w	#3,d0
	move.l	d0,width
	moveq	#0,d0
	move.w	bm_Rows(a2),d0
	move.l	d0,height			;Save height.
	
	move.l	#bm_SIZEOF,d0
	move.l	#MEMF_CHIP!MEMF_CLEAR,d1	;Chip mem. Clear it out.
	SYS	AllocMem			;Allocate the mem.
	move.l	d0,bm2				;Save prt for new BitMap. 
	beq	cleanup

	move.l	bm2,a0				;Init the Bitmap
	move.l	depth,d0
	move.l	width,d1
	move.l	height,d2
	SYS	InitBitMap,GBase

	move.l	depth,d3
	subq.w	#1,d3
	move.l	bm2,a2
	lea.l	bm_Planes(a2),a3
alloc:
	move.l	width,d0
	move.l	height,d1
	SYS	AllocRaster
	move.l	d0,(a3)+
	dbf	d3,alloc

	move.l	bm1,a0				;Copy the RastPorts BitMap to
	moveq	#0,d0				;our new BitMap.
	moveq	#0,d1
	move.l	bm2,a1
	moveq	#0,d2
	moveq	#0,d3
	move.l	width,d4
	move.l	height,d5
	move.l	#$0c0,d6
	move.l	#$fff,d7
	move.l	#0,a2
	SYS	BltBitMap

	lea.l	newwin,a2			;Save Width & height in the
	move.l	width,d0			;proper fields.
	move.w	d0,nw_Width(a2)
	move.l	height,d0
	move.w	d0,nw_Height(a2)
	move.l	a2,a0
	SYS	OpenWindow,IBase		;Open the Window.
	move.l	d0,vacwin			;Save prt to mywin

	move.l	vacwin,a0			;Get the window's ViewPort.
	SYS	ViewPortAddress
	move.l	d0,vp				;Save vp.

	move.l	vacwin,a5			;Grab window's RastPort.
	move.l	wd_RPort(a5),d0
	move.l	d0,rp				;Save rp.

	move.l	bm2,a0				;Blit the new BitMap to our
	moveq	#0,d0				;window's RastPort.
	moveq	#0,d1
	move.l	rp,a1
	moveq	#0,d2
	moveq	#0,d3
	move.l	width,d4
	move.l	height,d5
	move.l	#$0c0,d6
	SYS	BltBitMapRastPort,GBase

* SetPointer to Vaccume Cleaner

	lea.l	vaccolors,a2			;get colors
	moveq	#0,d5	
	moveq	#0,d6
setprt:						;Set register colors.
	cmp.w	#3,d5
	beq	1$
	move.w	(a2)+,d6
	move.l	d6,d0
	and.w	#$f00,d6
	lsr.w	#8,d6
	move.l	d6,red
	move.l	d0,d6
	and.w	#$0f0,d6
	lsr.w	#4,d6
	move.l	d6,green
	move.l	d0,d6
	and.w	#$00f,d6
	move.l	d6,blue
	move.l	vp,a0				;Put vp in a0.
	move.l	d5,d0
	add.w	#17,d0				;Calculate proper register.
	move.l	red,d1				;Move colors into registers.
	move.l	green,d2
	move.l	blue,d3
	SYS	SetRGB4				;Set the colors.
	add.w	#1,d5				;Bump counter.
	jmp	setprt				;Do it again.
1$:
	moveq	#0,d0				;SetPointer to vaccume 
	move.l	vacwin,a0			;cleaner.
	lea.l	vacsprite,a1
	move.l	#VACHEIGHT,d0
	move.l	#VACWIDTH,d1
	moveq	#1,d2
	moveq	#1,d3
	SYS	SetPointer,IBase

	move.l	vacwin,a0
	SYS	ActivateWindow			;Active the Window.

****** Vac: The heart of the code ******

	move.l	IBase,a5
	move.l	ib_ActiveScreen(a5),a5
	moveq	#0,d0
	move.b	sc_DetailPen(a5),d0
	move.l	d0,bgpen
	move.b	sc_BlockPen(a5),d0
	move.l	d0,fgpen
	move.l	vacwin,a3
vac:
	move.l	wd_UserPort(a3),a0		;Grab the UserPort and get
	SYS	GetMsg,4			;a message.
	tst.l	d0
	beq	1$
	move.l	d0,a1
	SYS	ReplyMsg			;Reply to the message and get
	bra	cleanup				;out.
1$:
	moveq	#0,d0
	move.w	sc_MouseX(a5),d0		
	move.l	d0,mx				;Save MouseX in mx
	move.w	sc_MouseY(a5),d0
	move.l	d0,my				;Save MouseY in my
	moveq	#10,d5				;Size of Matrix
	moveq	#10,d6				;Offset for x
	moveq	#10,d7				;Offset for y
2$:
	move.l	rp,a1				;Put rp in a1
	move.l	mx,d2
	move.l	my,d3
	sub.l	d6,d2
	move.l	d2,d0				;Put x in d0 for ReadPixel
	sub.l	d7,d3
	move.l	d3,d1				;put y in d1 for ReadPixel.
	SYS	ReadPixel,GBase			;ReadPixel at location.
	cmp.l	fgpen,d0			;is it the same color a fgpen?
	bne	3$				;Nope. Go get another pixel.
	move.l	rp,a1				;Must be equal to fgpen.
	move.l	bgpen,d0			;Put bgpen in d0.
	SYS	SetAPen				;Set pen to background color.

	move.l	rp,a1
	move.l	d2,d0
	move.l	d3,d1
	SYS	WritePixel			;Change Pixel to background.

	move.l	rp,a1				
	move.l	fgpen,d0
	SYS	SetAPen				;Set pen back to fgpen.

	move.l	rp,a1				;Write the pixel @ x+1 y+1
	addq	#1,d2
	move.l	d2,d0
	addq	#1,d3
	move.l	d3,d1
	SYS	WritePixel
3$:	
	subq	#1,d6
	tst.l	d6
	beq	4$
	bra	2$
4$:
	moveq	#10,d6
	subq	#1,d7
	subq	#1,d5
	beq	clearem				;Hit Zero. Start over
	bra	2$				;Else, continue.

clearem:
	move.l	rp,a1				;Clear out pixels close to
	move.l	bgpen,d0			;sprite.
	SYS	SetAPen

	moveq	#3,d4
	lea.l	mxoffsets,a0
	lea.l	myoffsets,a2
1$:
	move.l	rp,a1
	move.l	mx,d2
	sub.w	(a0)+,d2
	move.l	d2,d0
	move.l	my,d3
	sub.w	(a2)+,d3
	move.l	d3,d1
	SYS	WritePixel
	dbne	d4,1$
	bra	vac

****** Clean up everything and exit program ******

cleanup:
	move.l	svp,a0
	lea.l	ctable,a1
	move.l	count,d0
	SYS	LoadRGB4,GBase

	moveq	#0,d0
	move.l	depth,d4
	subq.w	#1,d4
	move.l	bm2,a3
	lea.l	bm_Planes(a3),a3
free:
	move.l	(a3)+,a0
	move.l	width,d0
	move.l	height,d1
	SYS	FreeRaster,GBase
	dbf	d4,free

	move.l	bm2,a1
	move.l	#bm_SIZEOF,d0
	SYS	FreeMem,4

	move.l	vacwin,a0
	SYS	ClearPointer,IBase		;Clear out the Vac pointer.

	move.l	vacwin,a0
	SYS	CloseWindow			;Close the Window.

	move.l	IBase,a1			;Put gfxbase in a1
	SYS	CloseLibrary,4			;Close graphics.library

	move.l	GBase,a1			;Put gfxbase in a1
	SYS	CloseLibrary			;Close graphics.library
	rts					;Exit program.

**************************************************************************
*                            VacBench Data                               *
**************************************************************************

	section	VacBenchdata,data

****** Libraries ******
IntuiName:
	dc.b	'intuition.library',0
	ds	0

GfxName:
	dc.b	'graphics.library',0
	ds	0

****** Library Pointers ******
	XDEF	GBase
GBase:
	ds.l	1
	XDEF	IBase
IBase:
	ds.l	1

****** NewWindow *****
newwin:
	dc.w	0,0				;Left edge, top edge.
	dc.w	0,0				;Width, height.
	dc.b	0,1				;Detail and block pen.
	dc.l	MOUSEBUTTONS		  	;IDCMP flags.
	dc.l	BORDERLESS!SIMPLE_REFRESH 	;Window flags.
	dc.l	0				;Prt to first gadget.
	dc.l	0				;Image (check mark).
	dc.l	0				;Window title ptr.
	dc.l	0				;Screen ptr.
	dc.l	0				;BitMap ptr.
	dc.w	0,0				;MinWidth, MinHeight.
	dc.w	640,200				;MaxWidth, MaxHeight.
	dc.w	WBENCHSCREEN			;Screen type.

****** Window Stuff ******
vacwin:
	ds.l	1
svp:
	ds.l	1
ctable:
	ds.w	32
count:
	ds.l	1
vp:
	ds.l	1
rp:
	ds.l	1
bm1:
	ds.l	1
bm2:
	ds.l	1
depth:
	ds.l	1
width:
	ds.l	1
height:
	ds.l	1
mx:
	ds.l	1
my:
	ds.l	1
fgpen:
	ds.l	1
bgpen:
	ds.l	1
red:
	ds.l	1
green:
	ds.l	1
blue:
	ds.l	1

****** Sprite Data ******
vacsprite:
	dc.w	$0000,$0000
	dc.w	$0000,$7000
	dc.w	$0000,$f800
	dc.w	$0000,$f800
	dc.w	$0c00,$fc00
	dc.w	$1e00,$7e00
	dc.w	$1f00,$1c00
	dc.w	$0f80,$0800
	dc.w	$07c0,$0000
	dc.w	$03e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0000
	dc.w	$01e0,$0210
	dc.w	$03f0,$0618
	dc.w	$07f8,$0e1c
	dc.w	$0ffc,$1e1e
	dc.w	$1ffe,$3e1f
	dc.w	$1ffe,$3e1f
	dc.w	$1ffe,$3f3f
	dc.w	$1ffe,$3fff
	dc.w	$0ffc,$3fff
	dc.w	$17fa,$3fff
	dc.w	$1bf6,$3fff
	dc.w	$1c0e,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff	
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$1dee,$3fff
	dc.w	$0dee,$1fff
	dc.w	$05ec,$0ffe
	dc.w	$01e8,$07fc
	dc.w	$0000,$03f8
	dc.w	$0000,$0000

vaccolors:
	dc.w	$567,$000,$e30
	ds	0

mxoffsets:
	dc.w	1,1,0,0

myoffsets:
	dc.w	0,1,1,0
	END
