*****************************************************************
*	Added on 22/8						*
*	Brush flipping x and y					*
*	Coords print relative to start of operation.		*
*	27/8							*
*	Palette remapping					*
*	ST LOW neo conversion					*
*	28/8							*
*	Brush Halving						*
*	fixed findfurthest					*
*	added color getting in palette section			*
*	fixed error in halving					*
*	Added PNT handling properly				*
*	Fixed color problem with PI1 and PC1 files		*
*	added removing background color from user brush(2badded2)	*
*								*
*	Fixed overflow on elipses				*
*	Added right button prefs exits with that tool as current*


	opt	o+,w-

hblhighest:	equ	2	;for dragging the menu

patternfill:	equ	1	;filltype values
solidfill:	equ	0
emptyfill:	equ	2
isuni:		equ	0	;values for trans mode
isvert:		equ	1
ishoriz:	equ	2
issphere:	equ	3

clipxmin:	equ	0
clipymin:	equ	0
clipxmax:	equ	319
clipymax:	equ	479
first_ade:	equ	36
off_table:	equ	72
data_table:	equ	76
form_width:	equ	80
form_height:	equ	82
wmode:		equ	36
clip:		equ	54
xmincl:		equ	56
ymincl:		equ	58
xmaxcl:		equ	60
ymaxcl:		equ	62
xdda:		equ	64
ddainc:		equ	66
scaldir:	equ	68
mono:		equ	70
sourcex:	equ	72
sourcey:	equ	74
destx:		equ	76
desty:		equ	78
delx:		equ	80
dely:		equ	82
fbase:		equ	84
fwidth:		equ	88
style:		equ	90
litemask:	equ	92
skewmask:	equ	94
weight:		equ	96
roff:		equ	98
loff:		equ	100
scale:		equ	102
chup:		equ	104
textfg:		equ	106
scrthcp:	equ	108
scrpt2:		equ	112
textbg:		equ	114

doingcopy:	equ	0	;values for palettemode
doingex:	equ	1
doingspread:	equ	2
doingnothing:	equ	3
fcolorset:	equ	0	;values for paletteloop
bcolorset:	equ	1
replacemode:	equ	0	;values for brush mode
mattemode:	equ	1
colormode:	equ	2
cyclemode:	equ	3
transparentmode:	equ	4
bltintobrush:	equ	1	;values for blittype
bltbrush:	equ	-1
bltclear:	equ	0
usepixel:	equ	0	;values for todraw (for boxes, lines etc)
usebrush:	equ	1
wmeor:		equ	0	;wirte mode values
wmreplace:	equ	1
gettingbrush:	equ	0	;paintmode values
drawing:	equ	1
drawingline:	equ	2
filling:	equ	3
drawingbox:	equ	4
drawingspray:	equ	5
drawingcurve:	equ	6
drawingelipse:	equ	7
b_wd:		equ	0	;bitblt values
b_ht:		equ	2
plane_ct:	equ	4
fg_col:		equ	6
bg_col:		equ	8
op_tab:		equ	10
s_xmin:		equ	14
s_ymin:		equ	16
s_form:		equ	18
s_nxwd:		equ	22
s_nxln:		equ	24
s_nxpl:		equ	26
d_xmin:		equ	28
d_ymin:		equ	30
d_form:		equ	32
d_nxwd:		equ	36
d_nxln:		equ	38
d_nxpl:		equ	40
p_addr:		equ	42
p_nxln:		equ	46
p_nxpl:		equ	48
p_mask:		equ	50
space:		equ	52

numpanels:	equ	2
panel1:		equ	0	;whichpanel is currently up
panel2:		equ	1
panel3:		equ	2
panel4:		equ	3
mminx:	equ	0		;mouse values
mmaxx:	equ	319
mminy:	equ	0
mmaxy:	equ	479
leftbuttondown:		equ	1
rightbuttondown:	equ	2
*maxy:		equ	400
*miny:		equ	350
iea:	equ	$fffa07
ieb:	equ	$fffa09
isra:	equ	$fffa0f
ima:	equ	$fffa13
tbcr:	equ	$fffa1b
tbdr:	equ	$fffa21
vbl_vec:	equ	$70
key_vec:	equ	$118
hbl_vec:	equ	$120
scrn1:		equ	0	;equates for current screen
scrn2:		equ	1
egetshift:	equ	81
egetpalette:	equ	85
ftypepnt:	equ	0
ftypeneo:	equ	4
ftypepi1:	equ	8
ftypepc1:	equ	12
ftypeiff:	equ	16
iwidth:		equ	8
iplanes:	equ	12
iheight:	equ	10
icolors:	equ	6
icompressed:	equ	14
isize:		equ	16

extern_lib	macro
	xref	_lvo\1
	endm
structure	macro
\1	equ	0
soffset	set	\2
	endm
bool	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
byte	macro
\1	equ	soffset
soffset	set	soffset+1
	endm
ubyte	macro
\1	equ	soffset
soffset	set	soffset+1
	endm
word	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
uword	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
short	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
ushort	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
long	macro
\1	equ	soffset
soffset	set	soffset+4
	endm
ulong	macro
\1	equ	soffset
soffset	set	soffset+4
	endm
float	macro
\1	equ	soffset
soffset	set	soffset+4
	endm
aptr	macro
\1	equ	soffset
soffset	set	soffset+4
	endm
cptr	macro
\1	equ	soffset
soffset	set	soffset+4
	endm
rptr	macro
\1	equ	soffset
soffset	set	soffset+2
	endm
struct	macro
\1	equ	soffset
soffset	set	soffset+\2
	endm
label	macro
\1	equ	soffset
	endm
bitdef	macro
	bitdef0	\1,\2,b_,\3
\@bitdef	set	1<<\3
	bitdef0	\1,\2,f_,\@bitdef
	endm
bitdef0	macro
\1\3\2	equ	\4
	endm
library_version	equ	33

      structure	bitmapheader,0
      uword	_w
      uword	_h
      word	_x
      word	_y
      ubyte	_nplanes
      ubyte	_masking
      ubyte	_compression
      ubyte	_pad1
      uword	_transparentcolor
      ubyte	_xaspect
      ubyte	_yaspect
      word	_pagewidth
      word	_pageheight
      label	_sizeof


*********************************************************
*	TTPaint						*
*	The first tentative steps to a great 256 color	*
*	TT painting program.				*
*							*
*********************************************************
ttpaint:
	move.l	a7,stacksave
	move.l	4(a7),a3      ;basepage
	move.l	$C(a3),d0     ;text length
	add.l 	$14(a3),d0    ;data length
	add.l 	$1C(a3),d0    ;BSS length
	add.l 	#500,d0     ;any additional memory
	add.l 	#$100,d0      ;basepage length
*	move.l	#mystack,a7   ;before shrinking
	move.l	d0,-(a7)
	move.l	a3,-(a7)
	clr.w 	-(a7)
	move.w	#$4a,-(a7)
	trap	#1		;do the shrink
	lea	12(sp),sp     

	jsr	gosup
	move.w	#200,panely
	move.w	#61,panelheight
	jsr	initstuff	;initialise Gem
				;res, address, colors
paintloop:
	jsr	keyboard
	bsr	followmouse
	jsr	domag
	bra	domousemenu
	bra	paintloop

*********************************************************
*	Routine to make the currently selected brush	*
*	follow the mouse around.			*
*********************************************************
followmouse:
	tst.w	onpanel
	bpl	isonpanel
	tst.w	shouldfollow
	bmi	isonpanel
	move.w	#1,doinloop
	bsr	waitvbl
	move.w	mx,d6
	move.w	my,d7
	cmp.w	oldmx,d6
	bne	isdiff
	cmp.w	oldmy,d7
	bne	isdiff
	bra	noneedbf
isdiff:
	movem.w	d6/d7,-(sp)
	bsr	undrawbrush
	movem.w	(sp)+,d6/d7
	move.w	d6,oldmx
	move.w	d7,oldmy
	move.w	#bltintobrush,blittype
	move.l	currentbrush,a1
	lea	savebrush,a0
	move.w	brushw(a1),brushw(a0)
	move.w	brushh(a1),brushh(a0)
	move.w	#1,brushsaved(a0)
	bsr	clipndrawbrush
	move.w	#bltbrush,blittype
	move.w	#1,canceltransparent
	move.w	oldmx,d6
	move.w	oldmy,d7
	move.w	currentfcolor,color
	move.l	currentbrush,a0
	bsr	clipndrawbrush
	move.w	#-1,canceltransparent
	tst.w	drawcursorflag
	bmi	nodc
	lea	drawspritesave,a2
	move.l	lineaundrawsprite,a6
	jsr	(a6)
	move.w	oldmx,d0
	move.w	oldmy,d1
	move.l	currentdrawsprite,a0
	lea	drawspritesave,a2
	move.l	lineadrawsprite,a6
	jsr	(a6)
	move.w	#-1,doinloop
nodc:
	rts
noneedbf:
	tst.w	drawcursorflag
	bmi	nodc
	lea	drawspritesave,a2
	move.l	lineaundrawsprite,a6
	jsr	(a6)
	move.w	oldmx,d0
	move.w	oldmy,d1
	move.l	currentdrawsprite,a0
	lea	drawspritesave,a2
	move.l	lineadrawsprite,a6
	jsr	(a6)
	move.w	#-1,doinloop
	rts
isonpanel:			;don't bother if the mouse is over the panel
	rts
undrawbrush:
	lea	savebrush,a0	;first check that there is a back to restr
	tst.w	brushsaved(a0)
	bmi	nosaved		;none at all
	move.w	#-1,brushsaved(a0)	;signal we are getting rid of save
	bsr	blitback	;put it back
nosaved:
	rts

doundo:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait1
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
nowait1:
	bsr	undrawbrush
	move.l	undoscreen,a1
	move.l	currentscreen,a0
	move.w	#479,d0
	bsr	cudl2
	move.w	#-1,oldmx
	move.w	#-1,oldmy
	move.w	(sp)+,mouseonoff
	rts
copyforundo:
	move.l	currentscreen,a1
	move.l	undoscreen,a0
	move.w	#479,d0
cudl:
	rept	80
	move.l	(a1)+,(a0)+
	endr
	dbf	d0,cudl
	rts

cudl2:
	rept	80
	move.l	(a1),d1
	move.l	(a0),(a1)+
	move.l	d1,(a0)+
	endr
	dbf	d0,cudl2
	rts
*********************************************************
*	Dotransparent					*
*	Plots the current brush transparently over the	*
*	background. Uses transparency.w a value between	*
*	0 and 15 to calculate color in which to plot.	*
*	Address of already clipped brush in a0		*
*********************************************************
dotransparent:
	move.w	#usepixel,todraw
	move.w	#wmreplace,writemode
	lea	userbrushmem,a5		;add for getpoint
	move.l	currentpalette,a3
	move.w	#-1,oldcolor0
	move.w	#-1,oldcolor15
	lea	color0,a2
	move.w	brushh2(a0),d6		;y count
	move.w	brushymin(a0),d5	;get y
	move.w	brushy(a0),d3
	lea	transylist,a1
	subq.w	#1,d6
dtlooph:
	move.w	brushw2(a0),d7		;xcount
	subq.w	#1,d7
	move.w	brushxmin(a0),d4	;get x
	move.w	brushx(a0),d2
	lea	transxlist,a4
dtloopw:
	move.l	a5,a6
	move.w	d4,d0		;get brush pixel
	move.w	d5,d1
	bsr	getpoint	;find color
	move.w	color,d0
	tst.w	bistrans
	bmi.s	notb
	tst.w	d0
	beq	noplot
notb:
	add.w	d0,d0
	move.w	(a3,d0.w),color15	;get actual rgb value
	move.l	currentscreen,a6
	move.w	d2,d0		;get screen pixel
	move.w	d3,d1
	bsr	getpoint3	;find color
	move.w	color,d0
	add.w	d0,d0
	move.w	(a3,d0.w),color0	;get actual rgb value
	move.w	oldcolor0,d0
	cmp.w	color0,d0
	bne	willget16
	move.w	oldcolor15,d0
	cmp.w	color15,d0
	bne	willget16
	bra	skip16
willget16:
	move.w	color0,oldcolor0
	move.w	color15,oldcolor15
	bsr	get16
skip16:
	move.w	currentdither,d0
	move.w	d0,d1
	beq.s	faster
	jsr	random
	exg	d0,d1
	jsr	random
faster:
	add.w	d0,d0
	add.w	d1,d1
	move.w	(a4,d0.w),d0
	tst.w	d0
	bpl.s	dithok1
	move	(a4),d0
dithok1:
	move.w	(a1,d1.w),d1
	tst.w	d1
	bpl.s	dithok2
	move.w	(a1),d1
dithok2:
	add.w	d1,d0
	asr.w	#2,d0
	add.w	d0,d0
	move.w	(a2,d0.w),d0		;get the calculated color
	move.l	a6,-(sp)
	lea	colortable,a6
	add.w	d0,d0
	move.w	(a6,d0.w),color
	move.l	(sp)+,a6
*	move.w	d2,d0		;get screen pixel
*	move.w	d3,d1
	bsr	putpoint3	;find color
noplot:
	addq.l	#2,a4
	addq.w	#1,d2
	addq.w	#1,d4
	dbf	d7,dtloopw
	addq.l	#2,a1
	addq.w	#1,d3
	addq.w	#1,d5
	dbf	d6,dtlooph
	rts


*********************************************************
*	Get16						*
*	Takes two color values in rgb format in		*
*	color0 and color15				*
*	and calculates the colors inbetween placing	*
*	them in color1,color2.....color14		*
*********************************************************
get16:
	movem.l	d0-d5/d7/a0/a2,-(sp)
	move.w	#$0f00,d3
	move.w	#$00f0,d4
	moveq	#$000f,d5
	move.w	color0,d0
	move.w	d0,d1
	move.w	d0,d2
	and.w	d3,d0
	and.w	d4,d1
	and.w	d5,d2
	asl.w	#4,d1
	asl.w	#8,d2
	move.w	d0,bottomred
	move.w	d1,bottomgreen
	move.w	d2,bottomblue
	move.w	color15,d0
	move.w	d0,d1
	move.w	d0,d2
	and.w	d3,d0
	and.w	d4,d1
	and.w	d5,d2
	asl.w	#4,d1
	asl.w	#8,d2
	move.w	d0,topred
	move.w	d1,topgreen
	move.w	d2,topblue

	moveq	#0,d0
	move.w	topred,d0
	sub.w	bottomred,d0
	ext.l	d0
	divs	#16,d0
	move.w	d0,redadd

	moveq	#0,d0
	move.w	topgreen,d0
	sub.w	bottomgreen,d0
	ext.l	d0
	divs	#16,d0
	move.w	d0,greenadd

	moveq	#0,d0
	move.w	topblue,d0
	sub.w	bottomblue,d0
	ext.l	d0
	divs	#16,d0
	move.w	d0,blueadd

	moveq	#16-1,d7
	lea	color0,a0
*	addq.l	#2,a0			;bottom color pointer
*	lea	$ff8400+512-32,a2
spreadloop16:
	move.w	bottomred,d0
	add.w	redadd,d0
	move.w	d0,bottomred
	and.w	d3,d0
	move.w	bottomgreen,d1
	add.w	greenadd,d1
	move.w	d1,bottomgreen
	asr.w	#4,d1
	and.w	d4,d1
	move.w	bottomblue,d2
	add.w	blueadd,d2
	move.w	d2,bottomblue
	asr.w	#8,d2
	and.w	d5,d2
	or.w	d2,d0
	or.w	d1,d0
	move.w	d0,(a0)+
*	move.w	d0,(a2)+
	dbf	d7,spreadloop16
nospread16:
	movem.l	(sp)+,d0-d5/d7/a0/a2
	rts

findnearest2:
	movem.l	d0/a0,-(sp)
	lea	colortable,a0
	move.w	rgb,d0
	add.w	d0,d0
	move.w	(a0,d0.w),color
	movem.l	(sp)+,d0/a0
	rts


*********************************************************
*	Findnearest					*
*	Given and RGB value in RGB find the nearest	*
*	to it in the current palette and return it in	*
*	color.						*
*********************************************************
findnearest:
	movem.l	d0-d7/a0,-(sp)
	move.w	rgb,d0		;get the color value
	move.w	#255,d7		;counter of colors
	moveq	#0,d6
	move.w	#$002d,d5	;fake start cmp
	move.l	currentpalette,a0
fnloop:
	move.w	(a0)+,d1
	move.w	d1,d2
	move.w	d2,d3
	and.w	#$0f00,d1	;seperate rgb
	and.w	#$00f0,d2
	and.w	#$000f,d3
	move.w	d0,d4
	and.w	#$0f00,d4
	sub.w	d4,d1
	bpl.s	nnr
	neg.w	d1
nnr:
	asr.w	#8,d1
	move.w	d0,d4
	and.w	#$00f0,d4
	sub.w	d4,d2
	bpl.s	nng
	neg.w	d2
nng:
	asr.w	#4,d2
	move.w	d0,d4
	and.w	#$000f,d4
	sub.w	d4,d3
	bpl.s	nnb
	neg.w	d3
nnb:
	add.w	d1,d3		;add for total differrence value
	add.w	d2,d3
	tst.w	d3
	beq.s	fnover		;if zero must be exactly right so we leave early
	cmp.w	d3,d5		;is it less different than previous least dif
	ble.s	nonearer
	move.w	d3,d5
	move.w	d6,color	;save the color
nonearer:
	addq.w	#1,d6
	dbf	d7,fnloop
	movem.l	(sp)+,d0-d7/a0
	rts
fnover:
	move.w	d6,color
	movem.l	(sp)+,d0-d7/a0
	rts
*********************************************************
*	Findfurthest					*
*	Given and RGB value in RGB find the furthest	*
*	to it in the current palette and return it in	*
*	color. This is greatest contrast.		*
*********************************************************
findfurthest:
	movem.l	d0-d7/a0,-(sp)
	move.w	rgb,d0		;get the color value
	move.w	#255,d7		;counter of colors
	moveq	#0,d6
	move.w	#$0000,d5	;fake start cmp
	move.l	currentpalette,a0
ffloop:
	move.w	(a0)+,d1
	move.w	d1,d2
	move.w	d2,d3
	and.w	#$0f00,d1	;seperate rgb
	and.w	#$00f0,d2
	and.w	#$000f,d3
	move.w	d0,d4
	and.w	#$0f00,d4
	sub.w	d4,d1
	bpl.s	ffr
	neg.w	d1
ffr:
	asr.w	#8,d1
	move.w	d0,d4
	and.w	#$00f0,d4
	sub.w	d4,d2
	bpl.s	ffg
	neg.w	d2
ffg:
	asr.w	#4,d2
	move.w	d0,d4
	and.w	#$000f,d4
	sub.w	d4,d3
	bpl.s	ffb
	neg.w	d3
ffb:
	add.w	d1,d3		;add for total differrence value
	add.w	d2,d3
	cmp.w	d3,d5		;is it less different than previous least dif
	bge.s	nofurther
	move.w	d3,d5
	move.w	d6,color	;save the color
nofurther:
	addq.w	#1,d6
	dbf	d7,ffloop
	movem.l	(sp)+,d0-d7/a0
	rts

rgb:	ds.w	1
*****************************************************************
*	Draw curve						*
*	First attempt at drawing a quadratic b spline.		*
*****************************************************************
dodrawcurve:
	move.w	#-1,mouseonoff		;turn the mouse off
	move.w	#bltbrush,blittype
	move.w	#-1,collided
	move.w	buttondat,savebuttondat
	move.w	currentfcolor,color
	move.w	#usepixel,todraw	;should be usebrush
	move.w	#wmreplace,writemode
	move.l	linepattern2,linepattern
	bsr	waitvbl
	bsr	undrawbrush
	bra	skipmcurve
curveloop:
	tst.w	buttondat
	beq	curveover
skipmcurve:
	bsr	findmaxnmin
	bsr	restoreunder
	move.w	mx,d6
	move.w	my,d7
	tst.w	collided	;already on know box
	bgt.s	movebox
	bsr	findbox		;see if the mouse
	bra.s	drawasis
movebox:
	move.l	whichbox,a0
	move.w	d6,(a0)
	move.w	d7,2(a0)
drawasis:
	move.l	linepattern2,linepattern
	bsr	drawcurve
	bsr	drawbrush
	bsr	drawpoints
	bra	curveloop
curveover:
	move.w	#1,mouseonoff		;turn the mouse off
	bra	paintloop

drawbrush:
	lea	curvepointlist,a6
	move.w	(a6),d6
	move.w	2(a6),d7
	move.l	currentbrush,a0
	bsr	clipndrawbrush
	lea	curvepointlist,a6
	move.w	12(a6),d6
	move.w	14(a6),d7
	move.l	currentbrush,a0
	bsr	clipndrawbrush
	rts

*****************************************************************
*	Takes the curve points and finds the max and min	*
*	x and y's for restoreunder				*
*****************************************************************
findmaxnmin:
	lea	curvepointlist,a6
	move	#319,d0			;min x
fmx0:
	cmp.w	(a6),d0
	blt.s	fmx1
	move.w	(a6),d0
fmx1:
	cmp.w	4(a6),d0
	blt.s	fmx2
	move.w	4(a6),d0
fmx2:
	cmp.w	8(a6),d0
	blt.s	fmx3
	move.w	8(a6),d0
fmx3:
	cmp.w	12(a6),d0
	blt.s	fmy0
	move.w	12(a6),d0	;d0 has min x

fmy0:
	move	#479,d1			;min x
	cmp.w	2(a6),d1
	blt.s	fmy1
	move.w	2(a6),d1
fmy1:
	cmp.w	6(a6),d1
	blt.s	fmy2
	move.w	6(a6),d1
fmy2:
	cmp.w	10(a6),d1
	blt.s	fmy3
	move.w	10(a6),d1
fmy3:
	cmp.w	14(a6),d1
	blt.s	fmax0
	move.w	14(a6),d1	;d1 has min y

fmax0:
	moveq	#0,d2			;min x
	cmp.w	(a6),d2
	bgt.s	fmax1
	move.w	(a6),d2
fmax1:
	cmp.w	4(a6),d2
	bgt.s	fmax2
	move.w	4(a6),d2
fmax2:
	cmp.w	8(a6),d2
	bgt.s	fmax3
	move.w	8(a6),d2
fmax3:
	cmp.w	12(a6),d2
	bgt.s	fmay0
	move.w	12(a6),d2	;d2 has max x

fmay0:
	move	#479,d3			;min x
	cmp.w	2(a6),d3
	bgt.s	fmay1
	move.w	2(a6),d3
fmay1:
	cmp.w	6(a6),d3
	bgt.s	fmay2
	move.w	6(a6),d3
fmay2:
	cmp.w	10(a6),d3
	bgt.s	fmay3
	move.w	10(a6),d3
fmay3:
	cmp.w	14(a6),d3
	bgt.s	fmmover
	move.w	14(a6),d3	;d1 has min y
fmmover:
	move.l	currentbrush,a0
	cmp.l	#userbrush,a0
	bne.s	othermethod
	move.w	brushw(a0),d4
	asr.w	#1,d4
	sub.w	d4,d0
	bpl.s	xminok
	moveq	#0,d0
xminok:
	move.w	brushh(a0),d5
	asr.w	#1,d5
	sub.w	d5,d1
	bpl.s	yminok
	moveq	#0,d1
yminok:
	add.w	d4,d2
	cmpi.w	#319,d2
	blt.s	xmaxok
	move.w	#319,d2
xmaxok:
	add.w	d5,d3
	cmpi.w	#479,d3
	blt.s	ymaxok
	move.w	#479,d3
ymaxok:
	move.w	d0,savex1
	move.w	d1,savey1
	move.w	d2,savex2
	move.w	d3,savey2
	rts
othermethod:
	sub.w	#10,d0
	bpl.s	xminokb
	moveq	#0,d0
xminokb:
	sub.w	#10,d1
	bpl.s	yminokb
	moveq	#0,d1
yminokb:
	add.w	#10,d2
	cmpi.w	#319,d2
	blt.s	xmaxokb
	move.w	#319,d2
xmaxokb:
	add.w	#10,d3
	cmpi.w	#479,d3
	blt.s	ymaxokb
	move.w	#479,d3
ymaxokb:
	move.w	d0,savex1
	move.w	d1,savey1
	move.w	d2,savex2
	move.w	d3,savey2
	rts



findbox:
	lea	curvepointlist,a6
	moveq	#3,d5		;total of 4 points to check
findboxl:
	movem.w	(a6)+,d0-d1	;get x,y of point
	subq.w	#5,d0
	subq.w	#5,d1		;account for size of box
	cmp.w	d6,d0
	bgt.s	nextbox
	cmp.w	d7,d1
	bgt.s	nextbox
	add.w	#15,d0
	add.w	#10,d1
	cmp.w	d6,d0
	blt.s	nextbox
	cmp.w	d7,d1
	blt.s	nextbox		;if we get through this this is our man
	move	#1,collided		;signal no box found
	subq.l	#4,a6
	move.l	a6,whichbox
	rts
nextbox:
	dbf	d5,findboxl
	move	#-1,collided		;signal no box found
	rts


drawpoints:
	lea	curvepointlist,a6
	move.w	(a6)+,d0
	move.w	(a6)+,d1
	move.l	a6,-(sp)
	lea	curvesp0,a0
	lea	c0save,a2
	move.l	lineadrawsprite,a5
	jsr	(a5)
	move.l	(sp)+,a6

	move.w	(a6)+,d0
	move.w	(a6)+,d1
	move.l	a6,-(sp)
	lea	curvesp1,a0
	lea	c1save,a2
	move.l	lineadrawsprite,a5
	jsr	(a5)
	move.l	(sp)+,a6

	move.w	(a6)+,d0
	move.w	(a6)+,d1
	move.l	a6,-(sp)
	lea	curvesp2,a0
	lea	c2save,a2
	move.l	lineadrawsprite,a5
	jsr	(a5)
	move.l	(sp)+,a6
	move.w	(a6)+,d0
	move.w	(a6)+,d1
	move.l	a6,-(sp)
	lea	curvesp3,a0
	lea	c3save,a2
	move.l	lineadrawsprite,a5
	jsr	(a5)
	move.l	(sp)+,a6
	rts
drawcurve:
	lea	curvepointlist,a0
	lea	p1x,a1
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+

	move.w	#-1,-(sp)		;to indicate end
	move.w	p1x,d0
	move.w	p1y,d1		;start at p1
pcl:
	move.w	p3x,-(sp)	;push p3,p4 onto stack
	move.w	p3y,-(sp)
	move.w	p4x,-(sp)
	move.w	p4y,-(sp)
	move.w	p2x,d2
	add.w	p3x,d2
	asr.w	#1,d2		;midpoint of p2,p3 ...x value
	move.w	p2y,d3
	add.w	p3y,d3
	asr.w	#1,d3		;midpoint of p2,p3 ...y value
	move.w	d2,p4x
	move.w	d3,p4y		;make it new p4
pcl1:
	move.w	d0,d2
	move.w	d1,d3
	sub.w	p4x,d2
	bpl.s	pcla
	neg.w	d2		;abs value
pcla:
	sub.w	p4y,d3
	bpl.s	pclb
	neg.w	d3		;abs value
pclb:
	cmpi.w	#5,d2
	bge.s	notthistime
	cmpi.w	#5,d3
	bge.s	notthistime
	move.w	d0,x1		;is it withinn 2root2 in x and y (ie 3 pixels)
	move.w	d1,y1
	move.w	p4x,x2
	move.w	p4y,y2
	movem.w	d0-d1,-(sp)
	bsr	drawline
	movem.w	(sp)+,d0-d1
	move.w	p4x,d0		;p1 <- p4
	move.w	p4y,d1

	move.w	(sp)+,d7
	bmi.s	pcover		;check end
	move.w	d7,p4y
	move.w	(sp)+,p4x	;pop p3,p4 calling them p2,p4
	move.w	(sp)+,p2y
	move.w	(sp)+,p2x
	bra	pcl1
notthistime:
	move.w	p2x,d2
	add.w	p4x,d2
	asr.w	#1,d2		;midpoint of p2,p4 ...x value
	move.w	p2y,d3
	add.w	p4y,d3
	asr.w	#1,d3		;midpoint of p2,p4 ...y value
	move.w	d2,p3x
	move.w	d3,p3y		;make it new p3

	move.w	p2x,d2
	add.w	d0,d2		
	asr.w	#1,d2		;midpoint of p1,p2 ...x value
	move.w	p2y,d3
	add.w	d1,d3
	asr.w	#1,d3		;midpoint of p1,p2 ...y value
	move.w	d2,p2x
	move.w	d3,p2y		;make it new p2
	bra	pcl

pcover:
	rts

*********************************************************
*	Spray						*
*********************************************************
dodrawspray:
	move.w	#-1,mouseonoff		;turn the mouse off
	move.w	buttondat,savebuttondat
	move.w	currentfcolor,color
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw	;should be usebrush
	move.l	#-1,linepattern
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	bsr	preparecycle
	move.w	sprayrate,generalvalue
	bra	skipmspray
sprayloop:
	tst.w	buttondat
	beq	sprayover
	cmpi.w	#cyclemode,brushmode
	bne	skipmspray
	addq.w	#1,color
	move.w	color,d0
	cmp.w	colorcyclehigh,d0
	ble	skipmspray
	move.w	colorcyclelow,color
skipmspray:
*	move.w	mx,d6
*	move.w	my,d7
	subq.w	#1,generalvalue
	bpl	sprayloop
	move.w	sprayrate,generalvalue

	move.w	#360,d0
	jsr	randomplus
	move.w	d0,d2		;random angle
	move.w	sprayradius,d0
	jsr	randomplus
	move.w	d0,d1		;random y
	moveq	#0,d0		;x=0
	bsr	rotatexy
	add.w	mx,d0
	add.w	my,d1
	bsr	putpoint
	bra	sprayloop

*********************************************************
*	Takes an x and y in d0,d1 and returns new x y	*
*	in d0,d1 rotated d2 degrees around 0,0.		*
*	Note, x,y are from origin.			*
*********************************************************
rotatexy:
	bsr	sincos
	moveq	#0,d4
	moveq	#0,d5
	move.w	d0,d4
	muls	d3,d4		;cos*x1
	move.w	d1,d5
	muls	d2,d5		;sin*y1
	lsl.l	#2,d4
	lsl.l	#2,d5
	swap	d4
	swap	d5
	sub.w	d5,d4
	move.w	d4,d6		;new x

	moveq	#0,d4
	moveq	#0,d5
	move.w	d1,d4
	muls	d3,d4		;cos*y1
	move.w	d0,d5
	muls	d2,d5		;sin*x1
	lsl.l	#2,d4
	lsl.l	#2,d5
	swap	d4
	swap	d5
	add.w	d5,d4
	move.w	d4,d1		;new y
	move.w	d6,d0
	rts


*********************************************************
*	Takes angle in d2 and returns sin in d2		*
*	and cos in d3					*
*********************************************************
sincos:
	tst.w	d2
	bpl.s	noaddi
	add.w	#360,d2
noaddi:
	lea	sintab,a1
	move.l	d2,d3
	lsl.w	#1,d2
	move.w	(a1,d2.w),d2	;d2 is sin
	cmp.w	#270,d3
	blt.s	plus9
	sub.w	#270,d3
	bra.s	sendsin
plus9:
	add.w	#90,d3
sendsin:
	lsl.w	#1,d3
	move.w	(a1,d3.w),d3	;d3 is cos
	rts



sprayover:
	move.w	#1,mouseonoff		;turn the mouse off
	bra	paintloop
*****************************************************************
*	Horizontal line draw					*
*	Draw a super quick horizontal line.			*
*	Uses Filltype variable to decide on pattern fill with	*
*	userbrush mem as source, or solid using currentfcolor	*
*								*
*	Pass d0,d1,d2 as x1,y1,x2				*
*****************************************************************
nohline:	rts
hline:
	move.w	d1,d7		;save y1
	tst.w	d7
	bmi.s	nohline
	move.l	currentscreen,a0
	lea	userbrushmem,a1
	lea	fillplane,a2

	and.l	#$0000ffff,d7
	move.l	d7,d4
	move.l	d4,d5
	
	asl.l	#5,d4
	asl.l	#3,d5
	add.l	d4,d5
	add.l	d5,a2		;fill plane line address

	move.l	d7,d6
	asl.l	#8,d7
	asl.l	#6,d6
	add.l	d7,d6
	add.l	d6,a0
	add.l	d6,a1

	move.w	currentfcolor,d6
	cmpi.w	#rightbuttondown,savebuttondat
	bne.s	notback
	move.w	currentbcolor,d6
notback:
	move.w	d2,d1

	cmp.w	d0,d1
	bge.s	ordok
	exg	d0,d1
ordok:
	tst.w	d0
	bge.s	lftok
	moveq	#0,d0
lftok:
	cmpi.w	#319,d1
	ble.s	rtok
	move.w	#319,d1
rtok:

	sub.w	d0,d1
	move.w	d0,d2
	and.w	#$fff0,d2
	add.w	d2,a0
	add.w	d2,a1
	asr.w	#3,d2
	add.w	d2,a2		;fill plane

	and.w	#$f,d0
	moveq	#-1,d2
	lsr.w	d0,d2
	add.w	d1,d0
	sub.w	#16,d0
	bpl.s	multi_word	;covers two or more lots of 16

	addq.w	#1,d0
	neg.w	d0
	moveq	#-1,d1
	lsl.w	d0,d1
	and.w	d1,d2
	move.w	d2,d3
	not	d3
	bra.s	write_chunk
multi_word:
	move.w	d2,d3
	not	d3
	bsr	write_chunk
	addq.w	#1,d0
	move.w	d0,d1
	lsr.w	#4,d1
	and.w	#$f,d0
	moveq	#0,d3
	moveq	#-1,d2
	bra.s	botlop
chunkloop:
	bsr	write_chunk	;writes the middle
botlop:
	dbf	d1,chunkloop

	moveq	#-1,d2
	lsr.w	d0,d2
	move.w	d2,d3
	not	d2
	
write_chunk:
*	and.w	d3,(a2)		;fillplane
	or.w	d2,(a2)+
	cmpi.w	#patternfill,filltype
	beq	dopatline
cmrmb:				;if rightmb pat fill and breplc return here
	and.w	d3,(a0)
	and.w	d3,2(a0)
	and.w	d3,4(a0)
	and.w	d3,6(a0)
	and.w	d3,8(a0)
	and.w	d3,10(a0)
	and.w	d3,12(a0)
	and.w	d3,14(a0)
hrplane0:
	btst	#0,d6
	beq	hrplane1
	or.w	d2,(a0)
hrplane1:
	btst	#1,d6
	beq	hrplane2
	or.w	d2,2(a0)
hrplane2:
	btst	#2,d6
	beq	hrplane3
	or.w	d2,4(a0)
hrplane3:
	btst	#3,d6
	beq	hrplane4
	or.w	d2,6(a0)
hrplane4:
	btst	#4,d6
	beq	hrplane5
	or.w	d2,8(a0)
hrplane5:
	btst	#5,d6
	beq	hrplane6
	or.w	d2,10(a0)
hrplane6:
	btst	#6,d6
	beq	hrplane7
	or.w	d2,12(a0)
hrplane7:
	btst	#7,d6
	beq	hrplaneover
	or.w	d2,14(a0)
hrplaneover:
	lea	16(a0),a0
	rts

dopatline:
	tst.w	d3
	beq	nomasking
	tst.w	bistrans
	bpl.s	doclever	;mask of pattern as well
	cmpi.w	#rightbuttondown,savebuttondat
	beq	cmrmb
	rept	8
	move.w	(a1)+,d4
	and.w	d2,d4
	and.w	d3,(a0)
	or.w	d4,(a0)+
	endr
	rts
doclever:
	move.w	(a1),d4
	or.w	2(a1),d4	
	or.w	4(a1),d4	
	or.w	6(a1),d4	
	or.w	8(a1),d4	
	or.w	10(a1),d4	
	or.w	12(a1),d4	
	or.w	14(a1),d4	
	and.w	d2,d4
	not	d4
	cmpi.w	#rightbuttondown,savebuttondat
	beq	wdcc1

	rept	8
	and.w	d4,(a0)
	move.w	(a1)+,d5
	and.w	d2,d5
	or.w	d5,(a0)+
	endr
	rts
wdcc1:
	rept	8
	and.w	d4,(a0)+
	addq.l	#2,a1
	endr
	rts

nomasking:
	tst.w	bistrans
	bpl.s	doclever2	;mask of pattern as well
	cmpi.w	#rightbuttondown,savebuttondat
	beq	cmrmb
	rept	4
	move.l	(a1)+,(a0)+
	endr
	rts
