/* ****************************************
 Programma Graf1.c - Dimostrativo grafico
**************************************** */

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

#ifdef LATTICE
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#endif

#define INOM   "intuition.library"
#define GNOM   "graphics.library"
#define RP myw->RPort
#define SECS 3

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Window *myw, *OpenWindow();
struct Screen *mys, *OpenScreen();
struct ViewPort *vp;

USHORT colormap[8] = {
 0xfff, 0xf00, 0x0f0, 0x00f,
 /* Bianco, Rosso, Verde, Blu */
 0x0ff, 0xf0f, 0xff0, 0x000
 /* Ciano, Viola, Giallo, Nero */
};
USHORT punti [12] = {
 225,10, 300,75, 225,150, 75,150, 5,75, 75,50
};

UWORD punti2 [14] = { /* Ottagono */
 180,50, 210,80, 210,120,
 180,150, 100,150, 70,120, 70,80
};

UWORD pattern [8] = {
 0x0ff0, 0xf00f, 0xaaaa, 0x5555,
 0xa5a5, 0x5a5a, 0xf0f0, 0x0f0f
};

struct NewScreen NewScreenS = {
 0,0, 320,255, 3, 6,4, NULL,
 CUSTOMSCREEN, NULL, NULL, NULL, NULL
};

struct NewWindow NewWindowS = {
 0,10, 320,245, 2,1, CLOSEWINDOW,
 SMART_REFRESH+ACTIVATE+WINDOWCLOSE+BORDERLESS,
 NULL, NULL, (UBYTE *)"Grafica 1", NULL, NULL,
 0, 0, 0, 0, CUSTOMSCREEN
};

void die( n )
LONG n;
{
 if ( myw ) CloseWindow( (struct Window *) myw );
 if ( mys ) CloseScreen( (struct Screen *) mys );
 if (IntuitionBase) CloseLibrary( IntuitionBase );
 if (GfxBase) CloseLibrary( GfxBase );
 _exit( n );
}

void _main()
{
 int x, y, i;
 struct AreaInfo AInfo;
 struct TmpRas TRas;
 UWORD AreaBuf[200];
 PLANEPTR TBuf;

/* Apriamo le librerie e assegnamo i puntatori per compilatori */

 IntuitionBase=(struct IntuitionBase *)OpenLibrary(INOM,33L);
 if ( IntuitionBase == NULL ) die( 10 );

 GfxBase=(struct GfxBase *) OpenLibrary( GNOM, 33L );
 if ( GfxBase == NULL ) die( 11 );

/* Apriamo lo schermo, poi una finestra al suo interno */

 if ( !( mys = OpenScreen( &NewScreenS ) ) ) die( 12 );
 
 NewWindowS.Screen = mys;
 if ( !( myw = OpenWindow( &NewWindowS ) ) ) die( 13 );

/* Leggiamo la tavolozza dei colori con LoadRGB4() */

 vp = (struct ViewPort *) ViewPortAddress( myw );
 LoadRGB4( vp, &colormap[0], 8 );

/* Cambiamo colori e tracciamo linee colorate */

 SetDrMd( RP, JAM1 );
 for ( y = 50 ; y < 151 ; y += 3 )
   {
   SetAPen( RP, 5 );
   Move( RP, y/2 , y ); Draw( RP, 320, 150 );
   SetAPen( RP, 7 );
   Move( RP, y/3 , y ); Draw( RP, 320, 150 );
   }
 Delay ( SECS * 50L );

/* Traccia linee verticali tratteggiate automaticamente */

 SetDrPt( RP, 0x3333 );
 for ( x = 50 ; x < 200 ; x += 6 )
 {
   Move( RP, x, 20 ); Draw( RP, x, 180 );
 }
 Delay ( SECS * 50L );
/* Traccia un rettangolo colorato con RectFill() */

 SetDrPt( RP, 0xffff ); SetAPen( RP, 5 );
 RectFill( RP, 250, 10, 310, 95 );
 Delay ( SECS * 50L );

/* Traccia un poligono con PolyDraw() */

 SetAPen( RP, 2 ); Move( RP, 75, 50);
 PolyDraw( RP, 6, &punti[0] );
 Delay ( SECS * 50L );

/* Predispone area di lavoro temporanea */

 InitArea( &AInfo, AreaBuf, 80 );
 RP -> AreaInfo = &AInfo;

 TBuf = (PLANEPTR) AllocRaster( 320, 255 );
 if ( TBuf == NULL ) die( 13 );
 
 RP -> TmpRas = (struct TmpRas *) InitTmpRas \
  ( &TRas, TBuf, RASSIZE( 320, 255 ) );

/* Traccia ottagono con AreaMove() e AreaDraw() */

 SetOPen( RP, 1 ); AreaMove( RP, 100, 50 );
 for ( i = 0 ; i < 14 ; i += 2 )
   AreaDraw( RP, punti2[i], punti2[i+1] );
 AreaEnd( RP );
 Delay ( SECS * 50L );

/* Traccia rettangolo in basso ed esegue Flood() */

 SetOPen( RP, 3 ); SetAPen( RP, 3 );
 SetDrMd( RP, JAM1 ); Move( RP, 10, 180 ); 

 Draw( RP, 100, 180 ); Draw( RP, 100, 200 );
 Draw( RP, 10, 200 ); Draw( RP, 10, 180 );

 SetAPen( RP, 3 ); Flood( RP, 0, 11, 181 );

 Delay ( SECS * 50L );
/* Traccia un triangolo e lo riempie con sagoma */

 SetAfPt( RP, &pattern[0], 3 ); SetAPen( RP, 2 );
 SetBPen( RP, 4 ); AreaMove( RP, 311, 150 );
 AreaDraw( RP, 311, 220 ); AreaDraw( RP, 230, 220 );
 AreaEnd( RP ); SetAPen( RP, 4 );

/* Libera raster di lavoro temporaneo */
 FreeRaster( TBuf, 320, 255 );

/* Attendiamo evento CLOSEWINDOW */
 Wait( 1L << myw -> UserPort -> mp_SigBit );
 die( 0L );
}
