  /* Fichier clock.dark.c				*/
/* Module appele par le programme principal	*/
/* pour effectuer un effet defini..appele par	*/
/* loadseg					*/
#include	<exec/types.h>
#include	<exec/ports.h>
#include	<graphics/display.h>
#include	<graphics/gfxbase.h>
#include	<intuition/intuition.h>
#include	<hardware/custom.h>
#include	<math.h>
#include	"/includes/struct.h"
#include	"/includes/tom_gadget.h"

extern	struct my_data	*p_info;
extern	struct	appel_proc	*p_data_proc;
extern	struct	GfxBase	*GfxBase;
int	compt,duree;

char	*p_text_info="Melt\n\nBased on a Program From ???\nAdapted and Modified!\n";

struct	Screen	*scr;
struct	RastPort *rp;

UBYTE         *TypeLabels[] = {
	"Type1",
	"Type2",
	"Type3",
	"Aleatoire",
	0l };

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


	/* Random(ULONG MaxValue):
	 *
	 *	Simple random number generation routine.
	 */
#ifndef custom
#define custom (*(struct Custom *)0xDFF000)
#endif	/* custom */


ULONG __regargs
Random(ULONG MaxValue)
{
	STATIC ULONG RandomSeed = 0xDEAD0123;

	RandomSeed = RandomSeed * custom . vhposr + 0xE153766F;

	return(RandomSeed % MaxValue);
}


void	melt(int	valy)
{
	int	x, y,			/* start positions 		*/
		dx, dy,			/* offsets         		*/
		u, v;			/* size            		*/
	int	TempA[32];		/* temp buffer     		*/
	char	mask;			/* bit-plane mask for blitter	*/

	while((tst_end()==FALSE)&&(compt++<duree)) {
	  WaitTOF();
	  for(mask=1; mask<+(1<<(scr->BitMap.Depth))-1; mask*=2) {
	    u = RangeRand(scr->Width - 3) + 1;
	    v = RangeRand(scr->Height - 3) + 1;
	    x = RangeRand(scr->Width - 1 - u) + 1;
	    y = RangeRand(scr->Height - 2 - v) + 1; 
	    dx = RangeRand(3) - 1;
	    dy = RangeRand(3) - valy;  /* also try dy = RangeRand(3) - 1;  */

	    BltBitMap(rp->BitMap, x, y,
	              rp->BitMap, x+dx, y+dy,
	              u, v, 0x0c0, mask, TempA);
	  } 	/* end of for mask */

	}     /* end of FOREVER */
}     /* end of melt */

void	get_uv(int	*u,int *v)
{
	    *u = RangeRand(scr->Width*2/3 ) + 3;
	    *v = RangeRand(scr->Height*2/3) + 3;
}
void	fade_out()
{

	int	color,cr,cg,cb,i,j;
	UWORD	save[32];

    for(j=0;(j<(1<<scr->BitMap.Depth))&&(j<32);j++){
	save[j]=GetRGB4(scr->ViewPort.ColorMap,j);
    }
    for(i=15;(i!=0)&&(tst_end()==FALSE);i--){
	Delay(5);
	WaitTOF();
	for(j=0;j<(1<<scr->BitMap.Depth);j++){
		color=save[j];
		cr=(color&0xf00)>>8;
		cg=(color&0xf0)>>4;
		cb=color&0xf;
	
		cr=(cr*i)>>4;
		cg=(cg*i)>>4;
		cb=(cb*i)>>4;
		SetRGB4(&(scr->ViewPort),j,cr,cg,cb);
	}
   }
    if(tst_end()==FALSE){
      fade_out();
     }

}

void	melt2()
{
	int	x, y,			/* start positions 		*/
		dx, dy,			/* offsets         		*/
		u, v;			/* size            		*/
	int	TempA[32];		/* temp buffer     		*/
	char	mask;			/* bit-plane mask for blitter	*/

        SetAPen(rp,0);
	Move(rp,0,0);
	Draw(rp,0,scr->Height-1);
	Draw(rp,scr->Width-1,scr->Height-1);
	Draw(rp,scr->Width-1,0);
	Draw(rp,0,0);

	while((tst_end()==FALSE)&&(compt++<duree)) {
	  WaitTOF();
	    get_uv(&u,&v);
	    x = 0;
	    y = 0; 
	    dx = RangeRand(3) ;
	    dy = RangeRand(3) ;  /* also try dy = RangeRand(3) - 1;  */

	    BltBitMap(rp->BitMap, x, y,
	              rp->BitMap, x+dx, y+dy,
	              u, v, 0x0c0, 0xff, TempA);
	    
	    get_uv(&u,&v);
	    x = scr->Width-u;
	    y = 0
	    dx = - RangeRand(3);
	    dy = RangeRand(3);  /* also try dy = RangeRand(3) - 1;  */

	    BltBitMap(rp->BitMap, x, y,
	              rp->BitMap, x+dx, y+dy,
	              u, v, 0x0c0, 0xff, TempA);

	    get_uv(&u,&v);
	    x = 0
	    y = scr->Height-v;
	    dx = RangeRand(3);
	    dy = - RangeRand(3);  /* also try dy = RangeRand(3) - 1;  */

	    BltBitMap(rp->BitMap, x, y,
	              rp->BitMap, x+dx, y+dy,
	              u, v, 0x0c0, 0xff, TempA);

	    get_uv(&u,&v);
	    x = scr->Width-u;
	    y = scr->Height-v;
	    dx = - RangeRand(3);
	    dy = - RangeRand(3);  /* also try dy = RangeRand(3) - 1;  */

	    BltBitMap(rp->BitMap, x, y,
	              rp->BitMap, x+dx, y+dy,
	              u, v, 0x0c0, 0xff, TempA);


	}     /* end of FOREVER */
}     /* end of melt */


/* Put here your own code */
/* But don't remember     */

void	__saveds	dark()
{
	int	effet;
	rp=p_data_proc->rp;
	scr=p_data_proc->s;

	FreeSprite (0);
	GfxBase->SpriteReserved|=1;
	effet=my_gadg[0].value;
	while(tst_end()==FALSE){
		compt=0;
		duree=2*my_gadg[1].value;

		if(my_gadg[0].value==3){
			effet=rand()%3;
		}else{
			effet=my_gadg[0].value;
		}

		switch(effet){
			case 0:	
				melt2();
				break;
			case 1:	
				melt(0);
				break;
			case 2:	
				melt(1);
				break;
		}
		fade_out();
	}

}

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