/*
 *  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 <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>

#include "/Garshnelib/Garshnelib_protos.h"
#include "/Garshnelib/Garshnelib_pragmas.h"

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

struct fPrefObject
{
	LONG Delay;
	LONG Percent;
	LONG TopScr;
};

extern struct fPrefObject nP;

LONG Blank( VOID *Prefs )
{
	struct fPrefObject *fP;
	struct Screen *FScr;
	LONG RetVal;
	
	if( FadeWnd )
		fP = &nP;
	else
		fP = ( struct fPrefObject * )Prefs;

	if( FScr = cloneTopScreen( FALSE, fP->TopScr ))
	{
		LONG ToFrontCount = 0, PctCount, BPG, Delay = fP->Delay + 1;
		struct Window *Wnd;
		ULONG *ColorTable;
		
		Wnd = BlankMousePointer( FScr );
		ColorTable = GetColorTable( FScr );
		
		BPG = AvgBitsPerGun( getTopScreenMode());
		PctCount = ( 1L << BPG ) * fP->Percent / 100;

		do
		{
			ToFrontCount++;
			WaitTOF();

			if( !( ToFrontCount%Delay ) && ( ToFrontCount/Delay ) < PctCount )
				FadeAndLoadTable( FScr, BPG, ColorTable );

			if(!( ToFrontCount % 60 ))
				ScreenToFront( FScr );

			if(!( ToFrontCount % 5 ))
				RetVal = ContinueBlanking();
		}
		while( RetVal == OK );

		UnblankMousePointer( Wnd );
		CloseScreen( FScr );
	}
	else
		RetVal = FAILED;

	return RetVal;
}
