		IDENTIFY	"GraphicsDemo"

		OPTION		NL
		INCLUDE		"exec/types.i"
		INCLUDE		"exec/ports.i"
		INCLUDE		"exec/memory.i"
		INCLUDE		"exec/tasks.i"
		INCLUDE		"libraries/dosextens.i"
		INCLUDE		"graphics/gfx.i"
		INCLUDE		"intuition/intuition.i"
		OPTION		L
*
*		Equates
*
MEM_REQS	equ		MEMF_CHIP+MEMF_PUBLIC+MEMF_CLEAR
RasWidth	equ		320
RasHeight	equ		256
TableSize	equ		500
Rinc		equ		$00F2
Ginc		equ             $0115
Binc		equ		$0086
*
*		XREFS
*
		XREF		_AbsExecBase
		XREF		_LVOAllocMem
		XREF		_LVOCloseLibrary
		XREF		_LVOCloseScreen
		XREF		_LVOFreeMem
		XREF		_LVOOpenLibrary
		XREF		_LVOOpenScreen
		XREF		_LVOSetRGB4
		XREF		_LVOWaitTOF
		XREF		_LVOSetAPen
		XREF		_LVOSetBPen
		XREF		_LVOGelsFuncF
		XREF		_LVOAreaEnd
		XREF		_LVOAllocRaster
		XREF		_LVOFreeRaster
		XREF		_LVOInitTmpRas
		XREF		_LVOInitArea
		XREF		_LVOLoadRGB4
		XREF		_LVOOpenWindow
		XREF		_LVOCloseWindow
		XREF		_LVOGetMsg
		XREF		_LVOReplyMsg
		XREF		_LVODisplayBeep
		XREF		_LVOSetMenuStrip
		XREF		_LVOClearMenuStrip
		XREF		_LVOScrollRaster
		XREF		_LVOForbid
		XREF		_LVOWaitPort
		XREF		_LVOFindTask
*
*		XDEFS
*
		XDEF		_main
*
*		Macro to test menu numbers
*
MenuTest	MACRO ; Menu,Item,SubItem,Register
		cmp.w	#(\1 & $1F)!((\2 & $3F)<<5)!((\3 & $1F)<<11),\4
		ENDM
*
*		Code		start
*

		SECTION		"Code",CODE

_main		move.l		a7,OldStack
		move		#Bval-IntuiBase,d0
		lea		IntuiBase,a0
Zero		clr.b		(a0)+
		dbra		d0,Zero
		move.l		_AbsExecBase,a6
		suba.l		a1,a1
		jsr		_LVOFindTask(a6)
		move.l		d0,a4
		tst.l		pr_CLI(a4)
		bne		Start
		lea		pr_MsgPort(a4),a0
		jsr		_LVOWaitPort(a6)
		lea		pr_MsgPort(a4),a0
		jsr		_LVOGetMsg(a6)
		move.l		d0,WBenchMsg
Start		bsr		Init
NextMove	movea.l		GfxBase,a6
		jsr		_LVOWaitTOF(a6)
		tst.b		Paint
		beq		SkipPaint
		move.l		MyWindow,a2
		move.w		wd_MouseX(a2),d0
		move.w		wd_MouseY(a2),d1
		move.l		wd_RPort(a2),a1
		movea.l		a1,a4
		move.w		Radius,d2
		move.w		Radius,d3
		movem.l		a0-a1/d0-d3,Regs
		jsr		_LVOGelsFuncF(a6)
		movea.l		a4,a1
		jsr		_LVOAreaEnd(a6)
		movem.l		Regs,a0-a1/d0-d3
		neg.w		d0
		add.w		#RasWidth,d0
		jsr		_LVOGelsFuncF(a6)
		movea.l		a4,a1
		jsr		_LVOAreaEnd(a6)
		movem.l		Regs,a0-a1/d0-d3
		neg.w		d1
		add.w		#RasHeight,d1
		jsr		_LVOGelsFuncF(a6)
		movea.l		a4,a1
		jsr		_LVOAreaEnd(a6)
		movem.l		Regs,a0-a1/d0-d3
		neg.w		d0
		add.w		#RasWidth,d0
		neg.w		d1
		add.w		#RasHeight,d1
		jsr		_LVOGelsFuncF(a6)
		movea.l		a4,a1
		jsr		_LVOAreaEnd(a6)