doclever2:
	move.w	(a1),d4
	or.w	2(a1),d4	
	or.w	4(a1),d4	
	or.w	6(a1),d4	
	or.w	8(a1),d4	
	or.w	10(a1),d4	
	or.w	12(a1),d4	
	or.w	14(a1),d4	
	not	d4
	cmpi.w	#rightbuttondown,savebuttondat
	beq	dcc2
	rept	8
	and.w	d4,(a0)
	move.w	(a1)+,d5
	or.w	d5,(a0)+
	endr
	rts
dcc2:
	rept	8
	and.w	d4,(a0)+
	addq.l	#2,a1
	endr
	rts
*********************************************************
*	dodrawelipse just draws an elipse.		*
*********************************************************
dodrawelipse:
	move.w	#wmeor,writemode
	move.w	#usepixel,todraw	;should be usebrush
	move.w	mx,x1
	move.w	my,y1
	move.w	x1,initialx
	move.w	y1,initialy
	move.w	#-1,oldcoordx
	move.w	buttondat,savebuttondat
	move.w	#0,savex1
	move.w	#0,savey1
	move.w	#15,savex2
	move.w	#1,savey2
	move.w	currentfcolor,color
	move.w	#-1,mouseonoff		;turn the mouse off
	move.w	#-1,generalvalue
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	move.w	x1,d0
	move.w	y1,d1
	bra	skipmdde
ddeloop:
	tst.w	buttondat
	beq	deover
	move.w	mx,d0
	move.w	my,d1
	cmp.w	oldmx,d0
	bne	skipmdde
	cmp.w	oldmy,d1
	bne	skipmdde
	bra	ddeloop
skipmdde:
	tst.w	generalvalue
	bmi.s	noeraser
	movem.w	d0-d1,-(sp)
	cmpi.w	#emptyfill,filltype
	beq.s	emptyelipse
	bsr	restoreue
	bra.s	resover
emptyelipse:
	bsr	plotelipse
	bsr	drawelipse
resover:
	movem.w	(sp)+,d0-d1
noeraser:
	move.w	d0,x2
	move.w	d1,y2
	move.w	x2,oldmx
	move.w	y2,oldmy
	move.l	linepattern2,linepattern
	cmp.l	#userbrush,currentbrush
	bne	skipchlinee
	move.l	#%00000000000000100000000000000010,linepattern	;only draw the brush every 16th pixel
skipchlinee:
	bsr	getab
	bsr	plotelipse
	bsr	drawelipse
	move.w	#1,generalvalue
	bra	ddeloop		;put eor line draws in here for rubber banding
deover:
	bsr	getab
	bsr	restoreue
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw	;should be usebrush
	move.l	linepattern2,linepattern
	bsr	plotelipse
	bsr	drawelipse
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff		;turn the mouse off
	bra	paintloop

getab:
	moveq	#0,d0
	move.w	x2,d0
	sub.w	initialx,d0
	bpl.s	aok
	neg.w	d0
aok:
	ext.l	d0
	move.l	d0,a
	moveq	#0,d0
	move.w	y2,d0
	sub.w	initialy,d0
	bpl.s	bok
	neg.w	d0
bok:
	ext.l	d0
	move.l	d0,b
	rts

restoreue:
	move.l	a,d0
	move.w	initialx,d1
	sub.w	d0,d1
	bpl.s	nclx
	moveq	#0,d1
nclx:
	move.w	d1,savex1

	move.l	b,d0
	move.w	initialy,d1
	sub.w	d0,d1
	bpl.s	ncly
	moveq	#0,d1
ncly:
	move.w	d1,savey1

	move.l	a,d0
	move.w	initialx,d1
	add.w	d0,d1
	cmpi.w	#319,d1
	ble.s	ncrx
	move	#319,d1
ncrx:
	move.w	d1,savex2
	move.l	b,d0
	move.w	initialy,d1
	add.w	d0,d1
	cmpi.w	#479,d1
	ble.s	ncry
	move	#479,d1
ncry:
	move.w	d1,savey2
	bsr	restoreunder
	rts

drawelipse:
	move.l	screenad,a0	;where I stored the x,y coords of 1 quarter
	addq.l	#4,a0
	cmpi.w	#emptyfill,filltype
	beq	simpleelipse
solidelipse:
*	cmpi.w	#patternfill,filltype
*	beq	patternelipse
	move.l	a0,a1
	move.w	initialx,d3
	move.w	initialy,d4
sel1:
	move.w	(a0)+,d0
	bmi	sel2a
	move.w	(a0)+,d1
	move.w	d0,d2
	neg.w	d2
	add.w	d3,d0
	add.w	d3,d2
	add.w	d4,d1
*	exg	d0,d2
	movem.l	d0-d4/a0-a1,-(sp)
	bsr	hline
	movem.l	(sp)+,d0-d4/a0-a1
	bra.s	sel1	
sel2a:
	move.l	a1,a0
sel2:
	move.w	(a0)+,d0
	bmi	selover
	move.w	(a0)+,d1
	move.w	d0,d2
	neg.w	d2
	neg.w	d1
	add.w	d3,d0
	add.w	d3,d2
	add.w	d4,d1
*	exg	d0,d2
	movem.l	d0-d4/a0-a1,-(sp)
	bsr	hline
	movem.l	(sp)+,d0-d4/a0-a1
	bra.s	sel2
selover:
	rts

simpleelipse:
	move.l	a0,a1
deloop:
	move.w	(a0)+,d0	;for patterned lines to work
	bmi.s	deloop2a	;we must draw the elipse from start to
	move.w	(a0)+,d1	;finish one point after the other. This
	add.w	initialx,d0	;meanes doing 4 quarters calc'd in a special
	add.w	initialy,d1	;way
	bsr	putpoint
	bra.s	deloop
deloop2a:
	subq.l	#6,a0
deloop2:
	move.w	(a0),d0
	bmi.s	deloop3a
	move.w	2(a0),d1
	neg.w	d0
	add.w	initialx,d0
	add.w	initialy,d1
	bsr	putpoint
	subq.l	#4,a0
	bra.s	deloop2
deloop3a:
	move.l	a1,a0
deloop3:
	move.w	(a0)+,d0
	bmi.s	deloop4a
	move.w	(a0)+,d1
	neg.w	d0
	neg.w	d1
	add.w	initialx,d0
	add.w	initialy,d1
	bsr	putpoint
	bra.s	deloop3
deloop4a:
	subq.l	#6,a0
deloop4:
	move.w	(a0),d0
	bmi.s	deloop5a
	move.w	2(a0),d1
	neg.w	d1
	add.w	initialx,d0
	add.w	initialy,d1
	bsr	putpoint
	subq.l	#4,a0
	bra.s	deloop4
deloop5a:
	rts


a:	dc.l	20
b:	dc.l	199
xe:	dc.l	0
ye:	dc.l	0
asquare:	dc.l	0
bsquare:	dc.l	0
a22:		dc.l	0
b22:		dc.l	0
xslope:		dc.l	0
yslope:		dc.l	0
fmid:		dc.l	0

plotelipse:
	move.l	screenad,a0
	move.w	#-1,(a0)+	;end of list reverse
	move.w	#-1,(a0)+	;end of list reverse
	move.l	a,xe		;x=a
	move.l	#0,ye		;y=0
	move.l	a,d0
	mulu	d0,d0		;asquare=a*a
	move.l	d0,asquare
	move.l	b,d1		;bsquare=b*b
	mulu	d1,d1
	move.l	d1,bsquare
*	asl.l	#1,d0
*	asl.l	#1,d1
	add.l	d0,d0
	add.l	d1,d1
	move.l	d0,a22		;a22=asquare+asquare
	move.l	d1,b22		;b22=bsquare+bsquare
	move.l	a,d2
*	muls	d1,d2		;xslope=b22*a	(68030, muls.l)
*	dc.w	%0100110000000001
*	dc.w	%0010100000000000
	muls.l	d1,d2
	

	move.l	d2,xslope
	move.l	#0,yslope	;yslope=0
	move.l	bsquare,d0
	move.l	a,d1
	neg.l	d1
*	muls	d1,d0		;This may have to be muls.l (68030)
*	dc.w	%0100110000000001
*	dc.w	%0000100000000000
	muls.l	d1,d0
	
	add.l	asquare,d0
	move.l	d0,fmid		;fmid=bsquare*(.25-a)+asquare
	move.l	xe,d0
	move.l	ye,d1
	move.l	xslope,d2
	move.l	yslope,d3
	move.l	fmid,d4
	move.l	asquare,d5
	move.l	a22,d6
	move.l	b22,d7
eloop1:
	cmp.l	d3,d2
	ble	beginnext
*	bsr	doplote
	move.w	d0,(a0)+
	move.w	d1,(a0)+
	addq.l	#1,d1		;y=y+1
	add.l	d6,d3
	tst.l	d4		;if fmid<0
	bgt.s	fmt1
	add.l	d3,d4		;fmid=fmid+yslope+asquare
	add.l	d5,d4
	bra.s	eloop1
fmt1:
	subq.l	#1,d0		;x=x-1
	sub.l	d7,d2		;xslope=xslope-b22
	sub.l	d2,d4		;fmid=fmid-xslope+yslope+asquare
	add.l	d3,d4
	add.l	d5,d4
	bra	eloop1
beginnext:
	move.l	d2,d5
	add.l	d3,d5		;(yslope+xslope)/2
	asr.l	#1,d5
	sub.l	d5,d4
	move.l	bsquare,d5
eloop2:
	cmpi.w	#emptyfill,filltype
	bne.s	skipcoadd
*	bsr	doplote		;only add all points if it's an empty
	move.w	d0,(a0)+	;fill, otherwise only add 1 point per y
	move.w	d1,(a0)+
skipcoadd:
	subq.l	#1,d0		;x=x-1
	sub.l	d7,d2		;xslope=xslope-b22
	tst.l	d4		;if fmid<0
	blt.s	fmt2
	sub.l	d2,d4
	add.l	d5,d4
	tst.l	d0
	bge	eloop2
	move.w	#-1,(a0)+	;signify end of coord list
	rts
fmt2:
	cmpi.w	#emptyfill,filltype
	beq.s	skipcoadd2
	move.w	d0,(a0)+	;fill, otherwise only add 1 point per y
	move.w	d1,(a0)+
skipcoadd2:
	addq.l	#1,d1		;y=y+1
	add.l	d6,d3		;yslope=yslope+a22
	sub.l	d2,d4		;fmid=fmid-xslope+yslope+bsquare
	add.l	d3,d4
	add.l	d5,d4
	tst.l	xe
	bge	eloop2
	move.w	#-1,(a0)+	;signify end of coord list
	rts

*********************************************************
*	dodrawline just draws a line.			*
*********************************************************
dodrawline:
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw	;should be usebrush
	move.w	mx,x1
	move.w	my,y1
	move.w	x1,initialx
	move.w	y1,initialy
	move.w	#-1,oldcoordx
	move.w	buttondat,savebuttondat
	move.w	#0,savex1
	move.w	#0,savey1
	move.w	#15,savex2
	move.w	#1,savey2
	move.w	currentfcolor,color
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	bra	skipmddl
ddlloop:
	tst.w	buttondat
	beq	dlover
	move.w	mx,d0
	move.w	my,d1
	cmp.w	oldmx,d0
	bne	skipmddl
	cmp.w	oldmy,d1
	bne	skipmddl
	bra	ddlloop
skipmddl:
	bsr	restoreunder
	move.w	mx,x2
	move.w	my,y2
	move.w	x2,oldmx
	move.w	y2,oldmy
	move.l	linepattern2,linepattern
	cmp.l	#userbrush,currentbrush
	bne	skipchline
	move.l	#%00000000000000100000000000000010,linepattern	;only draw the brush every 16th pixel
skipchline:
	bsr	drawline
	move.w	x1,savex1
	move.w	y1,savey1
	move.w	x2,savex2
	move.w	y2,savey2

	move.w	savex2,d0		;get finishing x and y
	move.w	savey2,d1
	cmp.w	savex1,d0		;if 2nd x is < 1st then swap em
	bge	noswapdlx
	move.w	savex1,d2
	move.w	d0,savex1
	move.w	d2,d0
noswapdlx:
	cmp.w	savey1,d1		;if 2nd y is < 1st then swap em
	bge	noswapdly
	move.w	savey1,d2
	move.w	d1,savey1
	move.w	d2,d1
noswapdly:
	move.w	d0,savex2
	move.w	d1,savey2

	move.l	currentbrush,a0
	move.w	savex1,d0
	move.w	brushw(a0),d1
	asr.w	#1,d1		;halve it
	addq.w	#1,d1
	sub.w	d1,d0
	bge	ddlx1ok
	move.w	#0,savex1
	bra	ddlx2
ddlx1ok:
	move.w	d0,savex1
ddlx2:
	move.w	savex2,d0
	add.w	d1,d0
	cmpi.w	#319,d0
	ble	ddlx2ok
	move.w	#319,savex2
	bra	ddly1
ddlx2ok:
	move.w	d0,savex2
ddly1:
	move.w	savey1,d0
	move.w	brushh(a0),d1
	asr.w	#1,d1		;halve it
	addq.w	#1,d1
	sub.w	d1,d0
	bge	ddly1ok
	move.w	#0,savey1
	bra	ddly2
ddly1ok:
	move.w	d0,savey1
ddly2:
	move.w	savey2,d0
	add.w	d1,d0
	cmpi.w	#479,d0
	ble	ddly2ok
	move.w	#479,savey2
	bra	ddly3
ddly2ok:
	move.w	d0,savey2
ddly3:
	bra	ddlloop		;put eor line draws in here for rubber banding
dlover:
	bsr	restoreunder
	move.l	linepattern2,linepattern
	bsr	drawline
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff		;turn the mouse off
	bra	paintloop

*********************************************************
*	dodrawline just draws a line.			*
*********************************************************
dodrawbox:
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw	;should be usebrush
	move.w	currentfcolor,color
ddbns:
	move.w	mx,x1
	move.w	my,y1
	move.w	x1,initialx
	move.w	y1,initialy
	move.w	#-1,oldcoordx
	move.w	buttondat,savebuttondat
	move.w	#0,savex1
	move.w	#0,savey1
	move.w	#15,savex2
	move.w	#1,savey2
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	bra	skipmddb
ddbloop:
	tst.w	buttondat
	beq	dbover
	move.w	mx,d0
	move.w	my,d1
	cmp.w	oldmx,d0
	bne	skipmddb
	cmp.w	oldmy,d1
	bne	skipmddb
	bra	ddbloop
skipmddb:
	bsr	restoreunder
	move.w	mx,x2
	move.w	my,y2
	move.w	x2,oldmx
	move.w	y2,oldmy
	move.l	linepattern2,linepattern
	cmp.l	#userbrush,currentbrush
	bne	skipchbox
	move.l	#%00000000000000100000000000000010,linepattern	;only draw the brush every 16th pixel
skipchbox:
	bsr	drawbox2
	move.w	x1,savex1
	move.w	y1,savey1
	move.w	x2,savex2
	move.w	y2,savey2

	move.w	savex2,d0		;get finishing x and y
	move.w	savey2,d1
	cmp.w	savex1,d0		;if 2nd x is < 1st then swap em
	bge	noswapdbx
	move.w	savex1,d2
	move.w	d0,savex1
	move.w	d2,d0
noswapdbx:
	cmp.w	savey1,d1		;if 2nd y is < 1st then swap em
	bge	noswapdby
	move.w	savey1,d2
	move.w	d1,savey1
	move.w	d2,d1
noswapdby:
	move.w	d0,savex2
	move.w	d1,savey2

	move.l	currentbrush,a0
	move.w	savex1,d0
	move.w	brushw(a0),d1
	asr.w	#1,d1		;halve it
	addq.w	#1,d1
	sub.w	d1,d0
	bge	ddbx1ok
	move.w	#0,savex1
	bra	ddbx2
ddbx1ok:
	move.w	d0,savex1
ddbx2:
	move.w	savex2,d0
	add.w	d1,d0
	cmpi.w	#319,d0
	ble	ddbx2ok
	move.w	#319,savex2
	bra	ddby1
ddbx2ok:
	move.w	d0,savex2
ddby1:
	move.w	savey1,d0
	move.w	brushh(a0),d1
	asr.w	#1,d1		;halve it
	addq.w	#1,d1
	sub.w	d1,d0
	bge	ddby1ok
	move.w	#0,savey1
	bra	ddby2
ddby1ok:
	move.w	d0,savey1
ddby2:
	move.w	savey2,d0
	add.w	d1,d0
	cmpi.w	#479,d0
	ble	ddby2ok
	move.w	#479,savey2
	bra	ddby3
ddby2ok:
	move.w	d0,savey2
ddby3:
	bra	ddbloop		;put eor line draws in here for rubber banding
dbover:
	bsr	restoreunder
	move.l	linepattern2,linepattern
	bsr	drawbox2
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff		;turn the mouse off
	bra	paintloop


restoreunder:
	move.w	savey2,d1
	sub.w	savey1,d1
	move.l	currentscreen,a1	;where to get it from
	move.l	undoscreen,a2		;where to put it
	moveq	#0,d2
	move.w	savey1,d2		;get y
*	mulu	#320,d2			;320 bytes per screen line
*	add.l	d2,a1		;correct screen line address
*	add.l	d2,a2

	move.l	d2,d3
	asl.l	#8,d2
	asl.l	#6,d3
	add.l	d2,d3
	add.l	d3,a2
	add.l	d3,a1


	move.w	savex1,d2
	move.w	d2,d0
	and.w	#$fff0,d2	;remove last 4 bits
	add.w	d2,a1		;a1 has the address of top left corner
	add.w	d2,a2		;a1 has the address of top left corner
	move.w	d1,d7		;number of lines to copy

	and.w	#$fff0,d0	;remove last 4 bits
	asr.w	#4,d0
	move.w	savex2,d1
	and.w	#$fff0,d1	;remove last 4 bits
	asr.w	#4,d1
	sub.w	d0,d1
	move.w	d1,d2
	addq.w	#1,d1
	asl.w	#4,d1		;mulu 16
	neg.w	d1
	add.w	#320,d1
quickblitloop1c:
	move.w	d2,d3		;count across
quickblitloop2c:
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	dbf	d3,quickblitloop2c
	add.w	d1,a1
	add.w	d1,a2
	dbf	d7,quickblitloop1c
	rts
*********************************************************
*********************************************************
drawbox2:
	cmpi.w	#emptyfill,filltype
	beq	drawbox
	move.w	x1,savex1
	move.w	y1,savey1
	move.w	x2,savex2
	move.w	y2,savey2

	move.w	savex2,d0		;get finishing x and y
	move.w	savey2,d1
	cmp.w	savex1,d0		;if 2nd x is < 1st then swap em
	bge	noswapdbx2
	move.w	savex1,d2
	move.w	d0,savex1
	move.w	d2,d0
noswapdbx2:
	cmp.w	savey1,d1		;if 2nd y is < 1st then swap em
	bge	noswapdby2
	move.w	savey1,d2
	move.w	d1,savey1
	move.w	d2,d1
noswapdby2:
	move.w	d0,savex2
	move.w	d1,savey2

	sub.w	savex1,d0		;xwidth
	sub.w	savey1,d1		;yheight
	addq.w	#1,d0
	addq.w	#1,d1

*	cmpi.w	#rightbuttondown,savebuttondat
*	beq	dosolidfill

	subq.w	#1,d1
	move.w	d1,d7
	move.w	savex1,d0
	move.w	savex2,d2
	move.w	savey1,d1
dbml1:
	movem.w	d0/d1/d2/d7,-(sp)
	bsr	hline
	movem.w	(sp)+,d0/d1/d2/d7
	addq.w	#1,d1
	dbf	d7,dbml1
	rts

dosolidfill:
	move.w	currentfcolor,d2
	cmpi.w	#rightbuttondown,savebuttondat
	bne.s	nocolch
	move.w	currentbcolor,d2
nocolch:
	lea	drawbltblk,a6
	move.w	d0,b_wd(a6)
	move.w	d1,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	d2,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	savex1,d_xmin(a6)
	move.w	savey1,d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts


*********************************************************
*	Draw the box from x1,y1 to x2,y2		*
*	Using writemode.				*
*							*
*	If it is to be drawn with a brush set todraw	*
*	to usebrush.					*
*							*
*	Set writemode to either:			*
*				wmeor			*
*				wmreplace		*
*********************************************************
drawbox:
	move.w	x1,x1save
	move.w	y1,y1save
	move.w	x2,x2save
	move.w	y2,y2save
	move.w	x1save,x1
	move.w	y1save,y1
	move.w	x2save,x2
	move.w	y1save,y2
	bsr	drawline
	move.w	x2save,x1
	move.w	y1save,y1
	move.w	x2save,x2
	move.w	y2save,y2
	bsr	drawline
	move.w	x2save,x1
	move.w	y2save,y1
	move.w	x1save,x2
	move.w	y2save,y2
	bsr	drawline
	move.w	x1save,x1
	move.w	y2save,y1
	move.w	x1save,x2
	move.w	y1save,y2
	bsr	drawline
	move.w	x1save,x1
	move.w	y1save,y1
	move.w	x2save,x2
	move.w	y2save,y2
	rts

*********************************************************
*	Drawline					*
*							*
*	Linea has let me down as predicted by Atari	*
*	when they said it would not keep pace with new	*
*	developements.					*
*	So what we have here is my ow simple Bresham	*
*	DDA implimentation.				*
*	It uses the value in todraw (brush or pixel)	*
*	And writemode to do its stuff.			*
*	Start and end points must be in x1,y1,x2,y2.	*
*********************************************************
drawline:
	move.w	x1,d0
	sub.w	x2,d0
	bge.s	li10
	neg.w	d0
li10:
	move.w	y1,d1
	sub.w	y2,d1
	bge.s	li20
	neg.w	d1
li20:
	cmp.w	d0,d1
	bgt	ymajor

	move.w	d1,d4
	sub.w	d0,d4
	move.w	d4,d5
	add.w	d4,d4
	add.w	d1,d5
	move.w	d1,d3
	add.w	d1,d3

	move.w	#1,xadd
	move.w	#1,yadd
	move.w	x1,d6
	move.w	y1,d7
	move.w	x2,d2
	cmp.w	x2,d6
	ble.s	li30
	move.w	#-1,xadd
li30:
	cmp.w	y2,d7
	ble.s	xmajor1
	move.w	#-1,yadd
xmajor1:
	move.w	d6,d0
	move.w	d7,d1
	bsr	putpoint
	bra.s	xm1li80
xm1li50:
	add.w	xadd,d6
	tst.w	d5
	bge.s	xm1li60
	add.w	d3,d5
	bra.s	xm1li70
xm1li60:
	add.w	yadd,d7
	add.w	d4,d5
xm1li70:
	move.w	d6,d0
	move.w	d7,d1
	bsr	putpoint
xm1li80:
	cmp.w	d6,d2
	bne.s	xm1li50
	rts
ymajor:
	move.w	d0,d4
	sub.w	d1,d4
	move.w	d4,d5
	add.w	d4,d4
	add.w	d0,d5
	move.w	d0,d3
	add.w	d0,d3
	move.w	#1,xadd
	move.w	#1,yadd
	move.w	y1,d6
	move.w	x1,d7
	move.w	y2,d2
	cmp.w	y2,d6
	ble.s	li30b
	move.w	#-1,yadd
li30b:
	cmp.w	x2,d7
	ble.s	ymajor1
	move.w	#-1,xadd
ymajor1:
	move.w	d7,d0
	move.w	d6,d1
	bsr	putpoint
	bra.s	ym1li80
ym1li50:
	add.w	yadd,d6
	tst.w	d5
	bge.s	ym1li60
	add.w	d3,d5
	bra.s	ym1li70
ym1li60:
	add.w	xadd,d7
	add.w	d4,d5
ym1li70:
	move.w	d7,d0
	move.w	d6,d1
	bsr	putpoint
ym1li80:
	cmp.w	d6,d2
	bne.s	ym1li50
	rts

*********************************************************
*	This guy does the dirty work, using todraw and	*
*	writemode and color.				*
*	x,y in d0,d1					*
*********************************************************
putpoint:
	movem.l	d0-a6,-(sp)	;save the important guys
	cmpi.w	#clipxmin,d0
	blt	eplaneover
	cmpi.w	#clipymin,d1
	blt	eplaneover
	cmpi.w	#clipxmax,d0
	bgt	eplaneover
	cmpi.w	#clipymax,d1
	bgt	eplaneover
	cmpi.w	#usebrush,todraw
	beq	ppwithbrush
	move.l	currentscreen,a0

	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a0

	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a0		;a0 has the address of pixels word
	and.w	#$000f,d2	;get last 4 pixels
	move.w	#$8000,d1
	lsr.w	d2,d1		;d1 has bit in correct place to put up
	move.w	d1,d2
	eor.l	#-1,d2		;invert for clear mask
	cmpi.w	#wmreplace,writemode
	beq	ppreplace
ppeor:
	move.w	color,d0	;color value
eplane0:
	btst	#0,d0
	beq	eplane1
	eor.w	d1,(a0)
eplane1:
	btst	#1,d0
	beq	eplane2
	eor.w	d1,2(a0)
eplane2:
	btst	#2,d0
	beq	eplane3
	eor.w	d1,4(a0)
eplane3:
	btst	#3,d0
	beq	eplane4
	eor.w	d1,6(a0)
eplane4:
	btst	#4,d0
	beq	eplane5
	eor.w	d1,8(a0)
eplane5:
	btst	#5,d0
	beq	eplane6
	eor.w	d1,10(a0)
eplane6:
	btst	#6,d0
	beq	eplane7
	eor.w	d1,12(a0)
eplane7:
	btst	#7,d0
	beq	eplaneover
	eor.w	d1,14(a0)
eplaneover:
	movem.l	(sp)+,d0-a6
	rts
ppreplace:
	and.w	d2,(a0)		;clear out the old pixel
	and.w	d2,2(a0)
	and.w	d2,4(a0)
	and.w	d2,6(a0)
	and.w	d2,8(a0)
	and.w	d2,10(a0)
	and.w	d2,12(a0)
	and.w	d2,14(a0)
	move.w	color,d0	;color value
rplane0:
	btst	#0,d0
	beq	rplane1
	or.w	d1,(a0)
rplane1:
	btst	#1,d0
	beq	rplane2
	or.w	d1,2(a0)
rplane2:
	btst	#2,d0
	beq	rplane3
	or.w	d1,4(a0)
rplane3:
	btst	#3,d0
	beq	rplane4
	or.w	d1,6(a0)
rplane4:
	btst	#4,d0
	beq	rplane5
	or.w	d1,8(a0)
rplane5:
	btst	#5,d0
	beq	rplane6
	or.w	d1,10(a0)
rplane6:
	btst	#6,d0
	beq	rplane7
	or.w	d1,12(a0)
rplane7:
	btst	#7,d0
	beq	rplaneover
	or.w	d1,14(a0)
rplaneover:
	movem.l	(sp)+,d0-a6
	rts
ppreplace2:
	and.w	d2,(a0)		;clear out the old pixel
	and.w	d2,2(a0)
	and.w	d2,4(a0)
	and.w	d2,6(a0)
	and.w	d2,8(a0)
	and.w	d2,10(a0)
	and.w	d2,12(a0)
	and.w	d2,14(a0)
	move.w	color,d0	;color value
rplane02:
	btst	#0,d0
	beq	rplane12
	or.w	d1,(a0)
rplane12:
	btst	#1,d0
	beq	rplane22
	or.w	d1,2(a0)
rplane22:
	btst	#2,d0
	beq	rplane32
	or.w	d1,4(a0)
rplane32:
	btst	#3,d0
	beq	rplane42
	or.w	d1,6(a0)
rplane42:
	btst	#4,d0
	beq	rplane52
	or.w	d1,8(a0)
rplane52:
	btst	#5,d0
	beq	rplane62
	or.w	d1,10(a0)
rplane62:
	btst	#6,d0
	beq	rplane72
	or.w	d1,12(a0)
rplane72:
	btst	#7,d0
	beq	rplaneover2
	or.w	d1,14(a0)
rplaneover2:
	movem.l	(sp)+,d0-d2/a0
	rts

ppwithbrush:
	move.l	linepattern,d7
	ror.l	#1,d7
	move.l	d7,linepattern
	btst	#0,d7
	beq	skipp
	move.l	currentbrush,a0
	cmpi.w	#rightbuttondown,savebuttondat
	bne	ppwb2
	move.w	#bltclear,blittype
	bra.s	ppwb3
ppwb2:
	move.w	#bltbrush,blittype
ppwb3:
	move.w	d0,d6
	move.w	d1,d7
	bsr	clipndrawbrush
skipp:
	movem.l	(sp)+,d0-a6
	rts

getpoint4:
	movem.l	d0-d3/a0/a6,-(sp)	;save the important guys
*	bra.s	normalget
	lea	fillplane,a0
	and.l	#$0000ffff,d1
	move.l	d1,d2
	move.l	d2,d3
	asl.l	#5,d2
	asl.l	#3,d3
	add.l	d3,d2
	add.l	d2,a0

	move.w	d0,d2		;copy x
	move.w	d2,d3
	and.w	#$fff0,d3	;remove last 4 bits
	asr.w	#3,d3
	add.w	d3,a0

	and.w	#$000f,d2	;get last 4 pixels
	moveq	#15,d3
	sub.w	d2,d3
	move.w	(a0),d2
	btst	d3,d2
	beq	normalget
	move.w	#256,color
	movem.l	(sp)+,d0-d3/a0/a6
	rts

normalget:
*	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a6

	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a6		;a0 has the address of pixels word
	and.w	#$000f,d2	;get last 4 pixels
	moveq	#15,d1
	sub.w	d2,d1
	moveq	#0,d0
gp14:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp24
	or.w	#1,d0
gp24:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp34
	or.w	#2,d0
gp34:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp44
	or.w	#4,d0
gp44:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp54
	or.w	#8,d0
gp54:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp64
	or.w	#16,d0
gp64:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp74
	or.w	#32,d0
gp74:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp84
	or.w	#64,d0
gp84:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp94
	or.w	#128,d0
gp94:
	move.w	d0,color
	movem.l	(sp)+,d0-d3/a0/a6
	rts

*********************************************************
*	gets the color of a point in a screen pointed	*
*	to by a6					*
*	x,y in d0,d1					*
*********************************************************
getpoint:
	movem.l	d0-d7/a6,-(sp)	;save the important guys
	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a6

	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a6		;a0 has the address of pixels word
	and.w	#$000f,d2	;get last 4 pixels
	moveq	#15,d1
	sub.w	d2,d1
	moveq	#0,d0
gp1:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp2
	or.w	#1,d0
gp2:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp3
	or.w	#2,d0
gp3:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp4
	or.w	#4,d0
gp4:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp5
	or.w	#8,d0
gp5:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp6
	or.w	#16,d0
gp6:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp7
	or.w	#32,d0
gp7:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp8
	or.w	#64,d0
gp8:
	move.w	(a6)+,d2
	btst	d1,d2
	beq.s	gp9
	or.w	#128,d0
gp9:
	move.w	d0,color
	movem.l	(sp)+,d0-d7/a6
	rts
putpoint2:
	movem.l	d0-d2/a0,-(sp)	;save the important guys
	move.l	a6,a0
	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a0
	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a0		;a0 has the address of pixels word
	and.w	#$000f,d2	;get last 4 pixels
	move.w	#$8000,d1
	lsr.w	d2,d1		;d1 has bit in correct place to put up
	move.w	d1,d2
	eor.l	#-1,d2		;invert for clear mask
	bra	ppreplace2

putpoint3:
	movem.l	d0-d2/a0,-(sp)	;save the important guys
	move.l	a6save,a0
	move.w	d1save,d1
	move.w	d2save,d2
	bra	ppreplace2

*********************************************************
*	get a brush					*
*********************************************************
getabrush:
	move.w	mx,x1
	move.w	my,y1
	move.w	x1,initialx
	move.w	y1,initialy
	move.w	#-1,oldcoordx
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	move.w	#usepixel,todraw	;set what to draw box with
	move.w	#wmeor,writemode
	move.w	#255,color
	move.w	#-1,generalvalue
	move.w	#-1,x2
	move.w	#-1,y2
	move.w	buttondat,savebuttondat
	bra	skipmbrush
getbrushloop:
	tst.w	buttondat
	beq	gbover
skipmbrush:
	move.w	mx,d0
	move.w	my,d1
	cmp.w	x2,d0
	bne	willdrawbox
	cmp.w	y2,d1
	bne	willdrawbox
	bra	getbrushloop
willdrawbox:
	tst.w	generalvalue
	bmi	noboxtoundraw
	movem.w	d0/d1,-(sp)
	bsr	drawbox		;get rid of old box
	movem.w	(sp)+,d0/d1
noboxtoundraw:
	move.w	d0,x2
	move.w	d1,y2
	move.w	#1,generalvalue
	bsr	drawbox
	bra	getbrushloop
gbover:
	bsr	drawbox
	move.w	x2,d0		;get finishing x and y
	move.w	y2,d1
	cmp.w	x1,d0		;if 2nd x is < 1st then swap em
	bge	noswapgbx
	move.w	x1,d2
	move.w	d0,x1
	move.w	d2,d0
noswapgbx:
	cmp.w	y1,d1		;if 2nd y is < 1st then swap em
	bge	noswapgby
	move.w	y1,d2
	move.w	d1,y1
	move.w	d2,d1
noswapgby:
	addq.w	#1,d0		;no zero size brushes
	addq.w	#1,d1
gbtest:
	move.w	d0,x2
	move.w	d1,y2
	sub.w	x1,d0		;xwidth
	sub.w	y1,d1		;yheight
	lea	userbrush,a0	;user brush
	move.w	d0,brushw(a0)
	move.w	d1,brushh(a0)


	lea	drawbltblk,a6
	move.w	brushw(a0),b_wd(a6)	;now blit it into save area
	move.w	brushh(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	currentfcolor,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	x1,s_xmin(a6)
	move.w	y1,s_ymin(a6)
	move.l	currentscreen,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)

	move.w	#0,d_xmin(a6)
	move.w	#0,d_ymin(a6)
	move.l	brushdat(a0),d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	lea	userbrush,a0	;user brush
	move.w	brushw(a0),d0
	move.w	brushh(a0),d1
	asr.w	#1,d0
	asr.w	#1,d1
	move.w	d1,mouseoffsety
	move.w	d0,mouseoffsetx

	cmpi.w	#rightbuttondown,savebuttondat
	bne	normalgetbrush
	subq.w	#1,x2
	subq.w	#1,y2
	move.w	currentbcolor,color
	move.l	currentscreen,a0
	bsr	filledbox
normalgetbrush:
	bsr	removebackbrush
	bsr	makemask
	bsr	handlemosaic
	move.w	#mattemode,brushmode
	lea	p4sdmmatte,a0
	bsr	raisebuttons
	lea	p1getbrush,a0		;raise the button
	bsr	putbuttonup
	move.w	#1,mouseonoff
	move.l	#userbrush,currentbrush
*	move.w	oldpaintmode,paintmode
*	move.l	#-1,oldpaintmode
	move.w	#-1,oldcoordx
*	move.w	shouldfollowsave,shouldfollow
	move.l	linepattern2,linepattern
*	cmpi.w	#drawingcurve,paintmode
	bne.s	ndc1
*	bsr	drawcurve
*	bsr	drawbrush
*	bsr	drawpoints
ndc1:
	lea	p1dots,a0
	bsr	raisebuttons2
	lea	p1dots,a0
	bsr	putbuttondown
	bra	setdrawmodedots
*	bra	paintloop

makemosaic:
	lea	userbrush,a0	;now we mosaic the brush all over the
				;brush screen for patternfills
	move.l	#319,d0
	divu	brushw(a0),d0	;gives number of times across
	move.l	#479,d1
	divu	brushh(a0),d1	;gives number of times up and down
	moveq	#0,d7
mosaic1:
	moveq	#0,d6
	move.w	d0,d2
mosaic2:
	movem.l	d0-d2/d6/d7/a0,-(sp)
	bsr	clipndrawbrush2
	movem.l	(sp)+,d0-d2/d6/d7/a0
	add.w	brushw(a0),d6
	dbf	d2,mosaic2
	add.w	brushh(a0),d7
	dbf	d1,mosaic1
	rts

makemask:
	tst.w	currentbcolor
	beq	normalmask
normalmask:
	lea	userbrush,a0
	lea	userbrushmask,a6
	move.w	brushw(a0),brushw(a6)
	move.w	brushh(a0),brushh(a6)
*	move.w	brushw(a0),d0	;get width
*	subq.w	#1,d0
*	divu	#16,d0		;get number of words across
	move.l	brushdat(a0),a1
	move.l	brushdat(a6),a2
	move.w	brushh(a0),d1	;number of pixels high
	subq.w	#1,d1
