#include	<exec/types.h>
#include 	<exec/nodes.h>
#include	<exec/libraries.h>
#include        <exec/memory.h> /* soundprod */

#include	<graphics/gfx.h>
#include	<graphics/rastport.h>
#include	<graphics/text.h>
#include	<intuition/intuition.h>
#include 	<stdio.h> /* ook voor soundprod */
#include 	"df1:iff/jiff.c"

#include "hardware/custom.h" /* soundprod */



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


/*This should be in exec/libraries.h */
extern struct Library *OpenLibrary();

/*This should be in intuition/intuition.h */
extern struct Screen *OpenScreen();

/* These are the bases for the libraries you need to do any graphics
   much at all and still multi-task */
struct Library *GfxBase = NULL;
struct Library *LayersBase = NULL;
struct Library	*IntuitionBase = NULL;

/* Well the workbench Screen isn't 320x200x5 so...*/
struct Screen *PlopScreen = NULL;

/* Just one font for me... not static cause you might want it
   in your menu modules. */
struct TextAttr PlopFont =
    {
	"topaz.font",  /* I think this is the ROM font */
	8,
	0,
	0,
    };

/* a NewScreen - I don't know what half of this means either */
static
struct NewScreen PlopNewScreen =
	{
	0, 0, XMAX, YMAX, PLANES,
	0, 1,
	0,
	CUSTOMSCREEN,
	&PlopFont,
	"Plop on Top",
	NULL,
	NULL,
	};

/* To use clip blit need a RastPort.  Pretty useless, a BitMap should
   be enough I think ... or anyway something a little less massive */
struct RastPort PlopRastPort;

void
put_ea_cmap(cmap, colors)
unsigned char *cmap;
long colors;
{
long i;
unsigned long r, g, b;

if (colors > 32)	/*color clipping*/
	colors = 32;

/* This loop is modified to work with Aztec 3.4a 32bit by J. Van Houtven */
/* Modification Date : 20 July 1987 */

for (i=0; i<colors; i++)
	{
         r= *cmap++ >> 4;
         g= *cmap++ >> 4;
         b= *cmap++ >> 4;
         SetRGB4(&PlopScreen->ViewPort,i,(long)r,(long)g,(long)b);
	}
}

/* back out backwards */
void
plop_cleanup()
{
custom.dmacon = 0x0003;
if(sndbuffer)
        FreeMem(sndbuffer,filesize);
if (PlopScreen != NULL)
	CloseScreen(PlopScreen);
if (IntuitionBase != NULL)
	CloseLibrary(IntuitionBase);
if (LayersBase != NULL)
	CloseLibrary(LayersBase);
if (GfxBase != NULL)
	CloseLibrary(GfxBase);
}

main(argc, argv)
int argc;
char *argv[];
{
int i,j,x,y;
struct ILBM_info *info;

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


if ((GfxBase =  OpenLibrary("graphics.library", (long)0)) == NULL)
        return(-1);

if ((LayersBase =  OpenLibrary("layers.library", (long)0)) == NULL)
	{
	plop_cleanup();
	return(-2);
	}

if ((IntuitionBase =  OpenLibrary("intuition.library",(long) 0)) == NULL)
	{
	plop_cleanup();
	return(-3);
	}

if ( (PlopScreen = OpenScreen(&PlopNewScreen) ) == NULL)
	{
	plop_cleanup();
	return(-4);
	}

InitRastPort(&PlopRastPort);
PlopRastPort.Mask = (1<<PLANES)-1;
/*just play it safe in case someone tries to load 6 bit planes...*/

for (i=1; i<argc; i++)
{
 if ( (info = read_iff(argv[i], 0) ) != NULL)
 {
  put_ea_cmap(&info->cmap, (1 << info->bitmap.Depth));
  PlopRastPort.BitMap = &info->bitmap;
  ClipBlit( &PlopRastPort, (long)0, (long)0,
           &PlopScreen->RastPort, (long)info->header.x, (long)info->header.y,
	   (long)info->header.w, (long)info->header.h, (long)COPY_MINTERM);
  Delay( (long)360);  /*pause for a couple seconds */
 
  y = YMAX-1;
  for(j=0;j<80;j++)
  {
   x=j<<2;
   ScrollRaster(&PlopScreen->RastPort,0L,j-40L,x,0L,x+3L,y);
  }

  Delay( (long)300);

  SetAPen(&PlopScreen->RastPort,0);
  for(j=0;j<100;j++)
  {
   Move(&PlopScreen->RastPort,0,j);Draw(&PlopScreen->RastPort,XMAX-1,j);
   Move(&PlopScreen->RastPort,0,YMAX-1-j);Draw(&PlopScreen->RastPort,XMAX-1,YMAX-1-j);
  }

  free_planes(&info->bitmap);
  }
 }
plop_cleanup();
}
/* ====== end of main ======= */

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;
}




