/* COMPILEER : ex amyatom !!!!!!! voor chipmem !!!! */

#include "graphics/gfxbase.h" /* altijd voor andere */
#include "graphics/gfxmacros.h"
#include "graphics/copper.h"
#include "stdio.h"
#include "exec/types.h"
#include "exec/exec.h"
#include "exec/memory.h"
#include "hardware/custom.h"
#include "intuition/intuition.h"


struct IntuitionBase *IntuitionBase = 0L;
struct GfxBase *GfxBase = 0L;
struct Window *ControlWindow =0L;
struct ViewPort *vp;
struct RastPort *rp;

#define AANTCOL 15
UWORD colors[AANTCOL] =
{
 0Xf80,0Xe80,0Xd80,0Xc60,0Xb60,
 0Xa60,0X940,0X840,0X940,0Xa60,
 0Xb60,0Xc60,0Xd80,0Xe80,0Xf80
}; 

 UWORD imageone[] = 
 {

0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0080,0x0000,
0x0040,0x0000,0x01C0,0x0000,0x00E0,0x0000,
0x01C0,0x0000,0x00E0,0x0000,0x0080,0x0000,
0x0040,0x0000,0x0000,0xE001,0xC000,0x0000,
0x0001,0xC000,0xE000,0x0000,0x0001,0x8000,
0x6000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0738,0x0000,0x0000,
0x0000,0x0330,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x01E0,0x0000,0x01E0,0x0000,
0x03F0,0x0000,0x03F0,0x0000,0x07F8,0x0000,
0x07F8,0x0000,0x0FFC,0x3FFF,0x0FFC,0x0000,
0x0FFF,0xFFFF,0xFFFC,0x0000,0x0FFF,0xE001,
0xFFFC,0x0000,0x07FC,0x0000,0x0FF8,0x0000,
0x07F0,0x0000,0x03F8,0x0000,0x03E0,0x3003,
0x01F0,0x0000,0x01C0,0x7003,0x80E0,0x0000,

0x01C0,0x07F8,0x00E0,0x0000,0x01C0,0x3FFF,
0x00C0,0x0000,0x01C0,0x7003,0x80C0,0x0000,
0x00E0,0xF003,0xC1C0,0x0000,0x0070,0xF003,
0xC380,0x0000,0x0038,0x7807,0x8700,0x0000,
0x001E,0x1FFE,0x1E00,0x0000,0x0007,0x8000,
0x7800,0x0000,0x0001,0xF807,0xC000,0x0000,
0x0000,0x1FFE,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0xEFC4,0x4FEF,0x776F,0x726B,
0x696E,0x672E,0x2E2E,0x6E6C,0x792E,0x0A0A,
0x2062,0x6520,0x6672,0x6565,0x6C79,0x2064,
0x6973,0x7472,0x6962,0x7574,0x6564,0x2066,
0x6F72,0x206E,0x6F6E,0x2D70,0x726F,0x6669,
0x740A,0x7265,0x0A20,0x2020,0x2020,0x2020,
0x2020,0x2020,0x2020,0x2D63,0x203D,0x2063,
0x7265,0x6174,0x6520,0x636F,0x6C6F,0x7220,
0x7265,0x6769,0x7374,0x6572,0x2069,0x6E66,
0x6F72,0x6D61,0x7469,0x6F6E,0x0A20,0x2020,
0x2020,0x2020,0x2020,0x696E,0x6669,0x6C65,
0x203D,0x2062,0x7275,0x7368,0x2066,0x696C,
0x6520,0x746F,0x2063,0x6F6E,0x7665,0x7274,
0x0A20,0x2020,0x2020,0x2020,0x206F,0x7574,
0x6669,0x6C65,0x203D,0x206F,0x7574,0x7075

 };

struct Image BoolImage = 
 {
  0,0,          /* LeftEdge, TopEdge */
  49,22,2,      /* Width, Height, Depth */
  imageone,
  0x03, 0x0, NULL
 };


 struct IntuiText IntuiStr[10] =
 {
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 1", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 2", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 3", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 4", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 5", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 6", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 7", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 8", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)" 9", NULL},
  {3,1,JAM2,16,27,NULL,(UBYTE *)"10", NULL}
 };


/* gadget structure */

 struct Gadget BoolGadget[10] =
{ 
 {
  NULL,                         /* pointer to Next Gadget */
  18,15,49,22,                  /* Left, Top, Width, Height */
  GADGIMAGE|GADGHCOMP,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[0],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  1,                         /* ID */
  NULL                          /* pointer to special data */
 },

 {
  &BoolGadget[0],                /* pointer to Next Gadget */
  78,15,49,22,                  /* Left, Top, Width, Height */
  GADGIMAGE|GADGHCOMP,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[1],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  2,                          /* ID */
  NULL                          /* pointer to special data */
 },