SkipPaint	move.w		Pen,d0
		addq.w		#1,d0
		cmp.w		#32,d0
		bne		setcol
		moveq.w		#2,d0
setcol		move.w		d0,Pen
		movea.l		a4,a1
		jsr		_LVOSetAPen(a6)
		move.w		Radius,d0
		add.w		Direction,d0
		move.w		d0,Radius
		cmp.w		#5,d0
		bgt		NotSmallest
		move.w		#1,Direction
NotSmallest	cmp.w		#15,d0
		blt		NotLargest
		move.w		#-1,Direction
NotLargest	jsr		ColShift		
		move.l		MPort,a0
		move.l		_AbsExecBase,a6
		jsr		_LVOGetMsg(a6)
		tst.l		d0
		beq		NextMove
 		move.l		d0,a1
		move.w		im_Code(a1),d3
		move.l		im_Class(a1),d4
		jsr		_LVOReplyMsg(a6)
		cmp.l		#MOUSEBUTTONS,d4
		bne		CheckMenu
		cmp.w		#SELECTDOWN,d3
		bne		Release
		seq		Paint
Release		cmp.w		#SELECTUP,d3
		bne		NextMove
		sne		Paint
		bra		NextMove
CheckMenu	cmp.l		#MENUPICK,d4
		bne		NextMove
		MenuTest	0,0,NOSUB,d3
		beq		ClearScreen
		MenuTest	0,1,NOSUB,d3
		beq		ShutDown
		bra		NextMove

ClearScreen	move.w		#16,d6		;Uses a venetian blind wipe
		move.l		#0,d2
		move.l		#RasWidth,d4
Cloop		move.l		#15,d5
		move.l		#10,d3
		movea.l		GfxBase,a6
NextBlind	movea.l		RPort,a1
		move.l		#0,d0
		move.l		#1,d1
		jsr		_LVOScrollRaster(a6)
		move.w		d5,d3
		addq.w		#1,d3
		add.w		#16,d5
		cmpi.w		#RasHeight,d5
		bcs		NextBlind
		dbra		d6,Cloop
		bra		NextMove
;
;		Open Libraries and allocate structures
;		
Init		move.w		#2,Pen
		move.w		#5,Radius
		move.w		#1,Direction
		movea.l		_AbsExecBase,a6
		clr.b		Paint
		moveq		#0,d0
		lea		DOSName,a1
		jsr		_LVOOpenLibrary(a6)
		tst.l		d0
		beq		ShutDown
		move.l		d0,DOSBase
		moveq		#0,d0
		lea		IntuiLibName,a1
		jsr		_LVOOpenLibrary(a6)
		tst.l		d0
		beq		ShutDown
		move.l		D0,IntuiBase

		moveq		#0,d0
		lea		GfxLibName,a1
		jsr		_LVOOpenLibrary(a6)
		tst.l		d0
		beq		ShutDown
		move.l		d0,GfxBase

		movea.l		IntuiBase,a6
		lea		MyNewScreen,a0
		jsr		_LVOOpenScreen(a6)
		tst.l		d0
		beq		ShutDown
		move.l		d0,MyScreen
		move.l		d0,ScreenPtr
		lea		MyNewWindow,a0
		jsr		_LVOOpenWindow(a6)
		tst.l		d0
		beq		ShutDown
		move.l		d0,MyWindow
		move.l		d0,a0
		move.l		wd_RPort(a0),d0
		move.l		d0,RPort
		move.l		wd_UserPort(a0),d0
		move.l		d0,MPort
		lea		MyMenu,a1
		jsr		_LVOSetMenuStrip(a6)
