;/*
sc RESOPT DATA=NEAR UCHAR CONSTLIB STREQ NMINC STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE IGNORE=73+94 Noise.c
slink from LIB:c.o Noise.o to //Clients/Noise LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
delete Noise.o
quit

 Noise 1.0  (Client for BServer)

 Copyright © 1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/text.h>
#include <graphics/rastport.h>
#include <graphics/modeid.h>

#include <clib/alib_protos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/utility.h>

#include "/include/client.h"

#define DEPTH 5

char *ver = "$VER: Noise 1.0 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct DisplayIDInformation *dinfo;

extern ULONG RangeSeed;

void Noise( void )
{
struct Screen *scr;
struct Rectangle *rect;
UBYTE brightness, volume;
UWORD swidth, sheight;
ULONG id;
Sound *noise;

rect = GETTXTOSCANRECT(dinfo);

volume = GETVOLUME(dinfo);
swidth = RECTANGLEWIDTH(rect);
sheight = RECTANGLEHEIGHT(rect);
brightness = GETBRIGHTNESS(dinfo);
id = DISPLAYID(dinfo);

if ( id & HIRES )
	swidth >>= 1;
else
if ( id & SUPERHIRES )
	swidth >>= 2;

if ( id & LACE )
	sheight >>= 1;

if ( scr = OpenScreenTags( NULL,
	SA_DisplayID, DISPLAYID( dinfo ) & MONITOR_ID_MASK,
	SA_Width, swidth,
	SA_Height, sheight,
	SA_Depth, DEPTH,
	SA_Type, CUSTOMSCREEN,
	SA_Quiet, TRUE,
	SA_Interleaved, FALSE,
	SA_Behind, TRUE,
	TAG_END ) )
	{
	register struct ViewPort *vp = &(scr->ViewPort);
	register struct RastPort *rp = &(scr->RastPort);
	UWORD x, y, d, bpr = rp->BitMap->BytesPerRow;
	UBYTE *planeptr, color[4];

	noise = Open8SVX( "8SVX/Noise.snd" );

	for ( d = 0; d < DEPTH; d++ )
		{
		planeptr = rp->BitMap->Planes[d];
		for ( x = 0; x < bpr; x++ )
			for ( y = 0; y < scr->Height; y++ )
				*planeptr++ = ( d ? RangeRand( 255 ) : 255 );
		}

	color[0] = 0*brightness/100;
	color[1] = 5*brightness/100;
	color[2] = 10*brightness/100;
	color[3] = 15*brightness/100;

	SetRGB4( vp, 0, 0, 0, 0 );
	ScreenToFront( scr );
	SpritesOff();

	PlayAsynch8SVX( noise, volume );
	while( STILL_BLANKING )
		{
		register int a, which;

		WaitTOF();
		for ( a = 1; a < 1<<DEPTH; a++ )
			{
			which = RangeRand( 4 );
			SetRGB4( vp, a, color[which], color[which], color[which] );
			}
		}

	Close8SVX( noise );

	CloseScreen( scr );
	SpritesOn();
	}
else
	SendClientMsg( ACTION_FAILED );
}


void __main( void )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( dinfo = OpenCommunication() )
			{
			CurrentTime( &RangeSeed, &RangeSeed );
			Noise();
			CloseCommunication( dinfo );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
