/*
 * This is the "startup" code for many (all?) of the savers. It gets included
 * into them in the appropriate place, mostly because I got tired of tweaking
 * _all_ the savers when I made a change in this code...
 *
 * Copyright 1991, Mike Meyer
 * All Rights Reserved
 *
 * See the file "ShadowMaster:Distribution" for information on distribution.
 */

static USHORT __chip SpriteData[] = {
	0,0,
	0,0,
	0,0
	} ;
#include <string.h>
/*
 * This is the starting point. It sets up the world, and calls your
 * graphics routine (dographics()), then cleans up after itself when you
 * return. Nothing in here should need changing.
 */
#define done(x)	do { status = x; goto out; } while (0) ;
int __saveds
start(void) {
	int status = RETURN_OK, old_prio ;
#ifndef	NORAND
	struct DateStamp ds ;
#endif

	SysBase = *((struct ExecBase **) 4);
	if ((DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37)) == NULL)
		done(RETURN_FAIL) ;
	if ((IntuitionBase = (struct IntuitionBase *)
	    OpenLibrary("intuition.library", 37)) == NULL)
		done(RETURN_FAIL) ;
	if ((GfxBase = (struct GfxBase *)
	    OpenLibrary("graphics.library", 37)) == NULL)
		done(RETURN_FAIL) ;

	if ((screen = OpenScreenTagList(NULL, ScreenTags)) == NULL)
		done(RETURN_FAIL) ;
	WindowTags[0].ti_Data = (ULONG) screen ;

	/* Window is to clear the sprite */
	if ((window = OpenWindowTagList(NULL, WindowTags)) == NULL)
		done(RETURN_FAIL) ;
	SetPointer(window, SpriteData, 1, 1, 0, 0);

	/* Draw our name into the window, just in case... */
	Move(window->RPort, 
		(window->Width - TextLength(window->RPort, Title, strlen(Title))) / 2,
		screen->Height / 2) ;
	Text(window->RPort, Title, strlen(Title)) ;

#ifndef	NORAND
	/* Seed the random number generator before we drop task priority */
	DateStamp(&ds) ;
	srand(ds.ds_Tick) ;
#endif
	/* One last check before drop our priority through the ground */
	if (CheckSignal(SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) done(RETURN_OK) ;
	old_prio = SetTaskPri(FindTask(0L), -100L) ;
	
	Delay(50) ;
	SetRast(&screen->RastPort, 0) ;
	dographics() ;

	/* Here's how we exit this mess */
	SetTaskPri(FindTask(0L), old_prio) ;
out:
	if (window) {
		ClearPointer(window) ;
		CloseWindow(window) ;
		}
	if (screen) CloseScreen(screen) ;
	if (GfxBase)  CloseLibrary((struct Library *) GfxBase) ;
	if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase) ;
	if (DOSBase) CloseLibrary((struct Library *) DOSBase) ;
	return status ;
	}

/* Make sure we don't screw up any following code... */
#undef done