makemaskl1:
	rept	40
	moveq	#0,d3
	move.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	or.w	(a1)+,d3
	move.w	d3,(a2)+
	endr
	dbf	d1,makemaskl1
	rts

*********************************************************
*	Checks all the buttons the button in a0 should	*
*	raise and sets them as up.			*
*********************************************************
raisebuttons:
	move.w	#1,buttonselected(a0)
	lea	buttondeselects(a0),a0
rbuttonsup:
	move.l	(a0)+,d0
	beq	rbudover	;have we finished the list to raise
	move.l	d0,a2		;get address of button
	tst.w	buttonselected(a2)
	beq	rbuttonsup	;was it actually down ?
	move.w	#0,buttonselected(a2)	;set the select flag to not
	bra	rbuttonsup
rbudover:
	rts
*********************************************************
*	As above but actually does it graphically as	*
*	well.						*
*********************************************************
raisebuttons2:
	move.w	#1,buttonselected(a0)
	lea	buttondeselects(a0),a0
rbuttonsup2:
	move.l	(a0)+,d0
	beq	rbudover2	;have we finished the list to raise
	move.l	d0,a2		;get address of button
	tst.w	buttonselected(a2)
	beq	rbuttonsup2	;was it actually down ?
	movem.l	a0/a2,-(sp)
	move.l	a2,a0
	bsr	putbuttonup
	movem.l	(sp)+,a0/a2
	move.w	#0,buttonselected(a2)	;set the select flag to not
	bra	rbuttonsup2
rbudover2:
	rts

*********************************************************
*	Routine to remove background color from brush	*
*	Uses currentbcolor.				*
*********************************************************
removebackbrush:
	lea	userbrush,a0
	move.w	brushh(a0),d7
	move.w	brushw(a0),d6
	subq.w	#1,d7
	subq.w	#1,d6
	move.l	brushdat(a0),a0
	move.w	currentbcolor,d4
	beq	rmbb
	moveq	#0,d1
rmbbloop1:
	moveq	#0,d0
	move.w	d6,d5
rmbbloop2:
	move.l	a0,a6
	bsr	getpoint2
	cmp.w	color,d4
	bne	skiprmbb
*	move.w	#0,color
*	bsr	putpoint3

	move.w	#$8000,d2
	lsr.w	d3,d2		;d1 has bit in correct place to put up
	eor.w	#-1,d2		;invert for clear mask
	and.w	d2,(a6)		;clear out the old pixel
	and.w	d2,2(a6)
	and.w	d2,4(a6)
	and.w	d2,6(a6)
	and.w	d2,8(a6)
	and.w	d2,10(a6)
	and.w	d2,12(a6)
	and.w	d2,14(a6)

skiprmbb:
	addq.w	#1,d0
	dbf	d5,rmbbloop2
	addq.w	#1,d1
	dbf	d7,rmbbloop1
rmbb:
	rts

getpoint2:
	movem.l	d0-d1,-(sp)	;save the important guys
*	mulu	#320,d1		;y times line in bytes
*	add.l	d1,a6

	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a6

	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a6		;a0 has the address of pixels word
	and.w	#$000f,d2	;get last 4 pixels
	moveq	#15,d1
	sub.w	d2,d1
	move.w	d2,d3
	moveq	#0,d0
gp12:
	move.w	(a6),d2
	btst	d1,d2
	beq.s	gp22
	or.w	#1,d0
gp22:
	move.w	2(a6),d2
	btst	d1,d2
	beq.s	gp32
	or.w	#2,d0
gp32:
	move.w	4(a6),d2
	btst	d1,d2
	beq.s	gp42
	or.w	#4,d0
gp42:
	move.w	6(a6),d2
	btst	d1,d2
	beq.s	gp52
	or.w	#8,d0
gp52:
	move.w	8(a6),d2
	btst	d1,d2
	beq.s	gp62
	or.w	#16,d0
gp62:
	move.w	10(a6),d2
	btst	d1,d2
	beq.s	gp72
	or.w	#32,d0
gp72:
	move.w	12(a6),d2
	btst	d1,d2
	beq.s	gp82
	or.w	#64,d0
gp82:
	move.w	14(a6),d2
	btst	d1,d2
	beq.s	gp92
	or.w	#128,d0
gp92:
	move.w	d0,color
	movem.l	(sp)+,d0-d1
	rts
getpoint3:
	movem.l	d0-d3,-(sp)	;save the important guys
*	mulu	#320,d1		;y times line in bytes
*	add.l	d1,a6

	and.l	#$0000ffff,d1
	move.l	d1,d2
	asl.l	#8,d1
	asl.l	#6,d2
	add.l	d1,d2
	add.l	d2,a6


	move.w	d0,d2		;copy x
	and.w	#$fff0,d0	;remove last 4 bits
	add.w	d0,a6		;a0 has the address of pixels word

	and.w	#$000f,d2	;get last 4 pixels
	move.w	d2,d3
	move.w	#$8000,d1
	lsr.w	d2,d1		;d1 has bit in correct place to put up
	move.w	d1,d2
	eor.l	#-1,d2		;invert for clear mask
	move.w	d1,d1save
	move.w	d2,d2save
	move.w	d3,d2
	moveq	#15,d1
	sub.w	d2,d1
	move.w	d2,d3
	moveq	#0,d0
	move.l	a6,a6save
gp13:
	move.w	(a6),d2
	btst	d1,d2
	beq.s	gp23
	or.w	#1,d0
gp23:
	move.w	2(a6),d2
	btst	d1,d2
	beq.s	gp33
	or.w	#2,d0
gp33:
	move.w	4(a6),d2
	btst	d1,d2
	beq.s	gp43
	or.w	#4,d0
gp43:
	move.w	6(a6),d2
	btst	d1,d2
	beq.s	gp53
	or.w	#8,d0
gp53:
	move.w	8(a6),d2
	btst	d1,d2
	beq.s	gp63
	or.w	#16,d0
gp63:
	move.w	10(a6),d2
	btst	d1,d2
	beq.s	gp73
	or.w	#32,d0
gp73:
	move.w	12(a6),d2
	btst	d1,d2
	beq.s	gp83
	or.w	#64,d0
gp83:
	move.w	14(a6),d2
	btst	d1,d2
	beq.s	gp93
	or.w	#128,d0
gp93:
	move.w	d0,color
	movem.l	(sp)+,d0-d3
	rts

preparecycle:
	move.w	currentbcolor,d0	;for cycle mode
	move.w	currentfcolor,d1	;for cycle mode
	cmp.w	d0,d1
	bge	ppcok
	exg	d0,d1
ppcok:
	move.w	d0,colorcyclelow
	move.w	d1,colorcyclehigh
	move.w	currentfcolor,color
	rts


*********************************************************
*	Draw the brush					*
*********************************************************
ttdots:
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	undrawbrush
	bsr	copyforundo
	bsr	preparecycle
	move.w	mx,d6
	move.w	my,d7
	move.w	d6,initialx
	move.w	d7,initialy
	move.w	#-1,oldcoordx
	bra	skipmdots
ttdotsloop:
	tst.w	buttondat
	beq	dotsover
	move.w	mx,d6
	move.w	my,d7
	cmp.w	oldmx,d6
	bne	isdiffdots
	cmp.w	oldmy,d7
	bne	isdiffdots
	bra	ttdotsloop
isdiffdots:
	move.w	d6,oldmx
	move.w	d7,oldmy
	cmpi.w	#cyclemode,brushmode
	bne	skipmdots
	addq.w	#1,color
	move.w	color,d0
	cmp.w	colorcyclehigh,d0
	ble	skipmdots
	move.w	colorcyclelow,color
skipmdots:
	cmpi.w	#leftbuttondown,buttondat
	bne	dotsright
	move.l	currentbrush,a0
*	move.w	mx,d6
*	move.w	my,d7
	move.w	#bltbrush,blittype
	bsr	clipndrawbrush
	bra	ttdotsloop
dotsright:
	cmpi.w	#rightbuttondown,buttondat
	bne	dotsover
	move.l	currentbrush,a0
	move.w	#bltclear,blittype	;blitclear
	bsr	clipndrawbrush
	bra	ttdotsloop
dotsover:
*	bsr	setfcolor2
*	bsr	setbcolor2
*	bsr	setfcolorbar
*	bsr	setbcolorbar
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff
	bra	paintloop

*********************************************************
*	Draw the brush					*
*********************************************************
ttdraw:
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	move.l	#-1,linepattern
	bsr	undrawbrush
	bsr	copyforundo
	move.w	mx,x1
	move.w	my,y1
	move.w	x1,initialx
	move.w	x2,initialy
	move.l	#-1,linepattern
	move.w	#-1,oldcoordx
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw
	move.w	buttondat,savebuttondat
	bsr	preparecycle
	bra	skipmdraw
ttdrawloop:
	tst.w	buttondat
	beq	drawover
	cmpi.w	#cyclemode,brushmode
	bne	skipmdraw
	addq.w	#1,color
	move.w	color,d0
	cmp.w	colorcyclehigh,d0
	ble	skipmdraw
	move.w	colorcyclelow,color
skipmdraw:
	cmpi.w	#leftbuttondown,buttondat
	bne	drawright
	move.w	#bltbrush,blittype
*	move.w	currentfcolor,color
	move.w	mx,x2
	move.w	my,y2
	bsr	drawline
	move.w	x2,x1
	move.w	y2,y1
	bra	ttdrawloop
drawright:
	cmpi.w	#rightbuttondown,buttondat
	bne	drawover
	move.w	mx,x2
	move.w	my,y2
	move.w	#usebrush,todraw
	move.w	#bltclear,blittype
	bsr	drawline
	move.w	x2,x1
	move.w	y2,y1
	bra	ttdrawloop
drawover:
*	bsr	setfcolor2
*	bsr	setbcolor2
*	bsr	setfcolorbar
*	bsr	setbcolorbar
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff
	bra	paintloop

*********************************************************
*	Brush address in a0.				*
*	mx,in d6 my in d7				*
*	Calculates new w and h if need be and fills in	*
*	brush xmin, ymin, h2 w2.			*
*********************************************************
clipndrawbrush:
	move.w	d6,d0
	sub.w	mouseoffsetx,d0
	bge.s	noclipbxleft
	move.w	brushw(a0),d1	;get width
	add.w	d0,d1		;calc new width
	move.w	d1,brushw2(a0)
	move.w	#0,brushx(a0)
	neg.w	d0
	move.w	d0,brushxmin(a0)	;dist across brush to begin at
	bra	clipby
noclipbxleft:
	move.w	d0,brushx(a0)
	move.w	#320,d1
	sub.w	brushw(a0),d1
	sub.w	d1,d0
	bmi	noclipxatall
	move.w	#0,brushxmin(a0)
	move.w	brushw(a0),d1
	sub.w	d0,d1
	move.w	d1,brushw2(a0)
	bra	clipby
noclipxatall:
	move.w	brushw(a0),brushw2(a0)
	move.w	#0,brushxmin(a0)
clipby:
	move.w	d7,d0
	move.w	brushh(a0),d1	;and clip if necessary
	sub.w	mouseoffsety,d0
	bge.s	noclipbyup
	move.w	brushh(a0),d1	;get width
	add.w	d0,d1		;calc new width
	move.w	d1,brushh2(a0)
	move.w	#0,brushy(a0)
	neg.w	d0
	move.w	d0,brushymin(a0)
	bra	clipbover
noclipbyup:
	move.w	d0,brushy(a0)
	move.w	#480,d1
	sub.w	brushh(a0),d1
	sub.w	d1,d0
	bmi	noclipyatall
	move.w	#0,brushymin(a0)
	move.w	brushh(a0),d1
	sub.w	d0,d1
	move.w	d1,brushh2(a0)
	bra	clipbover
noclipyatall:
	move.w	brushh(a0),brushh2(a0)
	move.w	#0,brushymin(a0)
clipbover:
	cmpi.w	#bltintobrush,blittype
	beq	blitintobrush
	cmpi.w	#bltbrush,blittype
	beq	blitbrush
	cmpi.w	#bltclear,blittype
	beq	blitclear
	rts

clipndrawbrush2:
	move.w	mouseoffsetx,-(sp)
	move.w	mouseoffsety,-(sp)
	move.w	#0,mouseoffsetx
	move.w	#0,mouseoffsety

	move.w	d6,d0
	move.w	brushw(a0),d1	;and clip if necessary
	sub.w	mouseoffsetx,d0
	bge.s	noclipbxleft2
	move.w	brushw(a0),d1	;get width
	add.w	d0,d1		;calc new width
	move.w	d1,brushw2(a0)
	move.w	#0,brushx(a0)
	neg.w	d0
	move.w	d0,brushxmin(a0)	;dist across brush to begin at
	bra	clipby2
noclipbxleft2:
	move.w	d0,brushx(a0)
	move.w	#320,d1
	sub.w	brushw(a0),d1
	sub.w	d1,d0
	bmi	noclipxatall2
	move.w	#0,brushxmin(a0)
	move.w	brushw(a0),d1
	sub.w	d0,d1
	move.w	d1,brushw2(a0)
	bra	clipby2
noclipxatall2:
	move.w	brushw(a0),brushw2(a0)
	move.w	#0,brushxmin(a0)
clipby2:
	move.w	d7,d0
	move.w	brushh(a0),d1	;and clip if necessary
	sub.w	mouseoffsety,d0
	bge.s	noclipbyup2
	move.w	brushh(a0),d1	;get width
	add.w	d0,d1		;calc new width
	move.w	d1,brushh2(a0)
	move.w	#0,brushy(a0)
	neg.w	d0
	move.w	d0,brushymin(a0)
	bra	clipbover2
noclipbyup2:
	move.w	d0,brushy(a0)
	move.w	#480,d1
	sub.w	brushh(a0),d1
	sub.w	d1,d0
	bmi	noclipyatall2
	move.w	#0,brushymin(a0)
	move.w	brushh(a0),d1
	sub.w	d0,d1
	move.w	d1,brushh2(a0)
	bra	clipbover2
noclipyatall2:
	move.w	brushh(a0),brushh2(a0)
	move.w	#0,brushymin(a0)
clipbover2:
	lea	drawbltblk,a6
	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	brushlogic(a0),op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a0),s_form(a6)
	move.w	brushnxwd(a0),s_nxwd(a6)
	move.w	brushnxln(a0),s_nxln(a6)
	move.w	brushnxpl(a0),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	brushdat(a0),d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.w	(sp)+,mouseoffsety
	move.w	(sp)+,mouseoffsetx
	rts


brushx:		equ	0
brushy:		equ	2
brushw:		equ	4
brushh:		equ	6
brushlogic:	equ	8
brushplanes:	equ	12
brushnxwd:	equ	14
brushnxln:	equ	16
brushnxpl:	equ	18
brushw2:	equ	20
brushh2:	equ	22
brushxmin:	equ	24
brushymin:	equ	26
brushsaved:	equ	28
brushmask:	equ	30
brushdat:	equ	34
*********************************************************
*	On entry have a0 pointing at brush header	*
*							*
*	This will blit the thing onto current screen	*
*	using currentfcolor and currentbcolor and the	*
*	logic op in it's header				*
*********************************************************
blitbrush:
	tst.l	brushmask(a0)
	bne	isuserb			;if it's a user brush and has a mask
goreplace:
	lea	drawbltblk,a6
	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	brushlogic(a0),op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a0),s_form(a6)
	move.w	brushnxwd(a0),s_nxwd(a6)
	move.w	brushnxln(a0),s_nxln(a6)
	move.w	brushnxpl(a0),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts

isuserb:
	cmpi.w	#replacemode,brushmode
	beq	goreplace
	cmpi.w	#colormode,brushmode
	beq	doascolor
	cmpi.w	#cyclemode,brushmode
	beq	doascolor
	tst.w	canceltransparent
	bpl.s	skiptp
	cmpi.w	#transparentmode,brushmode
	beq	dotransparent
skiptp:
	lea	drawbltblk,a6
	move.l	brushmask(a0),a1

	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#0,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	brushlogic(a1),op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)	;use clipped values from brush not mask
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a1),s_form(a6)
	move.w	brushnxwd(a1),s_nxwd(a6)
	move.w	brushnxln(a1),s_nxln(a6)
	move.w	brushnxpl(a1),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	a0,-(sp)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.l	(sp)+,a0
	lea	drawbltblk,a6
	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#0,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$07070707,op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a0),s_form(a6)
	move.w	brushnxwd(a0),s_nxwd(a6)
	move.w	brushnxln(a0),s_nxln(a6)
	move.w	brushnxpl(a0),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts

doascolor:
	lea	drawbltblk,a6
	move.l	brushmask(a0),a1

	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)	;use clipped values from brush not mask
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a1),s_form(a6)
	move.w	brushnxwd(a1),s_nxwd(a6)
	move.w	brushnxln(a1),s_nxln(a6)
	move.w	brushnxpl(a1),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	a0,-(sp)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.l	(sp)+,a0
	rts

*********************************************************
*	On entry have a0 pointing at brush header	*
*							*
*	This will blit the thing onto current screen	*
*	using currentfcolor and currentbcolor and the	*
*	logic op in it's header				*
*********************************************************
blitclear:
	tst.l	brushmask(a0)
	beq	bcuser			;if it's a user brush and has a mask
	move.w	currentbcolor,color
	bra	doascolor
bcuser:
	lea	drawbltblk,a6
	move.w	brushw2(a0),b_wd(a6)
	move.w	brushh2(a0),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	currentbcolor,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	brushlogic(a0),op_tab(a6)
	move.w	brushxmin(a0),s_xmin(a6)
	move.w	brushymin(a0),s_ymin(a6)
	move.l	brushdat(a0),s_form(a6)
	move.w	brushnxwd(a0),s_nxwd(a6)
	move.w	brushnxln(a0),s_nxln(a6)
	move.w	brushnxpl(a0),s_nxpl(a6)

	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	currentscreen,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts


*********************************************************
*	On entry have a0 pointing at brush header	*
*							*
*	This will blit the current screen background	*
*	to our brush area				*
*********************************************************
blitintobrush:
	move.l	currentscreen,a1	;where to get it from
	move.l	brushdat(a0),a2		;where to put it
	moveq	#0,d0
	move.w	brushy(a0),d0		;get y
*	mulu	#320,d0			;320 bytes per screen line
*	add.l	d0,a1		;correct screen line address

	move.l	d0,d2
	asl.l	#8,d0
	asl.l	#6,d2
	add.l	d0,d2
	add.l	d2,a1

	move.w	brushx(a0),d2
	move.w	d2,d0
	move.w	d0,d1
	and.w	#$fff0,d2	;remove last 4 bits
	add.w	d2,a1		;a1 has the address of top left corner
	move.w	brushh(a0),d7		;number of lines to copy
	subq.w	#1,d7

	and.w	#$fff0,d0	;remove last 4 bits
	asr.w	#4,d0
*	move.w	savex2,d1
	add.w	brushw(a0),d1
	subq.w	#1,d1
	and.w	#$fff0,d1	;remove last 4 bits
	asr.w	#4,d1
	sub.w	d0,d1
	move.w	d1,d2
	addq.w	#1,d1
	asl.w	#4,d1		;mulu 16
	neg.w	d1
	add.w	#320,d1
quickblitloop1:
	move.w	d2,d3		;count across
quickblitloop2:
	move.l	(a1)+,(a2)+
	move.l	(a1)+,(a2)+
	move.l	(a1)+,(a2)+
	move.l	(a1)+,(a2)+
	dbf	d3,quickblitloop2
	add.w	d1,a1
	dbf	d7,quickblitloop1
	rts
*********************************************************
*	On entry have a0 pointing at brush header	*
*							*
*	This will blit the thing onto current screen	*
*	using currentfcolor and currentbcolor and the	*
*	logic op in it's header				*
*********************************************************
blitback:
	move.l	currentscreen,a1	;where to get it from
	move.l	brushdat(a0),a2		;where to put it
	moveq	#0,d0
	move.w	brushy(a0),d0		;get y
*	mulu	#320,d0			;320 bytes per screen line
*	add.l	d0,a1		;correct screen line address

	move.l	d0,d2
	asl.l	#8,d0
	asl.l	#6,d2
	add.l	d0,d2
	add.l	d2,a1


	move.w	brushx(a0),d2
	move.w	d2,d0
	move.w	d0,d1
	and.w	#$fff0,d2	;remove last 4 bits
	add.w	d2,a1		;a1 has the address of top left corner
	move.w	brushh(a0),d7		;number of lines to copy
	subq.w	#1,d7

	and.w	#$fff0,d0	;remove last 4 bits
	asr.w	#4,d0
*	move.w	savex2,d1
	add.w	brushw(a0),d1
	subq.w	#1,d1
	and.w	#$fff0,d1	;remove last 4 bits
	asr.w	#4,d1
	sub.w	d0,d1
	move.w	d1,d2
	addq.w	#1,d1
	asl.w	#4,d1		;mulu 16
	neg.w	d1
	add.w	#320,d1

quickblitloop1b:
	move.w	d2,d3		;count across
quickblitloop2b:
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	move.l	(a2)+,(a1)+
	dbf	d3,quickblitloop2b
	add.w	d1,a1
	dbf	d7,quickblitloop1b
	rts

clearfillplane:
	lea	fillplane,a0
	move.w	#479,d0
	moveq	#0,d1
cfpl:
	rept	10
	move.l	d1,(a0)+
	endr
	dbf	d0,cfpl
	rts

*****************************************************************
*	dofill							*
*	Does a contourfill bounded by color			*
*								*
*****************************************************************
dofill:
	move.w	#-1,mouseonoff		;turn the mouse off
	move.w	buttondat,savebuttondat
	bsr	waitvbl
	bsr	copyforundo
	bsr	clearfillplane
	move.w	#usepixel,todraw
	move.w	#wmreplace,writemode
	move.w	mx,d0
	move.w	my,d1
	move.l	currentscreen,a6
	bsr	getpoint		;find what the color is
	move.w	color,oldcolor		;color started on
	move.w	color,d5

	cmpi.w	#rightbuttondown,savebuttondat
	bne.s	skipbcolor
	cmp.w	currentbcolor,d5
	beq	fillstop
	bra	skipredund
skipbcolor
	cmpi.w	#patternfill,filltype
	beq.s	skipredund
	cmp.w	currentfcolor,d5
	beq	fillstop
skipredund:
	move.w	oldcolor,d3
	move.w	currentfcolor,newcolor
	move.w	newcolor,d4
	move.l	currentscreen,a1
	move.l	sp,a0			;to tell when stack empty
	move.w	d0,-(sp)		;x and y on stack
	move.w	d1,-(sp)		;and call my scanline routine
	bra	myfill
fillstop:
	move.w	#-1,oldcoordx
	move.w	#1,mouseonoff
	bra	paintloop
myfill:
	cmp.l	a0,sp		;while stack not empty
	beq	fillstop
	move.w	(sp)+,d1		;get y
	move.w	(sp)+,d0		;get x, (pop(x,y))
	move.w	d0,savex		;savex=x
trackright:			;fill the span to the right
	addq.w	#1,d0			;x=x+1
	cmpi.w	#319,d0
	bgt.s	extremeright
	move.l	a1,a6
	bsr	getpoint
	cmp.w	color,d3		;if pixel(x,y)<>boundry value
	bne.s	extremeright
	bra.s	trackright
extremeright:				;save extreme right pixel
	move.w	d0,xright		;xright=x-1
	subq.w	#1,xright
	move.w	savex,d0		;x=savex
trackleft:
	subq.w	#1,d0			;x=x-1
	tst.w	d0
	blt.s	extremeleft
	move.l	a1,a6
	bsr	getpoint
	cmp.w	color,d3		;if pixel(x,y)<>boundry value
	bne.s	extremeleft
	bra.s	trackleft
extremeleft:				;save extreme right pixel
	move.w	d0,xleft		;xright=x-1
	addq.w	#1,xleft
	movem.l	d0-a6,-(sp)
	move.w	xleft,d0
	move.w	xright,d2
	bsr	hline
	movem.l	(sp)+,d0-a6
gocheckbelow:	
	move.w	xleft,d0		;start at extremeleft
	move.w	d1,savey		;save y
	addq.w	#1,d1			;line below
	cmp.w	#479,d1
	bgt	myfill
checkbelow:
	move.l	a1,a6
	bsr	getpoint4		;get the pixel
	move.w	color,d5		;examine with a state change engine
checkbelowloop:
	addq.w	#1,d0
	cmp.w	xright,d0
	bgt.s	gocheckabove		;only check in the range xl<=x>=xr
	move.l	a1,a6
	bsr	getpoint4		;get this pixel
	cmp.w	color,d5		;did the state change
	beq.s	checkbelowloop
	cmp.w	d3,d5			;if the state changed from oldcolor
	beq.s	possibleseed1		;to any other this is one to push
	move.w	color,d5
	bra.s	checkbelowloop
possibleseed1:
	move.w	d0,d2
	subq.w	#1,d2
	move.w	d2,-(sp)		;push new seed on stack
	move.w	d1,-(sp)
	move.w	color,d5		;new oldcolor
	bra.s	checkbelowloop
gocheckabove:
	cmp.w	d3,d5
	bne.s	gcab
	move.w	d0,d2
	subq.w	#1,d2
	move.w	d2,-(sp)
	move.w	d1,-(sp)
gcab:
	move.w	xleft,d0		;start at extremeleft
	move.w	savey,d1		;save y
	subq.w	#1,d1			;line below
	tst.w	d1
	blt	myfill

checkabove:
	move.l	a1,a6
	bsr	getpoint4		;get the pixel
	move.w	color,d5		;examine with a state change engine
checkaboveloop:
	addq.w	#1,d0
	cmp.w	xright,d0
	bgt.s	gomyfill		;only check in the range xl<=x>=xr
	move.l	a1,a6
	bsr	getpoint4		;get this pixel
	cmp.w	color,d5		;did the state change
	beq.s	checkaboveloop
	cmp.w	d3,d5			;if the state changed from oldcolor
	beq.s	possibleseed2		;to any other this is one to push
	move.w	color,d5
	bra.s	checkaboveloop
possibleseed2:
	move.w	d0,d2
	subq.w	#1,d2
	move.w	d2,-(sp)		;push new seed on stack
	move.w	d1,-(sp)
	move.w	color,d5		;new oldcolor
	bra.s	checkaboveloop
gomyfill:
	cmp.w	d3,d5
	bne.s	gcbe
	move.w	d0,d2
	subq.w	#1,d2
	move.w	d2,-(sp)
	move.w	d1,-(sp)
gcbe:
	bra	myfill



savex:	ds.w	1
savey:	ds.w	1
xright:	ds.w	1
yright:	ds.w	1
xleft:	ds.w	1
yleft:	ds.w	1


fillabort:
        move.w  #$ff,-(sp)
        move.w  #6,-(sp)
        trap    #1
        addq.l  #4,sp
        tst.l   d0
        beq     fabortback
	moveq	#1,d0
fabortback:
	rts
*****************************************************************
*	filledbox						*
*	Draws a box filled with color 				*
*	x1,y1,x2,y2 for corners					*
*	A0 must have address of screen to draw.			*
*****************************************************************
filledbox:
	move.w	x2,d0
	sub.w	x1,d0
	addq.w	#1,d0
	move.w	y2,d1
	sub.w	y1,d1
	addq.w	#1,d1

	lea	drawbltblk,a6
	move.w	d0,b_wd(a6)
	move.w	d1,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	x1,d_xmin(a6)
	move.w	y1,d_ymin(a6)
	move.l	a0,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts
*********************************************************
*	Hatched version specifically for 'greying out'	*
*	areas.						*
*********************************************************
hatchedbox:
	move.w	x2,d0
	sub.w	x1,d0
	addq.w	#1,d0
	move.w	y2,d1
	sub.w	y1,d1
	addq.w	#1,d1

	lea	drawbltblk,a6
	move.w	d0,b_wd(a6)
	move.w	d1,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	x1,d_xmin(a6)
	move.w	y1,d_ymin(a6)
	move.l	a0,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#greypat,p_addr(a6)
	move.w	#2,p_nxln(a6)
	move.w	#0,p_nxpl(a6)
	move.w	#2,p_mask(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts
greypat:
	dc.w	%1010101010101010,%0101010101010101

*****************************************************************
*	Fourth of the preference screens			*
*	sprayprefs						*
*****************************************************************
dosprayprefs:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.w	currentpanel,savecpanel		;save current panel for restore on exit
	jsr	setuppanel4
	lea	sprayprefsbuttons,a0
	jsr	constructprefspanel		;make up the button area
	move.w	#1,mouseonoff
	move.l	#sprayprefsbuttons,currentbuttonlist
dosprayprefsloop:
	jsr	domousemenuprefs
	jsr	keyboard2
	bra	dosprayprefsloop
setsize1:
	move.w	#5,sprayradius
	rts
setsize2:
	move.w	#10,sprayradius
	rts
setsize3:
	move.w	#20,sprayradius
	rts
setsize4:
	move.w	#35,sprayradius
	rts
setsize5:
	move.w	#45,sprayradius
	rts
setsize6:
	move.w	#55,sprayradius
	rts
setsize7:
	move.w	#65,sprayradius
	rts
setsize8:
	move.w	#75,sprayradius
	rts
setsize9:
	move.w	#85,sprayradius
	rts
setsize10:
	move.w	#95,sprayradius
	rts

setrate1:
	move.w	#2000,sprayrate
	rts
setrate2:
	move.w	#1778,sprayrate
	rts
setrate3:
	move.w	#1556,sprayrate
	rts
setrate4:
	move.w	#1334,sprayrate
	rts
setrate5:
	move.w	#1112,sprayrate
	rts
setrate6:
	move.w	#890,sprayrate
	rts
setrate7:
	move.w	#668,sprayrate
	rts
setrate8:
	move.w	#446,sprayrate
	rts
setrate9:
	move.w	#224,sprayrate
	rts
setrate10:
	move.w	#0,sprayrate
	rts

*****************************************************************
*	third of the preference screens				*
*	fillprefs						*
*****************************************************************
dofillprefs:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.w	currentpanel,savecpanel		;save current panel for restore on exit
	jsr	setuppanel4
	lea	fillprefsbuttons,a0
	jsr	constructprefspanel		;make up the button area
	bsr	constructline
	move.w	#1,mouseonoff
	move.l	#fillprefsbuttons,currentbuttonlist
dofillprefsloop:
	jsr	domousemenuprefs
	jsr	keyboard2
	bra	dofillprefsloop

setfillbrush:
	move.w	#patternfill,filltype
	move.w	#-1,shouldfollow
	bsr	handlemosaic
	bsr	makemask
	rts
setfillsolid:
	move.w	#-1,shouldfollow
	move.w	#solidfill,filltype	;to be done
	rts
setfillempty:
	move.w	#1,shouldfollow
	move.w	#emptyfill,filltype
	rts



*****************************************************************
*	second of the preference screens			*
*	lineprefs						*
*****************************************************************
dolineprefs:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.w	currentpanel,savecpanel		;save current panel for restore on exit
	jsr	setuppanel4
	lea	lineprefsbuttons,a0
	jsr	constructprefspanel		;make up the button area
	bsr	constructline
	move.w	#1,mouseonoff
	move.l	#lineprefsbuttons,currentbuttonlist
dolineprefsloop:
	jsr	domousemenuprefs
	jsr	keyboard2
	bra	dolineprefsloop

lineprefsexit:
	addq.l	#4,sp		;remove old return address
pep1l:	cmpi.w	#panel1,savecpanel
	bne	pep1pl
	jsr	setuppanel1
	bra	makelastactive

*	bra	paintloop
pep1pl:	cmpi.w	#panel2,savecpanel
	bne	pep2l
	jsr	setuppanel2
	bra	makelastactive
*	bra	paintloop
pep2l:
	bra	returntogem

makelastactive:
	move.l	lastbutton,a1
	bsr	dobuttonsupdown		;some buttons mean others become unselected
	move.l	buttonlroutine(a1),d2
	beq	paintloop
	move.l	d2,a0
	jmp	(a0)


linebit0:
	move.l	linepattern2,d0
	bset	#0,d0
	move.l	d0,linepattern2
	rts
linebit0b:
	move.l	linepattern2,d0
	bclr	#0,d0
	move.l	d0,linepattern2
	lea	p5linebit0,a0
	bsr	putbuttonup
	rts
linebit1:
	move.l	linepattern2,d0
	bset	#1,d0
	move.l	d0,linepattern2
	rts
linebit1b:
	move.l	linepattern2,d0
	bclr	#1,d0
	move.l	d0,linepattern2
	lea	p5linebit1,a0
	bsr	putbuttonup
	rts
linebit2:
	move.l	linepattern2,d0
	bset	#2,d0
	move.l	d0,linepattern2
	rts
linebit2b:
	move.l	linepattern2,d0
	bclr	#2,d0
	move.l	d0,linepattern2
	lea	p5linebit2,a0
	bsr	putbuttonup
	rts
linebit3:
	move.l	linepattern2,d0
	bset	#3,d0
	move.l	d0,linepattern2
	rts
linebit3b:
	move.l	linepattern2,d0
	bclr	#3,d0
	move.l	d0,linepattern2
	lea	p5linebit3,a0
	bsr	putbuttonup
	rts
linebit4:
	move.l	linepattern2,d0
	bset	#4,d0
	move.l	d0,linepattern2
	rts
linebit4b:
	move.l	linepattern2,d0
	bclr	#4,d0
	move.l	d0,linepattern2
	lea	p5linebit4,a0
	bsr	putbuttonup
	rts
linebit5:
	move.l	linepattern2,d0
	bset	#5,d0
	move.l	d0,linepattern2
	rts
linebit5b:
	move.l	linepattern2,d0
	bclr	#5,d0
	move.l	d0,linepattern2
	lea	p5linebit5,a0
	bsr	putbuttonup
	rts
linebit6:
	move.l	linepattern2,d0
	bset	#6,d0
	move.l	d0,linepattern2
	rts
linebit6b:
	move.l	linepattern2,d0
	bclr	#6,d0
	move.l	d0,linepattern2
	lea	p5linebit6,a0
	bsr	putbuttonup
	rts
linebit7:
	move.l	linepattern2,d0
	bset	#7,d0
	move.l	d0,linepattern2
	rts
linebit7b:
	move.l	linepattern2,d0
	bclr	#7,d0
	move.l	d0,linepattern2
	lea	p5linebit7,a0
	bsr	putbuttonup
	rts
linebit8:
	move.l	linepattern2,d0
	bset	#8,d0
	move.l	d0,linepattern2
	rts
linebit8b:
	move.l	linepattern2,d0
	bclr	#8,d0
	move.l	d0,linepattern2
	lea	p5linebit8,a0
	bsr	putbuttonup
	rts
linebit9:
	move.l	linepattern2,d0
	bset	#9,d0
	move.l	d0,linepattern2
	rts
linebit9b:
	move.l	linepattern2,d0
	bclr	#9,d0
	move.l	d0,linepattern2
	lea	p5linebit9,a0
	bsr	putbuttonup
	rts
linebit10:
	move.l	linepattern2,d0
	bset	#10,d0
	move.l	d0,linepattern2
	rts
linebit10b:
	move.l	linepattern2,d0
	bclr	#10,d0
	move.l	d0,linepattern2
	lea	p5linebit10,a0
	bsr	putbuttonup
	rts
linebit11:
	move.l	linepattern2,d0
	bset	#11,d0
	move.l	d0,linepattern2
	rts
linebit11b:
	move.l	linepattern2,d0
	bclr	#11,d0
	move.l	d0,linepattern2
	lea	p5linebit11,a0
	bsr	putbuttonup
	rts
linebit12:
	move.l	linepattern2,d0
	bset	#12,d0
	move.l	d0,linepattern2
	rts
linebit12b:
	move.l	linepattern2,d0
	bclr	#12,d0
	move.l	d0,linepattern2
	lea	p5linebit12,a0
	bsr	putbuttonup
	rts
linebit13:
	move.l	linepattern2,d0
	bset	#13,d0
	move.l	d0,linepattern2
	rts
linebit13b:
	move.l	linepattern2,d0
	bclr	#13,d0
	move.l	d0,linepattern2
	lea	p5linebit13,a0
	bsr	putbuttonup
	rts
linebit14:
	move.l	linepattern2,d0
	bset	#14,d0
	move.l	d0,linepattern2
	rts
linebit14b:
	move.l	linepattern2,d0
	bclr	#14,d0
	move.l	d0,linepattern2
	lea	p5linebit14,a0
	bsr	putbuttonup
	rts
linebit15:
	move.l	linepattern2,d0
	bset	#15,d0
	move.l	d0,linepattern2
	rts
linebit15b:
	move.l	linepattern2,d0
	bclr	#15,d0
	move.l	d0,linepattern2
	lea	p5linebit15,a0
	bsr	putbuttonup
	rts
linebit16:
	move.l	linepattern2,d0
	bset	#16,d0
	move.l	d0,linepattern2
	rts
