 /* Fichier star.dark.c				*/
/* Module appele par le programme principal	*/
/* pour effectuer un effet defini..appele par	*/
/* loadseg					*/

#include	<exec/types.h>
#include	<dos/dos.h>
#include	<intuition/intuition.h>
#include	<intuition/intuitionbase.h>
#include	<intuition/gadgetclass.h>
#include	<graphics/gfx.h>
#include	<graphics/gfxbase.h>
#include	<graphics/gfxmacros.h>
#include	<graphics/rastport.h>
#include	<clib/exec_protos.h>
#include	<clib/intuition_protos.h>
#include 	<hardware/dmabits.h>
#include 	<hardware/custom.h>
#include	"/includes/struct.h"
#include	"/includes/tom_gadget.h"

struct	RastPort	*rp;
extern	struct	appel_proc	*p_data_proc;


char	p_ver[]=" $VER: Shuffle v1.1 (28/02/93)";

#define	WIDTH	640
#define	HEIGHT	256

char	*p_text_info="XSB-based Shuffle Blanker\n\nDivert and shuffle the\nfrontmost screen";

char *SpeedNames[] = /* List of Speed Modes */
 {"Slow","Medium","Fast","Very Fast",NULL};

struct tom_gadget my_gadg[]={
	{"_Speed",CYCLE,	125,20,100,15,0, 0,3,0,(char *)SpeedNames},
	{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}
};
int	speed=1;

struct timerequest *tr;
struct	RastPort	*rp,*rps;
int	width,height;

extern struct Custom __far custom;
extern	struct	GfxBase	*GfxBase;
extern	struct	IntuitionBase *IntuitionBase;

int	xs,ys,xb,yb,x1,y1;

#define rnd(x) (rand()%(x))
void	srand(int);
int	rand(void);
long	quitsig;
int	lastdir=-1;
int	rep;

/***************************************************************************/

void drawframe(int x,int y)
{
	SetAPen(rp,1);
	Move(rp,x*xs,y*ys);
	Draw(rp,x*xs+xs-1,y*ys);
	Draw(rp,x*xs+xs-1,y*ys+ys-1);
	Draw(rp,x*xs,y*ys+ys-1);
	Draw(rp,x*xs,y*ys);
}

/***************************************************************************/

void pause(void)
{
	WaitTOF();
}

/***************************************************************************/

void doscroll(int m)
{
	int c;
	switch(m) {
		case 0: for(c=0; (c<xs)&&(tst_end()==FALSE); c+=speed) { ScrollRaster(rp,-speed,0, xb*xs-xs, yb*ys, xb*xs+xs-1, yb*ys+ys-1); pause(); } break;
		case 1: for(c=0; (c<xs)&&(tst_end()==FALSE); c+=speed) { ScrollRaster(rp,speed,0, xb*xs, yb*ys, xb*xs+xs+xs-1, yb*ys+ys-1); pause(); } break;
		case 2: for(c=0; (c<ys)&&(tst_end()==FALSE); c+=speed) { ScrollRaster(rp,0,-speed, xb*xs, yb*ys-ys, xb*xs+xs-1, yb*ys+ys-1); pause(); } break;
		case 3: for(c=0; (c<ys)&&(tst_end()==FALSE); c+=speed) { ScrollRaster(rp,0,speed, xb*xs, yb*ys, xb*xs+xs-1, yb*ys+ys+ys-1); pause(); } break;
	}
}

/***************************************************************************/

void scroll(void)
{
	int m,rv=rand();

	for(;;) {
		rv+=VBeamPos()+rand();
		m=rv%4;
		if(lastdir==0 && m==1) continue;
		if(lastdir==1 && m==0) continue;
		if(lastdir==2 && m==3) continue;
		if(lastdir==3 && m==2) continue;
		if(lastdir==m) {
			if(++rep>4) continue;
		}
		else rep=0;
		if(!m && xb>0) { doscroll(0); xb--; break; }
		if(m==1 && xb<7) { doscroll(1); xb++; break; }
		if(m==2 && yb>0) { doscroll(2); yb--; break; }
		if(m==3 && yb<7) { doscroll(3); yb++; break; }
	}
	lastdir=m;
}

/***************************************************************************/

void __saveds dark()
{
	struct Screen *scr;
	ULONG 	x,y,n;
	ULONG	x2,y2,dir;
	
	FreeSprite (0);
	GfxBase->SpriteReserved|=1;
	srand(VBeamPos());
	rp=p_data_proc->rp;
	rps=p_data_proc->rps;
	scr=p_data_proc->s;
	width=p_data_proc->width;
	height=p_data_proc->height;

	xs=width/8; ys=height/8;
	xs=xs/8;
	xs=xs*8;
	ys=ys/8;
	ys=ys*8;
	for(x=0; x<8; x++) {
		for(y=0; y<8; y++) drawframe(x,y);
	}
	xb=rnd(8); yb=rnd(8);
	SetAPen(rp,0);

	/* Si on n'arrive pas a faire 8 carreau exactement de large,	*/
	/* on efface ce qui debord a gauche et a droite..		*/
	if(width!=xs*8)RectFill(rp,xs*8,0,width-1,height-1);
	if(height!=ys*8)RectFill(rp,0,ys*8,width-1,height-1);

	/*************************************/
	/* Maintenant disparition du carreau */
	/*************************************/
	x=xb*xs;
	y=yb*ys;
	x2=xs/2;
	y2=ys/2;
	if(rnd(2)==1)	dir=1;
	else		dir=-1;

	for(n=0;n<xs/2;n++){
		WaitTOF();
		ScrollRaster(rp,dir,dir,x,y,x+x2-1,y+y2-1);
		ScrollRaster(rp,-dir,dir,x+x2,y,x+xs-1,y+y2-1);
		ScrollRaster(rp,-dir,-dir,x+x2,y+y2,x+xs-1,y+ys-1);
		ScrollRaster(rp,dir,-dir,x,y+y2,x+x2-1,y+ys-1);
	}
	RectFill(rp,x,y,x+xs-1,y+ys-1);

	custom.dmacon=(1<<5);
	speed=(1<<my_gadg[0].value);
	while(tst_end()==FALSE) scroll();
}

/***************************************************************************/

void	proc_init()
{
	p_data_proc->type_screen=SCR_GIVEN;
}
void	proc_save()
{
}

void	proc_end()
{
}

