/*
 *  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 <hardware/custom.h>

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

#include <string.h>

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

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

#define RAND( base, offset ) (( LONG )( RangeRand( base ) + offset ))

struct ModulePrefs
{
	LONG Mode;
	LONG Delay;
	LONG Cycle;
	BYTE Text[128];
	BYTE fName[64];
	struct TextAttr Font;
};

typedef struct _mPoint
{
	LONG x;
	LONG y;
} mPoint;

extern struct ModulePrefs nP;
extern __far struct Custom custom;

LONG vals[] = {
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
	0x0D, 0x0E, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06,
	0x05, 0x04, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	};

LONG Blank( VOID *Prefs )
{
	LONG Wid, Hei, dx = -1, dy = -1, len, numc, count = 0;
	LONG c1 = 0, c2 = 14, c3 = 28, RetVal = OK;
	mPoint new, old, size, min, max;
	struct TextFont *font;
	struct ModulePrefs *tP;
	struct Screen *TextScr;
	struct RastPort *Rast;
	struct ViewPort *View;
	struct Window *Wnd;
	
	if( TextWnd )
		tP = &nP;
	else
		tP = ( struct ModulePrefs * )Prefs;

	tP->Font.ta_Name = tP->fName;
	font = OpenDiskFont( &tP->Font );
	if( !font )
		tP->Font.ta_Name = "topaz.font";
	
	TextScr = OpenScreenTags( 0l, SA_DisplayID, tP->Mode, SA_Depth, 1,
							 SA_Quiet, TRUE, SA_Overscan, OSCAN_STANDARD,
							 SA_Font, &( tP->Font ), SA_Behind, TRUE,
							 TAG_DONE );
	if( TextScr )
	{
		Rast = &( TextScr->RastPort );
		View = &( TextScr->ViewPort );
		Wid = TextScr->Width;
		Hei = TextScr->Height;
		
		numc = strlen( tP->Text );
		while(( len = TextLength( Rast, tP->Text, numc )) >= Wid - 5 )
			numc--;
		
		SetRGB4( View, 0, 0L, 0L, 0L );
		switch( tP->Cycle )
		{
		case 0:
			SetRGB4( View, 1, vals[c1], vals[c2], vals[c3] );
			break;
		case 1:
			SetRGB4( View, 1, RAND( 10, 5 ), RAND( 10, 5 ), RAND( 10, 5 ));
			break;
		case 2:
			SetRGB4( View, 1, 15, 15, 15 );
			break;
		case 3:
			setCopperList( Hei, 1, View, &custom );
			break;
		}		
		
		new.x = old.x = ( Wid - len ) / 2;
		new.y = old.y = ( Hei - tP->Font.ta_YSize ) / 2;
		size.x = len + 2;
		size.y = tP->Font.ta_YSize + 2;
		
		min.x = 0;
		min.y = 0;
		max.x = Wid - size.x - 3;
		max.y = Hei - size.y - 3;
		
		SetAPen( Rast, 1 );
		Move( Rast, old.x + 1, old.y + font->tf_Baseline + 1 );
		Text( Rast, tP->Text, numc );
		
		Wnd = BlankMousePointer( TextScr );
		ScreenToFront( TextScr );
		
		while( RetVal == OK )
		{
			WaitTOF();

			if(!( count++ % 60 ))
				ScreenToFront( TextScr );
			
			if( !tP->Cycle && !( count % 10 ))
			{
				c1 = ++c1 % 42;
				c2 = ++c2 % 42;
				c3 = ++c3 % 42;
				SetRGB4( View, 1, vals[c1], vals[c2], vals[c3] );
			}
			
			if( !tP->Delay || !( count % tP->Delay ))
			{
				new.x += dx;
				new.y += dy;
				
				if( new.x < min.x )
				{
					new.x = min.x;
					dx = 1;
				}
				else if( new.x > max.x )
				{
					new.x = max.x;
					dx = -1;
				}
				
				if( new.y < min.y )
				{
					new.y = min.y;
					dy = 1;
				}
				else if( new.y > max.y )
				{
					new.y = max.y;
					dy = -1;
				}
				
				BltBitMapRastPort( Rast->BitMap, old.x, old.y, Rast, new.x,
								  new.y, size.x, size.y, 0xC0 );
				
				old = new;
			}
			RetVal = ContinueBlanking();
		}
		UnblankMousePointer( Wnd );

		if( tP->Cycle == 3 )
			clearCopperList( View );
	}
	else
		RetVal = FAILED;
	
	if( TextScr )
		CloseScreen( TextScr );

	if( font )
		CloseFont( font );

	return RetVal;
}
