	section	mm,code_c
	opt	o+,ow-

; This program creates a view, clears its bitplanes using the blitter
; and then displays a line of text more than 20 chars long. Press the
; left mouse button to exit. M.Meany 7.7.90

	incdir	':include/'
	include	'graphics/gfx.i'
	include	'graphics/gfxbase.i'
	include	'graphics/graphics_lib.i'
	include	'graphics/view.i'
	include	'graphics/rastport.i'
	include	'exec/exec.i'
	include	'exec/exec_lib.i'
	include	'exec/execbase.i'
	include	'misc/easystart.i'
	

; Open the graphics library and store its base pointer

	moveq.l	#0,d0
	lea	GFXName,a1
	CALLEXEC	OpenLibrary
	move.l	d0,_GfxBase
	beq	error
	
; Get pointer to system copper list and save it for later

	move.l	d0,a0
	move.l	gb_ActiView(a0),oldcopper
	
; Initialise a View structure

	lea	my_view,a1
	CALLGRAF	InitView
	

; Initialise a View Port structure. This will be used to display my screen

	lea	my_viewport,a0
	CALLGRAF	InitVPort

; Link Viewport structure to the View structure.

	lea	my_view,a0
	move.l	#my_viewport,v_ViewPort(a0)

; Initialise a Bitmap structure

	lea	my_bitmap,a0
	moveq.l	#2,d0
	move.l	#320,d1
	move.l	#260,d2
	CALLGRAF	InitBitMap

; Allocate memory for bitplanes and link to bitmap structure

	lea	my_bitmap,a5
	adda.l	#bm_Planes,a5
	move.l	#320,d0
	move.l	#260,d1
	CALLGRAF	AllocRaster
	move.l	d0,(a5)+
	move.l	d0,bpl1
	beq	error_1
	move.l	#320,d0
	move.l	#260,d1
	CALLGRAF	AllocRaster
	move.l	d0,(a5)
	move.l	d0,bpl2
	beq	error_2
	
; Initialise a rastport structure and link bitmap structure to it.

	lea	my_rastport,a1
	CALLGRAF	InitRastPort
	lea	my_rastport,a0
	move.l	#my_bitmap,rp_BitMap(a0)
	
; Link Bitmap structure to a RasInfo structure and set screen x & y offsets

	lea	my_rasinfo,a0
	move.l	#my_bitmap,ri_BitMap(a0)
	move.w	#0,ri_RxOffset(a0)
	move.w	#0,ri_RyOffset(a0)

; Link Rasinfo structure to the ViewPort structure and set
; DWidth and DHeight which define height of visible bitmap in the ViewPort.

	lea	my_viewport,a0
	move.l	#my_rasinfo,vp_RasInfo(a0)
	move.w	#320,vp_DWidth(a0)
	move.w	#260,vp_DHeight(a0)

; Initialise a structure for 4 local colours to be linked to this ViewPort.

	moveq.l	#4,d0
	CALLGRAF	GetColorMap
	tst.l	d0
	beq	error_3

; Link ColourMap structure to ViewPort

	lea	my_viewport,a0
	move.l	d0,vp_ColorMap(a0)

; Copy colour values into this Structure using sys function.

	lea	my_colours,a1
	moveq.l	#4,d0
	CALLGRAF	LoadRGB4

; Get system to prepare display instructions for copper.

	lea	my_view,a0
	lea	my_viewport,a1
	CALLGRAF	MakeVPort

; Merge this list into a real copper list.

	lea	my_view,a1
	CALLGRAF	MrgCop

; Display this view.

	lea	my_view,a1
	CALLGRAF	LoadView

;****************************************************************
; Your program specifics go here.

; Use blitter to clear the bitplanes

	move.l	bpl1,a1
	move.l	#320*260/8,d0
	moveq.l	#0,d1
	CALLGRAF	BltClear
	move.l	bpl2,a1
	move.l	#320*260/8,d0
	moveq.l	#0,d1
	CALLGRAF	BltClear
	
; Set drawing mode to foreground colour

	moveq.l	#0,d0
	lea	my_rastport,a1
	CALLGRAF	SetDrMd
	
; Move pen head to start position

	move.l	#10,d0
	move.l	#30,d1
	lea	my_rastport,a1
	CALLGRAF	Move
	
; Print a line of text

	move.l	#msg_len,d0
	lea	msg,a0
	lea	my_rastport,a1
	CALLGRAF	Text


; Wait for user to press left mouse button
	
mouse_wait	btst	#6,$bfe001
	bne	mouse_wait

;****************************************************************
	
; Release memory used by intermidiate copper lists


	lea	my_viewport,a0
	CALLGRAF	FreeVPortCopLists
	lea	my_view,a0
	move.l	v_LOFCprList(a0),a0
	CALLGRAF	FreeCprList

; Release memory allocated for colour map
	
	lea	my_viewport,a0
	move.l	vp_ColorMap(a0),a0
	CALLGRAF	FreeColorMap

; Restore systems copper list.  

	move.l	oldcopper,a1
	CALLGRAF	LoadView

; Release memory allocated for bit planes

error_3	lea	my_bitmap,a0
	move.l	bm_Planes+4(a0),a0
	move.l	#320,d0
	move.l	#260,d1
	CALLGRAF	FreeRaster
error_2	lea	my_bitmap,a0
	move.l	bm_Planes(a0),a0
	move.l	#320,d0
	move.l	#260,d1
	CALLGRAF	FreeRaster

; Close the graphics library.

error_1	move.l	_GfxBase,a1
	CALLEXEC	CloseLibrary

; All done so return to system.

error	moveq.l	#0,d0	dont report any errors
	rts
	
; Program data defenition area. Note that all _SIZEOF equates
; are defined in the DevPacII include files.
	
	even
my_view	ds.b	v_SIZEOF
	even
my_viewport	ds.b	vp_SIZEOF
	even
my_rasinfo	ds.b	ri_SIZEOF
	even
my_bitmap	ds.b	bm_SIZEOF
	even
my_rastport	ds.b	rp_SIZEOF
	even
GFXName	dc.b	'graphics.library',0
	even

_GfxBase	dc.l	0
oldcopper	dc.l	0
bpl1	dc.l	0
bpl2	dc.l	0
	
my_colours	dc.w	$0,$FFF,$FB0,$6CE

msg	dc.b	'Hi World, nice to see you.'
msg_end	
	even
msg_len	equ	msg_end-msg
