***********************************************************
* $VER: PalScreen 1.2 (01.05.93)
*
* © 1993 Dancing Fool of Epsilon.
*
* This simple little program will open a screen in a way that is
* compatable with both 1.3 and 2.0+.  It will attempt to open the
* screen in PAL, if it can.  It does this using the ens_Extension
* field of the ExtNewScreen structure.  I wrote it because I have
* seen TOO MANY programs that require 2.0+, open a screen that
* is 256 lines, but open it with the default monitor.  This is
* VERY annoying!  It is also very easy to fix. :)
*
* If it can not open a PAL screen, it opens in the default mode,
* but it opens an AUTOSCROLL screen.  Either way, no data is lost
* off the bottom!
*
* You can contact me at the following INet address:
*	idr@rigel.cs.pdx.edu
*
* You can contact Epsilon at the following INet address:
*	Epsilon@matrix.rain.com
*
***********************************************************

		incdir	'Devel:include/'

		include	'exec/exec.i'
		include	'exec/exec_lib.i'

		include	'intuition/intuition_lib.i'
		include	'intuition/intuition.i'
		include	'intuition/screens.i'

		include	'graphics/gfx.i'
		include	'graphics/graphics_lib.i'
		include	'graphics/displayinfo.i'

		include	'libraries/dos_lib.i'
		include	'libraries/dos.i'

		include	'utility/tagitem.i'
	
***********************************************************

NULL		equ	0

CALL		macro
		jsr	_LVO\1(a6)
		endm

OPENLIB		MACRO
		lea	\1,a1		; library name
		move.l	#\2,d0		; lib version
		CALL	OpenLibrary
		ENDM

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

_main:		bsr.b	OpenAll			; open everything!
		tst.l	d0			; errors?
		bne.b	.closedown		; yup..

		move.l	#500,d1			; ticks to wait...
		CALLDOS	Delay			; wait for a bit.

.closedown:	bsr.w	CloseAll		; close everything!
		rts

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

OpenAll:	movea.l	4.w,a6			; exec base

		OPENLIB	IntuiName,34		; open it
		move.l	d0,_IntuitionBase	; did it open?
		beq.b	.errexit		; nope.

		OPENLIB	DOSName,34		; open it
		move.l	d0,_DOSBase		; did it open?
		beq.b	.errexit		; thppppt!

		lea	MyNewScreen,a0		; my ExtNewScreen struct
		CALLINT	OpenScreen		; open it.
		move.l	d0,MyScreen		; did it open?
		bne.b	.openwin		; yippie!

	; If it didn't open, it may be because they can't do PAL, so
	; we try it again but with NTSC.
		tst.l	ErrorCode		; was it a v36+ error?
		beq.b	.errexit		; no, dump out

		lea	MyNewScreen,a0		; my screen
		move.l	#NtscTagList,ens_Extension(a0)	; the tag list ptr.
		CALL	OpenScreen		; open it.
		move.l	d0,MyScreen		; did it open?
		beq.w	.errexit		; blurg!
		
.openwin:	lea	MyNewWindow,a0
		move.l	MyScreen,nw_Screen(a0)	; ptr to its screen
		CALL	OpenWindow
		move.l	d0,MyWindow
		beq.b	.errexit

.exit:		moveq	#0,d0			; return no error
		rts

.errexit:	moveq	#-1,d0			; return error code
		rts

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

CloseAll:	tst.l	_IntuitionBase		; did intui open?
		beq.b	.noint			; nope.

		tst.l	MyWindow		; did my window open?
		beq.b	.nowin			; nope.
		move.l	MyWindow,a0		; addr. of window
		CALLINT	CloseWindow		; close it.

.nowin:		tst.l	MyScreen		; did my screen open?
		beq.b	.noscr			; nope.
		move.l	MyScreen,a0		; addr. of screen
		CALLINT	CloseScreen		; close it.

.noscr:		movea.l	a6,a1			; addr. of intui base
		CALLEXEC	CloseLibrary	; close it

.noint:		tst.l	_DOSBase		; did dos open?
		beq.b	.nodos			; nope.
		movea.l	_DOSBase,a1		; addr. of dos base
		CALLEXEC	CloseLibrary

.nodos:		rts

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

		section	Screens,data

MyScreenTitle:	dc.b	'This is PAL if it can!',0

IntuiName:	INTNAME
DOSName:	DOSNAME
		even

MyNewScreen:	dc.w	0,0,640,256,2	; screen dimensions
		dc.b	1,2		; pens
		dc.w	HIRES_KEY
		dc.w	NS_EXTENDED!CUSTOMSCREEN	; custom screen
		dc.l	NULL
		dc.l	MyScreenTitle
		dc.l	NULL,NULL
		dc.l	PalTagList

PalTagList:	dc.l	SA_DisplayID,HIRES_KEY!PAL_MONITOR_ID
		dc.l	SA_ErrorCode,ErrorCode
		dc.l	TAG_DONE

NtscTagList:	dc.l	SA_DisplayID,HIRES_KEY
		dc.l	SA_AutoScroll,DOSTRUE
		dc.l	SA_ErrorCode,ErrorCode
		dc.l	SA_DClip,MyClipRect
		dc.l	TAG_DONE

MyNewWindow:	dc.w	0,0,200,220	; initial window size
		dc.b	1,2		; pens
		dc.l	0		; idcmp flags
		dc.l	WFLG_SIZEGADGET!WFLG_DRAGBAR	; window flags
		dc.l	NULL,NULL,NULL,NULL,NULL	; misc. pointers
		dc.w	20,20,-1,-1	; max and min size
		dc.w	CUSTOMSCREEN	; window (screen) type

MyClipRect:	dc.w	0,0,640,200	; bounds of screen rectangle.

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

		section	pointers,data

_IntuitionBase:	dc.l	0
_DOSBase:	dc.l	0
MyScreen:	dc.l	0
MyWindow:	dc.l	0
ErrorCode:	dc.l	0
		END
