#include "exec/types.h"
#include "hardware/custom.h"
#include "hardware/cia.h"
#include "hardware/blit.h"
#include "exec/exec.h"
#include "devices/audio.h"
#include "intuition/intuition.h"
#include "graphics/gfxmacros.h"
#include "stdio.h"

#define INTUITION_REV 31
#define GRAPHICS_REV 31

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *Scrn;
struct Window *NoBorder;
struct ViewPort *vp;
struct RastPort *r;
struct IntuiMessage *mesg;

struct NewScreen NewScrn = {
        0, 0,           /* LeftEdge, TopEdge */
        320, 200,       /* Width, Height */
        5,              /* Depth */
        0, 1,           /* DetailPen, BlockPen */
        NULL,           /* ViewModes ... value of 0 = low resolution */
        CUSTOMSCREEN,   /* Type of screen */
        NULL,           /* Font to be used as default for this screen */
        NULL,           /* DefaultTitle for its title bar */
        NULL,           /* screens user-gadgets, always NULL, ignored */
        NULL };         /* address of custom bitmap for screen */

struct NewWindow NewNoBorder = {
        0,              /* LeftEdge */
        0,              /* TopEdge  */
        320, 200,     /* Width, Height of this window */
        -1,             /* DetailPen */
        -1,             /* BlockPen */
        MOUSEBUTTONS,   /* IDCMP Flags */
        ACTIVATE|NOCAREREFRESH|BORDERLESS|GIMMEZEROZERO,
                        /* Window Flags:  (see below for more info) */
        NULL,           /* FirstGadget */
        NULL,           /* CheckMark   */
        NULL,           /* Window title */
        NULL,           /* Pointer to Screen if not workbench */
        NULL,           /* Pointer to BitMap if a SUPERBITMAP window */
        0, 0,           /* minimum width, minimum height */
        0,0,            /* maximum width, maximum height */
        CUSTOMSCREEN    /* type waarin window opend */
        };



                
struct CIA         *cia = 0xbfe001;
/* #define custom (*ccustom) */
/* struct Custom      *ccustom = 0xdff000; */
/* these lines only needed if custom is not declared in hardware/custom.h */

LONG s1,s2;
int per;

short *sndbuffer;

main(argc,argv)
int argc;
char *argv[];
{
 LONG i;
 ULONG mclass;
 VOID OpenALL(), CloseALL(), CleanUp();

   puts("Usage : Periode !");

 if(argc>1)
  per = atoi(argv[1]);
 else
  per = 280;


 OpenALL();

 if((Scrn = (struct Screen *) OpenScreen(&NewScrn)) == 0) CloseALL();
 
 NewNoBorder.Screen = Scrn;

 if((NoBorder = (struct Window *) OpenWindow(&NewNoBorder)) == 0) CloseALL();

 r=NoBorder->RPort;
 vp=ViewPortAddress(NoBorder);

 MakeSound();

 /* ======einde in stijl !====== */
for(;;)
 {
   while((mesg=(struct IntuiMessage *) GetMsg(NoBorder->UserPort))==NULL)
   {
    ;
   } /* end while */

   ReplyMsg(mesg);
   mclass=mesg->Class;
   if( (mclass=mesg->Class) == MOUSEBUTTONS)
   {
    break;
   }
 }

  CleanUp();
}

/*  -------------------------------- einde van main ------------------- */

VOID OpenALL()
{
 /*==Intuition==*/

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

 if(IntuitionBase==NULL)
    CloseALL();

 /*==Graphics Library==*/

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

 if(GfxBase==NULL)
    CloseALL();

}

VOID CloseALL()
{
 if(NoBorder) CloseWindow(NoBorder);
 if(Scrn) CloseScreen(Scrn);
 if(GfxBase) CloseLibrary(GfxBase);
} 

VOID CleanUp()
{
 custom.dmacon = 0x0003;
 FreeMem(sndbuffer,60000L);
 CloseALL();
}


MakeSound()
{
LoadSound();
/* Forbid(); */
PlaySong();
}  

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

sndbuffer = AllocMem(60000L,MEMF_CHIP);
ptr = sndbuffer;

custom . dmacon = 0x0003;
fp = fopen("df1:UB40","r");
if (fp == NULL) return(0);

s1 = 0; s2 =56724 ;
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 = per;
custom . aud[0].ac_vol = 42;

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

custom . dmacon = 0x8203;
}
