/********************************************************************
 ** SAMPLE MANIPULATION PROGRAM
 ** (c) Spak, Darrell Tam, c9107253@mystra.newcastle.edu.au (1993)
 ** phone (Australia) 049-829-710
 **                    49-829-710
 ** (c) SST 1993
 **
 ** GENERIC + GRAPHICS ROUTINES
 **
 ** TABS to 4
 ********************************************************************/

#include "everything.h"
#include "sndthings.h"

/********************************************************************/
		void FlushWindowInput(struct Window *wdw)
/********************************************************************/
{
struct IntuiMessage *im;
	while (im = (struct IntuiMessage *)GetMsg(wdw->UserPort))
			ReplyMsg((struct Message *)im);
}

/********************************************************************/
void SaveCols(struct Window *wdw, USHORT *table, short n)
/********************************************************************/
{
short i;
	for(i = 0; i<n; i++)
		*table++ = GetRGB4(wdw->WScreen->ViewPort.ColorMap,i);
}


/********************************************************************/
			void ClearWindow(struct Window *wdw)
/********************************************************************/
{
short	left = wdw->BorderLeft,
		top = wdw->BorderTop,
		width = wdw->Width-wdw->BorderRight,
		height = wdw->Height-wdw->BorderBottom;

	if(width<1) width = 1;
	if(height<1) height = 1;
	SetDrMd(wdw->RPort, JAM1);
	SetWrMsk(wdw->RPort, 0xff);
	RectFill(wdw->RPort, left, top, width-1, height-1);
}

/********************************************************************/
void InvertBox(struct Window *wdw,
		short x, short y, short w, short h, UBYTE mask)
/********************************************************************/
{
short x1 = x, y1  = y, x2 = x+w, y2 = y+h;
	if(x1 > x2) { x1 = x2; x2 = x; }
	if(y1 > y2) { y1 = y2; y2 = y; }
	SetDrMd(wdw->RPort, COMPLEMENT);
	SetWrMsk(wdw->RPort, mask);
	RectFill(wdw->RPort, x1, y1, x2, y2);
}


/********************************************************************/
void DrawSample8Down(struct Window *wdw,
			char *smpl, unsigned long smpllen,
			short x, short y, short width, short height )
/********************************************************************
     Write an 8 bit sample into a window, with scaling and all
             Should work with any sized sample now
          Draw the sample scaled to fit on the screen
 ********************************************************************/
{
short	mp;
ULONG	scale;

	if(height<1 || width<1 || smpllen<1) return;
	mp = (width>>1) + x;	/* mid point for the X axis */
	scale = (width-2)<<8;	/* <<16 /256 for the X axis */

/** DRAW A SCALED SAMPLE THING FROM LINES (ITS BIGGER THAN OUR DISPLAY WINDOW) */
	if(height < smpllen) {
	UWORD	i, l, s, fracstep, fraccounter = 0;
	ULONG	counter = 0, intstep;
		intstep = smpllen / height;
		fracstep = ((smpllen % height)<<16) / height;
		for(i = y+height, l = y; l < i; l++)
		{
			s = (smpl[counter]*scale)>>16;
			Move(wdw->RPort, mp-s, l);
			Draw(wdw->RPort, mp+s, l);
			counter += intstep;
			if(fraccounter > (fraccounter += fracstep)) counter++;
		}
	} else {

/** DRAW THE SAMPLE AS IT REALLY IS (ITS SMALLER THAN OUR DISPLAY WINDOW) */
	UWORD	i, l, s;
	ULONG	step, counter = 0;

		step = (height<<16)/smpllen;	/* height is in pixels (<<65k) so ok */
		Move(wdw->RPort, mp+(s=((*smpl++)*scale)>>16), y);
		counter = (y<<16) + step;
		Draw(wdw->RPort, mp+s, l = (counter>>16)-1);
		for(i = 1; i < smpllen; i++) {
			Draw(wdw->RPort,
				mp+(s=((*smpl++)*scale)>>16), l);
			counter += step;
			Draw(wdw->RPort, mp+s, l = (counter>>16)-1);
		}
	}
}


/*	for(k = 0, s = 0, j = counter>>16; j < (counter+step)>>16; j++, k++)
			s += ABS(smpl[j]); if(k<1) k = 1; s = ((s/k) * scale) >> 16; */


