/*
 * Article 2308 of net.sources:
 * ion: version B 2.10.2 9/17/84 chuqui version 1.9 3/12/85; site unisoft.UUCP
 * Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site amiga.amiga.UUCP
 * Path: unisoft!dual!vecpyr!lll-lcc!lll-crg!seismo!harvard!bbnccv!bbncca!wanginst!decvax!decwrl!pyramid!amiga!dale
 * From: dale@amiga.UUCP (Dale Luck)
 * Newsgroups: net.sources
 * Subject: source to amiga dot graphics demo
 * Message-ID: <137@amiga.amiga.UUCP>
 * Date: 25 Oct 85 01:27:11 GMT
 * Date-Received: 30 Oct 85 05:04:57 GMT
 * References: <1359@ihlpg.UUCP>
 * Reply-To: dale@tooter.UUCP (Dale Luck)
 * Distribution: net
 * Organization: Commodore-Amiga Inc., 983 University Ave #D, Los Gatos CA 95030
 * Lines: 151
 * 
 * For those that are interested, here is a sample program that uses the
 * window libraries and message stuff on the amiga.  If there is going to be
 * significant quantities of shared amiga source maybe we should have an
 * net.sources.amiga
 *     Happy Hacking
 *       Dale
 * 
 */

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/ports.h>
#include <graphics/gfx.h>
#include <graphics/clip.h>
#include <graphics/view.h>
#include <graphics/rastport.h>
#include <graphics/layers.h>
#include <intuition/intuition.h>

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

#define DEPTH   2

#define WIDTH  100
#define HEIGHT 50

int waiting_for_message = FALSE;

char dotty[] = "Dotty Window";

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);
}

main()
{
   struct RastPort *rp;
   struct Window *window;
   struct NewWindow nw;
   int x,y,c;
   int xoffset,yoffset;
   int width,height;
   struct IntuiMessage *message;
   ULONG MessageClass;

   if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",1)))
   {
      printf("no graphics library!!!\n");
      exit(1);
   }

   if (!(IntuitionBase = (struct IntuitionBase *)
                           OpenLibrary("intuition.library",1)) )
   {
      CloseLibrary(GfxBase);
      printf("no intuition here!!\n");
      exit(1);
   }

   nw.LeftEdge = 50;
   nw.TopEdge = 50;
   nw.Width = WIDTH;
   nw.Height = HEIGHT;
   nw.DetailPen = -1;
   nw.BlockPen = -1;
   nw.IDCMPFlags = CLOSEWINDOW
                 | REFRESHWINDOW
                 | SIZEVERIFY
                 | NEWSIZE;
   nw.Flags = WINDOWDEPTH
            | WINDOWSIZING
            | WINDOWDRAG
            | WINDOWCLOSE
            | SIMPLE_REFRESH;
   nw.FirstGadget = NULL;
   nw.CheckMark = NULL;
   nw.Title = dotty;
   nw.Screen = NULL;
   nw.BitMap = NULL;

   nw.MinHeight = 10;
   nw.MinWidth = 10;
   nw.MaxHeight = 200;
   nw.MaxWidth = 640;

   nw.Type = WBENCHSCREEN;

   if (!(window = (struct Window *)OpenWindow(&nw) ))
   {
      printf("could not open the window\n");
      CloseLibrary(IntuitionBase);
      CloseLibrary(GfxBase);
      exit(2);
   }
   width = usable_width(window);
   height = usable_height(window);
   rp = window->RPort;
   xoffset = window->BorderLeft;
   yoffset = window->BorderTop;
   while (TRUE)      /* do this forever, or until user gets bored */
   {
      while (!(message = (struct IntuiMessage *)GetMsg(window->UserPort)) )
      {
         if (waiting_for_message)   Wait(1<<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(message);
      switch (MessageClass)
      {
         case CLOSEWINDOW  :  CloseWindow(window);
                              CloseLibrary(IntuitionBase);
                              CloseLibrary(GfxBase);
                              exit(0);

        case SIZEVERIFY   :  waiting_for_message = TRUE;
                             break;

        case REFRESHWINDOW:  BeginRefresh(window);
                             SetAPen(rp,0);
                             /* should not have to recalculate these */
                             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;
     }
  }
}


