/***********************************************************************/
/* Demo3 fuer AreaFill-Funktionen / fliegendes Dreieck                 */
/* fuer Markt&Technik von H. Knappe                                    */
/***********************************************************************/

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

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 Demo3",0,0,50,50,640,250,WBENCHSCREEN};
struct Window *MyWindow;
struct RastPort *MyRastPort;
struct IntuiMessage *MyMessage;
struct AreaInfo MyAreaInfo;
struct TmpRas MyTmpRas;

UWORD buffer[20];
long  *speicher;

short col;                 /* Farbe des Dreiecks */
short x0,y0;               /* Fenstergroesse */
short x[3],y[3];           /* Endkoordinaten des Dreiecks */
short incx[3],incy[3],i;

 
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_()
{
 /* Anpassung an die augenblickliche Fenstergroesse */
 x0=MyWindow->Width,y0=MyWindow->Height;
 for(i=0;i<3;++i)
 {
    x[i]+=incx[i],y[i]+=incy[i];
    if(x[i]>=x0) {col=RangeRand(4);x[i]=x0,incx[i]*=-1;}
    if(y[i]>=y0) {col=RangeRand(4);y[i]=y0;incy[i]*=-1;}
    if(x[i]<=0)  {col=RangeRand(4);x[i]=0; incx[i]*=-1;}
    if(y[i]<=0)  {col=RangeRand(4);y[i]=0; incy[i]*=-1;}
 }
 SetAPen(MyRastPort,col);    /* Farbe Rot(?) */
 SetOPen(MyRastPort,3-col);  /* Randfarbe Weiss(?) */
 AreaMove(MyRastPort,x[0],y[0]);
 for(i=1;i<3;++i)
    AreaDraw(MyRastPort,x[i],y[i]);
 AreaEnd(MyRastPort);
 WaitTOF();
}
 

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();
 for(i=0;i<3;incx[i]=incy[2-i]=i+1,++i);
}

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

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