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

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <dos/dos.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <graphics/displayinfo.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#include <clib/diskfont_protos.h>


#include "/includes/struct.h"
#include "/includes/tom_gadget.h"

struct Library		*DiskfontBase;
extern  struct  GfxBase *GfxBase;
extern	struct	appel_proc	*p_data_proc;

#define	VOID_ARG	void	__regargs

char	*p_text_info=
"           Text v1.0\n"
"\n"
"This effect allow you to show\n"
"a little text like on your screen\n"
"\n"
"Nothing really fantastic, but evry\n"
"blanker should have this minima....\n"
"\n"
" @ Thomas Landspurg\n";

WORD	areabuffer[25];
struct	AreaInfo	myarea;
struct	TmpRas	tmpras;

struct	RastPort	*rp;
struct	Screen		*s;
struct	TextAttr myta={"topaz                   ",0,0,0};

UWORD	px,py,tx,ty,x,y;
int	dirx,diry;
#define	WIDTH	640
#define	HEIGHT	256

UWORD  ScreenColors[] = {
    0x000,
    0xFFF,
    0xFE2,
    0x68B,
    0xF90,
    0x0F0,
    0x00F,
    0xF0F,
    0x000 };

struct NewScreen Screen= {
     0, 0, WIDTH, HEIGHT, 1,0,1,HIRES,CUSTOMSCREEN,NULL,NULL,NULL,NULL};


#define	TAILLE_BUF	100
char	buffer_text[TAILLE_BUF]="";

struct	tom_gadget	my_gadg[]={
	{"T_ext",	STRING,  50, 10,250,14,0,TAILLE_BUF,0,0,buffer_text},
	{"_Fonts",	FONT,    80, 25, 90,12,0,         0,0,0,(char *)&myta},
	{"_Resolution",	SCREEN,  80, 40, 90,12,0,         0,0,0,0},
	{"Speed_X",	SLIDER,  80, 60, 90,10,1,0,5,0,0},
	{"Speed_Y",	SLIDER,  80, 80, 90,10,1,0,5,0,0},
	{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}};

static	short	int	centre_x,centre_y,old_x,old_y,rayon;


VOID_ARG	init_rp(struct	RastPort *rp)
{
        struct TextFont *myfont;

	if(DiskfontBase){
               if (myfont = OpenDiskFont(&myta))
                    {
	                    SetFont(rp, myfont);
                            SetSoftStyle(rp,   myta.ta_Style ^ myfont->tf_Style,
                                      (FSF_BOLD | FSF_UNDERLINED | FSF_ITALIC));
		    }
	}
}

void	aff_texte(struct	RastPort *rp)
{
	int	len;
	struct	TextExtent	result;

	SetAPen(rp,1);
	len=strlen(my_gadg[0].p_data);
	ty=myta.ta_YSize;
	tx=TextLength(rp,my_gadg[0].p_data,len);
	if(tx+10>s->Width){
		tx=s->Width-10;
		len=TextFit(rp,my_gadg[0].p_data,len,&result,NULL,1,s->Width-10,ty);
		dirx=0;
	}
	x=(s->Width-tx)/2;
	y=(s->Height-ty)/2;
	Move(rp,x,y+ty);
	ty+=8;
	Text(rp,my_gadg[0].p_data,len);
	
}

void	dark()
{
  struct	tom_gadget *pg;

  pg=&my_gadg[2];
  if((GfxBase->LibNode.lib_Version>=37)&&(pg->d3!=0)){
	s=OpenScreenTags(  NULL,
			   SA_Width,(ULONG)pg->d1,
			   SA_Height,(ULONG)pg->d2,
                           SA_Depth,(ULONG)pg->d3,
			   SA_Overscan,(ULONG)pg->value,
                           SA_DisplayID,(ULONG)pg->p_data,
                           SA_Quiet,TRUE,TAG_DONE);
/*
	 width=pg->d1;
	 height=pg->d2;
*/
   }else{
	s=OpenScreen(&Screen);
   }
   if (s){

	rp = &(s->RastPort);

	SetRast(rp,0);
	FreeSprite (0);
	GfxBase->SpriteReserved|=1;
	LoadRGB4(&s->ViewPort,ScreenColors,1<<3);

	dirx=-my_gadg[3].value;
	diry=-my_gadg[4].value;
	init_rp(rp);
	aff_texte(rp);
	while(tst_end()==FALSE){
		WaitTOF();
		ScrollRaster(rp,dirx,diry,x,y,x+tx,y+ty);
		x-=dirx;
		y-=diry;
		if(x-dirx<=0){
			dirx=-dirx;
		}
		if(y-diry<=0){
			diry=-diry;
		}
		if(x-dirx+tx>=s->Width){
			dirx=-dirx;
		}
		if(y-diry+ty>=s->Height){
			diry=-diry;
		}
	}
	DCloseScreen(s);
   }
}

void	proc_init()
{
        DiskfontBase = OpenLibrary("diskfont.library", 37L);
	p_data_proc->code_ret=DARK_WB_20;
}

void	proc_save()
{
}
void	proc_end()
{
	if(DiskfontBase)CloseLibrary(DiskfontBase);
}

