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

#include <exec/types.h>
#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 "Rain.h"
#include "//defs.h"
#include "/main.h"

struct ModulePrefs
{
	LONG Mode;
	LONG Depth;
	LONG Drops;
};

extern struct ModulePrefs nP;

#ifndef max
#define max( x, y ) ( x > y ? x : y )
#endif

LONG Blank( VOID *Prefs )
{
	LONG ToFrontCount = 0, Wid, Hei, Drops, x, y, r, i, incr, RetVal = OK;
	struct ModulePrefs *mP;
	struct RastPort *Rast;
	struct Screen *Scr;
	struct Window *Wnd;
	
	if( RainWnd )
		mP = &nP;
	else
		mP = ( struct ModulePrefs * )Prefs;
	
	Drops = mP->Drops;
	
	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 )
	{
		Wid = Scr->Width;
		Hei = Scr->Height;
		
		Rast = &( Scr->RastPort );
		SetRast( Rast, 0 );
		
		for( i = 0; i < 4; i++ )
			SetRGB4(&( Scr->ViewPort ), i, 4 * i, 4 * i, 4 * i );
		
		Wnd = BlankMousePointer( Scr );
		ScreenToFront( Scr );
		
		while( RetVal == OK )
		{
			if(!( ++ToFrontCount % 60 ))
				ScreenToFront( Scr );
			
			if(!( ToFrontCount % Drops ))
				SetRast(&( Scr->RastPort ), 0 );
			
			r = RangeRand( Wid/13 ) + Wid/25;
			x = RangeRand( Wid - 2*r ) + r;
			y = RangeRand( Hei - 2*r ) + r;
			
			incr = max( Wid/160, 1 );

			for( i = 0; i < r; i += incr )
			{
				WaitTOF();
				SetAPen(&( Scr->RastPort ), ( ULONG )RangeRand(( 1L << mP->Depth ) - 1 ) + 1 );
				DrawEllipse(&( Scr->RastPort ), x, y, i, i );
				if( i )
				{
					SetAPen(&( Scr->RastPort ), 0 );
					DrawEllipse(&( Scr->RastPort ), x, y, i - incr, i - incr );
				}
			}
			
			RetVal = ContinueBlanking();
		}
		UnblankMousePointer( Wnd );
	}
	else
		RetVal = FAILED;
	
	if( Scr )
		CloseScreen( Scr );
	
	return RetVal;
}
