/*
 * Name:        demo.c
 * Description: Simple demonstration of safe clipping routines
 * Author:      pak@star.sr.bham.ac.uk (Peter Knight)
 *
 */

#include <exec/types.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <stdio.h>
#include "safeclip.h"

/* Defines */
#define WIDTH 320
#define HEIGHT 200
#define DEPTH 1
#define XC (WIDTH>>1)
#define YC (HEIGHT>>1)
#define AREASIZE 1000

/* Variables */
struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;
struct Window *win;
struct Screen *scr;
struct RastPort *rp;
UWORD areaBuffer[AREASIZE];

/* Prototypes */
VOID SafeDemo (VOID);

int
main ()
{

  if (IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 36L))
    {

      if (GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 36L))
	{
 
	  if (scr = OpenScreenTags (NULL, 
				    SA_Width, WIDTH,
				    SA_Height, HEIGHT,
				    SA_Depth, DEPTH,
				    SA_Title, "Test of safe clipping routines...",
				    TAG_END))
	    {

	      if (win = OpenWindowTags (NULL, 
					WA_CustomScreen, scr,
					WA_Activate, TRUE,
					WA_Borderless, TRUE,
					WA_Backdrop, TRUE,
					TAG_END))
		{  

		  PLANEPTR planePtr;
		  if (planePtr = (PLANEPTR) AllocRaster (WIDTH, HEIGHT))
		    {
		      
		      /* Allocate standard area fill stuff */
		      struct AreaInfo areaInfo;
		      struct TmpRas tmpRas;
		      InitArea (&areaInfo, areaBuffer, (AREASIZE * 2) / 5);
		      InitTmpRas (&tmpRas, planePtr, RASSIZE (WIDTH, HEIGHT));
		      rp = win->RPort;
		      rp->AreaInfo = &areaInfo;
		      rp->TmpRas = &tmpRas;
		      
		      /* Mark boundary of clipping region */
		      Move (rp, XC - 75, YC - 75);
		      Draw (rp, XC - 75, YC + 75);
		      Draw (rp, XC + 75, YC + 75);
		      Draw (rp, XC + 75, YC - 75);
		      Draw (rp, XC - 75, YC - 75);
		      
		      /* Demonstrate clipping routines */
		      SafeDemo ();
		      
		      /* Free everything ... */
		      FreeRaster (planePtr, WIDTH, HEIGHT);
		      
		    }
		  else
		    fprintf (stderr, "Memory allocation failure\n");
		  
		  CloseWindow (win);
		  
		}
	      else
		fprintf (stderr, "Can't open window\n");
	      
	      CloseScreen (scr);      
	      
	    }
	  else
	    fprintf (stderr, "Can't open screen\n");

	  CloseLibrary ((struct Library *) GfxBase);

	}
      else
	fprintf (stderr, "Requires graphics.library v36+\n");
      
      CloseLibrary ((struct Library *) IntuitionBase);

    }
  else
    fprintf (stderr, "Requires intuition.library v36+\n");

}

/*
 * SafeDemo () - Simple demonstration of clipping routines.
 */
VOID
SafeDemo (VOID)
{

  /* Initialise clipping routines (up to 1000 vertices). */
  if (!SafeInit (1000))
    {
      WORD r;

      /* Set drawing limits. */
      SafeSetLimits (XC - 75 + 1, YC - 75 + 1,
		     XC + 75 - 1, YC + 75 - 1);

      /* Example of line clipping. */
      SetWindowTitles (win, 0, "SafeMove()/SafeDraw() ...");
      for (r = 0; r < 200; r+=4)
	{
	  SafeMove (XC, YC - r);
	  SafeDraw (rp, XC + r, YC);
	  SafeDraw (rp, XC, YC + r);
	  SafeDraw (rp, XC - r, YC);
	  SafeDraw (rp, XC, YC - r);
	  Delay (10);
	}
      Delay (100);

      /* Example of filled polygon clipping. */
      SetWindowTitles (win, 0, "SafeAreaDraw()/SafeAreaEnd() ...");
      for (r = 0; r < 200; r+=4)
	{
	  SafeAreaDraw (XC, YC - r);
	  SafeAreaDraw (XC + r, YC);
	  SafeAreaDraw (XC, YC + r);
	  SafeAreaDraw (XC - r, YC);
	  SafeAreaEnd (rp);
	  Delay (10);
	}
      Delay (100);

      /* Clearing the clipping region. */
      SetWindowTitles (win, 0, "SafeSetRast() ...");
      SafeSetRast (rp, 0);
      Delay (100);

      /* Example of safe blitting. */
      SetWindowTitles (win, 0, "SafeBltBitMapRastport() ...");
      DrawCircle (rp, 30, 30, 10);
      for (r = 0; r < 200; r++)
	{
	  LONG x = XC + RangeRand (200) - 100;
	  LONG y = YC + RangeRand (200) - 100;
	  SafeBltBitMapRastPort (rp->BitMap, 20, 20,
				 rp, x, y, 21, 21, 0x60);
	  Delay (2);
	}
      Delay (100);

      /* Setting the clipping region to a certain pen colour. */
      SetWindowTitles (win, 0, "SafeRectFill() ...");
      SafeRectFill (rp, 0, 0, WIDTH, HEIGHT);
      Delay (100);

      /* Free any resources. */
      SafeClose ();

    }

}


