	section	lard,code

	opt	d+

code:	IncDir	"DH0:Include/"
	Include	Intuition/Intuition.I
	Include	Intuition/Intuition_Lib.I
	Include	Libraries/dos_LIb.i
	Include	Libraries/Dos.i
	Include	Exec/Exec_Lib.I
	Include	Exec/Exec.I
	Include	Graphics/Graphics_Lib.i
	Include	Graphics/Gfx.i	
	include	Graphics/Display.i	
	include	Devices/Printer.i

	***************************************************

	Lea	DosName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l 	D0,_DOSBase
	Beq	error

	Lea	GFXName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l 	D0,_GFXBase
	Beq	error

	Lea	IntName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l	D0,_IntuitionBase
	Beq	error

	*-----------------------------------------------------------*

	; when ituition opens a screen it puts the screen name, gadgets
	; etc. on the top

	lea	picmem+256*40*0,a0
	lea	backup+20*40*0,a1
	bsr	copylines

	lea	picmem+256*40*1,a0
	lea	backup+20*40*1,a1
	bsr	copylines

	lea	picmem+256*40*2,a0
	lea	backup+20*40*2,a1
	bsr	copylines

	lea	picmem+256*40*3,a0
	lea	backup+20*40*3,a1
	bsr	copylines

	lea	picmem+256*40*4,a0
	lea	backup+20*40*4,a1
	bsr	copylines

	*-----------------------------------------------------------*
	
	Lea	MyScreen,a0
	CALLINT	OpenScreen
	Move.l	D0,_MyScrBase
	
	move.l	d0,a0
	lea	sc_ViewPort(a0),a0
	lea	picmem,a1

	move.w	#0,d0		; convert colourmap 
.cols	move.w	(a1),d1		; to GFX RGB values
	and.w	#$f00,d1
	lsr.w	#8,d1
	move.w	(a1),d2
	and.w	#$0f0,d2
	lsr.w	#4,d2
	move.w	(a1)+,d3
	and.w	#$00f,d3
	
	movem.l	d0-d7/a0-a6,-(a7)
	CALLGRAF	SetRGB4
	movem.l	(a7)+,d0-d7/a0-a6
	add.w	#1,d0
	cmp.w	#32,d0
	bne	.cols

	*-----------------------------------------------------------*

	lea	picmem+256*40*0,a1
	lea	backup+20*40*0,a0
	bsr	copylines

	lea	picmem+256*40*1,a1
	lea	backup+20*40*1,a0
	bsr	copylines

	lea	picmem+256*40*2,a1
	lea	backup+20*40*2,a0
	bsr	copylines

	lea	picmem+256*40*3,a1
	lea	backup+20*40*3,a0
	bsr	copylines

	lea	picmem+256*40*4,a1
	lea	backup+20*40*4,a0
	bsr	copylines

	*-----------------------------------------------------------*

	bsr	print_rp

.wait	btst	#6,$bfe001
	bne	.wait
	
CloseInt	Move.l	_MyScrBase,A0
	CALLINT	CloseScreen

	Move.l	_IntuitionBase,A1
	CALLEXEC	CloseLibrary

	Move.l	_GFXBase,A1
	CALLEXEC	CloseLibrary

	Move.l	_DOSBase,A1
	CALLEXEC	CloseLibrary

	*-------------------------------*

error	moveq	#0,D0
	rts

	*-------------------------------*
	