 {
  &BoolGadget[1],                /* pointer to Next Gadget */
  138,15,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[2],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  3,                          /* ID */
  NULL                          /* pointer to special data */
 },


 {
  &BoolGadget[2],                /* pointer to Next Gadget */
  198,15,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[3],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  4,                          /* ID */
  NULL                          /* pointer to special data */
 },


 {
  &BoolGadget[3],                /* pointer to Next Gadget */
  258,15,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[4],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  5,                          /* ID */
  NULL                          /* pointer to special data */
 },


 {
  &BoolGadget[4],                /* pointer to Next Gadget */
  18,69,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[5],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  6,                          /* ID */
  NULL                          /* pointer to special data */
 },


 {
  &BoolGadget[5],                /* pointer to Next Gadget */
  78,69,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[6],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  7,                          /* ID */
  NULL                          /* pointer to special data */
 },


 {
  &BoolGadget[6],                /* pointer to Next Gadget */
  138,69,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[7],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  8,                          /* ID */
  NULL                          /* pointer to special data */
 },

 {
  &BoolGadget[7],                /* pointer to Next Gadget */
  198,69,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,         /* Flags */
  GADGIMMEDIATE | RELVERIFY,                /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[8],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  9,                          /* ID */
  NULL                          /* pointer to special data */
 },

 {
  &BoolGadget[8],                /* pointer to Next Gadget */
  258,69,49,22,                  /* Left, Top, Width, Height */
  GADGHCOMP|GADGIMAGE,          /* Flags */
  GADGIMMEDIATE | RELVERIFY,   /* Activation flags */
  BOOLGADGET,                   /* Type */
  (APTR) &BoolImage,            /* pointer to Border Image */
  NULL,           /* pointer to Select Render */
  &IntuiStr[9],                 /* Pointer to GadgetText */
  0,                            /* MutualExclude */
  NULL,                         /* pointer to SpecialInfo */
  10,                          /* ID */
  NULL                          /* pointer to special data */
 }

};
 
 struct NewWindow NewControlWindow =
 { 
  0,0,
  640,111,
  3,1,
  GADGETDOWN|CLOSEWINDOW|GADGETUP, /* !!!!!! IDCMP Flags !!!!!!!! */
  WINDOWDRAG|WINDOWCLOSE|WINDOWDEPTH|ACTIVATE|SMART_REFRESH,
  &BoolGadget[9], /* pointer to first gadget */
  NULL,
  (UBYTE *) "'CLICK_A_PIG' COPYDISK v2.0 : A 'PIGHEADED' PRODUCTION by [BS ONE] !  ",
  NULL,
  NULL,
  0,0,
  640,111,
  WBENCHSCREEN
 };

LONG s1=10,s2=23428;
 /* s1 en s2 moeten hier geinit worden ! 
    Want verschillende functies gebruiken deze constante waarden ! */
short *sndbuffer = 0L;

_main() /* Let op : sound niet via argument uit te schakelen ! */
{
 ULONG MIClass;
 USHORT id;
 APTR MIAddress;
 LONG i;
 char *txt[10];
 VOID CloseALL(), CleanUp(),StopSound(),endcopper();
 struct IntuiMessage *MyIntuiMessage;

 /* OpenALL */

 IntuitionBase=(struct IntuitionBase *) OpenLibrary( "intuition.library",(LONG)LIBRARY_VERSION);

 GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",(LONG)LIBRARY_VERSION);


 txt[0]  = "  1) MARAUDER II v1.0A             ";
 txt[1]  = "  2) SUPERKIT v1.0                 ";
 txt[2]  = "  3) QUICKNIBBLE v1.4              ";
 txt[3]  = "  4) MIRROR v1.4 82 TRACKS         ";
 txt[4]  = "  5) ACOPY v1.0                    ";
 txt[5]  = "  6) BAZBO COPIER v1.0             ";
 txt[6]  = "  7) TURBO COPY v2.0               ";
 txt[7]  = "  8) QUICKCOPY v1.01               ";
 txt[8]  = "  9) WHITE LIGHTNING V1.1          ";
 txt[9]  = " 10) NEWCLI                        ";

 

 if(!(ControlWindow = (struct Window *) OpenWindow(&NewControlWindow))) CloseALL();

 rp=ControlWindow->RPort;
 vp=ViewPortAddress(ControlWindow);

 usecopper();

 SetAPen(rp,3);
 SetBPen(rp,0);
 for(i=0;i<10;i++)
 {
  Move(rp,345,22+i*9);
  Text(rp,txt[i],strlen(txt[i]));
 }

 MakeSound();

 for(;;)
 {
   WaitPort(ControlWindow->UserPort);
   MyIntuiMessage = (struct IntuiMessage *) GetMsg(ControlWindow->UserPort);
   ReplyMsg(MyIntuiMessage);
   MIClass = MyIntuiMessage->Class;
   MIAddress = MyIntuiMessage->IAddress;

   switch(MIClass)
   {
    case GADGETUP:
    id = ( ((struct Gadget *) (MIAddress))->GadgetID);
    endcopper();
    switch(id)
    {
     case 1:
      CleanUp();
      Execute("copy sys:sys64738 to ram:",0,0);
      fexecv("2",0);
      execv("ram:sys64738",0);
      break;
     case 2:
      CleanUp();  /* StopSound();CloseALL(); */
      Execute("sys:1",0,0);
      execv("sys64738",0);
      break;
     case 3:
      StopSound();Execute("sys:3",0,0); break;
     case 4:
      StopSound();Execute("sys:load sys:m",0,0); break;
     case 5:
      StopSound();Execute("sys:5",0,0); break;
     case 6:
      StopSound();Execute("sys:6",0,0); break;
     case 7:
      StopSound();Execute("sys:7",0,0); break;
     case 8:
      StopSound();Execute("sys:8",0,0); break;
     case 9:
      StopSound();Execute("sys:9",0,0);break;
     case 10:
      StopSound();Execute("sys:c/NewCLI",0,0);break;
    }
    usecopper();

    break;

    case CLOSEWINDOW:

    while(MyIntuiMessage=(struct IntuiMessage *)
     GetMsg(ControlWindow->UserPort)) ReplyMsg(MyIntuiMessage);
    endcopper();
    CleanUp();
    exit(TRUE);
    break;
   } /* switch */
 } /* for */
}
/* ************************************** end *************************** */

