
#include <intuition/intuition.h>
#include <exec/types.h>
#include <exec/execbase.h>
#include <graphics/text.h>

#include <proto/exec.h>
#include <proto/graphics.h>

#include "hame_pragmas.h"
#include "HameStruct.h"
#include "HameProtos.h"
#include "HameRev.h"

#include <string.h>
#include <exec/memory.h>
#include <exec/tasks.h>

#include <graphics/view.h>
#include <graphics/gfx.h>
#include <graphics/rastport.h>

#include <proto/intuition.h>


struct Library *HameBase;
struct HamePort *HamePort;

extern struct Screen *OpenScreen();
extern struct Window *OpenWindow();

extern struct ExecBase *SysBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;

struct Screen        *screen;
struct Window        *window;
struct HameFont *f1 = NULL;
struct HameFont *f2 = NULL;

#define WIDTH 640
#define HEIGHT 200

struct NewScreen my_screen =
  {
    0, 0, WIDTH, HEIGHT,
    4,
    0, 1,
    HIRES,
    CUSTOMSCREEN,
    (struct TextAttr *) NULL,
    NULL,
    NULL, NULL
  };

struct NewWindow my_window =
  {
    0, 0, WIDTH, HEIGHT,
    (UBYTE) 0, (UBYTE) 1,
    MOUSEBUTTONS
       | INTUITICKS,
    SMART_REFRESH
       | BORDERLESS
       | BACKDROP
       | ACTIVATE
       | NOCAREREFRESH
       | REPORTMOUSE,
    NULL, NULL,
    NULL,
    NULL, NULL,
    0, 0, 0, 0,
    CUSTOMSCREEN
  };


/*
  Clean up and exit
*/
void CloseStuff()
  {
    struct Library *result;
      if (f2)                HAME_CloseFont(f2);
      if (f1)                HAME_CloseFont(f1);
      if (window)             CloseWindow(window);
      if (screen)             CloseScreen(screen);
      if (HamePort)           HAME_Dispose(HamePort);
      if (HameBase)           CloseLibrary(HameBase);
      if (GfxBase)            CloseLibrary(GfxBase);
      if (IntuitionBase)      CloseLibrary(IntuitionBase);

/* This sequence expunges hame.library if it is no longer in use */
/* In normal use, this is not required */
      Forbid();
      if ( (result = (struct Library *) 
           FindName(&SysBase->LibList,"hame.library")) != NULL ) 
        {
          RemLibrary(result);
        }
      Permit();

      exit(0);
  }

/*
  Draw a vertical column of pixels at the left margin to prevent hame from
  turning off.
*/
void DrawLeftMargin()
  {
  int i;

  HAME_SetAPen( HamePort, 2 );
  for ( i = 0; i < 200; i++ )
    {
      HAME_WritePixel( HamePort, 0, i);
    }
  }

/*
  Initialize the pallette to random colors
*/
void palette1()
  {
  int i, r, g, b;

  for ( i = 1; i < 256; i++ )
    {
      r = rand() & 0xff;
      g = rand() & 0xff;
      b = rand() & 0xff;
      HAME_SetRGB8(HamePort, i, r, g, b);
    }

  /* faux noir */
  HAME_SetRGB8(HamePort, 2, 0, 0, 0);
  }