print_rp:	move.l	#0,a1
	CALLEXEC	FindTask

	lea	reply_port,a1
	move.l	d0,MP_SIGTASK(a1)
	CALLEXEC	AddPort
		
	lea	IO_port,a1
	move.l	#0,d0
	move.l	#0,d1
	lea	prt_dev,a0
	CALLEXEC	OpenDevice
	cmp.l	#0,d0
	bne	Error

	lea	IO_port,a1
	move.l	#reply_port,MN_REPLYPORT(a1)
	move.w	#PRD_DUMPRPORT,IO_COMMAND(a1)

	move.l	_IntuitionBase,a6
	move.l	_MyScrBase,a2
	
	lea	sc_RastPort(a2),a3
	move.l	a3,io_RastPort(a1)	
	
	lea	sc_ViewPort(a2),a3
	move.l	vp_ColorMap(a3),io_ColorMap(a1)
	
	lea	myscreen,a6
	move.w	ns_Width(a6),d0
	moveq	#0,d1
	move.w	ns_ViewModes(a6),d1
	
	move.l	d1,io_Modes(a1)
	move.w	#0,io_SrcX(a1)
	move.w	#0,io_SrcY(a1)
	
	move.w	d0,io_SrcWidth(a1)		; screen width
	move.w	#256,io_SrcHeight(a1)		; screen height
	move.l	#320*2,io_DestCols(a1)		; printer width
	move.l	#256,io_DestRows(a1)		; printer height

	move.l	#SPECIAL_FULLCOLS+SPECIAL_FULLROWS,io_Special(a1)
	CALLEXEC	DoIO

	lea	IO_port,a1
	move.b	#0,IO_ERROR(a1)

	; 0=no error
	; 1=Printing canceled
	; 2=Printer driver does not support graphics
	; 3=Printer driver obsolete
	; 4=Print dimensions are illegal
	; 5=No memory available for internal variables
	; 6=No memory available for print buffer
	; 7=Printer driver has taken control

.ok	lea	reply_port,a1
	CALLEXEC	RemPort
	
	lea	IO_port,a1
	CALLEXEC	CloseDevice

	rts

	*------------------------------------------------*

copylines:	move.w	#(10*20)-1,d0
.loop	move.l	(a0)+,(a1)+
	dbf	d0,.loop
	rts
	
	*------------------------------------------------*

_MyScrBase		Dc.l	0
_MyWinBase		Dc.l	0
_MyWinRPort		Dc.l	0
_MyWinUserPort	Dc.l	0
_MyWinVPort		Dc.l	0

_IntuitionBase	Dc.l	0
_DOSBase		Dc.l	0
_GFXBase		Dc.l	0

IO_port:		dcb.l	20,0
reply_port:		dcb.l	8,0

IntName		INTNAME
DosName		DOSNAME
GfxName		GRAFNAME
prt_dev:		dc.b	"printer.device",0
		even
		
**************************************************

MyScreen:	dc.w	0,0		;screen XY origin relative to View
	dc.w	320,256		;screen width and height
	dc.w	0		;screen depth (number of bitplanes)
	dc.b	0,0		;detail and block pens
	dc.w	0		;display modes for this screen
	dc.w	CUSTOMSCREEN+CUSTOMBITMAP+SCREENQUIET
				;screen type
	dc.l	0		;pointer to default screen font
	dc.l	0		;screen title
	dc.l	0		;first in list of custom screen gadgets
	dc.l	MyBitMap		;pointer to custom BitMap structure

MyBitMap:	dc.w	40		;bm_BytesPerRow (.w)
	dc.w	256		;bm_Rows (.w)
	dc.b	0		;bm_Flags (.b)
	dc.b	5		;bm_Depth (.b)
	dc.w	0		;bm_Pad (.w)
	dc.l	picmem+64+(0*40*256)	;bm_Planes (.l) 1
	dc.l	picmem+64+(1*40*256)	;bm_Planes (.l) 2
	dc.l	picmem+64+(2*40*256)	;bm_Planes (.l) 3
	dc.l	picmem+64+(3*40*256)	;bm_Planes (.l) 4
	dc.l	picmem+64+(4*40*256)	;bm_Planes (.l) 5
	dc.l	0		;bm_Planes (.l) 6
	dc.l	0		;bm_Planes (.l) 7
	dc.l	0		;bm_Planes (.l) 8

	section	ddp,code_c

picmem:	incbin	"dh0:data/pic.bin"
backup:	ds.l	10*20*6

	end