VOID CloseALL()
{
 if(ControlWindow) CloseWindow(ControlWindow);
 if(GfxBase) CloseLibrary(GfxBase);
 if(IntuitionBase) CloseLibrary(IntuitionBase);
} 

VOID StopSound()
{
 custom.dmacon = 0x0003;
 if(sndbuffer)FreeMem(sndbuffer,(long)(s2-s1));
 sndbuffer = 0;
}

VOID CleanUp()
{
 StopSound();
 CloseALL();
}


MakeSound()
{
LoadSound();
if(sndbuffer)PlaySong();
}  

LoadSound()
{
FILE *fopen(), *fp;
SHORT *ptr, *AllocMem();
LONG i;

custom . dmacon = 0x0003;

if((fp = fopen("sys:taon_3!","r")) == NULL) return(0);

sndbuffer = AllocMem((long)(s2-s1),MEMF_CHIP);
ptr = sndbuffer;

for (i=0; i<s1; i++)
	getc(fp);

for (i=s1/2; i<s2/2; i++)
	{
	*ptr++ = getc(fp)*256 + getc(fp);
	}

fclose (fp);
}

PlaySong()
{
custom . aud[0].ac_ptr = (UWORD *)sndbuffer;
custom . aud[0].ac_len = s2/2-s1/2;
custom . aud[0].ac_per = 500;
custom . aud[0].ac_vol = 64;

custom . aud[1].ac_ptr = (UWORD *)sndbuffer;
custom . aud[1].ac_len = s2/2-s1/2;
custom . aud[1].ac_per = 500;
custom . aud[1].ac_vol = 64;

custom . dmacon = 0x8203;
}

void endcopper()
{
 FreeVPortCopLists(vp);RemakeDisplay();
}
usecopper()
{
  UWORD i;
  struct UCopList *cl;
  void *dspins,*sprins,*clrins;  

  cl = AllocMem(sizeof(struct UCopList),MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR);
  /* Doesn't this use a little memory every time the function
     is invoked ??? Why don't we use Freemem somewhere ?
     Because we use FreeVPortCopLists()
  */

    for(i=0;i<90;i++)
    {
    CWAIT(cl,16+i,0);
    CMOVE(cl,custom.color[2],colors[i % AANTCOL]);
    }

    for(i=112;i<200;i++)
    {
    CWAIT(cl,i,0);
    CMOVE(cl,custom.color[0],0x000);
    CWAIT(cl,i,115);
    CMOVE(cl,custom.color[0],0xff0);
    CWAIT(cl,i,168);
    CMOVE(cl,custom.color[0],0xf00);
    CWAIT(cl,i,222);
    CMOVE(cl,custom.color[0],0x000);
    }
    CEND(cl);

    dspins=vp->DspIns; sprins=vp->SprIns; clrins=vp->ClrIns;

    Forbid();
     vp->DspIns=vp->SprIns=vp->ClrIns=0;
     FreeVPortCopLists(vp);
     vp->DspIns=dspins; vp->SprIns = sprins; vp->ClrIns=clrins;
     vp->UCopIns=cl;
    Permit();
 
    RethinkDisplay();
}  


