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

#include <time.h>
#include <math.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <graphics/gfx.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <graphics/displayinfo.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include "/includes/convert.h"
#include "/includes/struct.h"
#include "/includes/tom_gadget.h"

char	*p_text_info="   effets\n\npar Thomas landspurg\nIl y a en fait 3 effets:\n- Un effet base sur les interferences\n- Un effet base sur un article paru\n  dans le Scientific America\n- Un autre effet base sur le meme\n  article, qui a deja ete utilise\n  pour faire un blanker sur station\n  Sun.\n\nIl suffit de selectionner l'effet\ndesire!";

struct	Screen		*s;
struct	RastPort	*rp;

#define reg register
extern	void __asm MultiPlot(reg __a0 struct Screen *,reg __d0 WORD, reg __d1 WORD,reg __d2 WORD);

#define	WIDTH	320
#define	HEIGHT	256

struct ColorSpec  ScreenColors[] = {
     0, 0x00, 0x00, 0x00,
     1, 0x0F, 0x00, 0x00,
     2, 0x0F, 0x03, 0x00,
     3, 0x0F, 0x05, 0x00,
     4, 0x0F, 0x07, 0x00,
     5, 0x0F, 0x09, 0x00,
     6, 0x0F, 0x0A, 0x00,
     7, 0x0F, 0x0B, 0x00,
     8, 0x0F, 0x0C, 0x00,
     9, 0x0F, 0x0D, 0x00,
    10, 0x0F, 0x0E, 0x00,
    11, 0x0F, 0x0F, 0x00,
    12, 0x0B, 0x0F, 0x00,
    13, 0x08, 0x0F, 0x00,
    14, 0x07, 0x0D, 0x00,
    15, 0x05, 0x0B, 0x00,
    16, 0x00, 0x00, 0x00,
    17, 0x04, 0x09, 0x00,
    18, 0x03, 0x06, 0x00,
    19, 0x03, 0x04, 0x00,
    20, 0x04, 0x04, 0x04,
    21, 0x05, 0x05, 0x05,
    22, 0x06, 0x06, 0x06,
    23, 0x07, 0x07, 0x07,
    24, 0x08, 0x08, 0x08,
    25, 0x09, 0x09, 0x09,
    26, 0x0A, 0x0A, 0x0A,
    27, 0x0B, 0x0B, 0x0B,
    28, 0x0C, 0x0C, 0x0C,
    29, 0x0D, 0x0D, 0x0D,
    30, 0x0E, 0x0E, 0x0E,
    31, 0x0F, 0x0F, 0x0F,
    ~0, 0x00, 0x00, 0x00 };
int	duree;
struct NewScreen   Screen = {
    0, 0, WIDTH,HEIGHT, 5,0,1,NULL,CUSTOMSCREEN,NULL,NULL,NULL,NULL};

UBYTE         *TypeLabels[] = {
	"interferences",
	"dragon",
	"Frac Effect",
	"Random",
	0l };


struct tom_gadget my_gadg[]={
	{"Ty_pe",	MX,	167,14,17,40,0, 0,0,0,(char *)TypeLabels},
	{"_Duree",	SLIDER,	100,60,100,10,100,50,100,0,0},
		{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}
};
static  unsigned        int     seed;


unsigned        int     my_rand()
{
        
        seed=25173*seed+138490;
        return(seed);
}

