/* ------------------------------------------------------------------------- */
/* ----- Let 'em Fly!   V 1.2 ---------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* -------------------------------------- (c) 1991-93 by Oliver Scheel ----- */
/* --------------------------------------------- Freaky Deaky Software ----- */
/* ------------------------------------------------------------------------- */
/* ----- module: letemfly.c  init/exit routines ---------------------------- */
/* ------------------------------------------------------------------------- */

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <macros.h>
#include <tos.h>
#include <smallaes.h>
#include <vdi.h>
#include <portab.h>

#include <mxmalloc.h>
#include "ltmf_str.h"
#include "ltmf_def.h"
#ifdef LIGHT
	#include "let_lght.rsh"
	#include "let_lght.rh"
#else
	#ifdef ETV_CRITIC
		#include "let_crit.rsh"
		#include "let_crit.rh"
	#else
		#include "letemfly.rsh"
		#include "letemfly.rh"
	#endif
#endif

/* ------------------------------------------------------------------------- */

EXTERN	WORD	undo_obj;
EXTERN	WORD	help_obj;
EXTERN	WORD	*al_icon[MAXICON];

EXTERN	VSCRINF	*vscr;

EXTERN	CHAR	history[MAXHIST][HISTLEN+1];

/* ------------------------------------------------------------------------- */

LONG *get_cookie	_((LONG cookie));

/* ------------------------------------------------------------------------- */

DIALSAV	dsave[MAXDIAL];	/* saved dial data	*/
WORD	dialno;		/* dialog number	*/
OBJECT	*lasttree;	/* last tree (objc_draw)*/

WORD	initflag = FALSE,
	isalert = FALSE;
WORD	magic = FALSE;

#ifdef LIGHT
OBJECT	*info_dial;
#else
OBJECT	*alert_dial,
	*alert_icons,
	*char_dial,
	*info_dial;
#endif

WORD	errno;

/* ------------------------------------------------------------------------- */

CHAR	*alias = { "ToOmaS" };

/* ------------------------------------------------------------------------- */

#ifdef LIGHT
CHAR	*vers = { "@(#)Let 'em Fly! V1.20  light "__DATE__ };
#else
CHAR	*vers = { "@(#)Let 'em Fly! V1.20        "__DATE__ };
#endif
CHAR	*cpyright = { "½ 1991-93 by Oliver Scheel" };

/* ------------------------------------------------------------------------- */
/* ----- program control --------------------------------------------------- */
/* ------------------------------------------------------------------------- */

/* ----- init program variables -------------------------------------------- */

WORD ltmf_init()
{
	REG	WORD	i;

	initflag = TRUE;
	for(i = 0; i < MAXHIST; i++)
		history[i][0] = '\0';
	for(i = 0; i < MAXDIAL; i++)
		dsave[i].mem.fd_addr = NULL;
	dialno = -1;
	for(i = 0; i < NUM_OBS; i++)
		rsrc_obfix(rs_object, i);
#ifdef LIGHT
	info_dial = rs_trindex[INFOPAGE];
#else
	alert_dial = rs_trindex[ALRTPAGE];
	info_dial = rs_trindex[INFOPAGE];
	char_dial = rs_trindex[CHARPAGE];
	alert_icons = rs_trindex[ICONPAGE];
	al_icon[0] = alert_dial[ALICON].ob_spec.bitblk; /* ->bi_pdata; */
	for(i = 1; i < MAXICON; i++)
		al_icon[i] = alert_icons[ICON1-1+i].ob_spec.bitblk; /* ->bi_pdata; */
#endif
	info_dial[INFOVERS].ob_spec.tedinfo->te_ptext = &vers[4];
	info_dial[INFOCOPY].ob_spec.tedinfo->te_ptext = cpyright;
	info_dial[INFOLINE].ob_height = 1;
	char_dial[CHARLINE].ob_height = 1;
	alert_dial[ALICON].ob_height = 32;
	vscr = (VSCRINF *)get_cookie('VSCR');
	return(1);
}

/* ----- new appl_exit() (free lost memory) -------------------------------- */

WORD nap_exit()
{
	REG	WORD	i;

	for(i = dialno; i >= 0; i--)
	{
		if(dsave[i].mem.fd_addr != NULL)
			Mfree(dsave[i].mem.fd_addr);
		dsave[i].mem.fd_addr = NULL;
	}
	dialno = -1;
	return(appl_exit());
}

/* ----- load new icons ---------------------------------------------------- */

#ifndef LIGHT

WORD load_icons()
{
	WORD	h, i,
		ret;
/*	RSHDR	*buf;
*/	WORD	*buf;
	WORD	*bp1;
	BITBLK	*bp2;

	ret = FALSE;
	buf = (WORD *)MxMalloc(1024l);
	if(buf)
	{
		h = (WORD)Fopen("LET_ICON.RSC", FO_READ);
		if(h == -33)
			h = (WORD)Fopen("\\AUTO\\LET_ICON.RSC", FO_READ);
		if(h > 0)
		{
			Fread(h, 1024l, buf);
			Fclose(h);
/*			bp1 = (WORD *)(buf + buf->rsh_imdata);
			bp2 = (BITBLK *)(buf + buf->rsh_bitblk);
			buf->rsh_nbb = min(buf->rsh_nbb, MAXICON);
*/			bp1 = buf + buf[7]/2;
			bp2 = (BITBLK *)(buf + buf[4]/2);
			buf[14] = min(buf[14], MAXICON);
			for(i = 0; i < buf[14]; i++)
/*			for(i = 0; i < buf->rsh_nbb; i++)
*/			{
				rs_bitblk[1+i].bi_color = bp2[i].bi_color;
				memcpy(rs_bitblk[1+i].bi_pdata, &bp1[i*64], 128l);
			}
			ret = TRUE;
		}
		Mfree(buf);
	}
	return(ret);
}

#endif