;
;		Get a temporary Raster for Area filling.
;
		movea.l		GfxBase,a6
		move.l		#RasWidth,d0
		move.l		#RasHeight,d1
		jsr		_LVOAllocRaster(a6)
		tst.l		d0
		beq		ShutDown
		move.l		d0,TmpRasBuf
		lea		MyTmpRas,a0
		movea.l		TmpRasBuf,a1
		move.l		#RasWidth*RasHeight/8,d0
		jsr		_LVOInitTmpRas(a6)
		movea.l		RPort,a0
		move.l		d0,rp_TmpRas(a0)
		lea		MyAreaInfo,a0
		lea.l		AreaTable,a1
		move.l		#TableSize/5,d0
		jsr		_LVOInitArea(a6)
		movea.l		RPort,a0
		lea		MyAreaInfo,a1
		movea.l		a1,rp_AreaInfo(a0)
		
		
;
;		Initialise ColourMap
;
		move.w		#$000,Col0
		move.w		#$FFF,Col1
		move.w		#$3A6,CMap
		move.w		#$3E45,Rval
		move.w		#$15A2,Gval
		move.w		#$206C,Bval
		rts
;
;		Shift Colours
;
ColShift	move.w		#29,d0		;Move colours 2 to 30 up one
		lea		CMap+60,a0
		lea		CMap+62,a1
CLoop		move.w		-(a0),-(a1)
		dbra		d0,CLoop
;
		add.w		#Rinc,Rval	;Add Red increment
		move.b		Rval,d0		;Get Red value
		btst.b          #4,d0		;If b4 set, counting down
		beq		Rup
		not.b		d0		;Reverse count
Rup		and.w		#$F,d0		;Mask colour value
		move.w		d0,d1		;Build colour in d1
		add.w		#Ginc,Gval	;Now process green
		move.b		Gval,d0
		btst.b          #4,d0
		beq		Gup
		not.b		d0
Gup		and.w		#$F,d0
		lsl.w		#4,d1
		or.b		d0,d1
		add.w		#Binc,Bval	;And Blue
		move.b		Bval,d0
		btst.b          #4,d0
		beq		Bup
		neg.b		d0
Bup		and.w		#$F,d0
		lsl.w		#4,d1
		or.b		d0,d1
		move.w		d1,CMap
		move.l		MyScreen,a0
		lea		sc_ViewPort(a0),a0
		lea		Col0,a1
		move.l		#32,d0
		movea.l		GfxBase,a6
		jsr		_LVOLoadRGB4(a6)
		rts
;
;		Deallocate Memory and Structures
;
ShutDown	move.l		TmpRasBuf,a0
		cmpa.l		#0,a0
		beq		NoTmpRas
		move.l		#RasWidth,d0
		move.l		#RasHeight,d1
		movea.l		GfxBase,a6
		jsr		_LVOFreeRaster(a6)
NoTmpRas	movea.l		MyWindow,a0
		cmpa.l		#0,a0
		beq		NoWindow
		movea.l		IntuiBase,a6
		jsr		_LVOClearMenuStrip(a6)
		movea.l		MyWindow,a0
		jsr		_LVOCloseWindow(a6)
NoWindow	movea.l		MyScreen,a0
		cmpa.l		#0,a0
		beq		NoScreen
		jsr		_LVOCloseScreen(a6)
NoScreen	movea.l		_AbsExecBase,a6
		movea.l		GfxBase,a1
		cmpa.l		#0,a1
		beq		NoGraphics
		jsr		_LVOCloseLibrary(a6)
NoGraphics	movea.l		IntuiBase,a1
		cmpa.l		#0,a1
		beq		NoIntuition
		jsr		_LVOCloseLibrary(a6)
NoIntuition	movea.l		DOSBase,a1
		cmpa.l		#0,a1
		beq		NoDOS
		jsr		_LVOCloseLibrary(a6)
NoDOS		tst.l		WBenchMsg
		beq		Exit
		jsr		_LVOForbid(a6)
		move.l		WBenchMsg,a1
		jsr		_LVOReplyMsg(a6)
Exit		movea.l		OldStack,a7
		clr.l		d0
		rts


		SECTION		"Bss",BSS

		CNOP		2
OldStack	DS.L		1
IntuiBase	DS.L		1
GfxBase		DS.L		1
DOSBase		DS.L		1
WBenchMsg	DS.L		1
MyScreen	DS.L		1
MyWindow	DS.L		1
RPort		DS.L		1
MPort		DS.L		1
TmpRasBuf	DS.L		1
Radius		DS.W		1
Direction	DS.W		1
Paint		DS.W		1
Regs		DS.L		6
Col0		DS.W		1
Col1		DS.W		1
CMap		DS.W		30
Rval            DS.W            1
Gval            DS.W            1
Bval            DS.W            1
Pen		DS.W		1

