/*
 * ColorTest.c  -- compile with "dcc Colortest.c -o ColorTest"  
 *							
 *   A totally hacked up version of an old, old program I found
 *   somewhere!  I just needed something quick and dirty that opened
 *   up a window so I could draw some colors in it....
 */


#include "exec/types.h"
#include "intuition/intuition.h"
#include "graphics/display.h"
#include "graphics/gfxmacros.h"

struct IntuitionBase *IntuitionBase;
struct Window *Wind;
struct Screen *Scrn;
struct ViewPort *vpt;
struct RastPort *rp;

struct IntuiMessage *mes;

#define INTUITION_REV 29

#define S_HEIGHT 400
#define S_WIDTH 640
#define S_MODE HIRES|INTERLACE

#define DEPTH 3

#define NUMCOLORS (1 << DEPTH)


UWORD colortable[] =
        {
        0x0111, 0x0f33, 0x0555, 0x0777, 0x0999, 0x0bbb,
        0x0ddd, 0x0fff, 0x0000, 0x0f11, 0x0f33,
        0x0f55, 0x0f77, 0x0f99, 0x0fbb, 0x0fff
        };
LONG GfxBase;

void main(void)
{
  ULONG flags,iflags,class;
  SHORT x,y,w,h,d,mx,my,boxw,border;
  USHORT code,mode,i;
  UBYTE *title,c0,c1;
  VOID delay_func(),OpenAll(),CreateMes();
  APTR Iadr;


  
  OpenAll();

 
/*  ===== Open a hi-res custom screen */

 title="Colorsaver Test Screen ";
 y=0;
 w=S_WIDTH;
 h=S_HEIGHT;
 d=DEPTH;
 c0=0;
 c1=12;
 mode=S_MODE;  /*HIRES|INTERLACE;*/


 Scrn=(struct Screen *)make_screen(y,w,h,d,c0,c1,mode,title);

/*  ===== Open a plain window ===== */

 x=y=0;
 w=S_WIDTH;
 h=S_HEIGHT;
 flags=ACTIVATE|WINDOWDRAG|WINDOWCLOSE;
 iflags=CLOSEWINDOW;
 c0=6;
 c1=7;
 Wind=(struct Window *)
          make_window(x,y,w,h,"ColorSaver Test",flags,iflags,c0,c1,Scrn);
 if(Wind) rp = Wind->RPort;

 vpt =&(Scrn->ViewPort);

 LoadRGB4(vpt,&colortable[0],NUMCOLORS);

 border  = 10;
 boxw = (640-(border*2))/NUMCOLORS;

 for (i = 0; i< NUMCOLORS; i++)
 {
  SetAPen(rp,i);
  RectFill(rp,(i*boxw)+border,20,(i*boxw)+boxw+border,380);
 }

/* ===== Set up an IDCMP Read Loop ===== */

    for (;;)
    {
        if ((mes = (struct Message *)GetMsg(Wind->UserPort)) == NULL) {
            Wait(1L << Wind->UserPort->mp_SigBit);
            continue;
        }
        class = mes->Class;
        code = mes->Code;
        mx = mes->MouseX;
        my = mes->MouseY;
        Iadr = mes->IAddress;
        ReplyMsg(mes);
	        switch (class) {
 
           case CLOSEWINDOW:   CloseThings();
                               exit(NULL);
                               break;
    
        }
    }
}


VOID OpenAll()
/*  This function will open any necessary libraries*/
{

/*   ====== Open intuition =====*/

 IntuitionBase=(struct IntuitionBase *)
            OpenLibrary("intuition.library",INTUITION_REV);
 
 if (IntuitionBase==NULL)
     exit(FALSE);

 GfxBase=OpenLibrary("graphics.library",0);
 
 if (GfxBase==NULL)
     exit(FALSE);
}



make_window(x,y,w,h,name,flags,iflags,color0,color1,screen)
SHORT x,y,w,h;
UBYTE *name,color0,color1;
ULONG flags,iflags;
struct Screen *screen;

/* This function will initialize the NewWindow structure and open the window */
{
   struct NewWindow NewWindow;
   NewWindow.LeftEdge=x;
   NewWindow.TopEdge=y;
   NewWindow.Width=w;
   NewWindow.Height=h;
   NewWindow.DetailPen=-1;
   NewWindow.BlockPen=-1;
   NewWindow.Title=name;
   NewWindow.Flags=flags;
   NewWindow.IDCMPFlags=iflags;
   NewWindow.Type=CUSTOMSCREEN;
   NewWindow.FirstGadget=NULL;
   NewWindow.CheckMark=NULL;
   NewWindow.Screen=screen;
   NewWindow.BitMap=NULL;
   NewWindow.MinWidth=0;
   NewWindow.MinHeight=0;
   NewWindow.MaxWidth=640;
   NewWindow.MaxHeight=200;

   return(OpenWindow(&NewWindow));
   
}


make_screen(y,w,h,d,color0,color1,mode,name)
SHORT y,w,h,d;
UBYTE color0,color1,*name;
USHORT mode;
{
 struct NewScreen NewScreen;

   NewScreen.LeftEdge=0;
   NewScreen.TopEdge=y;
   NewScreen.Width=w;
   NewScreen.Height=h;
   NewScreen.Depth=d;
   NewScreen.DetailPen=color0;
   NewScreen.BlockPen=color1;
   NewScreen.ViewModes=mode;
   NewScreen.Type=CUSTOMSCREEN;
   NewScreen.Font=NULL;
   NewScreen.DefaultTitle=name;
   NewScreen.Gadgets=NULL;
   NewScreen.CustomBitMap=NULL;
  
   return(OpenScreen(&NewScreen));
   
}

 
CloseThings()

{
    if(IntuitionBase) CloseLibrary(IntuitionBase);
    if(GfxBase)       CloseLibrary(GfxBase);
    if(Wind)          CloseWindow(Wind);
    if(Scrn)          CloseScreen(Scrn);
}
