/***********************************************************************/
/* Demo2 fuer ClipBlit()                                               */
/* fuer Markt&Technik von H. Knappe                                    */
/***********************************************************************/

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

#define WIDTH   640
#define HEIGHT  250
#define ANZ 4

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct NewWindow MyNewWindow=
 {0,0,WIDTH,HEIGHT,0,1,CLOSEWINDOW,WINDOWCLOSE+ACTIVATE+NOCAREREFRESH+
 BACKDROP,0,0,"ClipBlit Demo2",0,0,0,0,0,0,WBENCHSCREEN};
struct Window *MyWindow;
struct Screen *WbScreen;
struct RastPort *MyRastPort;
struct RastPort *WbRastPort;
struct IntuiMessage *MyMessage;

short mode=0xC0; /* nicht invertiert */


main(argc,argv)
int argc;
char *argv[];
{
 if((argc==2)&&(*argv[1]=='i')) mode=0x30; /* Ausgabe invertiert? */
 o_(); /* Oeffnen der Libraries und des Windows */
 while((MyMessage=GetMsg(MyWindow->UserPort))==0)
    clip_();  /* Solange nichts passiert */
 ReplyMsg(MyMessage);
 c_();
}


clip_()
{
 short Mausx,Mausy,i,j;

 Mausx=WbScreen->MouseX,Mausy=WbScreen->MouseY;
 if(Mausx>(640-WIDTH/ANZ)) Mausx=640-WIDTH/ANZ;
 if(Mausy>(250-HEIGHT/ANZ)) Mausy=250-HEIGHT/ANZ;
 for(i=0;i<ANZ;++i)
    for(j=0;j<ANZ;++j)
        ClipBlit(WbRastPort,Mausx,Mausy,MyRastPort,i*WIDTH/ANZ,j*HEIGHT/ANZ,
                 WIDTH/ANZ,HEIGHT/ANZ,mode);
}
 

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;
 WbScreen=MyWindow->WScreen;
 WbRastPort=&(WbScreen->RastPort);
 ShowTitle(WbScreen,FALSE);
}

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