/* **************************************
 Graf2.c - Dimostrativo funzioni grafiche
************************************** */

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

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

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

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

UWORD punti [14] = {
 10,10, 90,10, 70,40,
 30,40, 90,10, 30,40, 10,10
};

UWORD sagoma [8] = {
 0x0000, 0x667e, 0x6618, 0x7e18,
 0x6618, 0x6618, 0x667e, 0x0000
};

struct NewScreen NewScreenS = {
 0,0, 320,255, 2, 2,1, NULL,
 CUSTOMSCREEN, NULL, NULL, NULL, NULL
};

struct NewWindow NewWindowS = {
 0,10, 320,200, -1,-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()
{
 register int ri, co;
 void die();

/* 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 );

/* Cicliamo istantaneamente il colore con SetRast */

 SetWindowTitles( myw, "SetRast()", "CustomScreen" );
 Delay( 50 * 3L ); SetRast( RP, 3 );
 Delay( 50 * 3L ); SetRast( RP, 0 );

/* Tracciamo dei rettangoli da fare scorrere */

 SetWindowTitles( myw, "ScrollRaster()", NULL ); 
 for ( ri = 1 ; ri < 5 ; ri++ )
   for ( co = 1; co < 8 ; co++ )
     RectFill( RP,co*30, ri*20, co*30+20, ri*20+12 );

/* Inizia lo scrolling */

 for ( ri = 10 ; ri < 100 ; )
   {
   for ( co = 0 ; co < 81 ; co++ )
   ScrollRaster( RP,-1,0, co,ri, co+230,ri+82 );
   ScrollRaster( RP, 0,-10, 80,ri, co+310,ri+92 );
   ri += 10;
  
   for ( co = 0 ; co < 81 ; co++ )
     ScrollRaster( RP, 1,0, 80-co,ri, 310-co,ri+82 );
   ScrollRaster( RP, 0,-10, 0,ri, 230,ri+92 );
     ri += 10;
   };
 SetRast( RP, 0 );

/* Tracciamo poligonale e copiamo con ClipBit() */

 SetWindowTitles( myw, "ClipBit()", NULL );
 Move( RP, 10, 10 ); PolyDraw( RP, 7, &punti[0] ); 
 Delay ( 3L * 50 );
 ClipBlit( RP, 10,10, RP, 10,100, 80,40, 0x30 );
 ClipBlit( RP, 10,10, RP, 100,100, 80,40, 0x60 );
 ClipBlit( RP, 10,10, RP, 100,10, 80,40, 0xc0 );

/* Attendiamo un evento CLOSEWINDOW */
 SetWindowTitles( myw, "<- Click qui per finire", NULL ); 
 Wait( 1L << myw -> UserPort -> mp_SigBit );
 die( 0 );
}