void	rotate()
{
	static	unsigned	int	compt=0;
	unsigned	int	n,i;

	n=compt;
	for(i=1;i<32;i++){
		n=n%31;
		SetRGB4(&s->ViewPort,i,ScreenColors[n+1].Red,ScreenColors[n+1].Green,ScreenColors[n+1].Blue);
		n++;
	}
	compt++;
}
void	interferences()
{
	int	i,x,y,factor,col;

	factor=my_rand()%20+1;
	for(y=-HEIGHT/2+1;(y<=0)&&(tst_end()==FALSE);y++){
		for(x=-WIDTH/2+1;x<=0;x++){
			col=((x*x+y*y)/factor)%31+1;
			MultiPlot(s,(WORD)(WIDTH/2-1+x),(WORD)(HEIGHT/2-1+y),col);
			MultiPlot(s,(WORD)(WIDTH/2-1-x),(WORD)(HEIGHT/2-1+y),col);
			MultiPlot(s,(WORD)(WIDTH/2-1+x),(WORD)(HEIGHT/2-1-y),col);
			MultiPlot(s,(WORD)(WIDTH/2-1-x),(WORD)(HEIGHT/2-1-y),col);

		}
		rotate();
	
	}
	for(i=0;(i<100)&&(tst_end()==FALSE);i++){
		Delay(2);
		WaitTOF();
		rotate();
	}
}

/************************************************************ carre */
/* suite de point quit provoaue des effets sympas */

void	carre()
{
	int	i,flg_end;
	float	xx,yy,x,y;
	float	a,t;

	t=(float)(my_rand()%100+10);
	a=PI-t/5000;
	SetRast(rp,0);
	SetRGB4(&s->ViewPort,1,0xf,0xf,0xf);
	SetAPen(rp,1);
	x=y=0;
	flg_end=FALSE;
	for(i=0;(i<duree)&&(flg_end==FALSE);i++){
		if(i%100==0){
			flg_end=tst_end();
		}
		xx=y-sin(x);
		yy=a-x;
		WritePixel(rp,WIDTH/2+(int)(2.0*x),HEIGHT/2+(int)(2.0*y));
		x=xx;
		y=yy;
	}
	SetRast(rp,0);
}
#define	SGN(x)	((x)<0?-1:1)
#define	ABS(x)	((x)<0?(-(x)):(x))

float	one_param()
{
	return ((float)(my_rand()%1000)/10-50.0);
}

/************************************************************ effet_sun */

void	effet_sun()
{
	float	x,y,xx,yy,a,b,c;
	int	i,xe,ye,flg_end;

	flg_end=FALSE;
	SetRast(rp,0);
	SetRGB4(&s->ViewPort,1,0xf,0xf,0xf);
	SetAPen(rp,1);
	x=0;y=0;

	a=(float)one_param();
	do{
		b=(float)one_param();
	}while(b==0.0);
	c=(float)one_param();
	for(i=0;(i<duree)&&(flg_end==FALSE);i++){
		if(i%100==0){
			flg_end=tst_end();
		}
		xx=y-SGN(x)*sqrt(ABS(b*x-c));
		yy=a-x;
		xe=WIDTH/2+(int)(1.0*x);
		ye=HEIGHT/2+(int)(1.0*y);
		if((xe>=0)&&(xe<WIDTH)&&(ye>=0)&&(ye<HEIGHT)){
			MultiPlot(s,(WORD)(xe),(WORD)(ye),(ReadPixel(rp,xe,ye)+1)%31+1);
		}
		x=xx;
		y=yy;
	}
	SetRast(rp,0);
}

void	__saveds	dark()
{
   int	effet,i;

   if (( s = OpenScreen( &Screen ))){
	for(i=0;i<32;i++){
		SetRGB4(&s->ViewPort,i,ScreenColors[i].Red,ScreenColors[i].Green,ScreenColors[i].Blue);
	}
	rp = &(s->RastPort);
	SetRast(rp,0);
	while(tst_end()==FALSE){
		if(my_gadg[0].value==3){
			effet=my_rand()%3;
		}else{
			effet=my_gadg[0].value;
		}
		duree=my_gadg[1].value*100;
		switch(effet){
			case 0:	
				interferences();
				break;
			case 1:	
				carre();
				break;
			case 2:	
			default:
				effet_sun();
				break;
		}
	}
	CloseScreen(s);
      }
}

void	proc_init()
{
        seed=VBeamPos();
}

void	proc_save()
{
}
void	proc_end()
{
}

