/* Window.c
   Copyright (c) 1990,1991,1992 by Thomas E. Janzen
   All Rights Reserved

   THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
   BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
   CHANGES FOR FREE DISTRIBUTION.  NO TITLE TO AND OWNERSHIP OF THE
   SOFTWARE IS HEREBY TRANSFERRED.  THOMAS E. JANZEN ASSUMES NO 
   RESPONSBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
   
   Thomas E. Janzen
   58A School St. Apt. 2-L
   Hudson, MA 01749
   (508)562-1295
*/
/*
**  FACILITY:
**
**	AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
**	compiled with SAS/C V5.10b
**
**  ABSTRACT:
**
**	Window.c Opens and manages the Workbench-type window used by
**	AlgoRhythms.
**
**  AUTHORS: Thomas E. Janzen
**
**  CREATION DATE:	26-MAR-1990
**
**  MODIFICATION HISTORY:
**    DATE	NAME	DESCRIPTION
**    24 SEP 91 T. Janzen use algorhythms.col to specify screen colors
**    8 DEC 91 T. Janzen conform to SAS/C 5.10b remove extern from functs
**  4 Jan 92 TEJ  last changes for 2.0
**--
*/
#include <stdio.h>
#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <intuition/intuition.h>
#include <graphics/text.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/mathffp.h>
#include <math.h>
#include <stdlib.h>
#include <graphics/display.h>

extern void OpenWind (void);
extern void OpenLibs (void);
extern void CloseLibs (void);
extern void MakeWindow (void);

/* Library base addresses */
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct DOSBase *DOSBase;
struct MathBase *MathBase;
struct ReqBase *ReqBase;

ULONG class,classb;	/* Intu Event class */
USHORT code,codeb;	/* Intu Event code */

/*WINDOW*/
static struct Screen *Screen_1;
struct Window *w;
struct RastPort *rp;
static struct ViewPort *vp;
struct IntuiMessage *message;
struct TextAttr font_choice = {(STRPTR)"topaz.font", TOPAZ_EIGHTY, 0, 0};

void MakeWindow (void) 
{
	OpenLibs ();
	OpenWind ();
   return;
}

void OpenWind (void) 
{
   FILE *Color_File;
   static char Screen_Title[] = 
   "AlgoRhythms ©1992 Thomas E. Janzen All Rights Reserved";
   struct NewScreen ns = {
      0, 0, 640, 200, 3 /*depth*/, 1 /* DetailPen*/, 2 /* BlockPen */,
      HIRES, CUSTOMSCREEN, &font_choice, Screen_Title, NULL, NULL};
   auto long int Reds[8]   = {0x4, 0xF, 0xC, 0x7, 0x8, 0xF, 0x0, 0xF};
   auto long int Greens[8] = {0x0, 0xF, 0x0, 0x9, 0xF, 0xF, 0x0, 0x0};
   auto long int Blues[8]  = {0x4, 0xF, 0x0, 0xF, 0x0, 0xF, 0x0, 0xF};
   auto long int Color_Index;
   
	struct NewWindow nw = {
	   0, 10, 640, 190,	   /* Left, Top, Width, height */
	   2, 1, 		         /* detail, block pens */
	   REQSET|NEWSIZE|CLOSEWINDOW|MENUPICK|GADGETUP,	/* IDCMP flags */
	   WINDOWSIZING|SMART_REFRESH|WINDOWDEPTH|WINDOWDRAG|
	   WINDOWCLOSE|GIMMEZEROZERO|ACTIVATE,
	   NULL,			         /* Pointer to first gadget */
	   NULL,			         /* Pointer to checkmark */
	   "Random Form",		   /* title */
	   NULL,			         /* screen pointer */
	   NULL,			         /* bitmap pointer */
	   400, 190, 640, 190,	/* window not sized */
	   CUSTOMSCREEN		   /* type of screen */
   };
   Color_File = fopen ("AlgoRhythms.Col", "r");
   if (Color_File != NULL)
   {
      fscanf (Color_File, "%d %d %d %d %d %d %d %d", &Reds[0], &Reds[1], 
         &Reds[2], &Reds[3], &Reds[4], &Reds[5], &Reds[6], &Reds[7]);
      fscanf (Color_File, "%d %d %d %d %d %d %d %d", &Greens[0], 
         &Greens[1], &Greens[2], &Greens[3], &Greens[4], &Greens[5], 
         &Greens[6], &Greens[7]);
      fscanf (Color_File, "%d %d %d %d %d %d %d %d", &Blues[0], &Blues[1], 
         &Blues[2], &Blues[3], &Blues[4], &Blues[5], &Blues[6], &Blues[7]);
      fclose (Color_File);
   }
   Screen_1 = OpenScreen (&ns);
   for (Color_Index = 0; Color_Index < 8; Color_Index++)
   {
      SetRGB4 (&Screen_1->ViewPort, 
               Color_Index, 
               Reds[Color_Index],                
               Greens[Color_Index],
               Blues[Color_Index]);
   }
   nw.Screen = Screen_1;
	w = OpenWindow (&nw);
	rp = w->RPort;			/* Get the raster port pointer */
	vp = &w->WScreen->ViewPort;	/* Get the view port pointer */
   return;
}

void OpenLibs(void) 
{
	if (!(IntuitionBase =
	(struct IntuitionBase *)OpenLibrary ("intuition.library", 0L)))
   {
		CloseLibs ();
		exit (FALSE);
	}
	if (!(GfxBase = 
	(struct GfxBase *)OpenLibrary ("graphics.library",0L)))
   {
		CloseLibs ();
		exit(FALSE);
	}
	if ( !(MathBase = 
	(struct MathBase *)OpenLibrary ("mathffp.library",0L)))
   {
		CloseLibs ();
		exit (FALSE);
	}
	if (!(DOSBase = 
	(struct DOSBase *)OpenLibrary ("dos.library",0L)))
   {
		CloseLibs ();
		exit (FALSE);
	}
	if (!(ReqBase = 
	(struct ReqBase *)OpenLibrary ("req.library",0L)))
   {
		ReqBase = NULL;
	}
   return;
}

void CloseLibs(void) 
{
	if (GfxBase) CloseLibrary (GfxBase);
	if (IntuitionBase) CloseLibrary (IntuitionBase);
	if (MathBase) CloseLibrary (MathBase);
  	if (DOSBase) CloseLibrary (DOSBase);
  	if (ReqBase) CloseLibrary (ReqBase);
   return;
}

void ShutWindow(void) 
{
	CloseLibs ();
	CloseWindow (w);
   CloseScreen (Screen_1);
   OpenWorkBench ();
   return;
}
