#include "exec/types.h"
#include "exec/memory.h"
#include "graphics/gfxbase.h"
#include "graphics/gfxmacros.h"
#include "graphics/sprite.h"
#include "intuition/intuitionbase.h"
#include "intuition/intuition.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include  <stdio.h>                 /*for printf*/
#include "csprite.h"

struct ViewPort *WVP;               /*screen ViewPort*/
struct BitMap PicBitMap;            /*picture BitMap*/
struct BitMap ScreenBitMap;         /*screen BitMaps*/
struct BitMap Screen2BitMap;
struct RasInfo RasInfo;             /*second RasInfo*/
struct RasInfo *lastRasInfo;        /*saved RasInfo pointer*/
long libraries=0;
long i,k;                           /*loop variables*/
long level,joyvalue;
short j,quit_flag,code;
struct GfxBase *GfxBase;            /*graphics base*/
struct IntuitionBase *IntuitionBase;/*intuition base*/
struct Screen *CustScr;             /*pointer to screen structure*/
struct Window *Wdw;                 /*pointer to window structure*/
struct IntuiMessage *message;       /*pointer to message from window port*/


extern struct SimpleSprite *spritepointer[];  /*pointers to sprite 
                                              structures*/ 

 /*standard full size lo-res screen*/
static  struct NewScreen NewCustScr =
{
  0,0,
  320,
  256,
  6,
  3,1,
  SPRITES|DUALPF|PFBA,
  CUSTOMSCREEN | CUSTOMBITMAP,
  0,
  NULL,
  NULL,
  &ScreenBitMap,
};

 /*full size window for screen*/
static  struct NewWindow NewWdw =
{
  0,10,
  320,
  246,
  3,1,
  CLOSEWINDOW|RAWKEY|MOUSEBUTTONS,
  SUPER_BITMAP | ACTIVATE |WINDOWCLOSE,
  NULL,
  NULL,
  NULL,
  NULL,
  &ScreenBitMap,
  0,0,
  0,0,
  CUSTOMSCREEN
};


void main(),init(),cleanup(),closelibraries(),quit(),Handle_IDCMP();

void main()
{
  libraries= openlibraries();   /*open the libraries*/
  if (libraries == 0) exit(1);
  init();                       /*initialise display*/ 
  readfile("background",&PicBitMap,WVP,0); /*see iffread.c*/ 
                                /*copy background picture*/    
  BltBitMap(&PicBitMap,0,20,&ScreenBitMap,0,20,320,236,0xc0,0xff,0);
                               /*read and copy foreground picture*/
  readfile("foreground",&PicBitMap,WVP,8);      
  BltBitMap(&PicBitMap,0,20,&Screen2BitMap,0,20,320,236,0xc0,0xff,0);
  FreeBitMap(&PicBitMap);                    /*free buffer -see iffread.c*/
  for (i=1;i<8;i++) GetSprite(spritepointer[i],i); /* "bagsy" a sprite*/
                                 /*move them to make them visible*/
  for (i=1;i<8;i++) MoveSprite(WVP,spritepointer[i],i*32,100);
  Handle_IDCMP();                            /*main polling loop*/
  for (i=1;i<8;i++) FreeSprite(i);  /*liberate the sprites*/
  cleanup();
  closelibraries();
}

openlibraries()              /*open the libraries*/
{
  GfxBase= (struct GfxBase *)OpenLibrary("graphics.library",0);
  if (GfxBase == 0) return(0);
  IntuitionBase= (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  if (IntuitionBase == 0) return(0);
  return(1);
}

/*set up a lo-res screen and window*/
void init()
{
  InitBitMap(&ScreenBitMap,3,320,256);  /*set up a bitmap */
  for(i=0;i<3;i++)                      /*allocate screen for it*/
    {
       ScreenBitMap.Planes[i] = (PLANEPTR)AllocRaster(320,256);
       if (ScreenBitMap.Planes[i] == 0) exit(2);
       BltClear(ScreenBitMap.Planes[i],40*256,0); /*clear the screen*/
    }
  InitBitMap(&Screen2BitMap,3,320,256);  /*set up a bitmap */
  for(i=0;i<3;i++)                      /*allocate screen for it*/
    {
       Screen2BitMap.Planes[i] = (PLANEPTR)AllocRaster(320,256);
       if (Screen2BitMap.Planes[i] == 0) exit(2);
       BltClear(Screen2BitMap.Planes[i],40*256,0); /*clear the screen*/
    }
  CustScr= (struct Screen *) OpenScreen(&NewCustScr);
  if (CustScr == 0) exit(1);                 /*open the screen*/
  NewWdw.BitMap=(struct BitMap *) &ScreenBitMap;
  NewWdw.Screen = CustScr;                  /*open the window*/
  Wdw = (struct Window *)OpenWindow(&NewWdw);
  if (Wdw  == NULL) exit(FALSE);
  WVP=(struct ViewPort *)ViewPortAddress(Wdw); /*find viewport address*/
  RasInfo.BitMap=(struct BitMap *) &Screen2BitMap; /*attach second 
                                               BitMap to RasInfo structure*/
  lastRasInfo=WVP->RasInfo->Next;              /*save next RasInfo of ViewPort*/
  WVP->RasInfo->Next=&RasInfo;                 /*adds 2nd RasInfo&BitMap*/
  RemakeDisplay();
}

/*close windows and screens, free rasters,close file*/
void cleanup()
{
  int i;                               /*close window and screen*/
  CloseWindow(Wdw);
  WVP->RasInfo->Next=lastRasInfo;      /*replace last RasInfo of ViewPort*/
  CloseScreen(CustScr);
  for (i=0;i<3;i++)                   /*free the screen memory*/
    {
       FreeRaster(ScreenBitMap.Planes[i],320,256);
       FreeRaster(Screen2BitMap.Planes[i],320,256);
    }
}

void closelibraries()               /*close libraries*/
{
  CloseLibrary(GfxBase);
  CloseLibrary(IntuitionBase);
}

/*leave program*/
void quit()
{
  quit_flag=TRUE;
}


/*usual event handling loop*/
void Handle_IDCMP()
   {
   APTR object;
   long class;
   for (quit_flag = FALSE;!quit_flag;)
      {
      if (1<<Wdw->UserPort->mp_SigBit)
         {
      while (!(message=(struct IntuiMessage *)GetMsg(Wdw->UserPort)))
           {       
             /*background activity here*/
           }
         }
      class = message->Class;    
      code = message->Code;    
      object=message->IAddress;
      ReplyMsg((struct Message *) message );   

      switch ( class )
         {
         case CLOSEWINDOW:
              quit();
              break;
 
         case RAWKEY:               /*key pressed*/
           break;

         case MOUSEBUTTONS:
            quit();
            break; 
         }                           /* end switch */
      }                              /* end forloop */
   }