linebit16b:
	move.l	linepattern2,d0
	bclr	#16,d0
	move.l	d0,linepattern2
	lea	p5linebit16,a0
	bsr	putbuttonup
	rts
linebit17:
	move.l	linepattern2,d0
	bset	#17,d0
	move.l	d0,linepattern2
	rts
linebit17b:
	move.l	linepattern2,d0
	bclr	#17,d0
	move.l	d0,linepattern2
	lea	p5linebit17,a0
	bsr	putbuttonup
	rts
linebit18:
	move.l	linepattern2,d0
	bset	#18,d0
	move.l	d0,linepattern2
	rts
linebit18b:
	move.l	linepattern2,d0
	bclr	#18,d0
	move.l	d0,linepattern2
	lea	p5linebit18,a0
	bsr	putbuttonup
	rts
linebit19:
	move.l	linepattern2,d0
	bset	#19,d0
	move.l	d0,linepattern2
	rts
linebit19b:
	move.l	linepattern2,d0
	bclr	#19,d0
	move.l	d0,linepattern2
	lea	p5linebit19,a0
	bsr	putbuttonup
	rts
linebit20:
	move.l	linepattern2,d0
	bset	#20,d0
	move.l	d0,linepattern2
	rts
linebit20b:
	move.l	linepattern2,d0
	bclr	#20,d0
	move.l	d0,linepattern2
	lea	p5linebit20,a0
	bsr	putbuttonup
	rts
linebit21:
	move.l	linepattern2,d0
	bset	#21,d0
	move.l	d0,linepattern2
	rts
linebit21b:
	move.l	linepattern2,d0
	bclr	#21,d0
	move.l	d0,linepattern2
	lea	p5linebit21,a0
	bsr	putbuttonup
	rts
linebit22:
	move.l	linepattern2,d0
	bset	#22,d0
	move.l	d0,linepattern2
	rts
linebit22b:
	move.l	linepattern2,d0
	bclr	#22,d0
	move.l	d0,linepattern2
	lea	p5linebit22,a0
	bsr	putbuttonup
	rts
linebit23:
	move.l	linepattern2,d0
	bset	#23,d0
	move.l	d0,linepattern2
	rts
linebit23b:
	move.l	linepattern2,d0
	bclr	#23,d0
	move.l	d0,linepattern2
	lea	p5linebit23,a0
	bsr	putbuttonup
	rts
linebit24:
	move.l	linepattern2,d0
	bset	#24,d0
	move.l	d0,linepattern2
	rts
linebit24b:
	move.l	linepattern2,d0
	bclr	#24,d0
	move.l	d0,linepattern2
	lea	p5linebit24,a0
	bsr	putbuttonup
	rts
linebit25:
	move.l	linepattern2,d0
	bset	#25,d0
	move.l	d0,linepattern2
	rts
linebit25b:
	move.l	linepattern2,d0
	bclr	#25,d0
	move.l	d0,linepattern2
	lea	p5linebit25,a0
	bsr	putbuttonup
	rts
linebit26:
	move.l	linepattern2,d0
	bset	#26,d0
	move.l	d0,linepattern2
	rts
linebit26b:
	move.l	linepattern2,d0
	bclr	#26,d0
	move.l	d0,linepattern2
	lea	p5linebit26,a0
	bsr	putbuttonup
	rts
linebit27:
	move.l	linepattern2,d0
	bset	#27,d0
	move.l	d0,linepattern2
	rts
linebit27b:
	move.l	linepattern2,d0
	bclr	#27,d0
	move.l	d0,linepattern2
	lea	p5linebit27,a0
	bsr	putbuttonup
	rts
linebit28:
	move.l	linepattern2,d0
	bset	#28,d0
	move.l	d0,linepattern2
	rts
linebit28b:
	move.l	linepattern2,d0
	bclr	#28,d0
	move.l	d0,linepattern2
	lea	p5linebit28,a0
	bsr	putbuttonup
	rts
linebit29:
	move.l	linepattern2,d0
	bset	#29,d0
	move.l	d0,linepattern2
	rts
linebit29b:
	move.l	linepattern2,d0
	bclr	#29,d0
	move.l	d0,linepattern2
	lea	p5linebit29,a0
	bsr	putbuttonup
	rts
linebit30:
	move.l	linepattern2,d0
	bset	#30,d0
	move.l	d0,linepattern2
	rts
linebit30b:
	move.l	linepattern2,d0
	bclr	#30,d0
	move.l	d0,linepattern2
	lea	p5linebit30,a0
	bsr	putbuttonup
	rts
linebit31:
	move.l	linepattern2,d0
	bset	#31,d0
	move.l	d0,linepattern2
	rts
linebit31b:
	move.l	linepattern2,d0
	bclr	#31,d0
	move.l	d0,linepattern2
	lea	p5linebit31,a0
	bsr	putbuttonup
	rts
setlineall:
	move.l	#$ffffffff,linepattern2
	bsr	constructline
	lea	p5all,a0
	bsr	putbuttonup
	rts
setlinenone:
	move.l	#0,linepattern2
	bsr	constructline
	lea	p5none,a0
	bsr	putbuttonup
	rts
linepreset1:
	move.l	#%11111111000000001111111100000000,linepattern2
	bsr	constructline
	lea	p5preset1,a0
	bsr	putbuttonup
	rts
linepreset1b:
	move.l	#%11111111000000001111111100000000,d0
	eor.l	#-1,d0
	move.l	d0,linepattern2
	bsr	constructline
	lea	p5preset1,a0
	bsr	putbuttonup
	rts
linepreset2:
	move.l	#%10101010101010101010101010101010,linepattern2
	bsr	constructline
	lea	p5preset2,a0
	bsr	putbuttonup
	rts
linepreset2b:
	move.l	#%10101010101010101010101010101010,d0
	eor.l	#-1,d0
	move.l	d0,linepattern2
	bsr	constructline
	lea	p5preset2,a0
	bsr	putbuttonup
	rts
linepreset3:
	move.l	#%11110000111100001111000011110000,linepattern2
	bsr	constructline
	lea	p5preset3,a0
	bsr	putbuttonup
	rts
linepreset3b:
	move.l	#%11110000111100001111000011110000,d0
	eor.l	#-1,d0
	move.l	d0,linepattern2
	bsr	constructline
	lea	p5preset3,a0
	bsr	putbuttonup
	rts
linepreset4:
	move.l	#%11111010111110101111101011111010,linepattern2
	bsr	constructline
	lea	p5preset4,a0
	bsr	putbuttonup
	rts
linepreset4b:
	move.l	#%11111010111110101111101011111010,d0
	eor.l	#-1,d0
	move.l	d0,linepattern2
	bsr	constructline
	lea	p5preset4,a0
	bsr	putbuttonup
	rts
linepreset5:
	move.l	#%11101010111010101110101011101010,linepattern2
	bsr	constructline
	lea	p5preset5,a0
	bsr	putbuttonup
	rts
linepreset5b:
	move.l	#%11101010111010101110101011101010,d0
	eor.l	#-1,d0
	move.l	d0,linepattern2
	bsr	constructline
	lea	p5preset5,a0
	bsr	putbuttonup
	rts

constructline:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	lea	bitlist,a0
	move.l	linepattern2,d0
	moveq	#0,d1
clineloop:
	move.l	(a0)+,d2
	beq	clineend
	move.l	d2,a1
	move.w	#0,buttonselected(a1)
	btst	d1,d0
	beq	bitnotset
	move.w	#1,buttonselected(a1)
bitnotset:
	addq.w	#1,d1
	bra	clineloop
clineend:
	lea	bitlist,a0
	bsr	docnp
	move.w	#1,mouseonoff
	rts

sprayprefsbuttons:
	dc.l	p7part1,p7part2,p7part3,p7part4,p7part5,p7part6
	dc.l	p7size1,p7size2,p7size3,p7size4,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,p4exit
	dc.l	p7rate1,p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7size1:
	dc.w	80,89	;x,y on panel screen
	dc.w	128,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize1	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size2,p7size3,p7size4,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size2:
	dc.w	96,89	;x,y on panel screen
	dc.w	144,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize2	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size3,p7size4,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size3:
	dc.w	112,89	;x,y on panel screen
	dc.w	160,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize3	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size4,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size4:
	dc.w	128,89	;x,y on panel screen
	dc.w	200,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize4	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size5:
	dc.w	144,89	;x,y on panel screen
	dc.w	216,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize5	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size4,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size6:
	dc.w	160,89	;x,y on panel screen
	dc.w	232,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize6	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size4
	dc.l	p7size7,p7size8,p7size9,p7size10,0
p7size7:
	dc.w	176,89	;x,y on panel screen
	dc.w	248,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize7	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size6
	dc.l	p7size4,p7size8,p7size9,p7size10,0
p7size8:
	dc.w	192,89	;x,y on panel screen
	dc.w	264,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize8	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size6
	dc.l	p7size7,p7size4,p7size9,p7size10,0
p7size9:
	dc.w	208,89	;x,y on panel screen
	dc.w	280,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize9	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size4,p7size10,0
p7size10:
	dc.w	224,89	;x,y on panel screen
	dc.w	296,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setsize10	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7size1,p7size2,p7size3,p7size5,p7size6
	dc.l	p7size7,p7size8,p7size9,p7size4,0

p7rate1:
	dc.w	80,129	;x,y on panel screen
	dc.w	128,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate1	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate2:
	dc.w	96,129	;x,y on panel screen
	dc.w	144,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate2	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate1,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate3:
	dc.w	112,129	;x,y on panel screen
	dc.w	160,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate3	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate1,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate4:
	dc.w	128,129	;x,y on panel screen
	dc.w	200,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate4	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate1,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate5:
	dc.w	144,129	;x,y on panel screen
	dc.w	216,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate5	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate1,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate6:
	dc.w	160,129	;x,y on panel screen
	dc.w	232,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate6	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate1
	dc.l	p7rate7,p7rate8,p7rate9,p7rate10,0
p7rate7:
	dc.w	176,129	;x,y on panel screen
	dc.w	248,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate7	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate1,p7rate8,p7rate9,p7rate10,0
p7rate8:
	dc.w	192,129	;x,y on panel screen
	dc.w	264,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate8	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate1,p7rate9,p7rate10,0
p7rate9:
	dc.w	208,129	;x,y on panel screen
	dc.w	280,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate9	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate1,p7rate10,0
p7rate10:
	dc.w	224,129	;x,y on panel screen
	dc.w	296,417	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setrate10	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p7rate2,p7rate3,p7rate4,p7rate5,p7rate6
	dc.l	p7rate7,p7rate8,p7rate9,p7rate1,0

p7part1:
	dc.w	132,69
	dc.w	0,465
	dc.w	58,12
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0
p7part2:
	dc.w	135,114
	dc.w	58,465
	dc.w	51,9
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0
p7part3:
	dc.w	72,94
	dc.w	88,281
	dc.w	5,9
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0
p7part4:
	dc.w	243,84
	dc.w	0,477
	dc.w	15,29
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0
p7part5:
	dc.w	62,124
	dc.w	15,477
	dc.w	15,29
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0
p7part6:
	dc.w	243,125
	dc.w	30,477
	dc.w	15,29
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0


*****************************************************************
*	First of the preference screens				*
*	Brushprefs						*
*****************************************************************
dobrushprefs:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.w	currentpanel,savecpanel		;save current panel for restore on exit
	bsr	setuppanel4
	lea	ubprefsbuttons,a0
	jsr	constructprefspanel		;make up the button area
	bsr	drawspot
	move.w	#1,mouseonoff
	move.l	#ubprefsbuttons,currentbuttonlist
dobrushprefsloop:
	jsr	domousemenuprefs
	jsr	keyboard2
	bra	dobrushprefsloop
prefsexit:
	addq.l	#4,sp		;remove old return address
	bsr	calctransparencies
	bsr	calctransparencies
pep1:	cmpi.w	#panel1,savecpanel
	bne	pep1p
	jsr	setuppanel1
	bra	makelastactive
pep1p:	cmpi.w	#panel2,savecpanel
	bne	pep2
	jsr	setuppanel2
	bra	makelastactive
pep2:
	bra	returntogem


sdmcolor:
	move.w	#colormode,brushmode
	rts
sdmcycle:
	move.w	#cyclemode,brushmode
	rts
sdmmatte:
	move.w	#mattemode,brushmode
	rts
sdmreplace:
	move.w	#replacemode,brushmode
	rts
sdmtransparent
	move.w	#transparentmode,brushmode
	rts


setspot:
	move.w	transpx,d0	;was it over the spot as well
	move.w	transpy,d1
	move.w	mx,d6
	move.w	my,d7
	sub.w	panely,d7
	move.w	d6,d4
	move.w	d7,d5
	cmp.w	d6,d0
	bgt	notonspot
	cmp.w	d7,d1
	bgt	notonspot
	addq.w	#8,d0
	addq.w	#8,d1
	cmp.w	d6,d0
	blt	notonspot
	cmp.w	d7,d1
	blt	notonspot		;it was
	subq.w	#8,d0
	subq.w	#8,d1
	sub.w	d0,d6		;offset to box tl corner
	sub.w	d1,d7
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bra	skipmspot
setspotloop:
	tst.w	buttondat
	beq	setspotover
	move.w	mx,d4
	move.w	my,d5
	sub.w	panely,d5
	cmp.w	oldmx,d4
	bne	isdiffspot
	cmp.w	oldmy,d5
	bne	isdiffspot
	bra	setspotloop
isdiffspot:
	move.w	d4,oldmx
	move.w	d5,oldmy
	sub.w	d6,d4
	sub.w	d7,d5
skipmspot:
	cmpi.w	#216,d4
	bge.s	sks1
	move.w	#216,d4
sks1:
	cmpi.w	#89,d5
	bge.s	sks2
	moveq	#89,d5
sks2:
	cmpi.w	#279-8,d4
	ble.s	sks3
	move.w	#279-8,d4
sks3:
	cmpi.w	#152-8,d5
	ble.s	sks4
	move.w	#152-8,d5
sks4:
	move.w	d4,transpx
	move.w	d5,transpy
	bsr	drawspot
	bra	setspotloop
setspotover:
	move.w	#1,mouseonoff		;turn the mouse off
notonspot:
	rts
drawspot:
	movem.w	d6-d7,-(sp)
	lea	p4spot,a1
	lea	buttonbltblk,a6
	move.w	buttonwidth(a1),b_wd(a6)
	move.w	buttonheight(a1),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#1,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonparty(a1),s_ymin(a6)
	move.w	buttonpartx(a1),s_xmin(a6)
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a1),d_xmin(a6)
	move.w	buttony(a1),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.w	#15,color
	move.l	panelad,a0
	move.w	transpx,x1
	move.w	transpy,y1
	move.w	x1,x2
	move.w	y1,y2
	addq.w	#8,x2
	addq.w	#8,y2
	bsr	filledbox
	movem.w	(sp)+,d6-d7
	rts

draggingprefs:
	cmpi.w	#leftbuttondown,buttondat
	beq	dcarry
	rts
dcarry:
	move.w	my,d2
	sub.w	d1,d2
	cmpi.w	#hblhighest,d2
	bgt.s	dok1pf
	move.w	#hblhighest,d2
	bra	dok2pf
dok1pf:
	move.w	#479,d3
	sub.w	panelheight,d3
	subq.w	#1,d3
	cmp.w	d3,d2
	blt.s	dok2pf
	move.w	d3,d2
dok2pf:
	move.w	d2,panely
	move.w	d0,mx
	bra	draggingprefs

setunitransp:
	move.w	#isuni,transmode
	move.w	centretrans,edgetrans	;makeboth the same
	lea	edgebuttons,a0
	move.w	centretrans,d0
	asl.w	#2,d0
	move.l	(a0,d0.w),a1
	bsr	dobuttonsupdown
	rts
edgebuttons:
	dc.l	eb0,eb1,eb2,eb3,eb4,eb5,eb6,eb7,eb8,eb9,eb10
	dc.l	eb11,eb12,eb13,eb14,eb15
centrebuttons:
	dc.l	cb0,cb1,cb2,cb3,cb4,cb5,cb6,cb7,cb8,cb9,cb10
	dc.l	cb11,cb12,cb13,cb14,cb15


setverttransp:
	move.w	#isvert,transmode
	rts
sethoriztransp:
	move.w	#ishoriz,transmode
	rts
setspheretransp:
	move.w	#issphere,transmode
	rts

calctransparencies:
	lea	userbrush,a6
	cmpi.w	#isuni,transmode
	bne	calctvert
	move.w	edgetrans,d0
	add.w	d0,d0
	lea	transxlist,a0		;both axis same
	move.w	brushw(a6),d1
	subq.w	#1,d1
fbxloop:
	move.w	d0,(a0)+
	dbf	d1,fbxloop
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	lea	transylist,a0
	move.w	brushh(a6),d1
	subq.w	#1,d1
fbyloop:
	move.w	d0,(a0)+
	dbf	d1,fbyloop
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	rts

calctvert:
	cmpi.w	#isvert,transmode
	bne	calcthoriz		;this mode has all y's as zero
	moveq	#0,d0
	lea	transylist,a0
	move.w	brushh(a6),d1
	subq.w	#1,d1
fbyloopv:
	move.w	d0,(a0)+
	dbf	d1,fbyloopv
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	lea	transxlist,a0		;whichlist to fill
	lea	p4spot,a1
	moveq	#0,d0
	move.w	transpx,d0		;which to use for middle count
	addq.w	#4,d0
	sub.w	buttonx(a1),d0		;make it 0-63
	and.w	#$fff8,d0		;remove last 3 bits
	asr.w	#3,d0			;gives a number between 0-7
	move.w	brushw(a6),d1
	bsr	calcranges
	rts
calcthoriz:
	cmpi.w	#ishoriz,transmode
	bne	calctsphere		;this mode has all y's as zero
	moveq	#0,d0
	lea	transxlist,a0
	move.w	brushw(a6),d1
	subq.w	#1,d1
fbylooph:
	move.w	d0,(a0)+
	dbf	d1,fbylooph
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	lea	transylist,a0		;whichlist to fill
	lea	p4spot,a1
	moveq	#0,d0
	move.w	transpy,d0		;which to use for middle count
	addq.w	#4,d0
	sub.w	buttony(a1),d0		;make it 0-63
	and.w	#$fff8,d0		;remove last 3 bits
	asr.w	#3,d0			;gives a number between 0-7
	move.w	brushh(a6),d1
	bsr	calcranges
	rts
calctsphere:
	lea	transylist,a0		;whichlist to fill
	lea	p4spot,a1
	moveq	#0,d0
	move.w	transpy,d0		;which to use for middle count
	addq.w	#4,d0
	sub.w	buttony(a1),d0		;make it 0-63
	and.w	#$fff8,d0		;remove last 3 bits
	asr.w	#3,d0			;gives a number between 0-7
	move.w	brushh(a6),d1
	bsr	calcranges

	lea	transxlist,a0		;whichlist to fill
	lea	p4spot,a1
	moveq	#0,d0
	move.w	transpx,d0		;which to use for middle count
	addq.w	#4,d0
	sub.w	buttonx(a1),d0		;make it 0-63
	and.w	#$fff8,d0		;remove last 3 bits
	asr.w	#3,d0			;gives a number between 0-7
	move.w	brushw(a6),d1
	bsr	calcranges
	rts
startvalue:	dc.w	0
numpixels:	dc.w	0
amplitude:	dc.w	0
startangle:	dc.w	0
reverseflag:	dc.w	0
*************************************************************************
*	Pass translist to fill in a0, transpx after calcs		*
*	in d0, and the width or height in d1(depending			*
*	on the transmode.						*
*	Uses edgetrans, centretrans.					*
*************************************************************************
calcranges:
	move.w	#1,shifter			;this for 1 or 2 active axis
	cmpi.w	#issphere,transmode
	beq	skida1
	move.w	#2,shifter
skida1:
	moveq	#0,d2
	move.w	d1,d2			;copy number of pixels to calc trans of
	asr.w	#3,d2			;div by 8
	cmpi.w	#7,d0
	bne	isntmax
	move.w	d1,d2
	bra	skipmax
isntmax:
	mulu	d0,d2			;so the first range goes 0 to d2
	tst.w	d2
	beq	nofirstrange
skipmax:
	move.w	#0,startangle
	move.w	d2,numpixels
	move.w	#-1,reverseflag
	move.w	centretrans,d4
	move.w	edgetrans,d5
	move.w	d5,startvalue
	sub.w	d5,d4
	tst.w	d4
	bgt.s	noswaptrans
	move.w	#1,reverseflag
	neg.w	d4		;gives actual number of segments=radius
	move.w	centretrans,startvalue
noswaptrans:
	addq.w	#1,d4
	move.w	d4,amplitude
	movem.w	d0-d2,-(sp)
	bsr	docalcrange
	movem.w	(sp)+,d0-d2
	cmpi.w	#7,d0
	bne	nofirstrange
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	rts
nofirstrange:
	sub.w	d2,d1
	move.w	d1,d2

	move.w	#95,startangle
	move.w	d2,numpixels
	move.w	centretrans,d5
	move.w	edgetrans,d4
	move.w	d4,startvalue
	cmp.w	d5,d4
	blt	noswapst
	move.w	d5,startvalue
noswapst:

	move.w	#1,reverseflag
	move.w	centretrans,d5
	move.w	edgetrans,d4
	sub.w	d5,d4
	tst.w	d4
	bgt.s	noswaptrans2
	move.w	#-1,reverseflag
	neg.w	d4		;gives actual number of segments=radius
noswaptrans2:
	addq.w	#1,d4
	move.w	d4,amplitude

	bsr	docalcrange
	rept	10
	move.w	#-1,(a0)+	;for dither check
	endr
	rts

docalcrange:
	tst.w	reverseflag
	bpl	doreversed
	move.l	#90*256,d0		;to number of angles
	divu	numpixels,d0
	and.l	#$0000ffff,d0
	move.l	d0,angleadd		;amount to add to angle each time through
	moveq	#0,d0		;starting angle
	move.w	startangle,d0
	asl.l	#8,d0		;scale up
	move.w	amplitude,d1	;radius
	move.w	numpixels,d7		;number of segments
	subq.w	#1,d7
	lea	sintab,a1
plotsinloop:
	moveq	#0,d4
	move.w	d0,d5
	asr.l	#8,d5		;scale down
	add.w	d5,d5
	move.w	(a1,d5.w),d4
	mulu	d1,d4
	asl.l	#2,d4		;scale down
	swap	d4		;by shifting 14 right
	add.w	startvalue,d4
	moveq	#0,d5
	move.w	shifter,d5
	lsl.w	d5,d4
	move.w	d4,(a0)+
	add.l	angleadd,d0
	cmp.l	#180*256,d0
	ble.s	aisok
	move.l	#180*256,d0
aisok:
	dbf	d7,plotsinloop
	rts

doreversed:
	move.l	#90*256,d0		;to number of angles
	divu	numpixels,d0
	and.l	#$0000ffff,d0
	move.l	d0,angleadd		;amount to add to angle each time through
	moveq	#0,d0		;starting angle
	move.w	startangle,d0
	asl.l	#8,d0		;scale up
	move.w	amplitude,d1	;radius
	move.w	numpixels,d7		;number of segments
	subq.w	#1,d7
	lea	sintab,a1
plotsinloop2:
	moveq	#0,d4
	move.w	d0,d5
	asr.l	#8,d5		;scale down
	add.w	d5,d5
	move.w	(a1,d5.w),d4
	mulu	d1,d4
	asl.l	#2,d4		;scale down
	swap	d4		;by shifting 14 right
	move.w	d1,d5
	subq.w	#1,d5
	sub.w	d4,d5
	move.w	d5,d4
	add.w	startvalue,d4
	moveq	#0,d5
	move.w	shifter,d5
	lsl.w	d5,d4
	move.w	d4,(a0)+
	add.l	angleadd,d0
	cmp.l	#180*256,d0
	ble.s	aisok2
	move.l	#180*256,d0
aisok2:
	dbf	d7,plotsinloop2
	rts

angleadd:	dc.l	0
shifter:	dc.w	0
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
transxlist:	ds.w	320
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
transylist:	ds.w	480
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
		dc.w	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1


docb0:
	move.w	#0,centretrans
	bra	check4bothc
docb1:
	move.w	#1,centretrans
	bra	check4bothc
docb2:
	move.w	#2,centretrans
	bra	check4bothc
docb3:
	move.w	#3,centretrans
	bra	check4bothc
docb4:
	move.w	#4,centretrans
	bra	check4bothc
docb5:
	move.w	#5,centretrans
	bra	check4bothc
docb6:
	move.w	#6,centretrans
	bra	check4bothc
docb7:
	move.w	#7,centretrans
	bra	check4bothc
docb8:
	move.w	#8,centretrans
	bra	check4bothc
docb9:
	move.w	#9,centretrans
	bra	check4bothc
docb10:
	move.w	#10,centretrans
	bra	check4bothc
docb11:
	move.w	#11,centretrans
	bra	check4bothc
docb12:
	move.w	#12,centretrans
	bra	check4bothc
docb13:
	move.w	#13,centretrans
	bra	check4bothc
docb14:
	move.w	#14,centretrans
	bra	check4bothc
docb15:
	move.w	#15,centretrans
check4bothc:
	cmpi.w	#isuni,transmode	;only this mode requires both to be
	bne.s	notsamec		;same
	lea	edgebuttons,a0
	move.w	centretrans,d0
	move.w	d0,edgetrans
	asl.w	#2,d0
	move.l	(a0,d0.w),a1
	bsr	dobuttonsupdown
notsamec:
	rts

doeb0:
	move.w	#0,edgetrans
	bra	check4bothe
doeb1:
	move.w	#1,edgetrans
	bra	check4bothe
doeb2:
	move.w	#2,edgetrans
	bra	check4bothe
doeb3:
	move.w	#3,edgetrans
	bra	check4bothe
doeb4:
	move.w	#4,edgetrans
	bra	check4bothe
doeb5:
	move.w	#5,edgetrans
	bra	check4bothe
doeb6:
	move.w	#6,edgetrans
	bra	check4bothe
doeb7:
	move.w	#7,edgetrans
	bra	check4bothe
doeb8:
	move.w	#8,edgetrans
	bra	check4bothe
doeb9:
	move.w	#9,edgetrans
	bra	check4bothe
doeb10:
	move.w	#10,edgetrans
	bra	check4bothe
doeb11:
	move.w	#11,edgetrans
	bra	check4bothe
doeb12:
	move.w	#12,edgetrans
	bra	check4bothe
doeb13:
	move.w	#13,edgetrans
	bra	check4bothe
doeb14:
	move.w	#14,edgetrans
	bra	check4bothe
doeb15:
	move.w	#15,edgetrans
check4bothe:
	cmpi.w	#isuni,transmode	;only this mode requires both to be
	bne.s	notsamee		;differrent
	lea	centrebuttons,a0
	move.w	edgetrans,d0
	move.w	d0,centretrans
	asl.w	#2,d0
	move.l	(a0,d0.w),a1
	bsr	dobuttonsupdown
notsamee:
	rts
setdithoff:
	move.w	#0,currentdither
	rts
setdith1:
	move.w	#2,currentdither
	rts
setdith2:
	move.w	#4,currentdither
	rts
setdith3:
	move.w	#6,currentdither
	rts

setbtrans:
	move.w	#1,bistrans
	rts
setbreplc:
	move.w	#-1,bistrans
	rts


transpx:	ds.w	1
transpy:	ds.w	1



*****************************************************************
*	This guy checks if the mouse is over the panel or	*
*	not and see's if a button is pressed over a button	*
*****************************************************************
domousemenuprefs:
	move.w	buttondat,buttondat2	;copy this so it don't change
	tst.w	buttondat2
	beq	dmmoverprefs
	tst.w	mousehasgoneup
	beq	dmmoverprefs
	move.w	#0,mousehasgoneup
	tst.w	onpanel		;is it over the panel area
	bmi	dmmoverprefs
	move.l	currentbuttonlist,a0
	bra	findbuttonprefs
dmmoverprefs:
	rts
*********************************************************
*	address of buttonlist in a0			*
*********************************************************
findbuttonprefs:
	move.l	a0,a6	;save buttonlist
	move.w	mx,d0
	move.w	my,d1
	sub.w	panely,d1
findbuttonloopprefs:
	move.l	(a0)+,d7
	beq	fbprefsreturn	;are we at the end of the list ?
	move.l	d7,a1
	move.w	buttonx(a1),d2	;if not check if this button is
	move.w	buttony(a1),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextbuttonprefs
	cmp.w	d1,d3
	bgt.s	nextbuttonprefs
	add.w	buttonwidth(a1),d2
	add.w	buttonheight(a1),d3
	cmp.w	d0,d2
	blt.s	nextbuttonprefs
	cmp.w	d1,d3
	blt.s	nextbuttonprefs	;if we get through this this is our man
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbuttonprefs
	bsr	dobuttonsupdown	;some buttons mean others become unselected
	move.l	buttonlroutine(a1),d2
	beq	fbprefsreturn
	move.l	d2,a0
	jmp	(a0)
isrightbuttonprefs:
	cmpi.w	#rightbuttondown,buttondat2
	bne	fbprefsreturn
	move.l	buttonrroutine(a1),d2
	beq	fbprefsreturn
	move.l	d2,a0
	jmp	(a0)
nextbuttonprefs:
	bra	findbuttonloopprefs
fbprefsreturn:
	rts


constructprefspanel:
	move.l	a0,-(sp)
	move.w	#0,x1		;first clear the area to proper
	move.w	#63,y1		;color
	move.w	#319,x2
	move.w	#154,y2
	move.w	#12,color
	move.l	panelad,a0
	bsr	filledbox
	move.w	#0,x1		;first clear the area to proper
	move.w	#4,y1		;color
	move.w	#319,x2
	move.w	#57,y2
	move.w	#12,color
	move.l	panelad,a0
	bsr	hatchedbox
	move.l	(sp)+,a0
	bsr	docnp
	rts
*****************************************************************
*	Constructpanel						*
*	This guy uses currentpanel to determine which is active	*
*	and constructs the panel according to it's button list	*
*****************************************************************
constructpanel:
	move.w	#0,x1
	move.w	#3,y1
	move.w	#263,x2
	move.w	#58,y2
	move.w	#12,color
	move.l	panelad,a0
	bsr	filledbox

	cmpi.w	#panel1,currentpanel
	bne	cnp2
	lea	panel1buttonlist,a0	;a0, for findbutton routine
	bsr	docnp
	rts
cnp2:
	cmpi.w	#panel2,currentpanel
	bne	cnp3
	lea	panel2buttonlist,a0	;a0, for findbutton routine
	bsr	docnp
	bsr	printcolor
	rts
cnp3:
	cmpi.w	#panel3,currentpanel
	bne	cnp4
	lea	panel3buttonlist,a0	;a0, for findbutton routine
	bsr	docnp
	rts
cnp4:
	rts
docnp:
	move.l	(a0)+,d0
	beq	docnpover	;have we finished the list to raise
	move.l	d0,a1		;get address of button
	lea	buttonbltblk,a6
	move.w	buttonwidth(a1),b_wd(a6)
	move.w	buttonheight(a1),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#1,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonparty(a1),d0
	tst.w	buttonselected(a1)
	beq	copyup			;was it actually down ?
	add.w	buttonheight(a1),d0
copyup:
	move.w	d0,s_ymin(a6)
	move.w	buttonpartx(a1),s_xmin(a6)
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a1),d_xmin(a6)
	move.w	buttony(a1),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	movem.l	a0,-(sp)
	move.l	lineabitblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0
	bra	docnp
docnpover:
	rts


*buttonx:		equ	0
*buttony:		equ	2
*buttonpartx:		equ	4
*buttonparty:		equ	6
*buttonwidth:		equ	8
*buttonheight:		equ	10
*buttonselectable:	equ	12
*buttonlroutine:	equ	14
*buttonrroutine:	equ	18
*buttonselected:	equ	22
*buttondeselects:	equ	24
*****************************************************************
*	This guy checks if the mouse is over the panel or	*
*	not and see's if a button is pressed over a button	*
*****************************************************************
domousemenu:
	move.w	buttondat,buttondat2	;copy this so it don't change
	tst.w	buttondat2
	beq	nomousepress
	tst.w	mousehasgoneup
	beq	dmmover
	move.w	#0,mousehasgoneup
	tst.w	onpanel		;is it over the panel area
	bmi	notonpanel
	lea	panel1buttonlist,a0	;a0, for findbutton routine
	bra	findbutton
dmmover:
	bra	paintloop

*********************************************************
*	If it's here then it must be a draw function	*
*********************************************************
notonpanel:
	cmpi.w	#gettingbrush,paintmode
	beq	getabrush
	cmpi.w	#drawing,paintmode
	beq	jumptodraw
	cmpi.w	#drawingline,paintmode
	beq	dodrawline
	cmpi.w	#filling,paintmode
	beq	dofill
	cmpi.w	#drawingbox,paintmode
	beq	dodrawbox
	cmpi.w	#drawingspray,paintmode
	beq	dodrawspray
	cmpi.w	#drawingcurve,paintmode
	beq	dodrawcurve
	cmpi.w	#drawingelipse,paintmode
	beq	dodrawelipse

	bra	paintloop
jumptodraw:
	move.l	drawroutine,a0
	jmp	(a0)


nomousepress:
	bra	paintloop

*********************************************************
*	address of buttonlist in a0			*
*********************************************************
findbutton:
	move.l	a0,a6	;save buttonlist
	move.w	mx,d0
	move.w	my,d1
	sub.w	panely,d1
	tst.w	checkcolors	;flag for later when other things go in palette section ie: right click settings
	bmi	findbuttonloop
	cmpi.w	#64,d1		;is it below the tools section and thus color
	bge	oncolor
findbuttonloop:
	move.l	(a0)+,d7
	beq	findbuttonover	;are we at the end of the list ?
	move.l	d7,a1
	move.w	buttonx(a1),d2	;if not check if this button is
	move.w	buttony(a1),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextbutton
	cmp.w	d1,d3
	bgt.s	nextbutton
	add.w	buttonwidth(a1),d2
	add.w	buttonheight(a1),d3
	cmp.w	d0,d2
	blt.s	nextbutton
	cmp.w	d1,d3
	blt.s	nextbutton	;if we get through this this is our man
	move.l	a1,lastbutton
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbutton
	bsr	dobuttonsupdown	;some buttons mean others become unselected
	move.l	buttonlroutine(a1),d2
	beq	paintloop
	move.l	d2,a0
	jmp	(a0)
isrightbutton:
	cmpi.w	#rightbuttondown,buttondat2
	bne	wasboth
	move.l	buttonrroutine(a1),d2
	beq	paintloop
	move.l	d2,a0
	jmp	(a0)

wasboth:
	bra	paintloop

nextbutton:
	bra	findbuttonloop
findbuttonover:
	bra	paintloop

*****************************************************************
*	On entry a1 points to the current button .		*
*	run through the list of buttons that this one deselects	*
*	and raise them all. Then select this one.		*
*****************************************************************
dobuttonsupdown:
	movem.l	d0-d1/a1,-(sp)		;save this for later
	move.w	mouseonoff,-(sp)
	bmi.s	nowait2
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
nowait2:
	lea	buttondeselects(a1),a0
dbuttonsup:
	move.l	(a0)+,d0
	beq	dbudover	;have we finished the list to raise
	move.l	d0,a2		;get address of button
	tst.w	buttonselected(a2)
	beq	dbuttonsup	;was it actually down ?
	lea	buttonbltblk,a6
	move.w	buttonwidth(a2),b_wd(a6)
	move.w	buttonheight(a2),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#1,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonpartx(a2),s_xmin(a6)
	move.w	buttonparty(a2),s_ymin(a6)
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a2),d_xmin(a6)
	move.w	buttony(a2),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	movem.l	a0-a2,-(sp)
	move.l	lineabitblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0-a2
	move.w	#0,buttonselected(a2)	;set the select flag to not
	bra	dbuttonsup
dbudover:
	move.l	a1,a2		;get the hit button again
	tst.w	buttonselectable(a2)
	beq	notselectable
	move.w	#1,buttonselected(a2)	;set the select flag to selected

	lea	buttonbltblk,a6
	move.w	buttonwidth(a2),b_wd(a6)
	move.w	buttonheight(a2),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#15,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonpartx(a2),s_xmin(a6)
	move.w	buttonparty(a2),s_ymin(a6)
	move.w	buttonheight(a2),d0
	add.w	d0,s_ymin(a6)		;make it blit the selected version
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a2),d_xmin(a6)
	move.w	buttony(a2),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	movem.l	a0-a2,-(sp)
	move.l	lineabitblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0-a2
notselectable:
	move.w	(sp)+,mouseonoff
budb:
	movem.l	(sp)+,d0-d1/a1
	rts

