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

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

#include "LineBlank.h"
#include "/utility.h"

struct lPrefObject { USHORT Mode; };

extern	struct	lPrefObject	nP;
extern		UBYTE	*prefData;

USHORT doLines(struct Screen *LScr, USHORT Mode)
{
	struct RastPort *rp = &(LScr->RastPort);
	int i, x, y, midx, midy, x2,y2;
	
	SetAPen(rp, 1);
	switch (Mode)
	{
		case 0:
			/* Cross
			*/
			midy = LScr->Height-2 - (LScr->Height-1)%2;
			midx = LScr->Width-2 - (LScr->Width-1)%2;
			for ( i = 0; (i < 4) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)) ; i+=2)
			{
				for (x = i, x2 = midx-i;
						(x < LScr->Width-1) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C));
						 x+= 4, x2-= 4)
				{
					Move(rp,x2,LScr->Height-1);
					Draw(rp,x,0);
					Move(rp,x2,0);
					Draw(rp,x,LScr->Height-1);
				}
				for (y = i, y2 = midy-i;
						(y < LScr->Height-1) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C));
						 y+= 4, y2-= 4)
				{
					Move(rp,0,y2);
					Draw(rp,LScr->Width-1,y);
					Move(rp,LScr->Width-1,y2);
					Draw(rp,0,y);
				}
			}
			
			break;
		case 1:
			/* Cycle
			*/
			midx = LScr->Width / 2;
			midy = LScr->Height / 2;
			for ( i = 0; (i < 4) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)) ; i++)
			{
				for (x = i; (x < LScr->Width) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x+= 4)
				{
					Move(rp,midx,midy);
					Draw(rp,x,0);
				}
				for (y = i; (y < LScr->Height) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y+= 4)
				{
					Move(rp,midx,midy);
					Draw(rp,LScr->Width-1,y);
				}
				for (x = LScr->Width-1-i; (x > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x-= 4)
				{
					Move(rp,midx,midy);
					Draw(rp,x,LScr->Height-1);
				}
				for (y = LScr->Height-1-i; (y > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y-= 4)
				{
					Move(rp,midx,midy);
					Draw(rp,0,y);
				}
			}
			break;
		case 2:
			/* Perimeter
			*/
			for ( i = 0; (i < 3) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)) ; i++)
			{
				for (x = i; (x < LScr->Width) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x+= 3)
				{
					Move(rp,0,LScr->Height-1);
					Draw(rp,x,0);
				}
				for (y = i; (y < LScr->Height) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y+= 3)
				{
					Move(rp,0,LScr->Height-1);
					Draw(rp,LScr->Width-1,y);
				}
				for (y = LScr->Height-1-i; (y > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y-= 3)
				{
					Move(rp,LScr->Width-1,LScr->Height-1);
					Draw(rp,0,y);
				}
				for (x = i; (x < LScr->Width) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x+= 3)
				{
					Move(rp,LScr->Width-1,LScr->Height-1);
					Draw(rp,x,0);
				}
				for (x = LScr->Width-1-i; (x > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x-= 3)
				{
					Move(rp,LScr->Width-1,0);
					Draw(rp,x,LScr->Height-1);
				}
				for (y = LScr->Height-1-i; (y > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y-= 3)
				{
					Move(rp,LScr->Width-1,0);
					Draw(rp,0,y);
				}
				for (y = i; (y < LScr->Height) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); y+= 3)
				{
					Move(rp,0,0);
					Draw(rp,LScr->Width-1,y);
				}
				for (x = LScr->Width-1-i; (x > 0) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)); x-= 3)
				{
					Move(rp,0,0);
					Draw(rp,x,LScr->Height-1);
				}
			}
			break;
		case 3:
			/* Propeller
			*/
			for ( i = 0; (i < 4) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)) ; i++)
			{
				for (x = i, x2 = LScr->Width-1-i;
						(x < LScr->Width) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C));
						 x+= 4, x2-= 4)
				{
					Move(rp,x2,LScr->Height-1);
					Draw(rp,x,0);
				}
				for (y = i, y2 = LScr->Height-1-i;
						(y < LScr->Height) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C));
						 y+= 4, y2-= 4)
				{
					Move(rp,0,y2);
					Draw(rp,LScr->Width-1,y);
				}
			}
			
			break;
		case 4:
			/* Square
			*/
			for ( i = 0; (i < 10) && (!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)) ; i++)
			{
				for(x = i; x < LScr->Width; x+=20)
				{
					Move(rp,x,0);
					Draw(rp,x,LScr->Height-1);
				}
				for(y = i; y < LScr->Height; y+=20)
				{
					Move(rp,0,y);
					Draw(rp,LScr->Width-1,y);
				}
				for(x = 19-i; x < LScr->Width; x+=20)
				{
					Move(rp,x,0);
					Draw(rp,x,LScr->Height-1);
				}
				for(y = 19-i; y < LScr->Height; y+=20)
				{
					Move(rp,0,y);
					Draw(rp,LScr->Width-1,y);
				}
			}
			break;
	}
	if (SetSignal(0L, 0L) & SIGBREAKF_CTRL_C)
	{
		SetSignal (0L, SIGBREAKF_CTRL_C);
		return 0;
	}
	return 1;
}


VOID blank( VOID )
{
	struct	lPrefObject	*lP;
	struct	Screen		*LScr;

	if( LineBlankWnd ) lP = &nP;
	else lP = ( struct lPrefObject * )prefData;

	if( LScr = cloneTopScreen() ) {
		BlankMousePointer( LScr );
		if (doLines(LScr, lP->Mode))
		{
			/* fadeScreen just to be safe if the line colour wasn't black
			*/
			if( fadeScreen( LScr, 0 )) Wait( SIGBREAKF_CTRL_C );
			SetSignal( 0L, SIGBREAKF_CTRL_C );
		}
		UnblankMousePointer();
		closeTopScreen( LScr );
	}
}
