 /* 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/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/convert.h"
#include	"/includes/struct.h"
#include	"/includes/tom_gadget.h"

struct	RastPort	*rp;
extern	struct	appel_proc	*p_data_proc;

#define	WIDTH	640
#define	HEIGHT	256

char	*p_text_info="XSB 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,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;

void srand(int);
int rand(void);
#define rnd(x) (rand()%(x))
long quitsig;

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)
{
/*
	if(SetSignal(0,0)&quitsig) return;
	tr->tr_time.tv_secs=0;
	tr->tr_time.tv_micro=300;
	DoIO(tr);
*/
	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;
	}
}

int lastdir=-1,rep;
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;
	int x,y;
	
	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);
	RectFill(rp,xb*xs,yb*ys,xb*xs+xs-1,yb*ys+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()
{
}

