/***********************************************************************/
/* Demo fuer Draw()                                                    */
/* fuer Markt&Technik von H. Knappe                                    */
/***********************************************************************/

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

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

short col=0;                   /* Farbe des Linie */
short x0,y0;                   /* Fenstergroesse */
short xmin,ymin,xmax,ymax;     /* Endkoordinaten der Linie */
short incxmin=4,incxmax=2,incymin=3,incymax=4;


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


line_()
{
 /* Anpassung an die augenblickliche Fenstergroesse */
 x0=MyWindow->Width,y0=MyWindow->Height;
 xmin+=incxmin,xmax+=incxmax,ymin+=incymin,ymax+=incymax;
 if(xmax>=x0) {col=RangeRand(4);xmax=x0,incxmax*=-1;}
 if(ymax>=y0) {col=RangeRand(4);ymax=y0;incymax*=-1;}
 if(xmin<=0)  {col=RangeRand(4);xmin=0;incxmin*=-1;}
 if(ymin<=0)  {col=RangeRand(4);ymin=0;incymin*=-1;}
 if(xmin>=x0) {col=RangeRand(4);xmin=x0,incxmin*=-1;}
 if(ymin>=y0) {col=RangeRand(4);ymin=y0;incymin*=-1;}
 if(xmax<=0)  {col=RangeRand(4);xmax=0;incxmax*=-1;}
 if(ymax<=0)  {col=RangeRand(4);ymax=0;incymax*=-1;}
 SetAPen(MyRastPort,col);   /* Farbe der Line */
 Move(MyRastPort,xmin,ymin);
 Draw(MyRastPort,xmax,ymax);
}
 

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

c_()
{
 if(MyWindow!=0) CloseWindow(MyWindow);
 if(GfxBase!=0) CloseLibrary(GfxBase);
 if(IntuitionBase!=0)  CloseLibrary(IntuitionBase);
 exit(0);
}
