/*
 *  Copyright (c) 1994 Michael D. Bayne.
 *  All rights reserved.
 *
 *  Please see the documentation accompanying the distribution for distribution
 *  and disclaimer information.
 */

#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 "/Garshnelib/Garshnelib_protos.h"
#include "/Garshnelib/Garshnelib_pragmas.h"

#include <math.h>

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

struct ModulePrefs
{
	LONG Mode;
	LONG Depth;
};

extern struct ModulePrefs nP;

struct Screen *ModuleScreen( VOID )
{
	return 0L;
}

LONG FracBlank( struct Screen *Scr, UWORD Wid, UWORD Hei )
{
	float x = 0, y = 0, xx, yy, a, b, c;
	LONG i, xe, ye, flg_end = OK, mod = ( 1L << Scr->BitMap.Depth ) - 1;
	struct RastPort *Rast = &( Scr->RastPort );
	
	SetRast( Rast, 0 );
	ScreenToFront( Scr );
	
	a = (float)RangeRand( 1000 ) / 10 - 50.0;
	do
		b = (float)RangeRand( 1000 ) / 10 - 50.0;
	while( b == 0.0 );
	c = (float)RangeRand( 1000 ) / 10 - 50.0;
	
	for( i = 0; i < 50000 && flg_end == OK; i++ )
	{
		if(!( i % 50 ))
		{
			ScreenToFront( Scr );
			flg_end = ContinueBlanking();
		}
		
		if( x < 0 )
			xx = y + sqrt( fabs( b * x - c ));
		else
			xx = y - sqrt( fabs( b * x - c ));
		yy = a - x;
		xe = Wid / 2 + (SHORT)x;
		ye = Hei / 2 + (SHORT)y;
		
		if(( xe >= 0 )&&( ye >= 0 )&&( xe < Wid )&&( ye < Hei ))
		{
			SetAPen( Rast, ( ReadPixel( Rast, xe, ye ) + 1 ) % mod + 1 );
			WritePixel( Rast, xe, ye );
		}
		
		x = xx;
		y = yy;
	}

	return flg_end;
}

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

	Scr = OpenScreenTags( 0L, SA_DisplayID, mP->Mode, SA_Depth, mP->Depth,
						 SA_Overscan, OSCAN_STANDARD, SA_Quiet, TRUE,
						 SA_Behind, TRUE, TAG_DONE );

	if( Scr )
	{
		Triplet *ColorTable = RainbowPalette( Scr, 0L, 1L, 0L );
		Wnd = BlankMousePointer( Scr );
		
		while( RetVal == OK )
			RetVal = FracBlank( Scr, Scr->Width, Scr->Height );
		
		UnblankMousePointer( Wnd );
		RainbowPalette( 0L, ColorTable, 1L, 0L );
		CloseScreen( Scr );
	}
	else
		RetVal = FAILED;

	return RetVal;
}