*********************************************************
*	Pass address of buttons structure and down it	*
*	A0=button struct				*
*********************************************************
putbuttondown:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait3
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
nowait3:
	move.l	a0,a2		;get the hit button again
	tst.w	buttonselectable(a2)
	beq	notselectableup
	move.w	#1,buttonselected(a2)	;set the select flag to selected
	lea	buttonbltblk,a6
	move.w	buttonwidth(a2),b_wd(a6)
	move.w	buttonheight(a2),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#15,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonpartx(a2),s_xmin(a6)
	move.w	buttonparty(a2),s_ymin(a6)
	move.w	buttonheight(a2),d0
	add.w	d0,s_ymin(a6)		;make it blit the selected version
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a2),d_xmin(a6)
	move.w	buttony(a2),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
notselectableup:
	move.w	(sp)+,mouseonoff
	rts
*********************************************************
*	Pass address of buttons structure and up it	*
*	A0=button struct				*
*********************************************************
putbuttonup:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait4
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
nowait4:
	move.l	a0,a2		;get address of button
	tst.w	buttonselected(a2)
	beq	alreadyup	;was it actually down ?
	move.w	#0,buttonselected(a2)
	lea	buttonbltblk,a6
	move.w	buttonwidth(a2),b_wd(a6)
	move.w	buttonheight(a2),b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#1,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	buttonpartx(a2),s_xmin(a6)
	move.w	buttonparty(a2),s_ymin(a6)
	move.l	panelad,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	buttonx(a2),d_xmin(a6)
	move.w	buttony(a2),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
alreadyup:
	move.w	(sp)+,mouseonoff
	rts

*********************************************************
*	Routine comes here to check if I clicked on a	*
*	color button which are just the xy coords in	*
*	order.						*
*	When the button is found it sets a bar above 	*
*	the color if it was clicked with the left mouse	*
*	button, indicating it is the new foreground 	*
*	color (and clears the bar from the previous).	*
*	And sets a bar underneath the color if it was	*
*	the right m button indicating new background	*
*	color.						*
*	d0,d1 are x,y					*
*********************************************************
oncolor:
	lea	colorbuttons,a0
	move.w	#255,d7		;check 256 colors
	moveq	#0,d6
checkcolorsloop:
	move.w	(a0),d2		;get x and y of color box
	move.w	2(a0),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextcbutton
	cmp.w	d1,d3
	bgt.s	nextcbutton
	addq.w	#8,d2		;they're all 8*8
	addq.w	#8,d3
	cmp.w	d0,d2
	blt.s	nextcbutton
	cmp.w	d1,d3
	blt.s	nextcbutton	;if we get through this this is our man
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbuttonc
	bra	setfcolor
isrightbuttonc:
	cmpi.w	#rightbuttondown,buttondat2
	bne	wasboth
	bra	setbcolor
nextcbutton:
	addq.l	#4,a0		;increment to next color box coords
	addq.w	#1,d6		;next color value 0-255
	dbf	d7,checkcolorsloop
	bra	paintloop
*****************************************************************
*	Find the brightest color in the palette and put it in	*
*	brightest.w						*
*****************************************************************
findbrightest:
	move.l	currentpalette,a0
	moveq	#0,d7			;counter
	moveq	#0,d6			;current brightest
	move.w	#$0f00,d3
	move.w	#$00f0,d4
	move.w	#$000f,d5
	move.w	#0,brightest
fbloop:
	move.w	(a0)+,d0
	move.w	d0,d1
	move.w	d1,d2
	and.w	d3,d0		;isolate red
	lsr.w	#8,d0
	and.w	d4,d1		;isolate red
	lsr.w	#4,d1
	and.w	d5,d2		;isolate red
	add.w	d1,d0
	add.w	d2,d0		;d0 is the liminance value
	cmp.w	d0,d6
	bge	noswapb
	move.w	d0,d6
	move.w	d7,brightest
noswapb:
	addq.w	#1,d7
	cmpi.w	#255,d7
	ble	fbloop
	rts

	

*************************************************
*	A0 points at x,y			*
*************************************************
setfcolor:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait5
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait5:
	movem.l	d6/a0,-(sp)	;save the color
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentfcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	subq.w	#2,d1
	bsr	blitclearsp
	movem.l	(sp)+,d6/a0
	move.w	d6,currentfcolor
	move.w	(a0),d0
	move.w	2(a0),d1
	subq.w	#2,d1
	lea	fcolordat,a0
	bsr	blitcolorsp
	bsr	setfcolorbar
	move.w	(sp)+,mouseonoff
	bra	paintloop
setbcolor:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait6
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait6:
	movem.l	d6/a0,-(sp)	;save the color
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentbcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	add.w	#8,d1
	bsr	blitclearsp
	movem.l	(sp)+,d6/a0
	move.w	d6,currentbcolor
	move.w	(a0),d0
	move.w	2(a0),d1
	add.w	#8,d1
	lea	bcolordat,a0
	bsr	blitcolorsp
	bsr	setbcolorbar
	move.w	(sp)+,mouseonoff
	bra	paintloop
fcolordat:
	dc.w	%1111111100000000
	dc.w	%1111111100000000
bcolordat:
	dc.w	%0101010100000000
	dc.w	%1010101000000000
blitclearsp:
	lea	drawbltblk,a6
	move.w	#8,b_wd(a6)
	move.w	#2,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#0,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fcolordat,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#2,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	d0,d_xmin(a6)
	move.w	d1,d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts
blitcolorsp:
	lea	drawbltblk,a6
	move.w	#8,b_wd(a6)
	move.w	#2,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	a0,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#2,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	d0,d_xmin(a6)
	move.w	d1,d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts

*****************************************************************
*	Simple little wait for vertical blank			*
*****************************************************************
waitvbl:
	move.w	#1,vblflag
wvbloop:
	tst.w	vblflag
	bpl	wvbloop
	rts


*************************************************
*	This one sets the bars when the palette	*
*	is constructed. Uses currentfcolor	*
*	and currentbcolor.			*
*************************************************
setfcolor2:
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	move.w	mouseonoff,-(sp)
	bmi.s	nowait7
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait7:
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentfcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	subq.w	#2,d1
	lea	fcolordat,a0
	bsr	blitcolorsp
	move.w	(sp)+,mouseonoff
	rts

setbcolor2:
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	move.w	mouseonoff,-(sp)
	bmi.s	nowait8
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait8:
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentbcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	add.w	#8,d1
	lea	bcolordat,a0
	bsr	blitcolorsp
	move.w	(sp)+,mouseonoff
	rts
*****************************************************************
*	setfcolorbar						*
*	Draws a box of the fcolor in the bottom of the palette	*
*	area. Uses currentfcolor to do it's stuff.		*
*****************************************************************
setfcolorbar:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait9
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait9:
	lea	drawbltblk,a6
	move.w	#88,b_wd(a6)
	move.w	#8,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	currentfcolor,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	#232,d_xmin(a6)
	move.w	#141,d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.w	(sp)+,mouseonoff
	rts
*****************************************************************
*	setbcolorbar						*
*	Draws a box of the fcolor in the bottom of the palette	*
*	area. Uses currentfcolor to do it's stuff.		*
*****************************************************************
setbcolorbar:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait10
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait10:
	lea	drawbltblk,a6
	move.w	#88,b_wd(a6)
	move.w	#8,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	currentbcolor,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)

	move.w	#144,d_xmin(a6)
	move.w	#141,d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	move.w	(sp)+,mouseonoff
	rts
	
fillpat1:
	dc.w	-1


*********************************************************************************
*	ALL THINGS FILE START HERE						*
*********************************************************************************
dofile:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	lea	p1file,a0
	bsr	putbuttonup
	bsr	undrawbrush
	move.l	#0,savebutton
	bsr	setuppanel3
	move.w	#1,mouseonoff
dofileloop:
	jsr	keyboard2
	bra	domousemenu3
	bra	dofileloop

*****************************************************************
*	This guy checks if the mouse is over the panel or	*
*	not and see's if a button is pressed over a button	*
*****************************************************************
domousemenu3:
	move.w	buttondat,buttondat2	;copy this so it don't change
	tst.w	buttondat2
	beq	nomousepress3
	tst.w	mousehasgoneup
	beq	dmmover3
	move.w	#0,mousehasgoneup
	tst.w	onpanel		;is it over the panel area
	bmi	dofileloop
	lea	panel3buttonlist,a0	;a0, for findbutton routine
	bra	findbutton3
dmmover3:
	bra	dofileloop
nomousepress3:
	bra	dofileloop
*********************************************************
*	address of buttonlist in a0			*
*********************************************************
findbutton3:
	move.l	a0,a6	;save buttonlist
	move.w	mx,d0
	move.w	my,d1
	sub.w	panely,d1
findbuttonloop3:
	move.l	(a0)+,d7
	beq	findbuttonover3	;are we at the end of the list ?
	move.l	d7,a1
	move.w	buttonx(a1),d2	;if not check if this button is
	move.w	buttony(a1),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextbutton3
	cmp.w	d1,d3
	bgt.s	nextbutton3
	add.w	buttonwidth(a1),d2
	add.w	buttonheight(a1),d3
	cmp.w	d0,d2
	blt.s	nextbutton3
	cmp.w	d1,d3
	blt.s	nextbutton3	;if we get through this this is our man
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbutton3
	bsr	dobuttonsupdown	;some buttons mean others become unselected
	move.l	buttonlroutine(a1),d2
	beq	dofileloop
	move.l	d2,a0
	jmp	(a0)
isrightbutton3:			;no right button routines for palette
	bra	dofileloop
nextbutton3:
	bra	findbuttonloop3
findbuttonover3:
	bra	dofileloop


setlneo:
	move.b	#1,pictype
	move.w	#ftypeneo,filetype
	bra	dofileloop
setlpnt:
	move.b	#6,pictype
	move.w	#ftypepnt,filetype
	bra	dofileloop
setliff:
	move.b	#3,pictype
	move.w	#ftypeiff,filetype
	bra	dofileloop
setlpc1:
	move.b	#8,pictype
	move.w	#ftypepc1,filetype
	bra	dofileloop
setlpi1:
	move.b	#5,pictype
	move.w	#ftypepi1,filetype
	bra	dofileloop

setsneo:
	move.w	#ftypeneo,filetypesave
	bra	dofileloop
setspnt:
	move.w	#ftypepnt,filetypesave
	bra	dofileloop

panel3buttonlist:
	dc.l	p3loadword,p3saveword,p3setlneo,p3setlpnt
	dc.l	p3setliff,p3setlpc1,p3setlpi1,p3load
	dc.l	p3tools,p3drag,p3setsneo,p3setspnt,0
*  p3save,0

p3drag:
	dc.w	264,5	;x,y on panel screen
	dc.w	0,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	draggingf	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p3tools:
	dc.w	224,33	;x,y on panel screen
	dc.w	296,209	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	dotools3	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p3load:
	dc.w	184,5	;x,y on panel screen
	dc.w	264,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	loadpicture	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p3save:
	dc.w	184,33	;x,y on panel screen
	dc.w	240,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	savepicture	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0

p3loadword:
	dc.w	8,5	;x,y on panel screen
	dc.w	0,281	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	0	;does'nt depress
	dc.l	0	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p3saveword:
	dc.w	8,33	;x,y on panel screen
	dc.w	40,281	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	0	;does'nt depress
	dc.l	0	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p3setlneo:
	dc.w	48,5	;x,y on panel screen
	dc.w	168,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setlneo	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setlpnt,p3setliff,p3setlpc1,p3setlpi1,0
p3setlpnt:
	dc.w	72,5	;x,y on panel screen
	dc.w	192,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setlpnt	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setlneo,p3setliff,p3setlpc1,p3setlpi1,0
p3setliff:
	dc.w	96,5	;x,y on panel screen
	dc.w	216,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setliff	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setlpnt,p3setlneo,p3setlpc1,p3setlpi1,0
p3setlpc1:
	dc.w	120,5	;x,y on panel screen
	dc.w	240,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setlpc1	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setlpnt,p3setliff,p3setlneo,p3setlpi1,0
p3setlpi1:
	dc.w	144,5	;x,y on panel screen
	dc.w	264,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setlpi1	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setlpnt,p3setliff,p3setlpc1,p3setlneo,0

p3setsneo:
	dc.w	48,33	;x,y on panel screen
	dc.w	168,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setsneo	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setspnt,0
p3setspnt:
	dc.w	72,33	;x,y on panel screen
	dc.w	192,257	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setspnt	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p3setsneo,0

*********************************************************************************
*	ALL THINGS PALETTE START HERE						*
*********************************************************************************
dopalette:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	lea	p1palette,a0
	bsr	putbuttonup
	bsr	undrawbrush
*	bsr	copyforundo
	move.l	#0,savebutton
	move.w	#doingnothing,palettemode
	move.w	#-1,wascolorset
	bsr	setuppanel2
	move.w	#1,mouseonoff
	move.w	#-1,mustrecalc
dopaletteloop:
	cmpi.w	#fcolorset,wascolorset
	beq	isitcopy
	jsr	keyboard2
	bra	domousemenu2
	bra	dopaletteloop

isitcopy:
	cmpi.w	#doingcopy,palettemode
	bne	isitexchange
	move.w	#1,mustrecalc
	move.l	currentpalette,a0
	move.w	fcolorsave,d0
	add.w	d0,d0
	move.w	(a0,d0.w),d1
	move.w	currentfcolor,d0
	add.w	d0,d0
	move.w	d1,(a0,d0.w)
	lea	$ff8400,a1
	move.w	d1,(a1,d0.w)
	move.w	#doingnothing,palettemode
	move.w	#-1,wascolorset
	move.l	savebutton,a0
	bsr	putbuttonup
	move.l	#panelsprite,currentpanelsprite
	bra	dopaletteloop

isitexchange:
	cmpi.w	#doingex,palettemode
	bne	isitspread
	move.w	#1,mustrecalc
	move.l	currentpalette,a0
	move.w	fcolorsave,d0
	add.w	d0,d0
	move.w	(a0,d0.w),d2
	move.w	currentfcolor,d1
	add.w	d1,d1
	move.w	(a0,d1.w),d3
	move.w	d2,(a0,d1.w)	;exchange the colors
	move.w	d3,(a0,d0.w)
	lea	$ff8400,a1
	move.w	d3,(a1,d0.w)
	move.w	d2,(a1,d1.w)
	move.w	#doingnothing,palettemode
	move.w	#-1,wascolorset
	move.l	savebutton,a0
	bsr	putbuttonup
	move.l	#panelsprite,currentpanelsprite
	bra	dopaletteloop
isitspread:
	cmpi.w	#doingspread,palettemode
	bne	itsnothing
	move.w	#1,mustrecalc
	move.w	fcolorsave,d0
	move.w	currentfcolor,d1
	cmp.w	d0,d1
	bgt	noswapfs
	beq	nospread
	exg	d0,d1		;make sure we only go up
noswapfs:
	move.w	d0,bottomcolor
	move.w	d1,topcolor
	sub.w	d0,d1		;get number of colors
	move.w	d1,generalvalue	;count
	move.l	currentpalette,a0
	lea	$ff8400,a2
	move.l	a0,a1
	move.w	bottomcolor,d7
	add.w	d7,d7
	add.w	d7,a0
	add.w	d7,a2
	moveq	#0,d0
	move.l	d0,d1
	move.l	d1,d2
	move.w	(a0),d0		;color1
	move.w	d0,d1
	move.w	d0,d2
	and.w	#$0f00,d0
	and.w	#$00f0,d1
	and.w	#$000f,d2
	lsr.w	#8,d0		;these not needed as we scale up by
	lsr.w	#4,d1		;256 anyhow (=lsl.w #8)
	mulu	#1256,d0
	mulu	#1256,d1
	mulu	#1256,d2
	move.w	d0,bottomred
	move.w	d1,bottomgreen
	move.w	d2,bottomblue

	move.w	topcolor,d7
	add.w	d7,d7
	add.w	d7,a1
	move.l	d0,d1
	move.l	d1,d2
	move.w	(a1),d0		;color1
	move.w	d0,d1
	move.w	d0,d2
	and.w	#$0f00,d0
	and.w	#$00f0,d1
	and.w	#$000f,d2
	lsr.w	#8,d0		;these not needed as we scale up by
	lsr.w	#4,d1		;256 anyhow (=lsl.w #8)
	mulu	#1256,d0
	mulu	#1256,d1
	mulu	#1256,d2
	move.w	d0,topred
	move.w	d1,topgreen
	move.w	d2,topblue


*	subq.w	#2,generalvalue
*	ble	nospread
	moveq	#0,d0
	move.w	topred,d0
	sub.w	bottomred,d0
	ext.l	d0
	divs	generalvalue,d0
	move.w	d0,redadd

	moveq	#0,d0
	move.w	topgreen,d0
	sub.w	bottomgreen,d0
	ext.l	d0
	divs	generalvalue,d0
	move.w	d0,greenadd

	moveq	#0,d0
	move.w	topblue,d0
	sub.w	bottomblue,d0
	ext.l	d0
	divs	generalvalue,d0
	move.w	d0,blueadd
	move.w	generalvalue,d7
	subq.w	#2,d7
	ble	nospread
	moveq	#0,d0
	move.w	d7,d0
	addq.l	#2,a0			;bottom color pointer
	addq.l	#2,a2		;actual hardware
spreadloop:
	moveq	#0,d0
	move.w	bottomred,d0
	add.w	redadd,d0
	move.w	d0,bottomred
	divu	#1256,d0
	and.w	#$000f,d0
	asl.w	#8,d0
	moveq	#0,d1
	move.w	bottomgreen,d1
	add.w	greenadd,d1
	move.w	d1,bottomgreen
	divu	#1256,d1
	and.w	#$000f,d1
	asl.w	#4,d1
	moveq	#0,d2
	move.w	bottomblue,d2
	add.w	blueadd,d2
	move.w	d2,bottomblue
	divu	#1256,d2
	and.w	#$000f,d2
	or.w	d2,d0
	or.w	d1,d0
	move.w	d0,(a0)+
	move.w	d0,(a2)+
	dbf	d7,spreadloop
nospread:
	move.w	#doingnothing,palettemode
	move.w	#-1,wascolorset
	move.l	savebutton,a0
	bsr	putbuttonup
	move.l	#panelsprite,currentpanelsprite
	bra	dopaletteloop
itsnothing:
	move.w	#-1,wascolorset
	bra	dopaletteloop



*****************************************************************
*	This guy checks if the mouse is over the panel or	*
*	not and see's if a button is pressed over a button	*
*****************************************************************
domousemenu2:
	move.w	#-1,wascolorset
	move.w	buttondat,buttondat2	;copy this so it don't change
	tst.w	buttondat2
	beq	nomousepress2
	tst.w	mousehasgoneup
	beq	dmmover2
	move.w	#0,mousehasgoneup
	tst.w	onpanel		;is it over the panel area
	bmi	getacolor
	lea	panel2buttonlist,a0	;a0, for findbutton routine
	bra	findbutton2
dmmover2:
	bra	dopaletteloop
nomousepress2:
	bra	dopaletteloop

getacolor:
	move.w	mx,d0
	move.w	my,d1
	move.l	currentscreen,a6
	bsr	getpoint
	move.w	color,d6
	move.w	d6,d0
	asl.w	#2,d0
	lea	colorbuttons,a0
	add.w	d0,a0
	cmpi.w	#leftbuttondown,buttondat2
	bne	shouldsetback
	bra	setfcolorp
shouldsetback:
	bra	setbcolorp


*********************************************************
*	address of buttonlist in a0			*
*********************************************************
findbutton2:
	move.l	a0,a6	;save buttonlist
	move.w	mx,d0
	move.w	my,d1
	sub.w	panely,d1
	cmpi.w	#64,d1		;is it below the tools section and thus color
	bge	oncolor2
findbuttonloop2:
	move.l	(a0)+,d7
	beq	findbuttonover2	;are we at the end of the list ?
	move.l	d7,a1
	move.w	buttonx(a1),d2	;if not check if this button is
	move.w	buttony(a1),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextbutton2
	cmp.w	d1,d3
	bgt.s	nextbutton2
	add.w	buttonwidth(a1),d2
	add.w	buttonheight(a1),d3
	cmp.w	d0,d2
	blt.s	nextbutton2
	cmp.w	d1,d3
	blt.s	nextbutton2	;if we get through this this is our man
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbutton2
	bsr	dobuttonsupdown	;some buttons mean others become unselected
	move.l	buttonlroutine(a1),d2
	beq	dopaletteloop
	move.l	d2,a0
	jmp	(a0)
isrightbutton2:			;no right button routines for palette
	bra	dopaletteloop
nextbutton2:
	bra	findbuttonloop2
findbuttonover2:
	bra	dopaletteloop
*********************************************************
*	Routine comes here to check if I clicked on a	*
*	color button which are just the xy coords in	*
*	order.						*
*	When the button is found it sets a bar above 	*
*	the color if it was clicked with the left mouse	*
*	button, indicating it is the new foreground 	*
*	color (and clears the bar from the previous).	*
*	And sets a bar underneath the color if it was	*
*	the right m button indicating new background	*
*	color.						*
*	d0,d1 are x,y					*
*********************************************************
oncolor2:
	lea	colorbuttons,a0
	move.w	#255,d7		;check 256 colors
	moveq	#0,d6
checkcolorsloop2:
	move.w	(a0),d2		;get x and y of color box
	move.w	2(a0),d3	;under our pointer
	cmp.w	d0,d2
	bgt.s	nextcbutton2
	cmp.w	d1,d3
	bgt.s	nextcbutton2
	addq.w	#8,d2		;they're all 8*8
	addq.w	#8,d3
	cmp.w	d0,d2
	blt.s	nextcbutton2
	cmp.w	d1,d3
	blt.s	nextcbutton2	;if we get through this this is our man
	cmpi.w	#leftbuttondown,buttondat2
	bne	isrightbuttonc2
	move.w	#fcolorset,wascolorset
	bra	setfcolorp
isrightbuttonc2:
	cmpi.w	#rightbuttondown,buttondat2
	bne	wasboth2
	move.w	#bcolorset,wascolorset
	bra	setbcolorp
nextcbutton2:
	addq.l	#4,a0		;increment to next color box coords
	addq.w	#1,d6		;next color value 0-255
	dbf	d7,checkcolorsloop2
wasboth2:
	bra	dopaletteloop
*************************************************
*	A0 points at x,y d6 has colornum	*
*************************************************
setfcolorp:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait11
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait11:
	movem.l	d6/a0,-(sp)	;save the color
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentfcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	subq.w	#2,d1
	bsr	blitclearsp
	movem.l	(sp)+,d6/a0
	move.w	d6,currentfcolor
	move.w	(a0),d0
	move.w	2(a0),d1
	subq.w	#2,d1
	lea	fcolordat,a0
	bsr	blitcolorsp
	bsr	setfcolorbar
	bsr	printcolor
	move.w	(sp)+,mouseonoff
	bra	dopaletteloop
setbcolorp:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait12
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait12:
	movem.l	d6/a0,-(sp)	;save the color
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest
	moveq	#0,d0
	lea	colorbuttons,a6
	move.w	currentbcolor,d7
	asl.w	#2,d7		;mult by 4
	move.w	(a6,d7.w),d0	;x of old fcolor
	move.w	2(a6,d7.w),d1	;y of old fcolor
	add.w	#8,d1
	bsr	blitclearsp
	movem.l	(sp)+,d6/a0
	move.w	d6,currentbcolor
	move.w	(a0),d0
	move.w	2(a0),d1
	add.w	#8,d1
	lea	bcolordat,a0
	bsr	blitcolorsp
	bsr	setbcolorbar
	move.w	(sp)+,mouseonoff
	bra	dopaletteloop

*****************************************************************
*	When a routine is called the address of the button	*
*	is in a1 and mx and my at time of collision are in d0,d1*
*****************************************************************
dopcopy:
	move.l	a1,savebutton	;save for upping after
	move.l	#tosprite,currentpanelsprite
	move.w	#doingcopy,palettemode
	move.w	currentfcolor,fcolorsave
	bra	dopaletteloop
dopexchange:
	move.l	a1,savebutton	;save for upping after
	move.l	#tosprite,currentpanelsprite
	move.w	#doingex,palettemode
	move.w	currentfcolor,fcolorsave
	bra	dopaletteloop
spread:
	move.l	a1,savebutton	;save for upping after
	move.l	#tosprite,currentpanelsprite
	move.w	#doingspread,palettemode
	move.w	currentfcolor,fcolorsave
	bra	dopaletteloop
redup:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$00ff,d1
	and.w	#$0f00,d0		;isolate red
	add.w	#$0100,d0
	and.w	#$0f00,d0		;isolate red
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop
reddown:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$00ff,d1
	and.w	#$0f00,d0		;isolate red
	sub.w	#$0100,d0
	and.w	#$0f00,d0		;isolate red
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop
greenup:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$0f0f,d1
	and.w	#$00f0,d0
	add.w	#$0010,d0
	and.w	#$00f0,d0
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop
greendown:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$0f0f,d1
	and.w	#$00f0,d0		;isolate red
	sub.w	#$0010,d0
	and.w	#$00f0,d0		;isolate red
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop
blueup:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$0ff0,d1
	and.w	#$000f,d0
	add.w	#$0001,d0
	and.w	#$000f,d0
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop
bluedown:
	move.w	#1,mustrecalc
	move.l	#panelsprite,currentpanelsprite
	move.w	#doingnothing,palettemode
	move.l	a1,savebutton	;save for upping after
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	move.w	(a0,d7.w),d0
	move.w	d0,d1
	and.w	#$0ff0,d1
	and.w	#$000f,d0		;isolate red
	sub.w	#$0001,d0
	and.w	#$000f,d0		;isolate red
	or.w	d0,d1
	move.w	d1,(a0,d7.w)
	lea	$ff8400,a0
	move.w	d1,(a0,d7.w)
	bsr	printcolor
	move.l	savebutton,a0
	bsr	putbuttonup
	bra	dopaletteloop


printcolor:
	move.l	$44e,-(sp)
	move.l	panelad,$44e
	move.w	currentfcolor,d7
	move.l	currentpalette,a0
	add.w	d7,d7
	moveq	#0,d0
	move.w	(a0,d7.w),d0

	moveq	#16,d1		;hex
	bsr	converttoassci
	dc.w	$a000
	move.w	#7,wmode(a0)
	move.w	#0,clip(a0)
	move.w	#0,xmincl(a0)
	move.w	#319,xmaxcl(a0)
	move.w	#0,ymincl(a0)
	move.w	#479,ymaxcl(a0)
	move.w	#7,textfg(a0)
	move.w	#12,textbg(a0)
	move.w	#41,destx(a0)
	move.w	#23,desty(a0)
	move.w	#0,style(a0)
	move.w	#0,scale(a0)
	move.w	#1,mono(a0)
	move.l	8(a1),a1
	move.l	data_table(a1),fbase(a0)
	move.w	form_width(a1),fwidth(a0)
	move.w	form_height(a1),dely(a0)

	lea	string,a2
	move.l	off_table(a1),a3
	moveq	#0,d0
printblit:
	moveq	#0,d0
	move.b	(a2)+,d0
	ble	tblitover
	sub.w	first_ade(a1),d0
	lsl.w	#1,d0
	move.w	0(a3,d0.w),sourcex(a0)
	move.w	2(a3,d0.w),d0
	sub.w	sourcex(a0),d0
	move.w	d0,delx(a0)
	clr.w	sourcey(a0)
	movem.l	a0-a3,-(sp)
	move.l	lineatextblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0-a3
	bra	printblit
tblitover:
	move.l	(sp)+,$44e
	rts

printcolor2:
	movem.l	d0-a6,-(sp)
	move.l	$44e,-(sp)
	move.l	panelad,$44e
	move.w	rgb,d0

	moveq	#16,d1		;hex
	bsr	converttoassci
	dc.w	$a000
	move.w	#7,wmode(a0)
	move.w	#0,clip(a0)
	move.w	#0,xmincl(a0)
	move.w	#319,xmaxcl(a0)
	move.w	#0,ymincl(a0)
	move.w	#479,ymaxcl(a0)
	move.w	#7,textfg(a0)
	move.w	#12,textbg(a0)
	move.w	#41,destx(a0)
	move.w	#23,desty(a0)
	move.w	#0,style(a0)
	move.w	#0,scale(a0)
	move.w	#1,mono(a0)
	move.l	8(a1),a1
	move.l	data_table(a1),fbase(a0)
	move.w	form_width(a1),fwidth(a0)
	move.w	form_height(a1),dely(a0)

	lea	string,a2
	move.l	off_table(a1),a3
	moveq	#0,d0
printblit2:
	moveq	#0,d0
	move.b	(a2)+,d0
	ble	tblitover2
	sub.w	first_ade(a1),d0
	lsl.w	#1,d0
	move.w	0(a3,d0.w),sourcex(a0)
	move.w	2(a3,d0.w),d0
	sub.w	sourcex(a0),d0
	move.w	d0,delx(a0)
	clr.w	sourcey(a0)
	movem.l	a0-a3,-(sp)
	move.l	lineatextblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0-a3
	bra	printblit2
tblitover2:
	move.l	(sp)+,$44e
	movem.l	(sp)+,d0-a6
	rts


printline:
	move.l	$44e,-(sp)
	move.l	panelad,$44e
	move.l	currentpalette,a0
	move.w	(a0),rgb
	bsr	findfurthest

	dc.w	$a000
	move.w	#7,wmode(a0)
	move.w	#0,clip(a0)
	move.w	#0,xmincl(a0)
	move.w	#319,xmaxcl(a0)
	move.w	#0,ymincl(a0)
	move.w	#479,ymaxcl(a0)
	move.w	color,textfg(a0)
	move.w	#0,textbg(a0)
	move.w	#53,destx(a0)
	move.w	#70,desty(a0)
	move.w	#0,style(a0)
	move.w	#0,scale(a0)
	move.w	#1,mono(a0)
	move.l	8(a1),a1
	move.l	data_table(a1),fbase(a0)
	move.w	form_width(a1),fwidth(a0)
	move.w	form_height(a1),dely(a0)

	lea	pwmessage,a2
	bsr	putit
	move.w	#113,destx(a0)
	move.w	#86,desty(a0)
	lea	pwmessage2,a2
	bsr	putit

	move.l	(sp)+,$44e
	rts
putit:
	move.l	off_table(a1),a3
	moveq	#0,d0
printblitm:
	moveq	#0,d0
	move.b	(a2)+,d0
	ble	tblitoverm
	sub.w	first_ade(a1),d0
	lsl.w	#1,d0
	move.w	0(a3,d0.w),sourcex(a0)
	move.w	2(a3,d0.w),d0
	sub.w	sourcex(a0),d0
	move.w	d0,delx(a0)
	clr.w	sourcey(a0)
	movem.l	a0-a3,-(sp)
	move.l	lineatextblt,a5
	jsr	(a5)
	movem.l	(sp)+,a0-a3
	bra	printblitm
tblitoverm:
	rts


pwmessage:	dc.b	'Calculating palette tables.',0
pwmessage2:	dc.b	'Please wait.',0
	even

*****************************************
*	number in d0.l			*
*	d1.l is base			*
*****************************************
converttoassci:
	lea	string,a0
	move.b	#'0',(a0)+
	move.b	#'0',(a0)+
	move.b	#'0',(a0)+
	move.b	#0,(a0)+
	subq.l	#1,a0
loop1ca:
	divu	d1,d0
	swap	d0
	addi	#'0',d0
	cmpi.w	#'9',d0
	ble.s	okca
	addq.w	#7,d0
okca:
	move.b	d0,-(a0)
	clr.w	d0
	swap	d0
	bne	loop1ca
	rts
converttoassci2:
	lea	string,a0
	move.l	#0,(a0)+
	subq.l	#1,a0
loop1ca2:
	divu	d1,d0
	swap	d0
okca2:
	move.b	d0,-(a0)
	clr.w	d0
	swap	d0
	bne	loop1ca2
	rts

string:	dc.l	0

savepalette:
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	move.w	#doingnothing,palettemode
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	begingem
	jsr	gouser
	move.l	#savepaltitle,-(sp)
	move.l	#palextender,-(sp)
	jsr	_fileselector
	addq.l	#8,sp
	tst.l	d0
	ble	spalcancelled
	move.l	currentpalette,a0
	sub.l	#12,a0
	move.w	currentfcolor,8(a0)
	move.w	currentbcolor,10(a0)
	move.l	a0,-(sp)
	move.l	#512+8+4,-(sp)
	jsr	_savefile	;load the pic into hold area
	addq.l	#8,sp
spalcancelled:
	jsr	gosup
	bsr	endgem
	lea	p2savepal,a0
	bsr	putbuttonup
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dopaletteloop

loadpalette:
	move.w	#1,mustrecalc
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	move.w	#doingnothing,palettemode
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	begingem
	jsr	gouser
	move.l	#loadpaltitle,-(sp)
	move.l	#palextender,-(sp)
	jsr	_fileselector
	addq.l	#8,sp
	tst.l	d0
	ble	lpalcancelled

	lea	temppalette,a0
	move.l	a0,-(sp)
	move.l	#512+8+4,-(sp)
	jsr	_loadfile	;load the pic into hold area
	addq.l	#8,sp
	jsr	gosup
	bsr	endgem

	lea	temppalette,a0	;is it really a palette
	cmpi.l	#'MVPX',(a0)+
	bne.s	notourpal
	cmpi.l	#'PALT',(a0)+
	bne.s	notourpal
	move.w	(a0)+,currentfcolor
	move.w	(a0)+,currentbcolor
	move.l	currentpalette,a1
	lea	$ff8400,a2
	move.w	#256-1,d1
dcpal:
	move.w	(a0),d0
	move.w	(a1),(a0)+
	move.w	d0,(a1)+
	move.w	d0,(a2)+
	dbf	d1,dcpal
	bsr	constructpalette
	bsr	setfcolor2
	bsr	setbcolor2
	bsr	setfcolorbar
	bsr	setbcolorbar
notourpal:
	lea	p2loadpal,a0
	bsr	putbuttonup
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dopaletteloop

lpalcancelled:
	jsr	gosup
	bsr	endgem
	lea	p2loadpal,a0
	bsr	putbuttonup
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dopaletteloop


savesp3:	dc.l	0
mysp:		ds.b	500
savepaltitle:
	dc.b	'Save Palette',0
	even
loadpaltitle:
	dc.b	'Load Palette',0
	even
palextender:	dc.b	'PAL',0
	even

panel2buttonlist:
	dc.l	p2tools,p2drag,p2copy,p2exchange,p2nums
	dc.l	p2redup,p2greenup,p2blueup
	dc.l	p2reddown,p2greendown,p2bluedown,p2spread
	dc.l	p2savepal,p2loadpal,0

p2savepal:
	dc.w	144,33	;x,y on panel screen
	dc.w	240,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	savepalette	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange
	dc.l	p2spread,0
p2loadpal:
	dc.w	176,33	;x,y on panel screen
	dc.w	264,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	loadpalette	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange
	dc.l	p2spread,0
p2spread:
	dc.w	144,5	;x,y on panel screen
	dc.w	184,209	;x,y of unselected part
	dc.w	55,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	spread	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,0	;null terminated list of buttons this one raises

p2redup:
	dc.w	41,5	;x,y on panel screen
	dc.w	312,161	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	redup	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2greenup:
	dc.w	49,5	;x,y on panel screen
	dc.w	312,161	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	greenup	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2blueup:
	dc.w	57,5	;x,y on panel screen
	dc.w	312,161	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	blueup	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2reddown:
	dc.w	41,44	;x,y on panel screen
	dc.w	312,185	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	reddown	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2greendown:
	dc.w	49,44	;x,y on panel screen
	dc.w	312,185	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	greendown	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2bluedown:
	dc.w	57,44	;x,y on panel screen
	dc.w	312,185	;x,y of unselected part
	dc.w	7,12	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	bluedown	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2exchange,p2spread,0	;null terminated list of buttons this one raises


p2nums:
	dc.w	33,19	;x,y on panel screen
	dc.w	56,257	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	0	;doesnt depress
	dc.l	0	;no routine just for construction
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises
p2tools:
	dc.w	208,33	;x,y on panel screen
	dc.w	296,209	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;doesnt depress
	dc.l	dotools	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises
p2copy:
	dc.w	88,5	;x,y on panel screen
	dc.w	128,209	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	dopcopy	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2exchange,p2spread,0	;null terminated list of buttons this one raises
p2exchange:
	dc.w	88,33	;x,y on panel screen
	dc.w	240,209	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	dopexchange	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p2copy,p2spread,0	;null terminated list of buttons this one raises
p2drag:
	dc.w	264,5	;x,y on panel screen
	dc.w	0,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	draggingp	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)

*************************************************************************
*	END OF PALETTE STUFF						*
*************************************************************************

