/***********************************************************************/
/* Demo fuer WritePixel                                                */
/* 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,
 "WritePixel Demo",0,0,50,50,640,250,WBENCHSCREEN};
struct Window *MyWindow;
struct RastPort *MyRastPort;
struct IntuiMessage *MyMessage;

short col=0;     /* Farbe des Punktes */
short x,y,x0,y0; /* Koordinaten des Punktes / Fenstergroesse */


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


point_()
{
 /* Anpassung an die augenblickliche Fenstergroesse */
 x0=MyWindow->Width,y0=MyWindow->Height;
 x=RangeRand(x0),y=RangeRand(y0);
 ++col;
 if(col==4) col=0;
 SetAPen(MyRastPort,col);
 WritePixel(MyRastPort,x,y);
}
 

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