
#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <math.h>

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;


void AttivaG()
{
 GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
 if (GfxBase==NULL) exit(1);

 IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
 if (IntuitionBase==NULL) {
      CloseLibrary(GfxBase);
      exit(1);
      };
}

void EndG()
{
 CloseLibrary(IntuitionBase);
 CloseLibrary(GfxBase);
}

 
struct Screen *Schermo(top,w,h,d,dp,bp,vm,font,title)
SHORT top,w,h,d;
UBYTE dp,bp,*title;
USHORT vm;
struct TextAttr *font;
{
 struct Screen *s;
 struct NewScreen ns;

 ns.LeftEdge=0;
 ns.TopEdge=top;
 ns.Width=w;
 ns.Height=h;
 ns.Depth=d;
 ns.DetailPen=dp;
 ns.BlockPen=bp;
 ns.ViewModes=vm;
 ns.Type=CUSTOMSCREEN;
 ns.Font=font;
 ns.DefaultTitle=title;
 ns.Gadgets=NULL;
 ns.CustomBitMap=NULL;

 s=(struct Screen *)OpenScreen(&ns);
 return(s);

}

struct Window *Finestra(le,te,wi,h,dp,bp,idcmp,f,g,i,t,s,mw,mh,xw,xh)
SHORT le,te,wi,h,mw,mh,xw,xh;
UBYTE dp,bp,*t;
USHORT idcmp;
ULONG f;

struct Gadget *g;
struct Image *i;
struct Screen *s;
{
 struct Window *w;
 struct NewWindow nw ;
 ULONG ts;

 ts=CUSTOMSCREEN;
 if (s==NULL) ts=WBENCHSCREEN;

 nw.LeftEdge=le;
 nw.TopEdge=te;
 nw.Width=wi;
 nw.Height=h;
 nw.DetailPen=dp;
 nw.BlockPen=bp;
 nw.IDCMPFlags=idcmp;
 nw.Flags=f;
 nw.FirstGadget=g;
 nw.CheckMark=i;
 nw.Title=t;
 nw.Screen=s;
 nw.BitMap=NULL;
 nw.MinWidth=mw;
 nw.MinHeight=mh;
 nw.MaxWidth=xw;
 nw.MaxHeight=xh;
 nw.Type=ts;

 w=(struct Window *)OpenWindow(&nw);
return(w);

}

struct RastPort *CRast(w)
struct Window *w;
{
 struct RastPort *rp;
 rp=w->RPort;
 return(rp);
}

void Line(w,x,y,x1,y1,c)
struct Window *w;
unsigned x,y,x1,y1,c;

{
 struct RastPort *rp;
 rp=(struct RastPort *)CRast(w);
 SetAPen(rp,c);
 SetDrMd(rp,JAM1);
 Move(rp,x,y);
 Draw(rp,x1,y1);
}

void Plot(w,x,y,c)
struct Window *w;
unsigned x,y,c;
{
 struct RastPort *rp;

 rp=(struct RastPort *)CRast(w);
 SetAPen(rp,c);
 SetDrMd(rp,JAM1);
 WritePixel(rp,x,y);
}

struct ViewPort *CView(s)
struct Screen *s;
{
 struct ViewPort *vp;
 vp=&s->ViewPort;
 return(vp);
}

void Box(w,x,y,x1,y1,c)
struct Window *w;
unsigned x,y,x1,y1,c;
{
 Line(w,x,y,x1,y,c);
 Line(w,x1,y,x1,y1,c);
 Line(w,x1,y1,x,y1,c);
 Line(w,x,y1,x,y,c);
}

void BoxF(w,x,y,x1,y1,c)
struct Window *w;
unsigned x,y,x1,y1,c;
{
 struct RastPort *rp;
 rp=(struct RastPort *)CRast(w);
 SetAPen(rp,c);
 SetDrMd(rp,JAM1);
 Move(rp,x,y);
 RectFill(rp,x,y,x1,y1);
}

