
#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;  /* These are all pointers to system */
struct IntuitionBase *IntuitionBase;  /* These are all pointers to system */
struct GfxBase       *GfxBase;        /* data structures I need access to */

struct Screen        *screen;
struct Window        *window;

struct Clip *clip;
UBYTE  *ptr1, *ptr2;
long ptsize;


#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, 2, WIDTH, HEIGHT-2,
    (UBYTE) 0, (UBYTE) 15,
    MOUSEBUTTONS
       | INTUITICKS | CLOSEWINDOW,
    SMART_REFRESH
       | BACKDROP
       | ACTIVATE
       | NOCAREREFRESH
       | REPORTMOUSE
       | RMBTRAP | WINDOWCLOSE,
    NULL, NULL,
    "Black Belt Systems hame.library DEMO - Programming: Pete Patterson",
    NULL, NULL,
    0, 0, 0, 0,
    CUSTOMSCREEN
  };


UBYTE apat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE bpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,  1,  1}
  };

UBYTE cpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,  1}
  };

UBYTE dpat[8][8] = { 
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,  1,  1}
  };

UBYTE epat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1}
  };

UBYTE fpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1}
  };

UBYTE gpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,255,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,  1}
  };

UBYTE hpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE ipat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,255,255,255,  1,  1,  1}
  };

UBYTE jpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,  1,255,255,255,  1,  1},
 {  1,  1,  1,  1,255,  1,  1,  1},
 {  1,  1,  1,  1,255,  1,  1,  1},
 {  1,  1,  1,  1,255,  1,  1,  1},
 {  1,255,  1,  1,255,  1,  1,  1},
 {  1,255,  1,  1,255,  1,  1,  1},
 {  1,  1,255,255,  1,  1,  1,  1}
  };

UBYTE kpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,255,  1,  1},
 {  1,255,  1,  1,  1,255,  1,  1},
 {  1,255,  1,  1,255,  1,  1,  1},
 {  1,255,222,255,  1,  1,  1,  1},
 {  1,255,  1,  1,255,  1,  1,  1},
 {  1,255,  1,  1,  1,255,  1,  1},
 {  1,255,  1,  1,  1,255,  1,  1}
  };

UBYTE lpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1}
  };

UBYTE mpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,  1,  1,255,255,  1},
 {  1,255,255,255,255,255,255,  1},
 {  1,255,  1,255,255,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE npat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,  1,  1,  1,255,  1},
 {  1,255,255,255,  1,  1,255,  1},
 {  1,255,  1,255,255,  1,255,  1},
 {  1,255,  1,  1,255,255,255,  1},
 {  1,255,  1,  1,  1,255,255,  1}
  };

UBYTE opat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,  1}
  };

UBYTE ppat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,  1,  1}
  };

UBYTE qpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,255,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,255}
  };

UBYTE rpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE spat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,  1,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,  1,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,  1}
  };

UBYTE tpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1},
 {  1,  1,  1,255,  1,  1,  1,  1}
  };

UBYTE upat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,255,255,255,  1,  1}
  };

UBYTE vpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,  1,  1,255, 1,  1},
 {  1,  1,255,  1,  1,255,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1}
  };

UBYTE wpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,255,255,  1,255,  1},
 {  1,255,255,255,255,255,255,  1},
 {  1,255,255,  1,  1,255,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE xpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,  1,255,  1,  1,255,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1},
 {  1,  1,255,  1,  1,255,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1}
  };

UBYTE ypat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,  1,  1,  1,  1,255,  1},
 {  1,255,255,  1,  1,255,255,  1},
 {  1,  1,255,255,255,255,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1}
  };

UBYTE zpat[8][8] = {
 {  1,  1,  1,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,255,  1},
 {  1,  1,  1,  1,  1,255,255,  1},
 {  1,  1,  1,  1,255,255,  1,  1},
 {  1,  1,  1,255,255,  1,  1,  1},
 {  1,  1,255,255,  1,  1,  1,  1},
 {  1,255,255,  1,  1,  1,  1,  1},
 {  1,255,255,255,255,255,255,  1}
  };

UBYTE *letters[] = {
    &apat[0][0],
    &bpat[0][0],
    &cpat[0][0],
    &dpat[0][0],
    &epat[0][0],
    &fpat[0][0],
    &gpat[0][0],
    &hpat[0][0],
    &ipat[0][0],
    &jpat[0][0],
    &kpat[0][0],
    &lpat[0][0],
    &mpat[0][0],
    &npat[0][0],
    &opat[0][0],
    &ppat[0][0],
    &qpat[0][0],
    &rpat[0][0],
    &spat[0][0],
    &tpat[0][0],
    &upat[0][0],
    &vpat[0][0],
    &wpat[0][0],
    &xpat[0][0],
    &ypat[0][0],
    &zpat[0][0]
  };


/*
  Clean up and exit
*/
void CloseStuff()
  {
    struct Library *result;
      if (clip)               HAME_DisposeClip(clip);
      if (ptr1)               FreeMem(ptr1, ptsize);
      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);

      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, 1 );
  for ( i = 0; i < 200; i++ )
    {
      HAME_WritePixel( HamePort, 0, i);
    }
  }

/*
  Initialize the palette to a grey, a red, a green and a blue scale
  Note that the four groups are intermingled, and not separate groups
*/
void palette1()
  {
  int i, j, r;

  j = 1;
  for ( i = 0; i < 64; i++ )
    {
      r = i << 2;
      HAME_SetRGB8(HamePort, j++, r, r, r);
      HAME_SetRGB8(HamePort, j++, r, 0, 0);
      HAME_SetRGB8(HamePort, j++, 0, r, 0);
      HAME_SetRGB8(HamePort, j++, 0, 0, r);
    }
  }

void DoLetter()
 {
   int l, x, y, c,q,s,e,d;
   UBYTE *ptr;

   l = rand() % 26;
   x = rand() % 41;
   y = rand() % 25;
   c = rand() % 251;
   s = (rand() % 253)+2;
   e = (rand() % 253)+2;
   d = rand() % 255;

   ptr = letters[l];
   for (q = 0; q < 64; q++)
     {
       if (*(ptr+q) != 1)
         {
           *(ptr+q) = c + 4;
         }
     }
   HAME_8BitRender(HamePort, ptr, x << 3, y << 3, 8, 8);
   if ((d > 32) && (d < 64))
     {
       HAME_CycleLeft(HamePort, s, e);
     }
 }


/*
  Main routine
*/
int main(argc, argv)
  int argc;
  char *argv[];
  {
  ULONG  msg_class;
  USHORT msg_code;
  int    finished=0;
  struct IntuiMessage *message;
  struct Gadget *gadget;


  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();
    }

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

  /* Set the pallette */
  palette1();

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

  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:
              break;

              case INTUITICKS:
                HAME_SetRGB8(HamePort, (rand() % 251)+4, (rand() % 253)+2,
                                       (rand() % 255)+1, (rand() % 251)+4);
              break;

              case CLOSEWINDOW:
                finished = 1;
              break;

              default:
                printf("?");
            }
        }
      else
        {
          DoLetter();
        }
     }

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

  CloseStuff();
}