*****************************************************************
*	When a routine is called the address of the button	*
*	is in a1 and mx and my at time of collision are in d0,d1*
*****************************************************************
dragging:
	cmpi.w	#leftbuttondown,buttondat
	bne	paintloop
	move.w	my,d2
	sub.w	d1,d2

	cmpi.w	#hblhighest,d2
	bgt.s	dok1
	move.w	#hblhighest,d2
	bra	dok2
dok1:
	move.w	#479,d3
	sub.w	panelheight,d3
	sub.w	#1,d3
	cmp.w	d3,d2
	blt.s	dok2
	move.w	d3,d2
dok2:
	move.w	d2,panely
	move.w	d0,mx
	bra	dragging
draggingp:
	cmpi.w	#leftbuttondown,buttondat
	bne	dopaletteloop
	move.w	my,d2
	sub.w	d1,d2

	cmpi.w	#hblhighest,d2
	bgt.s	dok1p
	move.w	#hblhighest,d2
	bra	dok2p
dok1p:
	move.w	#479,d3
	sub.w	panelheight,d3
	sub.w	#1,d3
	cmp.w	d3,d2
	blt.s	dok2p
	move.w	d3,d2
dok2p:
	move.w	d2,panely
	move.w	d0,mx
	bra	draggingp
draggingf:
	cmpi.w	#leftbuttondown,buttondat
	bne	dofileloop
	move.w	my,d2
	sub.w	d1,d2

	cmpi.w	#hblhighest,d2
	bgt.s	dok1f
	move.w	#hblhighest,d2
	bra	dok2f
dok1f:
	move.w	#479,d3
	sub.w	panelheight,d3
	sub.w	#1,d3
	cmp.w	d3,d2
	blt.s	dok2f
	move.w	d3,d2
dok2f:
	move.w	d2,panely
	move.w	d0,mx
	bra	draggingf
*****************************************************************
*	Call after the palette goes down to make sure its	*
*	not off screen.						*
*****************************************************************
adjustpanel:
	move.w	panely,d2
	cmpi.w	#1,d2
	bgt.s	dok1a
	move.w	#1,d2
	bra	dok2a
dok1a:
	move.w	#479,d3
	sub.w	panelheight,d3
	sub.w	#1,d3
	cmp.w	d3,d2
	blt.s	dok2a
	move.w	d3,d2
dok2a:
	move.w	d2,panely
	rts

setdrawmodedots:
	bsr	handlespecialcases
	move.w	#drawing,paintmode
	move.l	#ttdots,drawroutine
	move.w	#1,shouldfollow
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop
setdrawmodedraw:
	bsr	handlespecialcases
	move.w	#drawing,paintmode
	move.l	#ttdraw,drawroutine
	move.w	#1,shouldfollow
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop
adjustoffsets:
	move.w	brushw(a0),d0
	move.w	brushh(a0),d1
	asr.w	#1,d0
	asr.w	#1,d1
	move.w	d1,mouseoffsety
	move.w	d0,mouseoffsetx
	rts

setbrush1:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush1,currentbrush
	lea	brush1,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush2:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush2,currentbrush
	lea	brush2,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush3:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush3,currentbrush
	lea	brush3,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush4:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush4,currentbrush
	lea	brush4,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush5:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush5,currentbrush
	lea	brush5,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush6:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush6,currentbrush
	lea	brush6,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush7:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush7,currentbrush
	lea	brush7,a0
	bsr	adjustoffsets
	bra	paintloop
setbrush8:
	bsr	undrawbrush
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	move.l	#brush8,currentbrush
	lea	brush8,a0
	bsr	adjustoffsets
	bra	paintloop

getoldbrush:
	lea	p1getbrush,a0
	bsr	putbuttondown
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	lea	p1getbrush,a0
	bsr	putbuttonup
	lea	p1brush1,a0
	bsr	putbuttonup
	lea	p1brush2,a0
	bsr	putbuttonup
	lea	p1brush3,a0
	bsr	putbuttonup
	lea	p1brush4,a0
	bsr	putbuttonup
	lea	p1brush5,a0
	bsr	putbuttonup
	lea	p1brush6,a0
	bsr	putbuttonup
	lea	p1brush7,a0
	bsr	putbuttonup
	lea	p1brush8,a0
	bsr	putbuttonup
	move.l	#userbrush,currentbrush
	lea	userbrush,a0
	bsr	adjustoffsets
	move.w	#mattemode,brushmode
	lea	p4sdmmatte,a0
	bsr	raisebuttons
	bra	paintloop
*****************************************************************
*	Handles special cases when clicking on the panel	*
*	this might mean I have to get rid of curve stuff etc	*
*	So far only curve stuff is handled.			*
*****************************************************************
handlespecialcases:
	cmpi.w	#drawingcurve,paintmode
	bne.s	notdc
	bsr	findmaxnmin
	bsr	restoreunder
notdc:
	rts

getbrush:
	bsr	undrawbrush
	bsr	handlespecialcases
*	move.w	shouldfollow,shouldfollowsave
	move.w	#-1,shouldfollow
	move.w	#-1,doinloop
*	tst.l	oldpaintmode
*	bpl	nosp
*	move.w	paintmode,oldpaintmode
*nosp:
	move.w	#gettingbrush,paintmode
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop

line:
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#wmreplace,writemode
	move.w	#usebrush,todraw	;should be usebrush
	move.w	#drawingline,paintmode
	move.w	currentfcolor,color
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop
setfill:
	move.w	#-1,shouldfollow
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#filling,paintmode
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#fillsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop

dotools:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	move.l	savebutton,d0
	beq	nobuttonsaved
	move.l	d0,a0
	bsr	putbuttonup
nobuttonsaved:
	tst.w	mustrecalc
	bmi.s	noneedtorecalc
	bsr	printline
	bsr	makecolortable
noneedtorecalc:
	move.l	#panelsprite,currentpanelsprite
	lea	p2tools,a0
	bsr	putbuttonup
	bsr	setuppanel1p
	move.w	#1,mouseonoff
	bra	paintloop
dotools3:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	move.l	savebutton,d0
	beq	nobuttonsaved3
	move.l	d0,a0
	bsr	putbuttonup
nobuttonsaved3:
	move.l	#panelsprite,currentpanelsprite
	lea	p3tools,a0
	bsr	putbuttonup
	bsr	setuppanel1
	move.w	#1,mouseonoff
	bra	paintloop

setbox:
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#usebrush,todraw	;should be usebrush
	move.w	#drawingbox,paintmode
	move.w	currentfcolor,color
	cmpi.w	#emptyfill,filltype
	beq.s	bwf
	move.w	#-1,shouldfollow
bwf:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop

setspray:
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#usebrush,todraw	;should be usebrush
	move.w	#drawingspray,paintmode
	move.w	currentfcolor,color
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop
setupcurve:
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#-1,shouldfollow
	move.w	#usepixel,todraw	;should be usebrush
	move.w	#wmreplace,writemode
	move.w	#drawingcurve,paintmode
	lea	curvepointlist,a0
	move.w	#50,(a0)+
	move.w	#220,(a0)+
	move.w	#50+73,(a0)+
	move.w	#110,(a0)+
	move.w	#50+73+73,(a0)+
	move.w	#330,(a0)+
	move.w	#320-50,(a0)+
	move.w	#220,(a0)+
	move.w	currentfcolor,color
	bsr	copyforundo
	bsr	drawcurve
	bsr	drawbrush
	bsr	drawpoints
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop
setupelipse:
	bsr	undrawbrush
	bsr	handlespecialcases
	move.w	#1,shouldfollow
	move.w	#usepixel,todraw	;should be usebrush
	move.w	#wmreplace,writemode
	move.w	#drawingelipse,paintmode
	move.w	currentfcolor,color
	bsr	copyforundo
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.l	currentdrawsprite,a6
	move.w	8(a6),d7			;preserve color of sprite
	move.l	#drawsprite,currentdrawsprite
	move.l	currentdrawsprite,a6
	move.w	d7,8(a6)
	move.w	#1,mouseonoff
	bra	paintloop


doclear:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait13
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait13:
	bsr	undrawbrush
	lea	p1clear,a0
	bsr	putbuttondown
	bsr	copyforundo
	bsr	clearcurrent
	lea	p1clear,a0
	bsr	putbuttonup
	move.w	(sp)+,mouseonoff
	bra	paintloop
doundo2:
	move.w	mouseonoff,-(sp)
	bmi.s	nowait14
	move.w	#-1,mouseonoff
	bsr	waitvbl
nowait14:
	bsr	undrawbrush
	lea	p1undo,a0
	bsr	putbuttondown
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	doundo
	lea	p1undo,a0
	bsr	putbuttonup
	move.w	(sp)+,mouseonoff
	bra	paintloop



clearcurrent:
	move.l	currentscreen,a0
	move.w	#479,d7
	moveq	#0,d0
ccl:
	rept	320/4
	move.l	d0,(a0)+
	endr
	dbf	d7,ccl
	rts

panel1buttonlist:
	dc.l	p1dots,p1draw,p1drag,p1brush1,p1brush2,p1brush3
	dc.l	p1brush4,p1brush5,p1brush6,p1brush7,p1brush8
	dc.l	p1getbrush,p1line,p1fill,p1palette,p1clear
	dc.l	p1undo,p1box,p1spray,p1file,p1fill,p1curve,p1elipse,0


buttonx:	equ	0
buttony:	equ	2
buttonpartx:	equ	4
buttonparty:	equ	6
buttonwidth:	equ	8
buttonheight:	equ	10
buttonselectable:	equ	12
buttonlroutine:	equ	14
buttonrroutine:	equ	18
buttonselected:	equ	22
buttondeselects:equ	24
p1elipse:
	dc.w	72,5	;x,y on panel screen
	dc.w	72,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setupelipse	;routine to jump to on left button
	dc.l	dofillprefs	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1line,p1box,p1spray,p1fill,p1curve,0
p1curve:
	dc.w	120,5	;x,y on panel screen
	dc.w	120,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setupcurve	;routine to jump to on left button
	dc.l	dolineprefs	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1line,p1box,p1spray,p1fill,p1elipse,0
p1palette:
	dc.w	144,33	;x,y on panel screen
	dc.w	288,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	dopalette	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises
p1file:
	dc.w	168,33	;x,y on panel screen
	dc.w	144,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	dofile	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises
p1clear:
	dc.w	240,5	;x,y on panel screen
	dc.w	120,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	doclear	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises
p1undo:
	dc.w	240,33	;x,y on panel screen
	dc.w	96,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	doundo2	;routine to jump to on left button
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises

p1fill:
	dc.w	192,5	;x,y on panel screen
	dc.w	192,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setfill	;routine to jump to on left button
	dc.l	dofillprefs	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1line,p1box,p1spray,p1curve,p1elipse,0
p1spray:
	dc.w	144,5	;x,y on panel screen
	dc.w	144,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setspray	;routine to jump to on left button
	dc.l	dosprayprefs	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1line,p1box,p1fill,p1curve,p1elipse,0

p1box:
	dc.w	48,5	;x,y on panel screen
	dc.w	48,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbox	;routine to jump to on left button
	dc.l	dofillprefs		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1fill,p1line,p1spray,p1curve,p1elipse,0
p1line:
	dc.w	96,5	;x,y on panel screen
	dc.w	96,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	line	;routine to jump to on left button
	dc.l	dolineprefs		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots	;null terminated list of buttons this one raises
	dc.l	p1draw,p1getbrush,p1fill,p1box,p1spray,p1curve,p1elipse,0

p1getbrush:
	dc.w	168,5	;x,y on panel screen
	dc.w	168,161	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	getbrush	;routine to jump to on left button
	dc.l	dobrushprefs	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1brush2	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush6,p1brush7,p1brush8
	dc.l	p1brush1,p1fill,0

p1drag:
	dc.w	264,5	;x,y on panel screen
	dc.w	0,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	dragging	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p1dots:
	dc.w	0,5	;x,y on panel screen
	dc.w	0,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setdrawmodedots	;routine to jump to on left button
	dc.l	dobrushprefs		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1draw,p1line,p1getbrush,p1fill,p1box,p1spray,p1curve,p1elipse,0
p1draw:
	dc.w	24,5	;x,y on panel screen
	dc.w	24,161	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setdrawmodedraw	;routine to jump to on left button
	dc.l	dobrushprefs		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p1dots,p1line,p1getbrush,p1fill,p1box,p1spray,p1curve,p1elipse,0
p1brush1:
	dc.w	0,33	;x,y on panel screen
	dc.w	0,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush1	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush2	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush6,p1brush7,p1brush8,0
p1brush2:
	dc.w	16,33	;x,y on panel screen
	dc.w	16,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush2	;routine to jump to on left button
	dc.l	getoldbrush	;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush6,p1brush7,p1brush8,0
p1brush3:
	dc.w	32,33	;x,y on panel screen
	dc.w	32,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush3	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush2,p1brush4,p1brush5,p1brush6,p1brush7,p1brush8,0
p1brush4:
	dc.w	48,33	;x,y on panel screen
	dc.w	48,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush4	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush2,p1brush5,p1brush6,p1brush7,p1brush8,0
p1brush5:
	dc.w	64,33	;x,y on panel screen
	dc.w	64,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush5	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush2,p1brush6,p1brush7,p1brush8,0
p1brush6:
	dc.w	80,33	;x,y on panel screen
	dc.w	80,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush6	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush2,p1brush7,p1brush8,0
p1brush7:
	dc.w	96,33	;x,y on panel screen
	dc.w	96,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush7	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush6,p1brush2,p1brush8,0
p1brush8:
	dc.w	112,33	;x,y on panel screen
	dc.w	112,209	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	setbrush8	;routine to jump to on left button
	dc.l	getoldbrush		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p1brush1	;null terminated list of buttons this one raises
	dc.l	p1brush3,p1brush4,p1brush5,p1brush6,p1brush7,p1brush2,0

keyboard2:
        move.w  #$ff,-(sp)
        move.w  #6,-(sp)
        trap    #1
        addq.l  #4,sp
        tst.l   d0
        beq     keyback
        swap    d0
quitk2:
	cmpi.w  #$10,d0
        bne     keyback
        bra     returntogem

keyboard:
        move.w  #$ff,-(sp)
        move.w  #6,-(sp)
        trap    #1
        addq.l  #4,sp
        tst.l   d0
        beq     keyback
        swap    d0
quitk:   cmpi.w  #$10,d0
        bne     pkey
        bra     returntogem
pkey:
	cmpi.w	#$19,d0
	bne	deletekey
*	addq.w	#1,paletteshowing
*	cmpi.w	#2,paletteshowing
*	bne.s	dopaletteshowing
*	move.w	#-1,paletteshowing
	neg.w	paletteshowing
	bpl	dopaletteshowing
removepalette:
	cmpi.w	#panel2,currentpanel
	beq	cantremove		;if palette screen cant remove
	move.w	#-1,mouseonoff
	bsr	waitvbl
	bsr	setuppanel1
	move.w	#1,mouseonoff
	rts
dopaletteshowing:
	cmpi.w	#panel2,currentpanel
	beq	pok2
	move.w	#-1,mouseonoff
	bsr	waitvbl
*	tst.w	paletteshowing	;if its 1 do mag not palette
*	beq.s	justpal
*	jsr	setuppanel1m
*	move.w	#1,mouseonoff
*	rts
justpal:
	jsr	setuppanel1p
	move.w	#1,mouseonoff
pok2:
	rts
cantremove:
	move.w	#1,paletteshowing
	rts

deletekey:
	cmpi.w	#$53,d0
	bne	undokey
	neg.w	drawcursorflag
	rts
undokey:
	cmpi.w	#$61,d0
	bne	f1key
	jsr	doundo
	rts
f1key:
	cmpi.w	#$3b,d0
	bne	f2key
	move.w	#-1,oldmx
	move.w	#mattemode,brushmode
	lea	p4sdmmatte,a0
	bsr	raisebuttons
	rts
f2key:
	cmpi.w	#$3c,d0
	bne	f3key
	move.w	#-1,oldmx
	move.w	#replacemode,brushmode
	lea	p4sdmreplace,a0
	bsr	raisebuttons
	rts
f3key:
	cmpi.w	#$3d,d0
	bne	f4key
	move.w	#-1,oldmx
	move.w	#colormode,brushmode
	lea	p4sdmcolor,a0
	bsr	raisebuttons
	rts
f4key:
	cmpi.w	#$3e,d0
	bne	f5key
	move.w	#-1,oldmx
	move.w	#cyclemode,brushmode
	lea	p4sdmcycle,a0
	bsr	raisebuttons
	rts
f5key:
	cmpi.w	#$3f,d0
	bne	tabkey
	move.w	#-1,oldmx
	move.w	#transparentmode,brushmode
	bsr	calctransparencies
	lea	p4sdmtransparent,a0
	bsr	raisebuttons
	rts

tabkey:
	cmpi.w	#$f,d0
	bne	f10key
	neg.w	cycleflag
	bpl	wellsaveit
	move.l	currentpalette,a1
	lea	palette3,a0		;restore original
	bsr	copypal
	bsr	setpalette
	rts
wellsaveit:
	lea	palette3,a1
	move.l	currentpalette,a0	;save original
	bsr	copypal
	bsr	preparecycle2
	rts
f10key:
	cmpi.w	#$44,d0
	bne	bkey
	neg.w	panelshowing
	rts
bkey:
	cmpi.w	#$30,d0			;get brush
	bne	xkey
	lea	p1getbrush,a1
	bsr	dobuttonsupdown	;some buttons mean others become unselected
	addq.l	#4,sp		;remove return address
	move.l	buttonlroutine(a1),a0
	jmp	(a0)
	rts
xkey:
	cmpi.w	#$2d,d0			;flipx
	bne	ykey
	bsr	flipx
	rts
ykey:
	cmpi.w	#$15,d0			;flipy
	bne	hkey
	bsr	flipy
	rts
hkey:
	cmpi.w	#$23,d0			;1/2 size brush
	bne	jkey
	bsr	halvebrush
	rts
jkey:
	cmpi.w	#$24,d0			;swap screens
	bne	pluskey
	bsr	swapscreens
	rts
pluskey:
	cmpi.w	#$4e,d0			;circle
	bne	minuskey
	addq.w	#1,magtrack
	cmpi.w	#3,magtrack
	ble.s	maglevok1
	move.w	#3,magtrack
maglevok1:
	move.w	magtrack,d0
	mulu	#6,d0
	lea	maglist,a0
	add.w	d0,a0
	move.w	(a0)+,currentmag
	move.w	(a0)+,currentmh
	move.w	(a0)+,currentmw
	bsr	dodomag
	rts
minuskey:
	cmpi.w	#$4a,d0			;circle
	bne	spacekey
	subq.w	#1,magtrack
	cmpi.w	#0,magtrack
	bge.s	maglevok2
	move.w	#0,magtrack
maglevok2:
	move.w	magtrack,d0
	mulu	#6,d0
	lea	maglist,a0
	add.w	d0,a0
	move.w	(a0)+,currentmag
	move.w	(a0)+,currentmh
	move.w	(a0)+,currentmw
	bsr	dodomag
	rts

spacekey:
	cmpi.w	#$39,d0			;enter curve
	bne	keyback
	cmpi.w	#drawingcurve,paintmode
	bne	keyback
	move.w	#-1,mouseonoff
	bsr	waitvbl
	move.w	#drawing,paintmode
	move.l	#ttdots,drawroutine
	move.w	#1,shouldfollow
	lea	p1curve,a0
	bsr	putbuttonup
	lea	p1dots,a0
	bsr	putbuttondown
	bsr	findmaxnmin
	bsr	restoreunder
	move.l	linepattern2,linepattern
	move.w	#usebrush,todraw	;should be usebrush
	bsr	drawcurve
	move.w	#1,mouseonoff		;turn the mouse off
	rts
keyback:rts


halvebrush:
	cmp.l	#userbrush,currentbrush
	bne	anrts			;all purpose rts
	move.w	#-1,mouseonoff
	bsr	waitvbl
	jsr	undrawbrush
	lea	userbrush,a0
	move.w	brushh(a0),d7
	cmpi.w	#1,d7
	move.w	d7,saveheight
	beq	nohalvey
	asr.w	#1,d7			;div by 2
nohalvey:
	move.w	brushw(a0),d6
	cmpi.w	#1,d7
	beq	nohalvex
	asr.w	#1,d6			;div by 2
nohalvex:
	move.w	d7,brushh(a0)
	move.w	d6,brushw(a0)
	move.w	d6,generalvalue

	moveq	#2,d2
	moveq	#2,d3
	lea	savebrush,a1
	move.l	brushdat(a1),a1		;where to put it
	move.l	brushdat(a0),a0
	bsr	doresize
	bsr	makemask
	bsr	handlemosaic
	lea	userbrush,a0
	bsr	adjustoffsets
	move.w	#-1,oldmx
	move.w	#1,mouseonoff
	rts

handlemosaic:
	cmpi.w	#patternfill,filltype
	bne	skipmosaic
	bsr	makemosaic
skipmosaic:
	rts

*****************************************
*	doresize			*
*	used with halve, halvex and	*
*	halvey.				*
*	generalvalue,is x times, d7 y, a0 is	*
*	source image, a1 is dest.	*
*	d2 is x step d3 is y step	*
*****************************************
doresize:
	moveq	#0,d0		;source x
	moveq	#0,d1		;source y
	moveq	#0,d4		;dest x,y
	moveq	#0,d5
	move.w	generalvalue,d6
	lea	drawbltblk,a6
drsla:
	movem.l	d0-d7/a0/a1/a6,-(sp)	;first halve x
	move.w	#1,b_wd(a6)
	move.w	saveheight,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	d0,s_xmin(a6)
	move.w	d1,s_ymin(a6)
	move.l	a0,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)
	move.w	d4,d_xmin(a6)
	move.w	d5,d_ymin(a6)
	move.l	a1,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	movem.l	(sp)+,d0-d7/a0/a1/a6
	addq.w	#1,d4
	add.w	d2,d0		;xstep
	dbf	d6,drsla

	move.w	generalvalue,d0
	asr.w	#4,d0
	mulu	#320,d3
halveyloopa:
	move.w	d0,d5			;x from
	move.l	a0,a3
	move.l	a1,a2
halveyloopb:
	move.l	(a2)+,(a3)+
	move.l	(a2)+,(a3)+
	move.l	(a2)+,(a3)+
	move.l	(a2)+,(a3)+
	dbf	d5,halveyloopb
	add.l	d3,a1
	add.l	#320,a0
	dbf	d7,halveyloopa
	rts



flipx:
	cmp.l	#userbrush,currentbrush
	bne	flipxover
	move.w	#-1,mouseonoff
	bsr	waitvbl
	jsr	undrawbrush
	lea	savebrush,a1
	move.l	brushdat(a1),a2		;where to put it
	lea	userbrush,a0
	move.w	brushh(a0),d7		;y's
	move.l	brushdat(a0),a1		;where to get it
	move.w	brushw(a0),d6
	subq.w	#1,d6
	move.w	d6,d2
	moveq	#0,d0			;x,y
	moveq	#0,d1
flipxloopa:
	movem.l	d0/d1/d2/d6/d7/a1/a2,-(sp)
	lea	drawbltblk,a6
	move.w	#1,b_wd(a6)
	move.w	d7,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	color,fg_col(a6)
	move.w	currentbcolor,bg_col(a6)
	move.l	#$03030303,op_tab(a6)
	move.w	d0,s_xmin(a6)
	move.w	d1,s_ymin(a6)
	move.l	a1,s_form(a6)
	move.w	#16,s_nxwd(a6)
	move.w	#320,s_nxln(a6)
	move.w	#2,s_nxpl(a6)

	move.w	d2,d_xmin(a6)
	move.w	d1,d_ymin(a6)
	move.l	a2,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	movem.l	(sp)+,d0/d1/d2/d6/d7/a1/a2
	addq.w	#1,d0
	subq.w	#1,d2
	dbf	d6,flipxloopa

	lea	savebrush,a1
	move.l	brushdat(a1),a1		;where to put it
	lea	userbrush,a0
	move.w	brushh(a0),d0
	subq.w	#1,d0
	move.l	brushdat(a0),a0		;where to get it
	jsr	cudl
	bsr	makemask
	bsr	handlemosaic
	lea	userbrush,a0
	bsr	adjustoffsets
	move.w	#-1,oldmx
	move.w	#1,mouseonoff
flipxover:
anrts:
	rts

flipy:
	cmp.l	#userbrush,currentbrush
	bne	flipyover
	move.w	#-1,mouseonoff
	bsr	waitvbl
	jsr	undrawbrush
	lea	savebrush,a1
	move.l	brushdat(a1),a2		;where to put it
	lea	userbrush,a0
	move.w	brushh(a0),d7		;y's
	move.w	brushw(a0),d0
	subq.w	#1,d0
	move.w	#320,d1
	asr.w	#4,d0
	move.w	d0,d2
	addq.w	#1,d2
	asl.w	#4,d2
	sub.w	d2,d1
	
	subq.w	#1,d7
	moveq	#0,d6
	move.w	d7,d6
	mulu	#320,d6
	add.l	d6,a2
	move.l	brushdat(a0),a1		;where to get it
flipyloopa:
	move.w	d0,d5			;x from
	move.l	a2,a3
flipyloopb:
	move.l	(a1)+,(a3)+
	move.l	(a1)+,(a3)+
	move.l	(a1)+,(a3)+
	move.l	(a1)+,(a3)+
	dbf	d5,flipyloopb
	add.w	d1,a1
	sub.l	#320,a2
	dbf	d7,flipyloopa

	lea	savebrush,a1
	move.l	brushdat(a1),a1		;where to put it
	lea	userbrush,a0
	move.w	brushh(a0),d0
	subq.w	#1,d0
	move.l	brushdat(a0),a0		;where to get it
	jsr	cudl
	bsr	makemask
	bsr	handlemosaic
	lea	userbrush,a0
	bsr	adjustoffsets
	move.w	#-1,oldmx
	move.w	#1,mouseonoff
flipyover:
	rts

copypal:
	rept	256/2
	move.l	(a0)+,(a1)+
	endr
	rts
preparecycle2:
	move.w	currentbcolor,d0	;for cycle mode
	move.w	currentfcolor,d1	;for cycle mode
	cmp.w	d0,d1
	bge	ppcok2
	exg	d0,d1
ppcok2:
	move.w	d0,cyclelow
	move.w	d1,cyclehigh
	sub.w	d0,d1
	move.w	d1,cyclenumber
	rts


cycleflag:	dc.w	-1

*********************************************************
*	Return everything to how we found it and	*
*	exit to the desktop.				*
*********************************************************
returntogem:
	bsr	removehbl
*	restore hbl, vbl here	*

	move.l	save502,$502

	move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	oldmouse,16(a0)

	move.l	#savepal,-(sp)
	move.w	#256,-(sp)
	move.w	#0,-(sp)
	move.w	#84,-(sp)
	trap	#14
	add.l	#10,sp

	move.w	shiftsave,-(sp)
	move.l	screenad,-(sp)
	move.l	screenad,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp

	bsr	gouser
	clr.w	-(sp)
	trap	#1

putpal:
	move.l	d0,-(sp)
	move.w	#256,-(sp)
	move.w	#0,-(sp)
	move.w	#84,-(sp)
	trap	#14
	add.l	#10,sp
	rts

*********************************************************
*	For all gem operations we must flip back	*
*	to the original screen res and address so	*
*	Gem does it's stuff right.			*
*							*
*	begingem will set res's and ad's to original	*
*	for gem calls. endgem will set everything back	*
*	to how it should be for my program.		*
*********************************************************
begingem:
	bsr	removehbl
	jsr	undrawbrush
	lea	drawspritesave,a0
	move.l	#0,(a0)+
	move.l	#0,(a0)+
	move.w	#0,(a0)+
	move.l	#savepal,-(sp)
	move.w	#256,-(sp)
	move.w	#0,-(sp)
	move.w	#84,-(sp)
	trap	#14
	add.l	#10,sp
	move.w	#-1,-(sp)
	move.l	screenad,-(sp)
	move.l	screenad,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp

	move.w	shiftsave,-(sp)
	move.l	#-1,-(sp)
	move.l	#-1,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp
	move.w  #34,-(sp)	;reinstall mouse int
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	oldmouse,16(a0)
	dc.w	$a009
	rts
endgem:
	dc.w	$a00a

	move.w	#7,-(sp)	;should be the 256 mode
	move.l	#-1,-(sp)
	move.l	#-1,-(sp)	;do in 2 steps so os doesn't clear
	move.w	#5,-(sp)	;screen
	trap	#14
	add.l	#12,sp

	move.w	#-1,-(sp)	;should be the 256 mode
	move.l	currentscreen,-(sp)
	move.l	currentscreen,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp
	jsr	putrout
	lea	savebrush,a0		;no nasty brush stuff
	move.w	#-1,brushsaved(a0)	;signal we are getting rid of save
	cmpi.w	#panel1,currentpanel
	bne	rp2
	jsr	setuppanel1
	bra	rp3
rp2:
	cmpi.w	#panel2,currentpanel
	bne	rp3
	jsr	setuppanel2
rp3:
	bsr	setuphbl
	bsr	setpalette
	rts

setpalette:
	lea	$ff8400,a1
	move.l	currentpalette,a0
	rept	256/2
	move.l	(a0)+,(a1)+
	endr
	rts


*********************************************************
*	savefile					*
*							*
*********************************************************
savepicture:
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	lea	p3save,a0
	bsr	putbuttonup
	bsr	begingem
	jsr	gouser
trysaveagain:
*	move.l	#brushsave,picplace
	move.w	filetypesave,d0
	lea	typelist,a0
	move.l	(a0,d0.w),d0
	move.l	#savepictitle,-(sp)
	move.l	d0,-(sp)
	jsr	_fileselector
	addq.l	#8,sp
	tst.l	d0
	ble	spcancelled
	bsr	constructfile		;go off & make file
spcancelled:
	jsr	gosup
	bsr	endgem
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dofileloop

*********************************************************
*	This constructs and saves the picture		*
*	according to the file type.			*
*********************************************************
constructfile:
	cmpi.w	#ftypeneo,filetypesave
	beq.s	saveneo
	rts


saveneo:
	move.w	currentbcolor,d0
	move.b	d0,ramplow
	move.w	currentfcolor,d0
	move.b	d0,ramphi
	jsr	_openfile
	tst.w	fhandle
	bmi	ferrorsave
	pea	neohead
	move.l	#128,-(sp)
	jsr	_writetofile
	addq.l	#8,sp
	tst.l	d0
	bmi	closeandexit
	move.l	currentpalette,-(sp)
	move.l	#512,-(sp)
	jsr	_writetofile
	addq.l	#8,sp
	tst.l	d0
	bmi	closeandexit
	move.l	currentscreen,-(sp)
	move.l	#153600,-(sp)
	jsr	_writetofile
	addq.l	#8,sp
closeandexit:
	jsr	_closefile
ferrorsave:
	rts

neohead:
	dc.w	0
	dc.w	7	;resolution
	ds.w	16	;palette
	dc.b	'movingpixels'
	ds.b	76
ramplow:
	dc.b	0
ramphi:
	dc.b	0	;left and right ramp delimiters
	dc.w	0	;attribs

*********************************************************
*	loadfile					*
*							*
*	Brings up the Fileselector with the current	*
*	file type as the search pattern. Gets the file	*
*	and loads it, calls handlepix and returns.	*
*							*
*	To add-						*
*	Check to see if current pic has been modified	*
*	and if so ask if they want to save it first.	*
*	If it's an ST low res pic ask if they wanna	*
*	expand the pic vetically to 400 lines		*
*********************************************************
savesp2:	dc.l	0
loadpicture:
	move.w	#-1,mouseonoff		;turn the mouse off
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	bsr	waitvbl
	lea	p3load,a0
	bsr	putbuttonup
	bsr	begingem
	jsr	gouser
trypicagain:
	move.l	#brushsave,picplace
	move.w	filetype,d0
	lea	typelist,a0
	move.l	(a0,d0.w),d0
	move.l	#loadpictitle,-(sp)
	move.l	d0,-(sp)
	jsr	_fileselector
	addq.l	#8,sp
	tst.l	d0
	ble	lpcancelled
	pea	pichold
	move.l	#153600+10000,-(sp)
	jsr	_loadfile	;load the pic into hold area
	addq.l	#8,sp
	pea	pichold
	jsr	_hndlpix
	addq.l	#4,sp
	tst.l	d0
	bgt	handledata	;all was ok
	cmpi.b	#-1,picres	;was it really stuffed
	bne.s	perhapswrongres
	lea	notknownalert,a0	;do alert saying we don't handle
	jsr	_doalert	;TTMED res files
	cmpi.w	#1,_intout
	beq	trypicagain
	bra	lpcancelled
perhapswrongres:
	lea	picwrongresalert,a0
	jsr	_doalert
	cmpi.w	#1,_intout
	beq	trypicagain
lpcancelled:
	jsr	gosup
	bsr	endgem
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dofileloop

handledata:
	cmpi.b	#7,picres
	beq.s	justcopyit		;can only be ST Low
	lea	doublealert,a0	;do alert saying we don't handle
	jsr	_doalert	;TTMED res files
	cmpi.w	#1,_intout
	bne	nodouble

	move.w	#1,d0		;y times for each line
	move.w	#199,d1		;y lines
	lea	brushsave,a0
	move.l	currentscreen,a1
	moveq	#0,d3
	bsr	handledoubling
	moveq	#79,d7
	moveq	#0,d0
	move.l	a1,a0
	bsr	ccl
	bra	do16colors
nodouble:
	move.w	#0,d0		;y times for each line
	move.w	#199,d1		;y lines
	lea	brushsave,a0
	move.l	currentscreen,a1
	moveq	#0,d3
	bsr	handledoubling
	move	#480-200,d7
	moveq	#0,d0
	move.l	a1,a0
	bsr	ccl
	bra	do16colors
justcopyit:
	move.w	#479,d0
	lea	brushsave,a1
	move.l	currentscreen,a0
	jsr	cudl
do16colors:
	move.w	#-1,generalvalue
	lea	usethispalalert,a0	;do alert saying we don't handle
	jsr	_doalert	;TTMED res files
	cmpi.w	#1,_intout
	beq	noremap
	move.w	#1,generalvalue
	bra	alldonelp
noremap:
	cmpi.b	#7,picres
	beq	normpalette
	moveq	#15,d0
	bsr	copypalette
	bsr	dodialogue
	bsr	makecolortable
	bra	alldonelp
normpalette:
	move.w	#255,d0
	bsr	copypalette
	bsr	dodialogue
	bsr	makecolortable
alldonelp:
	jsr	gosup
	bsr	endgem
	tst.w	generalvalue
	bmi	nospecpalshit
	bsr	remappic
nospecpalshit:
	move.w	#1,mouseonoff		;turn the mouse off
	bra	dofileloop


get256best:
	lea	temppalette,a0
	move.l	currentpalette,a1
	lea	fitpalette,a2		;put em here
	moveq	#0,d0
	move.w	#255,d7
get256loop:
	move.w	d0,d1
	add.w	d1,d1
	move.w	(a0,d1.w),rgb
	jsr	findnearest
	move.w	color,(a2)+
	addq.w	#1,d0
	dbf	d7,get256loop
	rts

remappic:
	move.w	writemode,-(sp)
	move.w	todraw,-(sp)
*	bra	test
	move.w	#usepixel,todraw
	move.w	#wmreplace,writemode
	move.w	#479,d7
	move.l	currentscreen,a5
	lea	colortable,a2
	lea	temppalette,a1
	moveq	#0,d1		;y
rmploop1:
	moveq	#0,d0		;x
	move.w	#319,d6
rmploop2:
	move.l	a5,a6
	bsr	getpoint3
	move.w	color,d2
	add.w	d2,d2
	move.w	(a1,d2.w),d2
	add.w	d2,d2
	move.w	(a2,d2.w),color
	bsr	putpoint3
	addq.w	#1,d0
	dbf	d6,rmploop2
	addq.w	#1,d1
	dbf	d7,rmploop1
test:
	move.w	(sp)+,todraw
	move.w	(sp)+,writemode
	rts




handledoubling:
	move.w	d0,d2
	move.l	a0,a2
hdloop:
	rept	20
	move.w	(a2)+,(a1)+
	move.w	(a2)+,(a1)+
	move.w	(a2)+,(a1)+
	move.w	(a2)+,(a1)+
	move.l	d3,(a1)+
	move.l	d3,(a1)+
	endr
	move.l	a0,a2
	dbf	d2,hdloop
	lea	160(a0),a0
	dbf	d1,handledoubling
	rts



copypalette:
	lea	temppalette,a0
	move.l	currentpalette,a1
cpllp:
	move.w	(a0)+,(a1)+
	dbf	d0,cpllp
	rts