/*
  Main routine
*/
int main(argc, argv)
  int argc;
  char *argv[];
  {
  ULONG  msg_class;
  USHORT msg_code;
  int    fh, v, ww, i, finished=0;
  long l1, l2, l3, l4, l5, l6, bl1, bl2;
  struct IntuiMessage *message;
  struct Gadget *gadget;


  if (argc < 3)
    {
      printf("USAGE:\n  %s fontname.font size\n", argv[0] );
      exit(0);
    }

  if ((IntuitionBase = (struct IntuitionBase *)
       OpenLibrary("intuition.library",33L))==NULL)
    {
      CloseStuff();
    }

  if ((GfxBase = (struct GfxBase *)
       OpenLibrary("graphics.library" , 0L))==NULL)
    {
      CloseStuff();
    }

  /* Open the hame library */
  if ((HameBase = (struct Library *) 
       OpenLibrary("hame.library",HAMELIB_VERSION)) == NULL)
    {
      printf("No hame.library\n");
      CloseStuff();
    }


  /* Get a screen to play with */
  if ((screen = OpenScreen(&my_screen))     ==NULL)
    {
      CloseStuff();
    }

  my_window.Screen = screen;

  /* Get a window to play with */
  if ((window = OpenWindow(&my_window)) == NULL)
    {
      CloseStuff();
    }

  /* turn off the title bar */
  ShowTitle(screen, 0L);

  /* Tell the library where to put pixels */
  if ((HamePort = HAME_Init(screen, 640, 400, 400, 0, 10, 4, 186)) == NULL)
    {
      CloseStuff();
    }

  /* Set the pallette */
  palette1();

  /* Keep hame from turning off because of screen wide color 0 */
  DrawLeftMargin();

  fh = atoi(argv[2]);
  /* Open user font */
  f1 = HAME_OpenFont(argv[1], fh);
  if (!f1)
    {
      printf("No %s size %ld!\n", argv[1], fh);
      CloseStuff();
    }

  /* Open topaz 8 */
  f2 = HAME_OpenFont("topaz.font", 8);
  if (!f2)
    {
      printf("No topaz.font size 8!\n");
      CloseStuff();
    }

  /* Set replaceable pen to color 16 (black) */
  HAME_SetFontPen( f1, 16 );
  /* Read the font's palette into the HamePorts cookie at color 16 */
  HAME_GetFontPallete( HamePort, f1 );

  HAME_SetFontPen( f1, 32 );
  /* Read the font's palette into the HamePorts cookie at color 32 */
  HAME_GetFontPallete( HamePort, f1 );

  HAME_SetFontPen( f1, 48 );
  /* Read the font's palette into the HamePorts cookie at color 48 */
  HAME_GetFontPallete( HamePort, f1 );

  HAME_SetFontPen( f1, 64 );
  /* Read the font's palette into the HamePorts cookie at color 64 */
  HAME_GetFontPallete( HamePort, f1 );

  /* Modify this fonts palette for an alternate appearance at color 32*/
  for (i = 32; i < (32 + f1->NColors); i++)
    {
      HAME_GetRGB8(HamePort, i);
      HAME_SetRGB8(HamePort, i, HamePort->b, HamePort->g, HamePort->r );
    }
 
  /* Modify this fonts palette for an alternate appearance at color 48*/
  for (i = 48; i < (48 + f1->NColors); i++)
    {
      HAME_GetRGB8(HamePort, i);
      HAME_SetRGB8(HamePort, i, HamePort->g, HamePort->b, HamePort->r );
    }
 
  /* Modify this fonts palette for an alternate appearance at color 64*/
  for (i = 64; i < (64 + f1->NColors); i++)
    {
      HAME_GetRGB8(HamePort, i);
      HAME_SetRGB8(HamePort, i, HamePort->r, HamePort->g, HamePort->b );
    }
 
  /* Now set the fonts original palette to a grey scale */
  for (i = 16; i < (16 + f1->NColors); i++)
    {
      HAME_GetRGB8(HamePort, i);
      HAME_SetRGB8(HamePort, i, HamePort->r, HamePort->r, HamePort->r );
    }
 
  /* Prepare this font's quick tables for colorbase of 16 */
  HAME_MakeFTables(f1, 16);

  /* See above for description */
  HAME_SetFontPen( f2, 1 );
  HAME_GetFontPallete( HamePort, f2 );
  HAME_MakeFTables(f2, 0);

  /* Render using quick font method */
  l1 = HAME_QText(HamePort, f1, "HAME.LIBRARY", 16, 35 );
  l2 = HAME_QText(HamePort, f2, "(c) 1991", 5, 55 );
  l3 = HAME_QText(HamePort, f1, "BLACK", 19, 95 );
  l4 = HAME_QText(HamePort, f1, "BELT", 23, 125 );
  l5 = HAME_QText(HamePort, f1, "SYSTEMS", 27, 155 );
  l6 = HAME_QText(HamePort, f2, "programming : Pete Patterson", 2, 175 );

  /* Cycle the grey scale */
  for ( i = 0; i < 150; i++ )
    {
      HAME_CycleLeft(HamePort, 17, 31 );
    }

/* This simply tests font clipping - uncomment to check it out
*/
/*
  for ( i = 0; i < 75; i++ )
    {
      ww = HAME_QText(HamePort, f1, "HAME", -30-i, 25-i );
      ww = HAME_QText(HamePort, f1, "HAME", -30-i, 175+i );
      ww = HAME_QText(HamePort, f1, "HAME", 290+i, 25-i );
      ww = HAME_QText(HamePort, f1, "HAME", 290+i, 175+i );
    }

  Delay(250);
*/

  HAME_SetAPen( HamePort, 0 );

  /* Render using slow font method */
  /* Use all four font palletes */
  /* Erase before drawing if font is less than thirty points */

  bl1 = f1->BaseLine;
  bl2 = f2->BaseLine;

  HAME_SetFontPen( f1, 16 );
  HAME_Box(HamePort, 16, 35-bl1, 16+l1, 35-bl1+fh, 1 );
  ww = HAME_Text(HamePort, f1, "HAME.LIBRARY", 16, 35 );

  HAME_SetFontPen( f2, 1 );
  HAME_Box(HamePort, 5, 55-bl2, 5+l2, 63-bl2, 1 );
  ww = HAME_Text(HamePort, f2, "(c) 1991", 5, 55 );

  HAME_SetFontPen( f1, 32 );
  HAME_Box(HamePort, 19, 95-bl1, 19+l3, 95-bl1+fh, 1 );
  ww = HAME_Text(HamePort, f1, "BLACK", 19, 95 );

  HAME_SetFontPen( f1, 48 );
  HAME_Box(HamePort, 23, 125-bl1, 23+l4, 125-bl1+fh, 1 );
  ww = HAME_Text(HamePort, f1, "BELT", 23, 125 );

  HAME_SetFontPen( f1, 64 );
  HAME_Box(HamePort, 27, 155-bl1, 27+l5, 155-bl1+fh, 1 );
  ww = HAME_Text(HamePort, f1, "SYSTEMS", 27, 155 );

  HAME_SetFontPen( f2, 3 );
  HAME_Box(HamePort, 2, 175-bl2, 2+l6, 183-bl2, 1 );
  ww = HAME_Text(HamePort, f2, "programming : Pete Patterson", 2, 175 );

  /* Cycle all four palettes */
  for ( i = 0; i < 150; i++ )
    {
      HAME_CycleLeft(HamePort, 17, 31 );
      HAME_CycleRight(HamePort, 33, 47 );
      HAME_CycleRight(HamePort, 49, 63 );
      HAME_CycleRight(HamePort, 65, 79 );
    }

  v = 0;
  /* Wait for mouse... GLOW color 1 & 3 */
  while( !finished )
    {
      message = (struct IntuiMessage *) GetMsg(window->UserPort);
      if (message)
        {
          msg_class = message->Class;
          msg_code = message->Code;
          gadget = (struct Gadget *) message->IAddress;
          ReplyMsg((struct Message *)message);

          switch( msg_class )
            {
              case MOUSEBUTTONS:
                finished = 1;
              break;

              case MENUPICK:
              break;

              case GADGETUP:
              break;

              case INTUITICKS:
              break;

              case CLOSEWINDOW:
                finished = 1;
              break;

              default:
                printf("?");
            }
        }
      else
        {
          /* Make the single color text glow */
          v++;
          v %= 256;
          HAME_SetRGB8(HamePort, 1, v, v, v);
          HAME_SetRGB8(HamePort, 3, v, v, v);
        }
     }

  while ((message = (struct IntuiMessage *)
          GetMsg( window->UserPort )) != NULL)
    {
      ReplyMsg((struct Message *)message);
    }

  CloseStuff();
}
