/***********************************************************************/
/* Demo fuer AreaFill-Funktionen                                       */
/* fuer Markt&Technik von H. Knappe                                    */
/***********************************************************************/

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

#define N 10 /* maximales Polygon mit N Ecken */

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct NewWindow MyNewWindow=
 {10,10,200,100,0,1,NEWSIZE+CLOSEWINDOW,WINDOWDRAG+WINDOWDEPTH+
 WINDOWCLOSE+WINDOWSIZING+ACTIVATE+NOCAREREFRESH+GIMMEZEROZERO,0,0,
 "AreaFill Demo",0,0,50,50,640,250,WBENCHSCREEN};
struct Window *MyWindow;
struct RastPort *MyRastPort;
struct IntuiMessage *MyMessage;
struct AreaInfo MyAreaInfo;
struct TmpRas MyTmpRas;

UWORD buffer[5*N];
long  *speicher;


main()
{
 o_(); /* Oeffnen der Libraries und des Windows */
 for(;;)
 {
    while((MyMessage=GetMsg(MyWindow->UserPort))==0)
       area_();  /* Solange nichts passiert */
    if(MyMessage->Class==CLOSEWINDOW)
       break;
    SetRast(MyRastPort,0); /* Fensterinhalt loeschen */
 }
 ReplyMsg(MyMessage);
 c_();
}


area_()
{
 short x0,y0,x,y,x1,y1,col,n,i;

 col=RangeRand(4);
 SetAPen(MyRastPort,col);   /* Fuellfarbe */
 SetOPen(MyRastPort,3-col); /* Randfarbe */
 /* Anpassung an die augenblickliche Fenstergroesse */
 x0=MyWindow->Width,y0=MyWindow->Height;
 n=RangeRand(N);
 if(n<3) n=3;
 x1=RangeRand(x0),y1=RangeRand(y0);
 AreaMove(MyRastPort,x1,y1);
 for(i=0;i<n-1;++i)
 {
    x=RangeRand(x0),y=RangeRand(y0);
    AreaDraw(MyRastPort,x,y);
 }
 AreaDraw(MyRastPort,x1,y1);
 AreaEnd(MyRastPort);
}
 

o_()
{
 if((IntuitionBase=OpenLibrary("intuition.library",0))==0)
    c_();
 if((GfxBase=OpenLibrary("graphics.library",0))==0)
    c_();
 if((MyWindow=OpenWindow(&MyNewWindow))==NULL)
    c_();
 MyRastPort=MyWindow->RPort;
 init_fill();
}

init_fill()
{
 if((speicher=AllocRaster(640,250))==0)
    c_();
 MyRastPort->AreaInfo=&MyAreaInfo;
 MyRastPort->TmpRas=InitTmpRas(&MyTmpRas,speicher,RASSIZE(640,250));
 InitArea(&MyAreaInfo,&buffer[0],N);
}

c_()
{
 if(speicher!=0) FreeRaster(speicher,640,250);
 if(MyWindow!=0) CloseWindow(MyWindow);
 if(GfxBase!=0) CloseLibrary(GfxBase);
 if(IntuitionBase!=0)  CloseLibrary(IntuitionBase);
 exit(0);
}