typelist:	dc.l	pnt,neo,pi1,pc1,iff
pnt:	dc.b	'PNT',0
neo:	dc.b	'NEO',0
pi1:	dc.b	'PI1',0
pc1:	dc.b	'PC1',0
iff:	dc.b	'IFF',0
loadpictitle:
	dc.b	'Select picture to load',0
savepictitle:
	dc.b	'Save Picture',0
	even
*****************************************
*	number in d0.l			*
*	d1.l is base			*
*	d2,x cursor			*
*	d3,y cursor			*
*****************************************
prtnum:
	movem.l	d0/d1,-(sp)
	lea	posstring,a0
	add.w	#32,d3
	move.b	d3,2(a0)	
	add.w	#32,d2
	move.b	d2,3(a0)
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	movem.l	(sp)+,d0/d1

	lea	linebuffer,a0
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
loop1:
	divu	d1,d0
	swap	d0
	addi	#'0',d0
	cmpi.w	#'9',d0
	ble.s	ok
	addq.w	#7,d0
ok:
	move.b	d0,-(a0)
	clr	d0
	swap	d0
	bne	loop1
loop2:
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	rts

clearnums:
	lea	clearstring,a0
	add.w	#32,d3
	move.b	d3,2(a0)	
*	move.b	d3,10(a0)	
	add.w	#32,d2
	move.b	d2,3(a0)
*	move.b	d2,11(a0)	
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	rts
linebuffer:
	ds.l	5
digits:
	dc.b	0
posstring:	dc.b	27,'Y',0,0,0,0
clearstring:	dc.b	27,'Y',0,0,'   ',0,0
	even
*****************************************************************
*	setuphbl						*
*	Set up hbl for sliding panel				*
*								*
*								*
*	call removehbl to get rid of it, these two should	*
*	be called before and after gem involved calls		*
*****************************************************************
setuphbl:
	move.w	sr,srsave
	bsr	puthbl
	rts
removehbl:
	bsr	restoremfp
	rts

puthbl:
	move.w	#$2700,sr
	move.b	ieb,oldieb
	move.b	iea,oldiea
	move.b	ima,oldima
	move.l	key_vec,oldkeyvec
	move.l	key_vec,keyt+2
	move.l	#key_handler,key_vec		;put new key stuff in

	move.b	#%01000000,ieb
	and.b	#$fe,iea	;stop hblank
	move.l	hbl_vec,oldhblvec
	move.l	#hblhandler,hbl_vec
	move.b	#%00000001,iea
	or.b	#1,ima

	move.l	vbl_vec,vblt+2		;set up vbl handler
	move.l	vbl_vec,oldvblvec
	move.l	#vbl_handler,vbl_vec
	move.b	#0,tbcr
	move.b	#200,tbdr
	move.b	#8,tbcr
	move.w	#$2300,sr
	rts


*hblcolors:	equ	0	;followed by 16 words of palette
*hbladdress:	equ	1	;followed by 1 long of address
*hblwaitlines:	equ	2	;followed by 1 word for lines to next hbl
*hblend:	equ	3	;denotes end of this hbl
*hblover:	equ	4	;denotes end of hbl's for this frame
hblhandler:
	move.w	#$2700,sr	;lock out ints
	movem.l	d0-d1/a0-a2,-(sp)
	move.l	displaylistwork,a0	;get the current dlist
parsedlist:
	cmpi.w	#hblcolors,(a0)
	beq	dohblcolors
	cmpi.w	#hblcolors2,(a0)
	beq	dohblcolors2
	cmpi.w	#hblwaitlines,(a0)
	beq	dohblwaitlines
	cmpi.w	#hbladdress,(a0)
	beq	dohbladdress
	cmpi.w	#hblend,(a0)
	beq	dohblend
	cmpi.w	#hblover,(a0)
	beq	dohblover
dohblover:
	move.b	#0,tbcr
	move.b	#200,tbdr
	move.b	#8,tbcr
	move.l	#extrahbl,hbl_vec
	move.b	#255,extrahblcount
	bclr	#0,isra
	movem.l	(sp)+,d0-d1/a0-a2
	move.w	#$2300,sr
	rte
dohblend:
	addq.l	#2,a0
	move.l	a0,displaylistwork
	bclr	#0,isra
	movem.l	(sp)+,d0-d1/a0-a2
	move.w	#$2300,sr
	rte
dohblwaitlines:
	addq.l	#2,a0
	moveq	#0,d0
	move.w	(a0)+,d0
	move.b	#0,tbcr
	move.b	d0,tbdr
	move.b	#8,tbcr
	bra	parsedlist
dohblcolors:
	addq.l	#2,a0
	lea	$ff8402,a1
	rept	15
	move.w	(a0)+,(a1)+
	endr
	bra	parsedlist
dohblcolors2:
	addq.l	#2,a0
	move.l	currentpalette,a2
	addq.l	#2,a2
	lea	$ff8402,a1
	rept	15
	move.w	(a2)+,(a1)+
	endr
	bra	parsedlist
dohbladdress:
*	move.w	#%0000011100000000,$ff8262
	addq.l	#2,a0
	move.l	(a0)+,tempadd

	move.b	#0,tbcr
	move.b	#10,tbdr
	move.b	#8,tbcr
	MOVE.B	$FFFFFA21.W,D0
LINEND:
	CMP.B	$FFFFFA21.W,D0
	BEQ	LINEND			; WAIT FOR END OF LINE

	move.b	tempadd+1,$ff8205
	move.b	tempadd+2,$ff8207
	move.b	tempadd+3,$ff8209
	bra	parsedlist

dohblresync:
	addq.l	#2,a0
	lea	tbdr,a1
	move.b	#0,tbcr
	move.b	#200,(a1)
	move.b	#8,tbcr
	rept	4
	nop
	endr
	move.b	(a1),d0
waitr:
	cmp.b	(a1),d0
	beq	waitr
	bra	parsedlist

*********************************************************
*	This little guy takes care of hbl's further	*
*	down the screen than 255.			*
*							*
*	VBL sets extrahblcount.				*
*********************************************************
extrahbl:
	move.w	#$2700,sr
	move.l	#hblhandler,hbl_vec
	move.b	#0,tbcr
	move.b	extrahblcount,tbdr
	move.b	#8,tbcr
	bclr	#0,isra
	move.w	#$2300,sr
	rte
dummyhbl:
	move.w	#$2700,sr
	move.b	#0,tbcr
	move.b	#255,tbdr
	move.b	#8,tbcr
	bclr	#0,isra
	move.w	#$2300,sr
	rte


key_handler:
	move.w	#$2500,sr
keyt:	jmp	$0.L


restoremfp:
	move.w	#$2700,sr
	move.b	oldieb,ieb
	move.b	oldiea,iea
	move.b	oldima,ima
	move.l	oldkeyvec,key_vec
	move.l	oldhblvec,hbl_vec
	move.l	oldvblvec,vbl_vec
	move.w	srsave,sr
	rts

nomag:
	rts
maglist:	dc.w	2,88/2-1,320/2-1	;*2
		dc.w	4,88/4-1,320/4-1	;*2
		dc.w	8,88/8-1,320/4-1	;*2
		dc.w	16,88/16-1,320/8-1	;*2


currentmag:	dc.w	2	;current magnification factor
currentmh:	dc.w	88/2-1	;the number of magnified lines to do
currentmw:	dc.w	320/2-1	;320/mag = number pixels horiz to mag
mousemove:	dc.w	-1
magtrack:	dc.w	-1

domag:
	rts
	cmpi.w	#1,paletteshowing
	bne.s	nomag
	tst.w	mousemove
	bmi.s	nomag
dodomag:
	move.w	mx,d0
	move.w	my,d1
	move.w	currentmw,d2
	asr.w	#1,d2		;halve it
	sub.w	d2,d0
	bpl.s	mlisok
	moveq	#0,d0
mlisok:				;left of mouse is ok
	move.w	d0,d3
	add.w	d2,d3
	add.w	d2,d3
	cmpi.w	#320,d3		;is it too far right
	blt.s	mrisok
	move.w	#319,d0
	sub.w	currentmw,d0
mrisok:
	move.w	currentmh,d2
	asr.w	#1,d2		;halve it
	sub.w	d2,d1
	bpl.s	muisok
	moveq	#0,d1
muisok:				;left of mouse is ok
	move.w	d1,d3
	add.w	d2,d3
	add.w	d2,d3
	cmpi.w	#480,d3		;is it too far right
	blt.s	mdisok
	move.w	#479,d1
	sub.w	currentmh,d1
mdisok:
	move.l	currentscreen,a1
	mulu	#320,d1
	add.l	d1,a1
	and.w	#$fff0,d0
	add.w	d0,a1
	move.l	maghiden,a0

	move.l	a0,a2
	moveq	#87,d7
	moveq	#0,d0
clearmag:
	rept	20
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	endr
	dbf	d7,clearmag
	
*****************************************************************
*	generic routine for 2 4 and 8				*
*	Expands from a1 to a0.					*
*****************************************************************
expand:
	move.w	currentmag,d4		;bf width for set = magnification
	move.w	currentmh,d6		;precalc'd height
	moveq	#0,d5			;offset destination
	move.w	#15,a2			;counter for 15 shifts
	move.w	currentmw,a3		;precalc'd width in pixels






	movem.l	d4-d6/a0-a3,-(sp)
	move.l	a0,a4
	move.l	a1,a5
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
magloop1:
	move.w	a3,d7		;number of bits to test=width of source