*
*		Data areas
*
		SECTION		"Data",DATA
		CNOP		2

DOSName		CSTRING		'dos.library'
		CNOP		2

ScreenTitle	CSTRING		'***Colour Demo***   A.Bisson.'
		CNOP		2

IntuiLibName	CSTRING		'intuition.library'
		CNOP		2

GfxLibName	CSTRING		'graphics.library'
		CNOP		2

MyNewScreen	DC.W		0,0
		DC.W		RasWidth,RasHeight
		DC.W		5
		DC.B		0,1
		DC.W		0
		DC.W		CUSTOMSCREEN
		DC.L		0
		DC.L		ScreenTitle
		DC.L		0

		DC.L		0

		CNOP		2

MyNewWindow	DC.W		0,0
		DC.W		RasWidth,RasHeight
		DC.B		-1,-1		;Default Pens
		DC.L		VANILLAKEY!MOUSEBUTTONS!MENUPICK
		DC.L		ACTIVATE!BACKDROP!SIMPLE_REFRESH!BORDERLESS
		DC.L		0		;No gadgets
		DC.L		0		;Default Checkmark
		DC.L		0		;No text
ScreenPtr	DS.L		1		;Put window in my screen
		DC.L		0		;No SuperBitMap
		DS.W		4		;No Window Limits
		DC.W		CUSTOMSCREEN	;My ScreenType	

AreaTable	DS.W		TableSize/2

MyAreaInfo	DS.B		ai_SIZEOF
		CNOP		2

MyTmpRas	DS.B		tr_SIZEOF
		CNOP		2



MyMenu		DC.L		0 			; SymMenu
		DC.W		10			;Select Box Left Side
		DC.W		0			;Select Box Top
		DC.W		60			;Select Box Width
		DC.W		10			;Select Box Height
		DC.W		MENUENABLED
		DC.L		MenuName
		DC.L		ItemClear
		DS.W		4			;Internal use

ItemClear	DC.L		ItemQuit		;Next item
		DC.W		0			;Item Box Left Side
		DC.W		0			;Item Box Top
		DC.W		60			;Item Box Width
		DC.W		10			;Item Box Height
		DC.W		ITEMTEXT!HIGHBOX!ITEMENABLED
		DC.L		0			;No Mutual Exclude
		DC.L		ClearText
		DC.L		0			;No Select Image
		DC.B		0			;No Command Char
		CNOP		2
		DC.L		0			;No SubItem
		DC.W		0			;No Next Select

ItemQuit	DC.L		0			;Last item
		DC.W		0			;Item Box Left Side
		DC.W		15			;Item Box Top
		DC.W		60			;Item Box Width
		DC.W		10			;Item Box Height
		DC.W		ITEMTEXT!HIGHBOX!ITEMENABLED
		DC.L		0			;No Mutual Exclude
		DC.L		QuitText
		DC.L		0			;No Select Image
		DC.B		0			;No Command Char
		CNOP		2
		DC.L		0			;No SubItem
		DC.W		0			;No Next Select

MenuName	CSTRING		'Project'
		CNOP		2

QuitText	DC.B		0			;Front Pen
		DC.B		0			;Back Pen
		DC.B		RP_JAM1			;Render Mode
		CNOP		2
		DC.W		5			;Left Edge
		DC.W		1			;Top
		DC.L		0			;Default Font
		DC.L		Qtxt			;Text String
		DC.L		0			;End Of Text

Qtxt		CSTRING		'Quit'
		CNOP		2

ClearText	DC.B		0			;Front Pen
		DC.B		0			;Back Pen
		DC.B		RP_JAM1			;Render Mode
		CNOP		2
		DC.W		5			;Left Edge
		DC.W		1			;Top
		DC.L		0			;Default Font
		DC.L		Ctxt			;Text String
		DC.L		0			;End Of Text

Ctxt		CSTRING		'Clear'
		CNOP		2
		ENDSRC
