
#include <exec/types.h>
#include <stdio.h>
#include <intuition/intuition.h>
#include <graphics/gels.h>
#include "insert.c"
#define usr UserPort->mp_SigBit

void demo();


WORD Savebuf[_HEIGHT * WidinW * _DEPTH];
WORD Cmask[WidinW * _HEIGHT];
WORD Bline[WidinW];

struct VSprite v=

{

NULL,NULL,NULL,
NULL,NULL,NULL,        /* System variables */
OVERLAY | SAVEBACK,    /* Flags */
0,                    /* Y position */
0,                    /* X position */
_HEIGHT,               /* Height from defines in insert.c */
WidinW,                /* Words per row  64 bits */
_DEPTH,                /* Depth from defines in insert.c */
1,                     /* MeMask */
1,                     /* HitMask */
&mydata[0],            /* ImageData from insert.c*/
&Bline[0],             /* BorderLine */
&Cmask[0],             /* CollMask */
NULL,                  /* doesn't use sprite colors */
NULL,                  /* pointer to bob filled in by main */
PLANEPICK,             /* PlanePick from defines in insert.c */
PLANEONOFF,            /* PlaneOff all zeros */
NULL                   /* User data */
};


struct Bob b=
{
0,                     /* Flags */
&Savebuf[0],           /* pointer to savebuffer */
&Cmask[0],             /* ImageShadow */
NULL,                  /* Before */
NULL,                  /* After */
&v,                    /* pointer to BobVSprite to link */
NULL,                  /* Animation Component (BobComp) */
NULL,                  /* not double buffered (DBuffer) */
NULL                   /* user data */
};

/* BIRD SPRITE DATA */


UWORD b1data[]={

0x0000,0x0000,      /* Position pad bytes */
0x8020,0x0020,0x4140,0x0140,0x22c0,0x12c0,0x05c0,0x19c0,
0x03c0,0x1f40,0x07c0,0x06c0,0x03c0,0x05c0,0x03c0,0x07c0,
0x0100,0x03c0,0x0010,0x01ec,0x0020,0x00d8,0x0040,0x00b0,
0x0000,0x00e0,0x0000,0x0080,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,      /* Linkage pad bytes */
};

/* sizebody=           0x40 HEX     ....64 decimal */
/* width=              0x10 HEX     ....16 decimal */
/* Width in WORDS=     0x1 HEX      ....1 decimal */
/* height=             0x10 HEX     ....16 decimal */
/* number of planes=   0x2 HEX */



UWORD b2data[]={
0x0000,0x0000,      /* Position pad bytes */
0x8000,0x0000,0x4000,0x0000,0x2000,0x1000,0x0500,0x1900,
0x0380,0x1e80,0x2ec0,0x2fc0,0x30c0,0x27c0,0x3f40,0x37c0,
0x1f00,0x1fc0,0x0f10,0x0fe8,0x0220,0x02d0,0x0040,0x00a0,
0x0000,0x00c0,0x0000,0x0080,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,      /* Linkage pad bytes */
};

/* sizebody=           0x40 HEX     ....64 decimal */
/* width=              0x10 HEX     ....16 decimal */
/* Width in WORDS=     0x1HEX      ....1 decimal */
/* height=             0x10 HEX     ....16 decimal */
/* number of planes=   0x2 HEX */



UWORD b3data[]={
0x0000,0x0000,      /* Position pad bytes */
0x4000,0x0000,0x2000,0x0000,0x1200,0x0a00,0x03c0,0x0dc0,
0x01e0,0x0fe0,0x0760,0x07e0,0xf840,0xebc0,0xff80,0xdfc0,
0x7f80,0x7fe0,0x1e08,0x1e76,0x0010,0x006c,0x0020,0x0058,
0x0000,0x0070,0x0000,0x0040,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,      /* Linkage pad bytes */
};

/* sizebody=           0x40 HEX     ....64 decimal */
/* width=              0x10 HEX     ....16 decimal */
/* Width in WORDS=     0x1 HEX      ....1 decimal */
/* height=             0x10 HEX     ....16 decimal */
/* number of planes=   0x2 HEX */


/* *****************************SCREEN*********************************** */

struct NewScreen _NewScreen = {


0,                 /* LeftEdge */
0,                 /* TopEdge */
320,               /* Width */
200,               /* Height */
_DEPTH,            /* DEPTH  from defines in insert.c */
0,                 /* DetailPen */
1,                 /* BlockPen */
NULL,              /* ViewModes */
CUSTOMSCREEN,      /* Type */
NULL,              /* Font */
NULL,             /* DefaultTitle */
NULL,              /* Gadgets */
NULL               /* Custom  */
};

struct Screen *OpenScreen();
struct Window *OpenWindow();
struct IntuiMessage *GetMsg();
/* ********************************WINDOW********************************* */


struct NewWindow newwindow = {

