/************************************************************************

Programma ...... Dotty.c
Versione ....... 1.0
Autore ......... Dale Luck - Commodore Amiga Technical Support Staff
Adattamento .... Luigi Callegari
Compilatore .... Aztec C V3.6a oppure Lattice C V4.0/4.01/5.0/5.02

********************************************************************** */

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

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

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

#define DEPTH   2
#define WIDTH  100
#define HEIGHT 50

struct NewWindow nw = {
	80, 80,
	WIDTH,HEIGHT,
	-1, -1,
	CLOSEWINDOW+REFRESHWINDOW+SIZEVERIFY+NEWSIZE,
	WINDOWDEPTH+WINDOWSIZING+WINDOWDRAG+WINDOWCLOSE+SIMPLE_REFRESH,
	NULL,
	NULL,
	(UBYTE *)"Dotty Window",
	NULL,
	NULL,
	20, 20,
	640, 255,
	WBENCHSCREEN
};

BOOL waiting_for_message = FALSE;

int usable_width(w)
struct Window *w;
{
   return ( w->Width - w->BorderLeft - w->BorderRight );
}

int usable_height(w)
struct Window *w;
{
   return( w->Height - w->BorderTop - w->BorderBottom );
}

#ifdef LATTICE
void _main()
#else
void main()
#endif
{
   register struct RastPort *rp;
   register struct Window *window;
   register LONG x, y, c;
   int xoffset, yoffset;
   LONG width, height;
   struct IntuiMessage *message;
   register ULONG MessageClass;

   if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",1L)))
   {
      printf("Manca la libreria Grafica!\n");
      _exit( 20L );
   }

   if (!(IntuitionBase = (struct IntuitionBase *)
                           OpenLibrary("intuition.library",1L)) )
   {
      CloseLibrary( (struct Library *)GfxBase);
      printf("Qui non c'e' Intuition!\n");
      _exit( 20L );
   }

   if ( ! (window = (struct Window *)OpenWindow( &nw )))
   {
      printf("La finestra non si apre!\n");
      CloseLibrary((struct Library *)IntuitionBase);
      CloseLibrary((struct Library *)GfxBase);
      _exit(20L);
   }
   
   width = usable_width(window);
   height = usable_height(window);
   rp = window -> RPort;
   xoffset = window -> BorderLeft;
   yoffset = window -> BorderTop;
   FOREVER {
      while (!(message = (struct IntuiMessage *)GetMsg(window->UserPort)) )
      {
         if (waiting_for_message)
	 	Wait( 1L << window -> UserPort -> mp_SigBit);
         else
         {
            x = RangeRand(width);
            y = RangeRand(height);
            c = RangeRand(32);
            SetAPen( rp, c);
            WritePixel( rp, xoffset+x, yoffset+y );
         }
      }
      MessageClass = message->Class;
      ReplyMsg((struct Message *)message);
      switch (MessageClass)
      {
         case CLOSEWINDOW  :  CloseWindow((struct Window *)window);
                              CloseLibrary((struct Library *)IntuitionBase);
                              CloseLibrary((struct Library *)GfxBase);
                              _exit(0L);

        case SIZEVERIFY   :  waiting_for_message = TRUE;
                             break;

        case REFRESHWINDOW:  BeginRefresh((struct Window *)window);
                             SetAPen(rp,0);
                             width = usable_width(window);
                             height = usable_height(window);
                             RectFill(rp,xoffset,yoffset,
                                         xoffset+width-1,yoffset+height-1);
                             EndRefresh(window,TRUE);
                             break;

        case NEWSIZE      :  waiting_for_message = FALSE;
                             width = usable_width(window);
                             height = usable_height(window);
                             break;
     }
  }
}

/* ************************ FINE DEL FILE SORGENTE *********************/


