/*==========================================================================*
 * init.c - Initialization code for Discovery                               *
 * Copyright (c) 1985 by David Joiner (Talin)                               *
 *==========================================================================*/

#include "welcome.h"

BOOL init_gameport(void);
void close_all(int errnum);
int unpackbrush(char *name,struct BitMap *bm,short x,short y,UWORD *colors);

extern int				error;
extern struct Process	*myprocess;
extern struct WBStartup *WBMsg;
extern LOCK				olddir;
extern struct GfxBase	*GfxBase;
extern struct IntuitionBase *IntuitionBase;
extern struct Library	*TranslatorBase;
extern struct View		v_null,
						*oldview;
extern struct FileInfoBlock	*fileinfo;
extern WORD				xoffset,
						yoffset;
extern char 			outputstring[];
extern struct TextFont	*TopazFont;
extern APTR				oldWindowPtr;
extern UWORD			*mtextData;

extern struct TextAttr topaz_8;

#define MIN_VIEW_DX			128
#define PAGE_HEIGHT			108

#ifdef DEBUG

extern BYTE no_questions,no_music;

#define PLAY_MUSIC	(!no_music)

#else

#define PLAY_MUSIC	TRUE

#endif

int init_all(long alen, char *aptr)
{	struct Screen screen;
	struct MonitorInfo mon;
	struct DimensionInfo dim;
	WORD	copied = 0;
	int		i,argc;
	char	*text;
	struct	BitMap bm;

	myprocess = (struct Process *)FindTask(NULL);

	if (myprocess->pr_CLI)
	{
		argc = (alen ? 2 : 1);
	}
	else
	{	argc = 0;
		WaitPort(&myprocess->pr_MsgPort);
		WBMsg = (struct WBStartup *)GetMsg(&myprocess->pr_MsgPort);
		olddir = CurrentDir((LOCK)WBMsg->sm_ArgList->wa_Lock);
	}

#if 0
	if (argc > 1)
	{	text = aptr;
		i = alen;
		while (i && *text == ' ') { text++; i--; }
		while (i--)
		{	if (*text == 'm') no_music = 1;
			if (*text == 'd') no_questions = 1;
			text++;
		}
	}
#endif

	error = 0;
	screen.RastPort.Font = NULL;

	if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
		close_all(DERR_NO_LIBRARY);
	oldview = GfxBase->ActiView;

	if (!(IntuitionBase =
		(struct IntuitionBase *)OpenLibrary("intuition.library",0)))
			close_all(DERR_NO_LIBRARY);

	xoffset = oldview->DxOffset;
	yoffset = oldview->DyOffset;

		/* figure out how to center game on user's screen */

#if 0
	if (GetScreenData((char *)&screen,sizeof screen,WBENCHSCREEN,NULL))
	{
		/*	We will only shift things around if the display appears
			"normal"; i.e. there is no "vscreen", etc., lurking around. */	

		if (screen.ViewPort.DWidth >= screen.Width &&
			screen.ViewPort.DHeight >= screen.Height)
		{
			if (screen.ViewPort.Modes & HIRES)
				xoffset += (screen.Width - 640) / 4;
			else xoffset += (screen.Width - 320) / 2;

			if (screen.ViewPort.Modes & LACE)
				yoffset += (screen.Height - 400) / 4;
			else yoffset += (screen.Height - 200) / 2;
		}
	}
#endif

	if (xoffset < MIN_VIEW_DX) xoffset = MIN_VIEW_DX;

	FreeSprite(0);
	FreeSprite(1);		/* KS2.0 attached sprite handled */

	if (!InitKeyConvert()) close_all(DERR_NO_DEVICE);
	if (!init_gameport()) close_all(DERR_NO_DEVICE);
	if (!add_device()) close_all(DERR_NO_DEVICE);
	if (!add_interrupt()) close_all(DERR_NO_INTERRUPT);

	if (PLAY_MUSIC && InitMusic() == 0) close_all(DERR_MUSIC_ON);

	patch_autorequest();

	if ( (fileinfo = AllocMem(sizeof *fileinfo,MEMF_PUBLIC)) == NULL)
		close_all(DERR_NO_MEMORY);

	if (!init_ucop()) close_all(DERR_NO_MEMORY);

	if (add_null()) close_all(DERR_NO_MEMORY);
	loadview(&v_null);

	return argc;
}