    0,0,                                       /* LeftEdge , TopEdge */
    320,200,                                   /* Width, Height */
    1,0,                                       /* DetailPen, BlockPen */
    CLOSEWINDOW | MOUSEMOVE | INTUITICKS,      /* IDCMPFlags */
    WINDOWCLOSE | REPORTMOUSE |
    NOCAREREFRESH | SMART_REFRESH,             /* flags */
    NULL,                                      /* FirstGadget */
    NULL,                                      /* Checkmark */
    "       BRUSH TO BOB DISPLAY   ",          /* Title */
    NULL,                                 /* Screen (pointer init by main) */
    NULL,                                 /* Bitmap */
    0,0,320,200,               /* MinWidth, MinHeight, MaxWidth, MaxHeight */
    CUSTOMSCREEN
  };
/* **************************color pallette**************************** */



    struct GfxBase *GfxBase;
    struct Screen *screen;
    struct Window *window;
    struct IntuitionBase *IntuitionBase; /* last two must be of same type */
    struct ViewPort *ViewPortAddress();  /* woo compiler */
    struct ViewPort *vp;

    struct VSprite s1,s2;          /* dummy sprites for gels list */
    struct GelsInfo gelsinfo;     /* gelsinfo to link to window Rp */
    struct collTable Ctable;



void main();
void handle_message();
void handle_window();
void OPPS();

UWORD *birdpointer[3];

void main()

     {
struct IntuiMessage *message;
struct Image *Iptr;
Iptr=&mydataImage;

         if ((IntuitionBase =
            (struct IntuitionBase *)OpenLibrary("intuition.library",0)) == NULL)
               OPPS("Error: couldn't open intuition.library\n");
         if ((GfxBase =
            (struct GfxBase *)OpenLibrary("graphics.library",0)) == NULL)
               OPPS("Error: couldn't open graphics.library\n");
         if ((screen = OpenScreen(&_NewScreen)) == NULL)
               OPPS("Error: couldn't open screen\n");

         newwindow.Screen = screen;

         if ((window = OpenWindow(&newwindow))== NULL)
               OPPS("Error: couldn't open window\n");

         vp = ViewPortAddress(window);

         LoadRGB4(vp,&colormap[0],NUMOFCOLORS);

         gelsinfo.nextLine = NULL;
         gelsinfo.lastColor = NULL;
         gelsinfo.collHandler = &Ctable;
         InitGels(&s1,&s2,&gelsinfo);
         window->RPort->GelsInfo = &gelsinfo;
         v.VSBob=&b;
         InitMasks(&v);
         AddBob(&b,window->RPort);

         DrawImage(window->RPort,Iptr,0,10);
         DrawImage(window->RPort,Iptr,0,200-_HEIGHT);
         DrawImage(window->RPort,Iptr,(320-(WidinW*16)),10);
         DrawImage(window->RPort,Iptr,(320-(WidinW*16)),200-_HEIGHT);

         birdpointer[0]=&b1data[0];
         birdpointer[1]=&b2data[0];
         birdpointer[2]=&b3data[0];


for (;;){
         Wait( 1<< (window->UserPort->mp_SigBit));
         while((window!=NULL)&&(message=GetMsg(window->UserPort))!=NULL)
         handle_message(message);

        }

} /*main*/



USHORT x,y;

void handle_message(msgs)
struct IntuiMessage *msgs;
{

   ULONG class = msgs->Class;
   USHORT code = msgs->Code;
   APTR address = msgs->IAddress;

   if(class==MOUSEMOVE){
      x = msgs->MouseX;
      y = msgs->MouseY;
                       }
   ReplyMsg(msgs);

   handle_window(class,code,address);

}


void handle_window(cls,cde,addrs)
ULONG cls;
USHORT cde;
APTR addrs;

{
 extern USHORT x;
 extern USHORT y;
 static USHORT birdi=0;
 static SHORT sign = 1;
 static USHORT toggle=1;

      switch(cls){
            case CLOSEWINDOW:
               OPPS(NULL);
               break;
            case MOUSEMOVE:
               toggle ^=1;
                 if(toggle){
                    SetPointer(window,birdpointer[birdi],16,16,0,0);
                    if(birdi==0)sign=1;
                    if(birdi==2)sign=-1;
                    birdi+=sign;
                           }
               break;
            case INTUITICKS:

                  if(b.BobVSprite->Y!=y || b.BobVSprite->X!=x){
                     b.BobVSprite->Y=y;
                     b.BobVSprite->X=x;
                     demo();
                                                              }
               break;

            default:
               OPPS("Dump out of Switch and Case Handle_window\n");
                     } /* switch and case */
} /* handle_message */



void demo()

{
    extern USHORT x;
    extern USHORT y;

    if(y<90)
      WaitTOF();
    else
      WaitBOVP();
    SortGList(window->RPort);
    DrawGList(window->RPort,vp);

}

void OPPS(ExitText)
     char *ExitText;

 {

          if (window) CloseWindow(window);
          if (screen) CloseScreen(screen);
          if (GfxBase) CloseLibrary(GfxBase);
          if (IntuitionBase) CloseLibrary(IntuitionBase);
          if (ExitText) fprintf(stderr,ExitText);
          exit(!!ExitText);
  }
