/* enkel deze includes nodig voor soundproductie */
#include "hardware/custom.h"
#include "stdio.h"

#include <exec/memory.h>

#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/sprite.h>

struct Preferences MyPrefs;
struct RastPort *rp;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct IntuiMessage *msg;
unsigned short color[3];
int red[3], green[3], blue[3];

#define NUM          6
#define SPRHEIGHT       16
#define WORDSPERSPR     (2 * SPRHEIGHT + 4)

struct NewWindow windef = {
        0,0, 640, 75,
        2, 3,
	FOLLOWMOUSE|MOUSEBUTTONS,
        REPORTMOUSE | WINDOWDRAG | ACTIVATE|SMART_REFRESH,
        NULL, NULL,
        (UBYTE *) "   BAMIGA SECTOR ONE PROUDLY PRESENTS  :  <<<<<<>  AMIGA  KARATE  !  <>>>>>>   ",
        NULL, NULL, 0, 0, 0, 0,
        WBENCHSCREEN
};

struct SimpleSprite     spr[NUM];
struct Window   *win;
struct ViewPort *vp;
UWORD           *sprbuf;
UWORD           *sprites[NUM];

LONG filesize,s1,s2,per;
char *filename;
short *sndbuffer, *AllocMem();

main()
{
int i,j;


/* aanroep van sound routine */
per = 280;
filename = "TAON_1";
filesize = 42676;
MakeSound();

openstuff ();
ClearMenuStrip(win);


color[1] = MyPrefs.color17;
color[2] = MyPrefs.color18;
color[3] = MyPrefs.color19;

for (i = 1; i <=3; i++)
{
red[i] = (color[i] >> 8) & 0x0F;
green[i] = (color[i] >> 4) & 0x0F;
blue[i] = color[i] & 0x0F;
}

for (i = 17; i < 31; i = i + 4)
{
SetRGB4(vp, i, red[1], green[1], blue[1]);
SetRGB4(vp, i+1, red[2], green[2], blue[2]);
SetRGB4(vp, i+2, red[3], green[3], blue[3]);
}

        setupsprites ();

        for (i=0; i<NUM; i++) {
                if (GetSprite (&spr[i], (long) i+1) < 0)
                        die ("Sprite allocation failed.");
                spr[i].x = (win->MouseX + win->LeftEdge + MyPrefs.XOffset);
                spr[i].y = (win->MouseY + win->TopEdge + MyPrefs.YOffset - 1);
                spr[i].height = SPRHEIGHT;
                ChangeSprite (vp, &spr[i], sprites[i]);
        }



     for(;;) {

                if (msg = GetMsg (win->UserPort))
                {
                 ReplyMsg (msg);
                 if (msg->Class == MOUSEBUTTONS)
                 {
                  while(msg=GetMsg(win->UserPort)) ReplyMsg(msg);
                  custom.dmacon = 0x0003;
                  closestuff();
                  exit();
                 }
	        }
                

                for (i=NUM-1; i>=0; i--) 
  	        {
		for(j=0;j<2;j++)WaitTOF();
		spr[i].x = (win->MouseX + win->LeftEdge + MyPrefs.XOffset);
		spr[i].y = (win->MouseY + win->TopEdge + MyPrefs.YOffset - 1);
		ChangeSprite (vp, &spr[i], sprites[i]);
        	}
	       }
}

openstuff ()
{
IntuitionBase = OpenLibrary ("intuition.library", 0);

	/* Geez, guys, what if Intuition isn't here?  Who cares?  We're
	   probably all dead anyways, if this happens... */

GfxBase = OpenLibrary ("graphics.library", 0);

	/* Ya, sure, you've pulled the Graphics.Library ROM out, right? */

        if (!(win = OpenWindow (&windef)))
                die ("No memory for window");

GetPrefs(&MyPrefs, sizeof(struct Preferences));

        vp = ViewPortAddress (win);
        rp = win.RPort;

}

closestuff ()
{
        int i;
 

        if(sndbuffer)
          FreeMem(sndbuffer,filesize);

        for (i=0; i<NUM; i++)
                if (spr[i].num)
                        FreeSprite ((long) spr[i].num);

        if (sprbuf)
                FreeMem (sprbuf,2L * WORDSPERSPR * NUM);
        if (win)
                CloseWindow (win);
        if (GfxBase)
                CloseLibrary (GfxBase);
        if (IntuitionBase)
                CloseLibrary (IntuitionBase);
}

die (str)
char *str;
{
        puts (str);
        closestuff ();
        exit (100);
}

setupsprites ()
{
/* This is what I stole.  Boy, I hope it wasn't copyrighted... */

        UWORD *cw;  
        UWORD *ballp; 
        int frame, scan;

        if (!(sprbuf = AllocMem(2L * WORDSPERSPR * NUM, MEMF_CHIP)))
                die ("Can't allocate sprite buffer.");

        cw = sprbuf;    /* current position at top of buffer */
        for (frame=0; frame<NUM; frame++) {
                sprites[frame] = cw;
                *cw++ = 0;              /* poscntl */
                *cw++ = 0;
                ballp = &MyPrefs.PointerMatrix[0]; 
                for (scan=0; scan<SPRHEIGHT; scan++) {
                        *cw++ = *ballp++;
                        *cw++ = *ballp++;
                }
                *cw++ = 0;      /* termination */
                *cw++ = 0;
        }
}


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

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

sndbuffer = AllocMem(filesize,MEMF_CHIP);
ptr = sndbuffer;

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

s1 = 0; s2 =filesize;
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 = 64;

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 = 64;

custom . dmacon = 0x8203;
}


