/****************************************************************
 * Polygon drawing demo © 1987 John M. Olsen.
 * This demo uses the AreaMove, AreaDraw and AreaEnd Functions.
 * It uses a window on the workbench screen, and the polygons can be
 * any of the possible 4 colors.
 *
 * My appologies for the sparceness of comments.  It is hopefully clear
 * what I was doing for the most part.
 *
 * This is meant to be compiled with the Manx compiler, and has not been
 * tried under Lettuce.
 *
 * Permission is given to distribute this code wherever you want as long
 * as this notice remains with it.  Do not use any part of it in a commercial
 * application without written consent of the author:
 *
 * John M. Olsen
 * 1547 Jamestown Drive
 * Salt Lake City, UT  84121
 ****************************************************************/

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

void *OpenLibrary();
struct Window *OpenWindow(), *w;
struct IntuiMessage *GetMsg(), *msg;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

BYTE *AllocRaster();
WORD areabuffer[250];
struct TmpRas tmpras;
struct AreaInfo myAreaInfo;

struct NewWindow ww =
{				/****************/
	0,			/* LeftEdge	*/
	10,			/* TopEdge	*/
	180,			/* Width	*/
	40,			/* Height	*/
	-1,			/* DetailPen	*/
	-1,			/* BlockPen	*/
	CLOSEWINDOW,		/* IDCMP	*/
	WINDOWCLOSE | ACTIVATE | NOCAREREFRESH | WINDOWDRAG | WINDOWDEPTH
	| WINDOWSIZING,
	NULL,			/* *FirstGadget	*/
	NULL,			/* *CheckMark	*/
	(UBYTE *)"Poly Window",	/* *Title	*/
	NULL,			/* Screen	*/
	NULL,			/* *Bitmap	*/
	180,20,640,400,		/* Min/Max w,h	*/
	WBENCHSCREEN		/* Type		*/
};

main(argc,argv)
int argc;
char *argv[];
{
	setup();
	drawstuff();
	die(0);
}

setup()
{
	if(!(GfxBase = OpenLibrary("graphics.library",0l)))
		die(1);
	if(!(IntuitionBase = OpenLibrary("intuition.library", 0l)))
		die(2);
	if(!(w = OpenWindow(&ww)))
		die(4);
	InitArea(&myAreaInfo, areabuffer, 100l);
	w->RPort->AreaInfo = &myAreaInfo;
	tmpras.RasPtr = (BYTE *) AllocRaster(640l, 400l);
	tmpras.Size = (long) RASSIZE(640l, 400l);
	w->RPort->TmpRas = &tmpras;
}

drawstuff()
{
	struct RastPort *r;
	long colr = 0l, loop, x, y;	

	r = w->RPort;
	msg = GetMsg(w->UserPort);
	while(msg->Class != CLOSEWINDOW)
	{
		msg = GetMsg(w->UserPort);
		SetAPen(r, colr);
		SetBPen(r, colr);
		colr++;
		if(colr > 3l)
			colr = 0l;
		for(loop = 0l; loop < 10l; loop++)
		{	/* define a vertex */
			x = (long)random(w->Width - w->BorderLeft
				- w->BorderRight) + w->BorderLeft;
			y = (long)random(w->Height - w->BorderTop
				- w->BorderBottom) + w->BorderTop;
			if(loop == 0l)
				AreaMove(r, x, y);
			else
				AreaDraw(r, x, y);
		}
		AreaEnd(r);
	}
}

/* A quick and dirty random number generator. */
random(max)
int max;
{
	static unsigned int num;

	num *= 3413;
	num += 4321;
	return(num % max);
}

die(kind)
int kind;
{
	static char *msgs[] = {	"",
		/* err 1 */	"Unable to open graphics library.\n",
		/* err 2 */	"Unable to open intuition library.\n",
		/* err 4 */	"Unable to open a window.\n",
				"\n"
				};

	if(kind)		puts(msgs[kind]);
	if(tmpras.RasPtr)	FreeRaster(tmpras.RasPtr,640l,400l);
	if(w)			CloseWindow(w);
	if(GfxBase)		CloseLibrary(GfxBase);
	if(IntuitionBase)	CloseLibrary(IntuitionBase);
	exit(kind);
}
