#include "fishes.h"
#include <stdio.h>

#define WIDTH   320
#define HEIGHT  200 
#define DEPTH 3 

short bpdata[] =
   {
   0x000, 0x5ef, 0x128, 0xd81, 0x326, 0x203, 0x086, 0x044,
   0x5ef, 0x633, 0x855, 0x857, 0x065, 0x669, 0x223, 0x547
   };

struct GelsInfo *GInfo;
struct VSprite *head, *tail;
struct collTable *boom;
struct BitMap   bitmap[2];

show(screen,view,vp,rp,window)
struct Screen *screen;
struct View *view;
struct ViewPort *vp;
struct RastPort *rp;
struct Window *window;
{
    short dispIndex;
    short i, i2, j,k;
	struct IntuiMessage *message;
	struct AnimOb *animKey;
	struct BitMap *OldI_BitMap;		

	OldI_BitMap = rp->BitMap;

    if ((boom = (struct collTable *)AllocMem(sizeof
        (struct collTable),MEMF_PUBLIC+MEMF_CLEAR )) == 0)
		{
		    exit(1);
        }
 
    if ((head = (struct VSprite *)AllocMem(sizeof(struct VSprite),
        MEMF_PUBLIC+MEMF_CLEAR )) == 0)
		{
		    exit(1);
        }

    if ((tail = (struct VSprite *)AllocMem(sizeof(struct VSprite),
        MEMF_PUBLIC+MEMF_CLEAR )) == 0)
		{
		    exit(1);
        }
 
    if ((GInfo = (struct GelsInfo *)AllocMem(sizeof(struct GelsInfo),
        MEMF_PUBLIC+MEMF_CLEAR )) == 0)
		{
		    exit(1);
        }

    GInfo->nextLine = 0;
    GInfo->lastColor = 0;
    GInfo->leftmost = 0;
    GInfo->topmost = 0;
    GInfo->rightmost = WIDTH -1;
    GInfo->bottommost = HEIGHT - 1;
    GInfo->collHandler = boom;
    GInfo->sprRsrvd = -1;
    rp->GelsInfo = GInfo;
    InitGels(head,tail,GInfo);
	

	LoadRGB4(vp,bpdata,16);
	SetRast(rp,0);

    for(i=0; i<2; i++)
    {	
		InitBitMap(&bitmap[i],DEPTH,WIDTH,HEIGHT);
    	for(j=0; j<DEPTH; j++)
		{	
		   if ((bitmap[i].Planes[j] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT)) == 0)
           {
              exit(2);
           }
		   else
		   {
		       BltClear(bitmap[i].Planes[j],RASSIZE(WIDTH,HEIGHT),0);
		   }
	    }
	};

	rp->Flags = DBUFFER;
    rp->Mask = -1;        

    vp->RasInfo->BitMap = &bitmap[1];
	rp->BitMap = &bitmap[1];
	SetRast(rp,0);

    vp->RasInfo->BitMap = &bitmap[0];
	rp->BitMap = &bitmap[0];
	SetRast(rp,0);

	animinit(&animKey,vp,rp);

    dispIndex = 0;

restart:
    while((message = (struct IntuiMessage *)GetMsg(window->UserPort)) == NULL)
        {
		Animate(&animKey, rp);
        SortGList(rp);
		WaitTOF();
		DrawGList(rp,vp); 
		vp->RasInfo->BitMap = &bitmap[dispIndex]; 
		WaitTOF();
	   	MakeScreen(screen);	
		WaitBlit();
		RethinkDisplay();
        dispIndex ^= 1;
        rp->BitMap = &bitmap[dispIndex];

        }
	switch(message->Class)
		{
        case CLOSEWINDOW:ReplyMsg(message);
				      free_anim_obj(rp);		
                      for (i = 0; i < 2; i++){
                      	for (j = 0; j < DEPTH; j++)
							{
                            FreeMem(bitmap[i].Planes[j],RASSIZE(WIDTH,HEIGHT)); 
                            };
						};	
						FreeMem(boom,sizeof(struct collTable));
						FreeMem(head,sizeof(struct VSprite));
						FreeMem(tail,sizeof(struct VSprite));
						FreeMem(GInfo,sizeof(struct GelsInfo));
                        vp->RasInfo->BitMap = OldI_BitMap;
                        rp->BitMap = OldI_BitMap; 
						return;
       } 
   ReplyMsg(message);
   goto restart;
              

}

main()
{
  startgfx(400,80,50,100,2,0,show,"CATHY'S FISH",SMART_REFRESH);

}

delay()
{
	int i;
	int x;
	for(i = 0; i<10000; i++)
	{
		x += i;
	}
}