void Palette(s,c,v1,v2,v3)
struct Screen *s;
unsigned c,v1,v2,v3;
{
 struct ViewPort *vp;
 vp=(struct ViewPort *)CView(s);
 SetRGB4(vp,c,v1,v2,v3);
}

unsigned Color(w,x,y)
struct Window *w;
unsigned x,y;
{
 struct RastPort *rp;
 unsigned c;
 rp=(struct RastPort *)CRast(w);
 c=ReadPixel(rp,x,y);
 return(c);
}


void Scroll(w,x1,y1,x2,y2,dx,dy)
struct Window *w;
UBYTE x1,y1,x2,y2;
int dx,dy;
{
 struct RastPort *rp;
 rp=(struct RastPort *)CRast(w);
 ScrollRaster(rp,dx,dy,x1,y1,x2,y2);
}

void Copy(w1,x1,y1,w2,x2,y2,l,h)
struct Window *w1,*w2;
unsigned x1,y1,x2,y2,l,h;
{
 struct RastPort *r1,*r2;
 r1=(struct RastPort *)CRast(w1);
 r2=(struct RastPort *)CRast(w2);
 ClipBlit(r1,x1,y1,r2,x2,y2,l,h,192);
}


void Clw(w)
struct Window *w;
{
 struct RastPort *rp;
 rp=(struct RastPort *)CRast(w);
 SetRast(rp,0);
}

void Scrivi(w,x,y,c,s)
struct Window *w;
unsigned x,y,c;
char *s;
{
 struct RastPort *rp;
 int l;


 rp=(struct RastPort *)CRast(w);
 l=strlen(s);

 SetAPen(rp,c);
 SetBPen(rp,0);
 SetDrMd(rp,JAM2);
 Move(rp,x,y);
 Text(rp,s,l);
}




 int flag = 0; /* Flag esterno per riempimento vettori seno-coseno */


void Circle(w,xo,yo,rx,ry,c)
struct Window *w;
unsigned xo,yo,rx,ry,c;
{
 static float sn[361],cs[361];
 unsigned x,y,xv,yv;
 int i;
 if (flag==0) {
                flag=5;
                for(i=0;i<361;i++)
                            {
                             sn[i]=sin(i*PI/180);
                             cs[i]=cos(i*PI/180);
                            };
              };

 xv=rx+xo;
 yv=yo;
 for(i=0;i<361;i++)
      {
       x=cs[i]*rx+xo;
       y=sn[i]*ry+yo;
       Line(w,xv,yv,x,y,c);
       xv=x;
       yv=y;
      };
}


UWORD StdPattern[]={0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff};

int Paint(w,x,y,c,p)
struct Window *w;
unsigned x,y,c;
UWORD *p;
{
 struct Screen *s;
 PLANEPTR *TBuf;
 struct RastPort *rp;
 struct TmpRas TRas;
 SHORT l;
 SHORT h;

 s=w->WScreen;  /* Trova lo schermo che contiene la finestra */

 l=s->Width;  /* Memorizza le misure dello schermo */
 h=s->Height;

 if (p==NULL) p=StdPattern;

 rp=(struct RastPort *)CRast(w);
 if ((TBuf=(PLANEPTR *)AllocRaster(l,h))==NULL)
                                                    return(-1);


 rp->TmpRas=(struct TmpRas *)InitTmpRas(&TRas,TBuf,RASSIZE(l,h));

 SetAfPt(rp,p,3);
 SetAPen(rp,c);
 SetBPen(rp,0);
 SetDrMd(rp,JAM2);
 Flood(rp,1,x,y);
 FreeRaster(TBuf,l,h);
 return(0);
}


int Alert(tipo,y,t,s)
UBYTE tipo,y;
UBYTE t[];
UWORD s;
{
 UBYTE c[2000]; /* Spazio per il messaggio: max 2K */
 int i;

 c[0]=0; /* x=0 y=y */
 c[1]=0;
 c[2]=y;

  i=0;
  while((c[i+3]=t[i])!=NULL) i++; /* Copia t in c */


 return(DisplayAlert(tipo,c,s)); /* ALERT */

}

