#include <exec/memory.h>
#include <intuition/intuition.h>
#include <dos/dos.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/alib_protos.h>

#include "Interference.h"
#include "//defs.h"
#include "/main.h"

struct ModulePrefs
{
	LONG Mode;
	LONG Depth;
};

extern struct ModulePrefs nP;

#define BLANKMOUSE
#define RAINBOWPALETTE
#include "/utility.c"

LONG Interference( struct Screen *Scr )
{
	LONG x, y, factor, mod = ( 1L << Scr->BitMap.Depth ) - 1, Wid = Scr->Width, Hei = Scr->Height;
	LONG RetVal = OK, Pixel = 0;
	struct RastPort *R = &( Scr->RastPort );

	ScreenToFront( Scr );

	factor = RangeRand( 20 ) + 1;
	for( y = -Hei / 2 + 1; y <= 0 && RetVal == OK; y++ )
	{
		for( x = -Wid / 2 + 1; x <= 0; x++ )
		{
			SetAPen( R, ((( x * x + y * y ) / factor ) % mod ) + 1 );
			WritePixel( R, Wid / 2 - 1 + x, Hei / 2 - 1 + y );
			WritePixel( R, Wid / 2 - 1 - x, Hei / 2 - 1 + y );
			WritePixel( R, Wid / 2 - 1 + x, Hei / 2 - 1 - y );
			WritePixel( R, Wid / 2 - 1 - x, Hei / 2 - 1 - y );
			if(!( ++Pixel % 600 ))
				RetVal = ContinueBlanking();
		}
		RainbowPalette( Scr, 1L );
	}

	for( x = 0; x < 400 && RetVal == OK; x++ )
	{
		WaitTOF();
		if(!( x % 3 ))
			RainbowPalette( Scr, 1L );
		RetVal = ContinueBlanking();
	}

	return RetVal;
}

LONG Blank( VOID *Prefs )
{
	struct Screen *Scr;
	struct ModulePrefs *mP;
	LONG RetVal = OK;
	
	if( InterferenceWnd )
		mP = &nP;
	else
		mP = ( struct ModulePrefs * )Prefs;

	Scr = OpenScreenTags( 0L, SA_Depth, mP->Depth, SA_Overscan, OSCAN_STANDARD, SA_DisplayID,
						 mP->Mode, SA_Quiet, TRUE, SA_Behind, TRUE, TAG_DONE );
	
	if( Scr )
	{
		SetRast(&( Scr->RastPort ), 0 );
		RainbowPalette( Scr, 1L );
		BlankMousePointer( Scr );

		ScreenToFront( Scr );

		while( RetVal == OK )
			RetVal = Interference( Scr );

		UnblankMousePointer();
		RainbowPalette( NULL, 1L );
		CloseScreen( Scr );
	}
	else
		RetVal = FAILED;

	return RetVal;
}
