/* 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 *chipimageone; 

 UWORD imageone[] = 
 {
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x800,0x0,0x400,
    0x1C00,0x0,0xE00,
    0x1C00,0x0,0xE00,
    0x800,0x0,0x400,
    0x0,0x0,0x0,
    0x1F,0x3E,0x0,
    0x18,0x6,0x0,
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x0,0x7380,0x0,
    0x0,0x7380,0x0,
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x0,0x0,0x0,
    0x30,0x3,0x0,
    0xFE,0x1F,0xC000,

    0x1E00,0x0,0x1E00,
    0x3F00,0x0,0x3F00,
    0x3F80,0x0,0x7F80,
    0xFFC3,0xFFF0,0xFFC0,
    0xFFFF,0xFFFF,0xFFC0,
    0xFFFE,0x1F,0xFFC0,
    0x7F80,0x0,0x7F80,
    0x7FFF,0xC0FF,0xFF80,
    0x3EE0,0xFFC1,0xDF00,
    0x1C20,0x8041,0xE00,
    0x1C00,0x7F80,0xE00,
    0xC03,0xFFF0,0xC00,
    0xC07,0x38,0xC00,
    0xE0F,0x3C,0x1C00,
    0x70F,0x40BC,0x3800,
    0x387,0x8078,0x7000,
    0x1E1,0xFFE1,0xE000,
    0x78,0x3F07,0x8000,
    0x1FF,0x807F,0xE000,
    0x7F1,0xFFE3,0xF800
 };

struct Image BoolImage = 
 {
  0,0,          /* LeftEdge, TopEdge */
  48,20,2,      /* Width, Height, Depth */
  0,
  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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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,48,20,                  /* 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 ! */
UWORD *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(!InitChipImageOne())
 {
  CloseALL();
  exit(1);
 }

 BoolImage.ImageData = (USHORT *) chipimageone; 

 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(chipimageone)FreeMem(chipimageone,240);
 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("df0:taon_3!","r")) == NULL) return(0);

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

for (i=0; i<s1/2; 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_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,15+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();
}  

InitChipImageOne()
{
 int i;
 UWORD *s,*d;

 if( (chipimageone = (UWORD *) AllocMem(240L,MEMF_CHIP)) == 0) return(0);
 s=imageone;
 d=chipimageone;
 for(i=0;i<120;i++) *d++ = *s++;
 return(1);
}


