/*
** OPENSCRN - open a screen and window, return window ptr
**
** Version 1.0 ©1990 by Edward Hutchins
** Authors:
**
**	Edward Hutchins: eah1@cec1.wustl.edu
**
** Revisions:
** 12/19/91 code released as freeware under the GNU general public license - Ed.
**
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation; either version 1, or (at your option)
**    any later version.
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include	<exec/types.h>
#include	<exec/memory.h>
#include	<intuition/intuition.h>
#include	<proto/intuition.h>

/*
** external stuff
*/

extern struct IntuitionBase *IntuitionBase;

/*
** default parameters
*/

#define DEFDEPTH		3
#define DEFWIDTH		320
#define DEFHEIGHT		32
#define DEFIDCMP		0
#define DEFFLAGS		(SMART_REFRESH|ACTIVATE)

static struct NewScreen new_screen =
{
	0, 0, DEFWIDTH, DEFHEIGHT + 1, DEFDEPTH,
	0, 0,
	NULL,
	CUSTOMSCREEN | SCREENQUIET | SCREENBEHIND,
	NULL, NULL
};

static struct NewWindow new_window =
{
	0, 1, DEFWIDTH, DEFHEIGHT,
	(1<<DEFDEPTH) - 1, 0,
	DEFIDCMP, DEFFLAGS,
	NULL, NULL,
	NULL,
	NULL, NULL,
	64, 40, 0, 0,
	CUSTOMSCREEN
};

/*
** OpenScrn - open a screen and window in intuition, return window ptr
*/

struct Window *OpenScrn( WORD w, WORD h, WORD d, ULONG IDCMP, ULONG Flags )
{
	struct Window		*window;

	/* Set up the graphics */

	if (w) new_screen.Width = new_window.Width = w;
	if (h) new_screen.Height = ((new_window.Height = h) + 1);
	if (d) new_screen.Depth = d, new_window.DetailPen = (1<<d) - 1;
	if (IDCMP) new_window.IDCMPFlags = IDCMP;
	if (Flags) new_window.Flags = Flags;

	new_window.Screen = OpenScreen( &new_screen );
	if (new_window.Screen == NULL) exit(20);

	window = OpenWindow( &new_window );
	if (window == NULL)
	{
		CloseScreen( new_window.Screen );
		exit(20);
	}

	return( window );
}

/*
** CloseScrn - close a screen that we opened, using a window ptr
*/

void CloseScrn( struct Window *window )
{
	struct Screen	*screen;

	/* extract the screen address from the window pointer */

	screen = window->WScreen;

	CloseWindow( window );
	CloseScreen( screen );
}