magloop2:
p0test:
	lsl.w	#1,d0
	bcc.s	p0clea			;bit was 0 so go clear in dest
	dc.w	%1110111011010000	;bfset	a0{d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
p0clea:
	lsl.w	#1,d1
	bcc.s	p1clea			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	2(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	2
p1clea:
	lsl.w	#1,d2
	bcc.s	p2clear			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	4(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	4
p2clear:
	lsl.w	#1,d3
	bcc.s	p3clear			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	6(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	6
p3clear:
	sub.w	#1,a2		;count shifts
	cmp.w	#-1,a2
	bne.s	offok
	move.w	#15,a2
	addq.l	#8,a1
*	lea	16(a1),a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
offok:
	add.w	d4,d5		;advance destination by magnification
	andi.w	#15,d5
	bne	offok2
	lea	16(a0),a0
offok2:
	dbf	d7,magloop2
	lea	320(a5),a5
	bsr	expanddown
	move.l	a0,a4
	move.l	a5,a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
	dbf	d6,magloop1

	movem.l	(sp)+,d4-d6/a0-a3
	addq.l	#8,a0
	addq.l	#8,a1
	move.l	a0,a4
	move.l	a1,a5
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3

magloop1b:
	move.w	a3,d7		;number of bits to test=width of source
magloop2b:
p0testb:
	lsl.w	#1,d0
	bcc.s	p0clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011010000	;bfset	a0{d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
p0clearb:
	lsl.w	#1,d1
	bcc.s	p1clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	2(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	2
p1clearb:
	lsl.w	#1,d2
	bcc.s	p2clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	4(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	4
p2clearb:
	lsl.w	#1,d3
	bcc.s	p3clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	6(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	6
p3clearb:
	sub.w	#1,a2		;count shifts
	cmp.w	#-1,a2
	bne.s	offokb
	move.w	#15,a2
	addq.l	#8,a1
*	lea	16(a1),a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
offokb:
	add.w	d4,d5		;advance destination by magnification
	andi.w	#15,d5
	bne	offok2b
	lea	16(a0),a0
offok2b:
	dbf	d7,magloop2b
	lea	320(a5),a5
	bsr	expanddown
	move.l	a0,a4
	move.l	a5,a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
	dbf	d6,magloop1b
	move.l	magseen,d0
	move.l	maghiden,magseen
	move.l	d0,maghiden
	rts

expanddown:
	move.w	d4,d0		;get mag
	subq.w	#2,d0		;-1 for dbf and -1 for line already done
	lea	320(a4),a0	;we get data from a4 and stick it to a0
	move.l	a4,a1
edl1:
	rept	20
	move.l	(a1)+,(a0)+	;one whole line
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	endr
	move.l	a4,a1
	subq.w	#1,d0
	bpl	edl1
	rts

vbl_handler:
	movem.l	d0-d7/a0-a6,-(sp)
	move.l	#hblhandler,hbl_vec
	move.b	currentscreen+1,$ff8205
	move.b	currentscreen+2,$ff8207
	move.b	currentscreen+3,$ff8209
paneldown:
*	move.l	panelad,tempadd
*	add.l	#320*80,tempadd
*	move.l	#fillplane,tempadd
*	move.b	tempadd+1,$ff8205
*	move.b	tempadd+2,$ff8207
*	move.b	tempadd+3,$ff8209


	bsr	docycle

	moveq	#0,d0
	move.w	panely,d0		;panel's y location
	move.l	d0,d1
	cmpi.w	#255,d0
	ble.s	pyok
	sub.w	#255,d0
	move.b	d0,extrahblcount
	move.l	#extrahbl,hbl_vec
	move.w	#255,d0
pyok:
	move.b	#0,tbcr
	move.b	d0,tbdr
	move.b	#8,tbcr

	tst.w	panelshowing
	bpl	panelup
	move.l	#dummyhbl,hbl_vec
panelup:
	move.l	currentscreen,a0	;calc where the draw screen
	mulu	#320,d1			;address to start redisplay
	add.l	d1,a0			;is, NOTE should change depending
	move.w	panelheight,d1
	mulu	#320,d1
	add.l	d1,a0		;on if pal visible
	move.l	redisplayadjust,a1
	move.l	a0,(a1)
	move.l	displaylist,displaylistwork
	move.l	magseen,magseenh
	bsr	domouse
	bsr	docoords
	move.w	#-1,vblflag
	movem.l	(sp)+,d0-d7/a0-a6
vblt:
	jmp	$0.L

docycle:
	tst.w	cycleflag
	bmi	nocycle
	move.w	cyclelow,d0
	add.w	d0,d0
	move.l	currentpalette,a0
	move.l	a0,a1
	lea	$ff8400,a2
	move.w	cyclehigh,d1
	add.w	d1,d1
	add.w	d1,a1
	add.w	d0,a0
	add.w	d1,a2
	move.w	cyclenumber,d7
	move.w	(a1),d6		;save highest color to put in lowest
dcycloop:
	move.w	-2(a1),(a2)
	move.w	-2(a1),(a1)
	subq.l	#2,a2
	subq.l	#2,a1
	dbf	d7,dcycloop
	move.w	d6,(a0)
	addq.l	#2,a2
	move.w	d6,(a2)
	rts
nocycle:
	lea	$ff8400,a0
	move.l	currentpalette,a1
	rept	8
	move.l	(a1)+,(a0)+
	endr
	rts

domouse:
	lea	drawspritesave,a2
	move.l	lineaundrawsprite,a5
	jsr	(a5)
	lea	panelspritesave,a2
	move.l	lineaundrawsprite,a5
	jsr	(a5)
	tst.w	mouseonoff		;should the mouse be visible
	bmi	nomouse
	move.w	panely,d6
	move.w	mx,d0
	move.w	my,d1
	cmp.w	d1,d6
	bgt	dodrawsprite
	tst.w	panelshowing
	bmi	dodrawsprite
	add.w	panelheight,d6
	cmp.w	d1,d6
	blt	dodrawsprite
dopanelsprite:
	move.w	#1,onpanel
	move.l	$44e,-(sp)
	move.l	panelad,$44e
	sub.w	panely,d1
	lea	panelspritesave,a2
	move.l	currentpanelsprite,a0
	move.l	lineadrawsprite,a5
	jsr	(a5)
	move.l	(sp)+,$44e
	rts
dodrawsprite:
	move.w	#-1,onpanel
	tst.w	doinloop
	bpl	nomouse
*	lea	drawsprite,a0
	move.l	currentdrawsprite,a0
	lea	drawspritesave,a2
	move.l	lineadrawsprite,a5
	jsr	(a5)
nomouse:
	rts

docoords:
	tst.w	onpanel
	bmi.s	dopc1
	rts
dopc1:
	move.w	mx,d0
	cmp.w	oldcoordx,d0
	bne	dopc
	move.w	my,d0
	cmp.w	oldcoordy,d0
	bne	dopc
	move.w	#-1,mousemove
	rts
dopc:
	move.w	mx,oldcoordx
	move.w	my,oldcoordy
	move.w	#1,mousemove
	move.l	$44e,-(sp)
	move.l	panelad,$44e

	lea	dummybltblk,a6
	move.w	#52,b_wd(a6)
	move.w	#11,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	#12,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	#$04040707,op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	#fillpat1,s_form(a6)
	move.w	#0,s_nxwd(a6)
	move.w	#0,s_nxln(a6)
	move.w	#0,s_nxpl(a6)
	move.w	#266,d_xmin(a6)
	move.w	#40,d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)

	move.w	mx,x2print
	move.w	my,y2print
	tst.w	mouseonoff
	bpl.s	normalcoords
	move.w	initialx,d0
	sub.w	mx,d0
	bpl.s	nonegx
	neg.w	d0
nonegx:
	move.w	d0,x2print
	move.w	initialy,d0
	sub.w	my,d0
	bpl.s	nonegy
	neg.w	d0
nonegy:
	move.w	d0,y2print

normalcoords:
	moveq	#0,d0
	move.w	x2print,d0
	moveq	#10,d1
	bsr	converttoassci2
	move.l	lineadrawsprite,a5
	lea	string,a3
	lea	numbersprites,a4
	lea	dummysave,a2
	move.w	#272,d0
	moveq	#40,d1
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5
	addq.w	#6,d0
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5
	addq.w	#6,d0
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5

	moveq	#0,d0
	move.w	y2print,d0
	moveq	#10,d1
	bsr	converttoassci2
	move.l	lineadrawsprite,a5
	lea	string,a3
	lea	numbersprites,a4
	move.w	#294,d0
	moveq	#40,d1
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	lea	dummysave,a2
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5
	addq.w	#6,d0
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5
	addq.w	#6,d0
	moveq	#0,d2
	move.b	(a3)+,d2
	asl.w	#2,d2
	move.l	(a4,d2.w),a0
	movem.l	d0-d1/a2-a5,-(sp)
	jsr	(a5)
	movem.l	(sp)+,d0-d1/a2-a5
noys:
	move.l	(sp)+,$44e
	rts

numbersprites:
	dc.l	sprite0,sprite1,sprite2,sprite3,sprite4,sprite5,sprite6
	dc.l	sprite7,sprite8,sprite9

onpanel:	dc.w	0
mouseonoff:	dc.w	1
dummysave:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)

spotspritesave:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
spotsprite:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,256,256,0,0,256,256,0
	dc.w	0,256,256,0,0,256,21588
	dc.w	43690,0,256,256,0,0,256
	dc.w	256,0,0,256,256,0,0,256,0,0


drawspritesave:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
drawsprite:
	dc.w	7,7	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,256,256,0,0,256,256,0
	dc.w	0,256,256,0,0,256,21588
	dc.w	43690,0,256,256,0,0,256
	dc.w	256,0,0,256,256,0,0,256,0,0
fillsprite:
	dc.w	1,15	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
*	dc.w	0,0,0,0,640,1728,1760,3440,3192
*	dc.w	6136,3064,1520,736,320,0,0,7168
*	dc.w	8704,8960,9088,9536,10528,14608
*	dc.w	29320,62340,59396,54276,51720,50448
*	dc.w	49824,16832,16384

	dc.w	0,7168,0,8704,0,8960,0,9088,640,9536,1728,10528
	dc.w	1760,14608,3440,29320,3192,62340,6136,59396,3064
	dc.w	54276,1520,51720,736,50448,320,49824,0,16832,0,16384


panelspritesave:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
panelsprite:
	dc.w	1,1	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	255	;foreground color
arrow:
	dc.w	49152,0,40960,16384,36864,24576,34816,28672
	dc.w	33792,30720,33280,31744,33024,32256,32896
	dc.w	32512,32832,32640,33728,31744,37376,27648,47360
	dc.w	17920,51456,1536,1152,768,1152,768,896,0

tosprite:
	dc.w	1,1	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	255	;foreground color
	dc.w	49152,0,40960,16384,36864,24576,34816,28672
	dc.w	33792,30720,33280,31744,40448,24576,49144,16384
	dc.w	49220,16312,31674,1092,2885,1154,2885,1154
	dc.w	2885,1154,2746,1092,2628,1080,3640,0
sprite0:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,34816,0,34816,0,34816,0,34816
	dc.w	0,0,0,34816,0,34816,0,34816,0,34816,0
	dc.w	28672,0,0,0,0,0,0,0,0,0,0
sprite1:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,8192,0,8192,0,8192,0,8192,0,8192
	dc.w	0,0,0,8192,0,8192,0,8192,0,8192,0,8192
	dc.w	0,0,0,0,0,0,0,0,0,0
sprite2:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,2048,0,2048,0,2048,0,2048,0
	dc.w	28672,0,32768,0,32768,0,32768,0,32768,0
	dc.w	28672,0,0,0,0,0,0,0,0,0,0
sprite3:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,2048,0,2048,0,2048,0,2048,0
	dc.w	28672,0,2048,0,2048,0,2048,0,2048,0,28672
	dc.w	0,0,0,0,0,0,0,0,0,0
sprite4:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,0,0,34816,0,34816,0,34816,0,34816,0
	dc.w	28672,0,2048,0,2048,0,2048,0,2048,0,0
	dc.w	0,0,0,0,0,0,0,0,0,0
sprite5:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,32768,0,32768,0,32768,0,32768
	dc.w	0,28672,0,2048,0,2048,0,2048,0,2048,0,28672
	dc.w	0,0,0,0,0,0,0,0,0,0
sprite6:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,32768,0,32768,0,32768,0,32768
	dc.w	0,28672,0,34816,0,34816,0,34816,0,34816
	dc.w	0,28672,0,0,0,0,0,0,0,0,0,0
sprite7:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,2048,0,2048,0,2048,0,2048,0,0,0
	dc.w	2048,0,2048,0,2048,0,2048,0,0,0,0,0,0,0,0,0,0,0,0
sprite8:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,34816,0,34816,0,34816,0,34816,0
	dc.w	28672,0,34816,0,34816,0,34816,0,34816,0,28672
	dc.w	0,0,0,0,0,0,0,0,0,0
sprite9:
	dc.w	0,0	;x,y hotspot offset
	dc.w	1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,28672,0,34816,0,34816,0,34816,0,34816,0,28672
	dc.w	0,2048,0,2048,0,2048,0,2048,0,28672,0,0,0,0,0,0,0,0,0,0
c0save:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
c1save:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
c2save:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
c3save:
	ds.w	5	;misc shit
	ds.w	256	;(64*vplanes)
curvesp0:
	dc.w	5,5	;x,y hotspot offset
	dc.w	-1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,65412,0,32908,0,48772,0,41604,0,41604,0
	dc.w	41614,0,48768,0,32896,0,65408,0,0,0,0,0,0
	dc.w	0,0,0,0,0,0,0,0
curvesp1:
	dc.w	5,5	;x,y hotspot offset
	dc.w	-1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,65436,0,32930,0,48772,0,41624,0,41632
	dc.w	0,41662,0,48768,0,32896,0,65408,0,0,0,0
	dc.w	0,0,0,0,0,0,0,0,0,0
curvesp2:
	dc.w	5,5	;x,y hotspot offset
	dc.w	-1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,65436,0,32930,0,48770,0,41628,0,41602
	dc.w	0,41634,0,48796,0,32896,0,65408,0,0,0,0
	dc.w	0,0,0,0,0,0,0,0,0,0
curvesp3:
	dc.w	5,5	;x,y hotspot offset
	dc.w	-1	;format flag
	dc.w	0	;background color
	dc.w	15	;foreground color
	dc.w	0,65412,0,32908,0,48788,0,41662,0,41604
	dc.w	0,41604,0,48768,0,32896,0,65408,0,0,0,0
	dc.w	0,0,0,0,0,0,0,0,0,0







gosup:
	clr.l	-(sp)
	move.w	#$20,-(sp)
	trap	#1
	addq.l	#6,sp
	move.l	d0,savesp
	rts
gosup2:
	clr.l	-(sp)
	move.w	#$20,-(sp)
	trap	#1
	addq.l	#6,sp
	move.l	d0,savesp2
	rts
gouser:
	move.l	savesp,-(sp)
	move.w	#$20,-(sp)
	trap	#1
	addq.l	#6,sp
	rts
gouser2:
	move.l	sp,-(sp)
	move.w	#$20,-(sp)
	trap	#1
	addq.l	#6,sp
	rts

tempadd:	dc.l	0
savesp:		dc.l	0
panely:		dc.l	0
displaylist:		dc.l	0
displaylistwork:	dc.l	0
bottom:		dc.l	0
oldvblvec:	dc.l	0
oldkeyvec:	dc.l	0
oldhblvec:	dc.l	0
oldieb:		dc.b	0
oldiea:		dc.b	0
oldima:		dc.b	0
extrahblcount:	dc.b	0
srsave:		dc.w	0
*screenflag:	dc.w	0
panelheight:	dc.w	0
	even
swapscreens:
	move.w	#-1,mouseonoff
	bsr	waitvbl
	jsr	undrawbrush
	move.l	currentscreen,a0
	move.l	screen2,a1
	move.w	#479,d0
	jsr	cudl2
	jsr	copyforundo
	move.w	#-1,oldmx
	move.w	#-1,oldmy
	move.w	#1,mouseonoff
	rts

mydump:
	rts



initstuff:
	move.l	$502,save502
	move.l	#mydump,$502
	dc.w	$a000
	move.l	d0,linea
	move.l	(a2)+,lineainit
	move.l	(a2)+,lineapixel
	move.l	(a2)+,lineagetpixel
	move.l	(a2)+,linealine
	move.l	(a2)+,lineahorizontalline
	move.l	(a2)+,linearectangle
	move.l	(a2)+,lineapolygon
	move.l	(a2)+,lineabitblt
	move.l	(a2)+,lineatextblt
	move.l	(a2)+,lineashowmouse
	move.l	(a2)+,lineahidemouse
	move.l	(a2)+,lineatransformmouse
	move.l	(a2)+,lineaundrawsprite
	move.l	(a2)+,lineadrawsprite
	move.l	(a2)+,lineacopyraster
	move.l	(a2)+,lineaseedfill

	move.l	$4ba,d0
	bsr	randomseed
	move.w	#2,-(sp)
	trap	#14
	addq.l	#2,sp
	move.l	d0,screenad

	move.w	#4,-(sp)	;get the shiftmode
	trap	#14
	addq.l	#2,sp
	move.w	d0,shiftsave

	move.l	#savepal,-(sp)
	move.w	#256,-(sp)
	move.w	#0,-(sp)
	move.w	#egetpalette,-(sp)
	trap	#14
	add.l	#10,sp

	bsr	dodialogue

	lea	palette1,a0
	move.l	#screens,d0
	addq.l	#8,d0
	and.w	#%1111111111111000,d0		;make sure of boundry
	move.l	d0,screen1
	add.l	#153600,d0
	move.l	d0,screen2
	add.l	#153600,d0
	move.l	d0,undoscreen
	move.l	#panel,d0
	addq.l	#8,d0
	and.w	#%1111111111111000,d0		;make sure of boundry
	move.l	d0,panelad
	lea	panel2a,a1
	move.l	panelad,a0
	move.w	#519,d0
	jsr	cudl
	move.l	panelad,d0
	add.l	#520*320,d0		;get to bottom of panel
	move.l	d0,magseen
	add.l	#88*320,d0
	move.l	d0,maghiden		;magnify seen and hidden areas

	move.l	#-1,oldpaintmode
	move.l	#palette1,currentpalette
	move.l	screen1,currentscreen

	lea	p4unitransp,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#isuni,transmode
	lea	cb15,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#15,centretrans
	lea	eb15,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#15,edgetrans

	lea	p4dithoff,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#0,currentdither

	lea	p1dots,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	lea	p1brush1,a0
	move.w	#1,buttonselected(a0)
	lea	brush1,a0
	bsr	adjustoffsets
	lea	p4sdmcolor,a0
	move.w	#1,buttonselected(a0)
	move.w	#colormode,brushmode
	move.w	#237+8,transpx
	move.w	#117,transpy

	lea	p7size3,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#20,sprayradius

	lea	p7rate3,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#1556,sprayrate

	lea	p6fillempty,a0
	move.w	#1,buttonselected(a0)
	move.w	#emptyfill,filltype

	lea	p4btrans,a0
	move.w	#1,buttonselected(a0)
	move.w	#1,bistrans

	move.l	#ttdots,drawroutine		;set draw routine for first time
	move.l	#brush1,currentbrush	;1 thru 8 are predefined brushes
	move.w	#255,currentfcolor
	move.w	#0,currentbcolor
	move.w	#1,drawcursorflag	;cursor on in paint area
	move.w	#drawing,paintmode
	move.w	#1,shouldfollow
	move.l	#panelsprite,currentpanelsprite
	move.l	#drawsprite,currentdrawsprite
	move.l	#-1,linepattern2
*	move.w	#5,sprayrate
*	move.w	#10,sprayradius
	move.b	#1,pictype
	move.w	#ftypeneo,filetype
	lea	p3setlneo,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	move.w	#ftypeneo,filetypesave
	lea	p3setsneo,a0			;setup defaults
	move.w	#1,buttonselected(a0)
	bsr	makecolortable
	bsr	setuppanel1
	jsr	endgem		;dual use routine sets hbl's vbl's
	rts

dodialogue:
	dc.w	$a00a
	move.l	screenad,a0
	add.l	#184*320,a0
	lea	112(a0),a0	;start ad of gem screen to put dialogue
	moveq	#112,d0		;113 lines high
	move.l	a0,a2
	lea	dialogue,a1
ddil:
	move.l	a2,a0
	rept	12
	move.w	(a1),(a0)+
	move.w	(a1),(a0)+
	move.w	(a1),(a0)+
	move.w	(a1)+,(a0)+
	endr
	lea	320(a2),a2
	dbf	d0,ddil
	dc.w	$a009
	rts


*****************************************************************
*	A Brilliant idea!!!					*
*	There can only be 4096 colors, so I will calculate	*
*	the equivalent in the existing palette, for each and	*
*	store it in a 4096 word array.				*
*****************************************************************
makecolortable:
	lea	colortable,a0
	move.w	#4095,d7		;counter
	moveq	#0,d0
mctloop:
	move.w	d0,rgb
	jsr	findnearest
	move.w	color,(a0)+
	addq.w	#1,d0
	dbf	d7,mctloop
	move.l	currentpalette,a0
	move.w	(a0),rgb
	jsr	findfurthest
	move.l	currentdrawsprite,a1
	move.w	color,8(a1)
	rts

constructpalette:
	move.w	#0,x1
	move.w	#63,y1
	move.w	#319,x2
	move.w	#154,y2
	move.w	#0,color
	move.l	panelad,a0
	jsr	filledbox

	lea	brushpalette,a6
	move.w	brushw(a6),brushw2(a6)
	move.w	brushh(a6),brushh2(a6)
	lea	colorbuttons,a5
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d6		;color counter
	moveq	#0,d4		;x value
	moveq	#69,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	moveq	#81,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	moveq	#93,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	moveq	#105,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	moveq	#117,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#39,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	move.w	#129,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	moveq	#15,d7		;number of boxes horizontally
	moveq	#0,d4		;x value
	move.w	#141,d5		;y value
	move.l	a6,a0
	bsr	dopalline
	rts

dopalline:
	move.w	d4,brushx(a0)
	move.w	d5,brushy(a0)
	move.w	d4,(a5)+	;save x and y for color check with mouse
	move.w	d5,(a5)+
	movem.l	d4-d7/a0/a5/a6,-(sp)
	bsr	blitpal
	movem.l	(sp)+,d4-d7/a0/a5/a6
	addq.w	#1,d6
	addq.w	#8,d4
	dbf	d7,dopalline
	rts

blitpal:
	lea	drawbltblk,a6
	move.w	#8,b_wd(a6)
	move.w	#8,b_ht(a6)
	move.w	#8,plane_ct(a6)
	move.w	d6,fg_col(a6)
	move.w	#0,bg_col(a6)
	move.l	brushlogic(a0),op_tab(a6)
	move.w	#0,s_xmin(a6)
	move.w	#0,s_ymin(a6)
	move.l	brushdat(a0),s_form(a6)
	move.w	brushnxwd(a0),s_nxwd(a6)
	move.w	brushnxln(a0),s_nxln(a6)
	move.w	brushnxpl(a0),s_nxpl(a6)
	move.w	brushx(a0),d_xmin(a6)
	move.w	brushy(a0),d_ymin(a6)
	move.l	panelad,d_form(a6)
	move.w	#16,d_nxwd(a6)
	move.w	#320,d_nxln(a6)
	move.w	#2,d_nxpl(a6)
	move.l	#0,p_addr(a6)
	move.l	lineabitblt,a5
	jsr	(a5)
	rts
setuppanel1:
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist1,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress1	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay1
	move.l	#redisplay1,redisplayadjust	; for vbl to alter
	move.w	#62,panelheight
	move.w	(sp)+,sr
	move.w	#-1,paletteshowing
	move.w	#panel1,currentpanel
	bsr	constructpanel
	bsr	adjustpanel
	rts
setuppanel1p:
	bsr	constructpalette
	bsr	setfcolor2
	bsr	setbcolor2
	bsr	setfcolorbar
	bsr	setbcolorbar
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist1p,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress2	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay2
	move.l	#redisplay2,redisplayadjust	; for vbl to alter
	move.w	#154,panelheight
	move.w	(sp)+,sr
	move.w	#panel1,currentpanel
*	move.w	#1,paletteshowing
	bsr	constructpanel
	bsr	adjustpanel
	rts
setuppanel1m:
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist1m,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress3	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay3
	move.l	#redisplay3,redisplayadjust	; for vbl to alter
	move.w	#154,panelheight
	move.l	magseen,magseenh
	move.w	(sp)+,sr
	move.w	#panel1,currentpanel
*	move.w	#1,paletteshowing
	bsr	constructpanel
	bsr	adjustpanel
	rts
setuppanel2:
	bsr	constructpalette
	bsr	setfcolor2
	bsr	setbcolor2
	bsr	setfcolorbar
	bsr	setbcolorbar
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist1p,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress2	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay2
	move.l	#redisplay2,redisplayadjust	; for vbl to alter
	move.w	#154,panelheight
	move.w	(sp)+,sr
	move.w	#panel2,currentpanel
	move.w	#1,paletteshowing
	bsr	constructpanel
	bsr	adjustpanel
	rts
setuppanel3:
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist1,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress1	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay1
	move.l	#redisplay1,redisplayadjust	; for vbl to alter
	move.w	#62,panelheight
	move.w	(sp)+,sr
	move.w	#-1,paletteshowing
	move.w	#panel3,currentpanel
	bsr	constructpanel
	bsr	adjustpanel
	rts
setuppanel4:
	move.w	sr,-(sp)
	move.w	#$2700,sr
	move.l	#hbllist4,displaylist
	move.l	panelad,a0
	move.l	a0,paneladdress4	;address for hblank
	move.l	currentscreen,a0		;calc where the draw screen
	move.l	a0,redisplay4
	move.l	#redisplay4,redisplayadjust	; for vbl to alter
	move.w	#160,panelheight
	move.w	(sp)+,sr
	move.w	#panel4,currentpanel
	bsr	adjustpanel
	rts


hblcolors:	equ	0	;followed by 16 words of palette
hbladdress:	equ	1	;followed by 1 long of address
hblwaitlines:	equ	2	;followed by 1 word for lines to next hbl
hblend:		equ	3	;denotes end of this hbl
hblover:	equ	4	;denotes end of hbl's for this frame
hblresync:	equ	5
hblcolors2:	equ	6
hbllist1:
		dc.w	hbladdress	;instruction
paneladdress1:	dc.l	0		;address of panel
		dc.w	hblcolors	;instruction
		dc.w	$0900,$0c00,$0080,$04F4,$007F,$0F76,$000F
		dc.w	$0c0F,$0321,$0543,$0654,$0876,$0ba9,$0Fed,$0FFF
		dc.w	hblwaitlines	;inst
		dc.w	59		;30 lines to bottom of panel
		dc.w	hblend
		dc.w	hbladdress	;instruction
redisplay1:	dc.l	0		;address of draw screen to begin displaying
		dc.w	hblcolors2	;instruction
		dc.w	hblover

hbllist1p:
		dc.w	hbladdress	;instruction
paneladdress2:	dc.l	0		;address of panel
		dc.w	hblcolors	;instruction
		dc.w	$0900,$0c00,$0080,$04F4,$007F,$0F76,$000F
		dc.w	$0c0F,$0321,$0543,$0654,$0876,$0ba9,$0Fed,$0FFF
		dc.w	hblwaitlines	;inst
		dc.w	64		;lines to bottom of panel + palette
		dc.w	hblend
		dc.w	hblcolors2	;instruction
		dc.w	hblwaitlines	;inst
		dc.w	86		;lines to bottom of panel + palette
*		dc.w	hbladdress	;instruction
*ppnew:		dc.l	panel+21760-320	;address of palette area on panel screen
*		dc.w	hblwaitlines	;inst
*		dc.w	85
		dc.w	hblend
		dc.w	hbladdress	;instruction
redisplay2:	dc.l	0		;address of draw screen to begin displaying

		dc.w	hblover

hbllist1m:
		dc.w	hbladdress	;instruction
paneladdress3:	dc.l	0		;address of panel
		dc.w	hblcolors	;instruction
		dc.w	$0900,$0c00,$0080,$04F4,$007F,$0F76,$000F
		dc.w	$0c0F,$0321,$0543,$0654,$0876,$0ba9,$0Fed,$0FFF
		dc.w	hblwaitlines	;inst
		dc.w	64		;lines to bottom of panel + palette
		dc.w	hblend
		dc.w	hblcolors2	;instruction
		dc.w	hbladdress
magseenh:	dc.l	0		;address of seen mag area
		dc.w	hblwaitlines	;inst
		dc.w	86		;lines to bottom of panel + palette
		dc.w	hblend
		dc.w	hbladdress	;instruction
redisplay3:	dc.l	0		;address of draw screen to begin displaying
		dc.w	hblover

magseen:	dc.l	0
maghiden:	dc.l	0

hbllist4:
		dc.w	hbladdress	;instruction
paneladdress4:	dc.l	0		;address of panel
		dc.w	hblcolors	;instruction
		dc.w	$0900,$0c00,$0080,$04F4,$007F,$0F76,$000F
		dc.w	$0c0F,$0321,$0543,$0654,$0876,$0ba9,$0Fed,$0FFF
		dc.w	hblwaitlines	;inst
		dc.w	156		;lines to bottom of panel + palette
		dc.w	hblend
		dc.w	hblcolors2	;instruction
		dc.w	hbladdress	;instruction
redisplay4:	dc.l	0		;address of draw screen to begin displaying

		dc.w	hblover


	include	'D:\mylibs\setgem2.s'
	include	'D:\mylibs\file.s'
	include	'pixmdule.s'
	even

*************puts my mouse interupt in***************** 
putrout:move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	16(a0),oldmouse
        move.l  #myrout,16(a0)
        rts
lastb:		dc.w	-1
**************my ubeut mouse interupt*******************
myrout:
	movem.l	d0-d2/a0-a1,-(sp)
	moveq	#0,d0	
	move.b	(a0),d0
	cmpi.b	#$fa,d0
	bne	myr1
	move.w	#1,buttondat
	bra	myrxy
myr1:
	cmpi.b	#$f9,d0
	bne	myr2
	move.w	#2,buttondat
	bra	myrxy
myr2:
	cmpi.b	#$fb,d0
	bne	myr3
	move.w	#3,buttondat
	bra	myrxy
myr3:
	move.w	#1,mousehasgoneup
	move.w	#0,buttondat
myrxy:
	moveq	#0,d0
	move.b	1(a0),d0
	ext.w	d0
	moveq	#0,d1
	move.b	2(a0),d1
	ext.w	d1
	add.w	d0,mx
	add.w	d1,my
*	sub.w	d1,my2
myr4:	move.w	mx,d0
	move.w	my,d1
	bsr	extra
	move.w	d0,mx
	move.w	d1,my
	movem.l	(sp)+,d0-d2/a0-a1
	rts

extra:  cmpi.w	#mminx,d0
        bge	extra1
        move.w	#mminx,d0
        bra	extra2
extra1: cmpi.w   #mmaxx,d0
        ble	extra2
        move.w  #mmaxx,d0
extra2:	cmpi.w  #mminy,d1
        bge	extra3
        move.w  #mminy,d1
        bra	extra4
extra3: cmpi.w   #mmaxy,d1
        ble	extra4
        move.w  #mmaxy,d1
extra4:
        rts

*****************************************
prtnuminternal: 
	movem.l	d0/d6/d7/a0/a5/a1/a2,-(sp)
        moveq  	#0,d7
        move.w  number,d7
        andi.l  #$ffff,d7
        lea	lineout,a5
	tst.w	d7
	bpl	dodec2
	neg.w	d7
dodec2:
	move.l  d7,d6
        divu    #10,d6
        move.w  d6,d7
        swap    d6
        move.b  d6,(a5)+
	subq.w	#1,d0
        tst.w  d7
        bne     dodec2
	lea	smallnumbers,a3
	move.l	numwhere,d6
out2:
	cmpa.l  #lineout,a5
        beq     decback2
	moveq	#0,d7
        move.b  -(a5),d7
	mulu	#28,d7
	move.l	a3,a0
	add.w	d7,a0
	moveq	#6,d0
	move.l	panelad,a2
	adda.l	d6,a2
oloop2:
	move.b	(a0)+,(a2)
	move.b	(a0)+,2(a2)
	move.b	(a0)+,4(a2)
	move.b	(a0)+,6(a2)
	lea	320(a2),a2
	dbf	d0,oloop2
	btst	#0,d6
	beq	oh2
	addq.l	#6,d6
oh2:	addq.l	#1,d6
	bra	out2
decback2:
	movem.l	(sp)+,d0/d6/d7/a0/a5/a1/a2
	rts

lineout:	ds.b	5
	even
RND:		ds.l	5
*******************************************************************
randomseed:     ADD.L   D0,D1           ;user seed in d0 (d1 too)
                MOVEM.L D0/D1,RND
; drops through to the main random function (not user callable)
LongRnd:        MOVEM.L D1-D3,-(SP)     
                MOVEM.L RND,D0/D1       ;D0=LSB's, D1=MSB's of random number
                ANDI.B  #$0E,D0         ;ensure upper 59 bits are an...
                ORI.B   #$20,D0         ;...odd binary number
                MOVE.L  D0,D2
                MOVE.L  D1,D3
                ADD.L   D2,D2           ;accounts for 1 of 17 left shifts
                ADDX.L  D3,D3           ;[D2/D3] = RND*2
                ADD.L   D2,D0
                ADDX.L  D3,D1           ;[D0/D1] = RND*3
                SWAP    D3              ;shift [D2/D3] additional 16 times
                SWAP    D2
                MOVE.W  D2,D3
                CLR.W   D2
                ADD.L   D2,D0           ;add to [D0/D1]
                ADDX.L  D3,D1
                MOVEM.L D0/D1,RND       ;save for next time through
                MOVE.L  D1,D0           ;most random part to D0
                MOVEM.L (SP)+,D1-D3
                RTS
random:         MOVE.w  D2,-(SP)
                MOVE.W  D0,D2           ;save upper limit
                BEQ.S   r1              ;range of 0 returns 0 always
                BSR.S   LongRnd         ;get a longword random number
                CLR.W   D0              ;use upper word (it's most random)
                SWAP    D0
                ext.l   d0
                DIVs    D2,D0           ;divide by range...
                CLR.W   D0              ;...and use remainder for the value
                SWAP    D0              ;result in D0.W
r1:             MOVE.w  (SP)+,D2
                RTS

randomplus:     MOVE.w  D2,-(SP)
                MOVE.W  D0,D2           ;save upper limit
                BEQ.S   r2              ;range of 0 returns 0 always
                BSR.S   LongRnd         ;get a longword random number
                CLR.W   D0              ;use upper word (it's most random)
                SWAP    D0
                DIVu    D2,D0           ;divide by range...
                CLR.W   D0              ;...and use remainder for the value
                SWAP    D0              ;result in D0.W
r2:             MOVE.w  (SP)+,D2
                RTS

	include	sintab.s

panelshowing:	dc.w	1
doinloop:	dc.w	-1
buttondat:	dc.w	0
mx:		dc.w	160
my:		dc.w	100
my2:		dc.w	0
mousehasgoneup:	dc.w	0

	cnop	0,8
panel:
	ds.w	100
panel2a:
	incbin	'panel.img'
	ds.b	320*88*2		;mag areas

brush1:
	dc.w	0,0		;x,y
	dc.w	1,1		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush1dat
brush1dat:
		dc.w	$8000
brush2:
	dc.w	0,0		;x,y
	dc.w	3,5		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush2dat
brush2dat:
		dc.w	$4000,$E000,$E000,$E000,$4000
brush3:
	dc.w	0,0		;x,y
	dc.w	5,9		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush3dat
brush3dat:
		dc.w	$2000,$7000,$F800,$F800,$F800,$F800,$F800,$7000
		dc.w	$2000
brush4:
	dc.w	0,0		;x,y
	dc.w	7,$d		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush4dat
brush4dat:
		dc.w	$3800,$7C00,$7C00,$FE00,$FE00,$FE00,$FE00,$FE00
		dc.w	$FE00,$FE00,$7C00,$7C00,$3800
brush5:
	dc.w	0,0		;x,y
	dc.w	2,4		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush5dat
brush5dat:
		dc.w	$C000,$C000,$C000,$C000
brush6:
	dc.w	0,0		;x,y
	dc.w	3,6		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush6dat
brush6dat:
		dc.w	$E000,$E000,$E000,$E000,$E000,$E000
brush7:
	dc.w	0,0		;x,y
	dc.w	4,9		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush7dat
brush7dat:
		dc.w	$F000,$F000,$F000,$F000,$F000,$F000,$F000,$F000
		dc.w	$F000
brush8:
	dc.w	0,0		;x,y
	dc.w	8,$d		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush8dat
brush8dat:
		dc.w	$FF00,$FF00,$FF00,$FF00,$FF00,$FF00,$FF00,$FF00
		dc.w	$FF00,$FF00,$FF00,$FF00,$FF00
brushpalette:
	dc.w	0,0		;x,y
	dc.w	8,8		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	0		;nxwd
	dc.w	2		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	0		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brush8dat
savebrush:
	dc.w	0,0		;x,y
	dc.w	16,16		;width height
	dc.l	$03030303	;logic op
	dc.w	8		;planes in image
	dc.w	16		;nxwd
	dc.w	320		;nxln
	dc.w	2		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	-1		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	brushsave
userbrush:
	dc.w	0,0		;x,y
	dc.w	16,16		;width height
	dc.l	$03030303	;logic op
	dc.w	8		;planes in image
	dc.w	16		;nxwd
	dc.w	320		;nxln
	dc.w	2		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	-1		;is there a background saved
	dc.l	userbrushmask	;is there a mask for matte mode
	dc.l	userbrushmem
userbrushmask:
	dc.w	0,0		;x,y
	dc.w	16,16		;width height
	dc.l	$04040707	;logic op
	dc.w	1		;planes in image
	dc.w	2		;nxwd
	dc.w	40		;nxln
	dc.w	0		;nxpl
	dc.w	0,0		;width height 2, for clip
	dc.w	0,0		;xmin ymin for clip
	dc.w	-1		;is there a background saved
	dc.l	0		;is there a mask for matte mode
	dc.l	userbrushmaskmem



fillprefsbuttons:
	dc.l	p5all,p5none,p5exit,p5part1,p5preset1,p5preset2
	dc.l	p5preset3,p5preset4,p5preset5,p5drag
	dc.l	p6fillbrush,p6fillsolid,p6fillempty,p6part1
	dc.l	p5linebit0,p5linebit1,p5linebit2
	dc.l	p5linebit3,p5linebit4,p5linebit5,p5linebit6,p5linebit7
	dc.l	p5linebit8,p5linebit9,p5linebit10,p5linebit11,p5linebit12
	dc.l	p5linebit13,p5linebit14,p5linebit15,p5linebit16,p5linebit17
	dc.l	p5linebit18,p5linebit19,p5linebit20,p5linebit21,p5linebit22
	dc.l	p5linebit23,p5linebit24,p5linebit25,p5linebit26,p5linebit27
	dc.l	p5linebit28,p5linebit29,p5linebit30,p5linebit31,0

p6fillbrush:
	dc.w	59,130	;x,y on panel screen
	dc.w	0,417	;x,y of unselected part
	dc.w	56,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setfillbrush	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p6fillsolid,p6fillempty,0
p6fillsolid:
	dc.w	131,130	;x,y on panel screen
	dc.w	56,417	;x,y of unselected part
	dc.w	56,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setfillsolid	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p6fillbrush,p6fillempty,0
p6fillempty:
	dc.w	203,130	;x,y on panel screen
	dc.w	112,417	;x,y of unselected part
	dc.w	56,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setfillempty	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p6fillbrush,p6fillsolid,0
p6part1:
	dc.w	135,117
	dc.w	167,402
	dc.w	48,12
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0


lineprefsbuttons:
	dc.l	p5all,p5none,p5exit,p5part1,p5preset1,p5preset2
	dc.l	p5preset3,p5preset4,p5preset5,p5drag
bitlist:
	dc.l	p5linebit0,p5linebit1,p5linebit2
	dc.l	p5linebit3,p5linebit4,p5linebit5,p5linebit6,p5linebit7
	dc.l	p5linebit8,p5linebit9,p5linebit10,p5linebit11,p5linebit12
	dc.l	p5linebit13,p5linebit14,p5linebit15,p5linebit16,p5linebit17
	dc.l	p5linebit18,p5linebit19,p5linebit20,p5linebit21,p5linebit22
	dc.l	p5linebit23,p5linebit24,p5linebit25,p5linebit26,p5linebit27
	dc.l	p5linebit28,p5linebit29,p5linebit30,p5linebit31,0


p5part1:
	dc.w	131,68
	dc.w	112,402
	dc.w	55,12
	dc.w	0
	dc.l	0
	dc.l	0
	dc.w	0
	dc.l	0

p5preset1:
	dc.w	40,105
	dc.w	232,305
	dc.w	40,9
	dc.w	1
	dc.l	linepreset1
	dc.l	linepreset1b
	dc.w	0
	dc.l	0
p5preset2:
	dc.w	88,105
	dc.w	232,323
	dc.w	40,9
	dc.w	1
	dc.l	linepreset2
	dc.l	linepreset2b
	dc.w	0
	dc.l	0
p5preset3:
	dc.w	136,105
	dc.w	232,359
	dc.w	40,9
	dc.w	1
	dc.l	linepreset3
	dc.l	linepreset3b
	dc.w	0
	dc.l	0
p5preset4:
	dc.w	184,105
	dc.w	232,377
	dc.w	40,9
	dc.w	1
	dc.l	linepreset4
	dc.l	linepreset4b
	dc.w	0
	dc.l	0
p5preset5:
	dc.w	232,105
	dc.w	232,341
	dc.w	40,9
	dc.w	1
	dc.l	linepreset5
	dc.l	linepreset5b
	dc.w	0
	dc.l	0

p5linebit31:
	dc.w	24,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit31
	dc.l	linebit31b
	dc.w	0
	dc.l	0
p5linebit30:
	dc.w	32,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit30
	dc.l	linebit30b
	dc.w	0
	dc.l	0
p5linebit29:
	dc.w	40,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit29
	dc.l	linebit29b
	dc.w	0
	dc.l	0
p5linebit28:
	dc.w	48,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit28
	dc.l	linebit28b
	dc.w	0
	dc.l	0
p5linebit27:
	dc.w	56,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit27
	dc.l	linebit27b
	dc.w	0
	dc.l	0
p5linebit26:
	dc.w	64,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit26
	dc.l	linebit26b
	dc.w	0
	dc.l	0
p5linebit25:
	dc.w	72,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit25
	dc.l	linebit25b
	dc.w	0
	dc.l	0
p5linebit24:
	dc.w	80,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit24
	dc.l	linebit24b
	dc.w	0
	dc.l	0
p5linebit23:
	dc.w	88,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit23
	dc.l	linebit23b
	dc.w	0
	dc.l	0
p5linebit22:
	dc.w	96,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit22
	dc.l	linebit22b
	dc.w	0
	dc.l	0
p5linebit21:
	dc.w	104,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit21
	dc.l	linebit21b
	dc.w	0
	dc.l	0
p5linebit20:
	dc.w	112,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit20
	dc.l	linebit20b
	dc.w	0
	dc.l	0
p5linebit19:
	dc.w	120,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit19
	dc.l	linebit19b
	dc.w	0
	dc.l	0
p5linebit18:
	dc.w	128,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit18
	dc.l	linebit18b
	dc.w	0
	dc.l	0
p5linebit17:
	dc.w	136,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit17
	dc.l	linebit17b
	dc.w	0
	dc.l	0
p5linebit16:
	dc.w	144,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit16
	dc.l	linebit16b
	dc.w	0
	dc.l	0
p5linebit15:
	dc.w	152,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit15
	dc.l	linebit15b
	dc.w	0
	dc.l	0
p5linebit14:
	dc.w	160,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit14
	dc.l	linebit14b
	dc.w	0
	dc.l	0
p5linebit13:
	dc.w	168,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit13
	dc.l	linebit13b
	dc.w	0
	dc.l	0
p5linebit12:
	dc.w	176,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit12
	dc.l	linebit12b
	dc.w	0
	dc.l	0
p5linebit11:
	dc.w	184,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit11
	dc.l	linebit11b
	dc.w	0
	dc.l	0
p5linebit10:
	dc.w	192,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit10
	dc.l	linebit10b
	dc.w	0
	dc.l	0
p5linebit9:
	dc.w	200,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit9
	dc.l	linebit9b
	dc.w	0
	dc.l	0
p5linebit8:
	dc.w	208,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit8
	dc.l	linebit8b
	dc.w	0
	dc.l	0
p5linebit7:
	dc.w	216,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit7
	dc.l	linebit7b
	dc.w	0
	dc.l	0
p5linebit6:
	dc.w	224,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit6
	dc.l	linebit6b
	dc.w	0
	dc.l	0
p5linebit5:
	dc.w	232,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit5
	dc.l	linebit5b
	dc.w	0
	dc.l	0
p5linebit4:
	dc.w	240,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit4
	dc.l	linebit4b
	dc.w	0
	dc.l	0
p5linebit3:
	dc.w	248,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit3
	dc.l	linebit3b
	dc.w	0
	dc.l	0
p5linebit2:
	dc.w	256,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit2
	dc.l	linebit2b
	dc.w	0
	dc.l	0
p5linebit1:
	dc.w	264,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit1
	dc.l	linebit1b
	dc.w	0
	dc.l	0
p5linebit0:
	dc.w	272,85
	dc.w	176,353
	dc.w	8,16
	dc.w	1
	dc.l	linebit0
	dc.l	linebit0b
	dc.w	0
	dc.l	0
p5none:
	dc.w	280,81	;x,y on panel screen
	dc.w	208,353	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setlinenone	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p5all:
	dc.w	0,81	;x,y on panel screen
	dc.w	184,353	;x,y of unselected part
	dc.w	24,24	;xwidth,yheight
	dc.w	1	;does'nt depress
	dc.l	setlineall	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0

p5exit:
	dc.w	304,77	;x,y on panel screen
	dc.w	96,353	;x,y of unselected part
	dc.w	16,64	;xwidth,yheight
	dc.w	0	;does'nt depress
	dc.l	lineprefsexit	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p5drag:
	dc.w	264,5	;x,y on panel screen
	dc.w	0,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	draggingprefs	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)

ubprefsbuttons:
	dc.l	p4sdmcolor,p4sdmcycle,p4exit,p4sdmmatte,p4sdmreplace
	dc.l	p4sdmtransparent,p4drag,p4part1,p4part2,p4part3,p4part4
	dc.l	p4unitransp,p4verttransp,p4horiztransp,p4spheretransp
	dc.l	p4spot,p4part5
	dc.l	eb15,eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0
	dc.l	p4dithoff,p4dith1,p4dith2,p4dith3,p4btrans,p4breplc,0


p4btrans:
	dc.w	112,125	;x,y on panel screen
	dc.w	159,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setbtrans	;routine to jump to on left button
	dc.l	setbtrans	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4breplc,0
p4breplc:
	dc.w	152,125	;x,y on panel screen
	dc.w	119,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setbreplc	;routine to jump to on left button
	dc.l	setbreplc	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4btrans,0

cb15:
	dc.w	208,89	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb15	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb14:
	dc.w	208,93	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb14	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb13:
	dc.w	208,97	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb13	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb12:
	dc.w	208,101	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb12	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb11:
	dc.w	208,105	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb11	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb10:
	dc.w	208,109	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb10	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb9:
	dc.w	208,113	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb9	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb8:
	dc.w	208,117	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb8	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb7:
	dc.w	208,121	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb7	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb6:
	dc.w	208,125	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb6	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb5
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb5:
	dc.w	208,129	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb5	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6
	dc.l	cb4,cb3,cb2,cb1,cb0,0
cb4:
	dc.w	208,133	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb4	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb3,cb2,cb1,cb0,0
cb3:
	dc.w	208,137	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb3	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb2,cb1,cb0,0
cb2:
	dc.w	208,141	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb2	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb1,cb0,0
cb1:
	dc.w	208,145	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb1	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb0,0
cb0:
	dc.w	208,149	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	docb0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	cb15,cb14,cb13,cb12,cb11,cb10,cb9,cb8,cb7,cb6,cb5
	dc.l	cb4,cb3,cb2,cb1,0

eb15:
	dc.w	280,89	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb15	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb14:
	dc.w	280,93	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb14	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb15,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb13:
	dc.w	280,97	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb13	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb15,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb12:
	dc.w	280,101	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb12	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb15,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb11:
	dc.w	280,105	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb11	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb15,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb10:
	dc.w	280,109	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb10	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb15,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb9:
	dc.w	280,113	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb9	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb15,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb8:
	dc.w	280,117	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb8	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb15,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb7:
	dc.w	280,121	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb7	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb15,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb6:
	dc.w	280,125	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb6	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb15,eb5
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb5:
	dc.w	280,129	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb5	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb15
	dc.l	eb4,eb3,eb2,eb1,eb0,0
eb4:
	dc.w	280,133	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb4	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb15,eb3,eb2,eb1,eb0,0
eb3:
	dc.w	280,137	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb3	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb15,eb2,eb1,eb0,0
eb2:
	dc.w	280,141	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb2	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb15,eb1,eb0,0
eb1:
	dc.w	280,145	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb1	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb15,eb0,0
eb0:
	dc.w	280,149	;x,y on panel screen
	dc.w	80,281	;x,y of unselected part
	dc.w	8,4	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	doeb0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	eb14,eb13,eb12,eb11,eb10,eb9,eb8,eb7,eb6,eb5
	dc.l	eb4,eb3,eb2,eb1,eb15,0


p4dithoff:
	dc.w	120,80	;x,y on panel screen
	dc.w	112,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setdithoff	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4dith1,p4dith2,p4dith3,0
p4dith1:
	dc.w	136,80	;x,y on panel screen
	dc.w	128,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setdith1	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4dithoff,p4dith2,p4dith3,0
p4dith2:
	dc.w	152,80	;x,y on panel screen
	dc.w	144,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setdith2	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4dith1,p4dithoff,p4dith3,0
p4dith3:
	dc.w	168,80	;x,y on panel screen
	dc.w	160,353	;x,y of unselected part
	dc.w	16,20	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setdith3	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4dith1,p4dith2,p4dithoff,0

p4spot:
	dc.w	216,89	;x,y on panel screen
	dc.w	32,353	;x,y of unselected part
	dc.w	64,64	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	setspot	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0
p4unitransp:
	dc.w	216,65	;x,y on panel screen
	dc.w	288,257	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setunitransp	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4verttransp,p4horiztransp,p4spheretransp,0
p4verttransp:
	dc.w	232,65	;x,y on panel screen
	dc.w	304,257	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setverttransp
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4unitransp,p4horiztransp,p4spheretransp,0
p4horiztransp:
	dc.w	248,65	;x,y on panel screen
	dc.w	200,305	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sethoriztransp
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4unitransp,p4verttransp,p4spheretransp,0
p4spheretransp:
	dc.w	264,65	;x,y on panel screen
	dc.w	216,305	;x,y of unselected part
	dc.w	16,24	;xwidth,yheight
	dc.w	1	;doesn't depress
	dc.l	setspheretransp
	dc.l	0		;routine for right button
	dc.w	0		;selected toggle
	dc.l	p4unitransp,p4horiztransp,p4verttransp,0


p4part1:
	dc.w	193,89	;x,y on panel screen (centre trans)
	dc.w	0,353	;x,y of unselected part
	dc.w	14,64	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p4part2:
	dc.w	289,89	;x,y on panel screen (edge trans)
	dc.w	14,353	;x,y of unselected part
	dc.w	14,64	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p4part3:
	dc.w	131,67	;x,y on panel screen (dither)
	dc.w	112,393	;x,y of unselected part
	dc.w	43,9	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p4part4:
	dc.w	119,101	;x,y on panel screen (background color)
	dc.w	232,395	;x,y of unselected part
	dc.w	66,22	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)
p4part5:
	dc.w	84,68	;x,y on panel screen (transparency prefs)
	dc.w	272,305	;x,y of unselected part
	dc.w	13,83	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	0	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)

p4drag:
	dc.w	264,5	;x,y on panel screen
	dc.w	0,257	;x,y of unselected part(not needed here cause button does not depress)
	dc.w	55,24	;xwidth,yheight
	dc.w	0	;doesn't depress
	dc.l	draggingprefs	;routine to jump to on left button
	dc.l	0		;routine for right button
	dc.w	0	;selected toggle
	dc.l	0	;null terminated list of buttons this one raises (none)

p4exit:
	dc.w	304,77	;x,y on panel screen
	dc.w	96,353	;x,y of unselected part
	dc.w	16,64	;xwidth,yheight
	dc.w	0	;does'nt depress
	dc.l	prefsexit	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	0

p4sdmtransparent:
	dc.w	40,97	;x,y on panel screen
	dc.w	160,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sdmtransparent	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4sdmcycle,p4sdmcolor,p4sdmmatte,p4sdmreplace,0
p4sdmreplace:
	dc.w	40,69	;x,y on panel screen
	dc.w	120,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sdmreplace	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4sdmcycle,p4sdmcolor,p4sdmmatte,p4sdmtransparent,0
p4sdmmatte:
	dc.w	0,125	;x,y on panel screen
	dc.w	80,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sdmmatte	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4sdmcycle,p4sdmcolor,p4sdmtransparent,p4sdmreplace,0
p4sdmcolor:
	dc.w	0,69	;x,y on panel screen
	dc.w	0,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sdmcolor	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4sdmcycle,p4sdmmatte,p4sdmtransparent,p4sdmreplace,0
p4sdmcycle:
	dc.w	0,97	;x,y on panel screen
	dc.w	40,305	;x,y of unselected part
	dc.w	40,24	;xwidth,yheight
	dc.w	1	;does depress
	dc.l	sdmcycle	;left
	dc.l	0	;routine for right button
	dc.w	0	;selected toggle
	dc.l	p4sdmcolor,p4sdmmatte,p4sdmtransparent,p4sdmreplace,0


smallnumbers:
	 dc.b	$00,$00,$44,$7C,$00,$00,$AA,$EE
	 dc.b	$00,$00,$54,$DE,$00,$00,$28,$BA
	 dc.b	$00,$00,$54,$F6,$00,$00,$AA,$EE
	 dc.b	$00,$00,$44,$7C,$00,$00,$20,$30
	 dc.b	$00,$00,$40,$70,$00,$00,$20,$30
	 dc.b	$00,$00,$00,$10,$00,$00,$00,$10
	 dc.b	$00,$00,$28,$38,$00,$00,$44,$7C
	 dc.b	$00,$00,$82,$FE,$00,$00,$44,$C6
	 dc.b	$00,$00,$04,$06,$00,$00,$82,$FE
	 dc.b	$00,$00,$40,$C0,$00,$00,$40,$C0
	 dc.b	$00,$00,$00,$FE,$00,$00,$82,$FE
	 dc.b	$00,$00,$44,$C6,$00,$00,$04,$06
	 dc.b	$00,$00,$22,$3E,$00,$00,$04,$06
	 dc.b	$00,$00,$44,$C6,$00,$00,$82,$FE
	 dc.b	$00,$00,$10,$1C,$00,$00,$28,$3C
	 dc.b	$00,$00,$50,$74,$00,$00,$AA,$EE
	 dc.b	$00,$00,$00,$FE,$00,$00,$0A,$0E
	 dc.b	$00,$00,$00,$04,$00,$00,$00,$FE
	 dc.b	$00,$00,$40,$C0,$00,$00,$40,$C0
	 dc.b	$00,$00,$82,$FE,$00,$00,$04,$06
	 dc.b	$00,$00,$04,$06,$00,$00,$02,$FE
	 dc.b	$00,$00,$82,$FE,$00,$00,$44,$C6
	 dc.b	$00,$00,$40,$C0,$00,$00,$02,$FE
	 dc.b	$00,$00,$44,$C6,$00,$00,$44,$C6
	 dc.b	$00,$00,$82,$FE,$00,$00,$00,$FE
	 dc.b	$00,$00,$04,$06,$00,$00,$0A,$0E
	 dc.b	$00,$00,$14,$1C,$00,$00,$28,$38
	 dc.b	$00,$00,$50,$70,$00,$00,$20,$60
	 dc.b	$00,$00,$82,$FE,$00,$00,$44,$C6
	 dc.b	$00,$00,$44,$C6,$00,$00,$82,$FE
	 dc.b	$00,$00,$44,$C6,$00,$00,$44,$C6
	 dc.b	$00,$00,$82,$FE,$00,$00,$82,$FE
	 dc.b	$00,$00,$40,$C2,$00,$00,$44,$C6
	 dc.b	$00,$00,$80,$FE,$00,$00,$04,$06
	 dc.b	$00,$00,$04,$06,$00,$00,$82,$FE

registration:
	dc.b	'Demo version, for SEW SIMPLE'
	dc.b	'Copyright, 1991, Tony Barker, Moving Pixels.'
	even
	dc.l	$01234321

dialogue:
	incbin	'dialogue.img'

	dc.b	'MVPXPALT'
	dc.w	0,0		;fore and background colors
palette1:
*	ds.b	512
	incbin	'startpal.img'

	dc.b	'MVPXPALT'
	dc.w	0,0		;fore and background colors
palette2:
	ds.b	512

notknownalert:
		dc.b	'[1][Picture file is wrong format|'
		dc.b	'or could not be determined.]'
		dc.b	'[Try again|Cancel]',0,0
doublealert:
		dc.b	'[1][Picture file is in ST|'
		dc.b	'Low resolution.|'
		dc.b	'Double vertical resolution ?]'
		dc.b	'[Yes|No]',0,0
usethispalalert:
		dc.b	'[1][Use this pictures palette|'
		dc.b	'or remap to the current palette.]'
		dc.b	'[USE|REMAP]',0,0

picwrongresalert:
		dc.b	'[1][File is in TT Medium|'
		dc.b	'resolution. OZPAINTT|'
		dc.b	'requires files to be in|'
		dc.b	'TT Low or lower.]'
		dc.b	'[Try again|Cancel]',0,0

	even
	cnop	0,8
	section bss
userbrushmem:
	ds.b	153600
userbrushmaskmem:
	ds.b	153600
brushsave:
	ds.b	153600
	ds.b	10000
numwhere:	ds.l	1
number:		ds.w	1
picplace:	ds.l	1
picres:		ds.b	1
pictype:	ds.b	1
	even
colorcyclehigh:	ds.w	1
colorcyclelow:	ds.w	1
redadd:		ds.w	1
greenadd:	ds.w	1
blueadd:	ds.w	1
bottomred:	ds.w	1
bottomgreen:	ds.w	1
bottomblue:	ds.w	1
topred:		ds.w	1
topgreen:	ds.w	1
topblue:	ds.w	1
bottomcolor:	ds.w	1
topcolor:	ds.w	1
wascolorset:	ds.w	1
palettemode:	ds.w	1
currentpanelsprite:	ds.l	1
currentdrawsprite:	ds.l	1
savebutton:	ds.l	1
paletteshowing:	ds.w	1
fcolorsave:	ds.w	1
linea:		ds.l	1
brushmode:		ds.w	1
oldpaintmode:		ds.w	1
generalvalue:		ds.w	1
x1save:			ds.w	1
x2save:			ds.w	1
y1save:			ds.w	1
y2save:			ds.w	1
color:			ds.w	1
writemode:		ds.w	1
todraw:			ds.w	1
lineainit:		ds.l	1
lineapixel:		ds.l	1
lineagetpixel:		ds.l	1
linealine:		ds.l	1
lineahorizontalline:	ds.l	1
linearectangle:		ds.l	1
lineapolygon:		ds.l	1
lineabitblt:		ds.l	1
lineatextblt:		ds.l	1
lineashowmouse:		ds.l	1
lineahidemouse:		ds.l	1
lineatransformmouse:	ds.l	1
lineaundrawsprite:	ds.l	1
lineadrawsprite:	ds.l	1
lineacopyraster:	ds.l	1
lineaseedfill:		ds.l	1
shouldfollow:	ds.w	1
x1:		ds.w	1
y1:		ds.w	1
x2:		ds.w	1
y2:		ds.w	1
savemx:		ds.w	1
savemy:		ds.w	1
paintmode:	ds.w	1
brightest:	ds.w	1
checkcolors:	ds.w	1
drawcursorflag:	ds.w	1
oldmy:		ds.w	1
oldmx:		ds.w	1
blittype:	ds.w	1
currentfcolor:	ds.w	1
currentbcolor:	ds.w	1
currentbrush:	ds.l	1
drawroutine:	ds.l	1
vblflag:	ds.w	1
buttondat2:	ds.w	1
currentpanel:	ds.w	1
redisplayadjust:	ds.l	1
oldmouse:	ds.l	1
filetype:	ds.w	1
filetypesave:	ds.w	1
currentscreen:	ds.l	1
currentpalette:	ds.l	1
_loadlength:	ds.l	1
stacksave:	ds.l	1
shiftsave:	ds.w	1
screenad:	ds.l	1
screen1:	ds.l	1
screen2:	ds.l	1
undoscreen:	ds.l	1
cyclelow:	ds.w	1
cyclehigh:	ds.w	1
cyclenumber:	ds.w	1
linepattern:	ds.l	1
linepattern2:	ds.l	1
savex1:		ds.w	1
savey1:		ds.w	1
savex2:		ds.w	1
savey2:		ds.w	1
savebuttondat:	ds.w	1
*savelinepattern:	ds.l	1
sprayrate:	ds.w	1
sprayradius:	ds.w	1
transparency:	ds.w	1
canceltransparent:	ds.w	1
color0:	ds.w	1
color1:	ds.w	1
color2:	ds.w	1
color3:	ds.w	1
color4:	ds.w	1
color5:	ds.w	1
color6:	ds.w	1
color7:	ds.w	1
color8:	ds.w	1
color9:	ds.w	1
color10:	ds.w	1
color11:	ds.w	1
color12:	ds.w	1
color13:	ds.w	1
color14:	ds.w	1
color15:	ds.w	1
oldcolor0:	ds.w	1
oldcolor15:	ds.w	1
savecpanel:	ds.w	1
transmode:	ds.w	1
edgetrans:	ds.w	1
centretrans:	ds.w	1
currentdither:	ds.w	1
oldcoordx:	ds.w	1
oldcoordy:	ds.w	1
initialx:	ds.w	1
initialy:	ds.w	1
x2print:	ds.w	1
y2print:	ds.w	1
xcentre:	ds.l	1
ycentre:	ds.l	1
cradius:	ds.l	1
newcolor:	ds.w	1
oldcolor:	ds.w	1
filltype:	ds.w	1
p1x:	ds.w	1
p1y:	ds.w	1
p2x:	ds.w	1
p2y:	ds.w	1
p3x:	ds.w	1
p3y:	ds.w	1
p4x:	ds.w	1
p4y:	ds.w	1
mustrecalc:	ds.w	1
xadd:	ds.w	1
yadd:	ds.w	1
saveheight:	ds.w	1
curvepointlist:	ds.w	8
mouseoffsetx:	ds.w	1
mouseoffsety:	ds.w	1
a6save:		ds.l	1
d1save:		ds.w	1
d2save:		ds.w	1
whichbox:	ds.l	1
bistrans:	ds.w	1
collided:	ds.w	1
lastbutton:	ds.l	1
shouldfollowsave:	ds.w	1
currentbuttonlist:	ds.l	1
colorbuttons:	ds.w	256*2
panelad:	ds.l	1
colortable:	ds.w	4096
save502:	ds.l	1
buttonbltblk:
	ds.b	76
drawbltblk:
	ds.b	76
dummybltblk:
	ds.b	76
screens:
	ds.b	153600*3+8
pichold:
	ds.b	153600+10000
savepal:
	ds.b	512
palette3:
	ds.b	512
temppalette:
	ds.b	512+8+4
fitpalette:
	ds.b	512
elipselist:
	ds.w	10000
fillplane:
	ds.b	153600/8
