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

struct ViewPort *WVP;               /*screen ViewPort*/
struct BitMap PicBitMap;            /*picture BitMap*/
struct BitMap ScreenBitMap;         /*screen BitMap*/
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*/
struct FileHandle *filehandle;      /*output filehandle for writing to screen*/

extern struct Gadget lGadget1;      /*pointer to first gadget in gads.c file*/

extern long playsig();              /*value returned from routine in stix2.a*/

 /*standard full size lo-res screen*/
static  struct NewScreen NewCustScr =
{
0,270,
320,
256,
5,
3,1,
SPRITES,
CUSTOMSCREEN | CUSTOMBITMAP,
0,
NULL,
NULL,
&ScreenBitMap,
};
 /*full size window for screen*/
static  struct NewWindow NewWdw =
{
0,0,
320,
256,
3,1,
CLOSEWINDOW|GADGETUP|GADGETDOWN|RAWKEY|MOUSEBUTTONS,
SUPER_BITMAP | ACTIVATE
 | BORDERLESS,
NULL,
NULL,
NULL,
NULL,
&ScreenBitMap,
0,0,
0,0,
CUSTOMSCREEN
};

void main(),init(),cleanup(),closelibraries(),quit(),Handle_Key(),Handle_IDCMP();
void program1(),program2(),fade();

void main()
{
 libraries= openlibraries();   /*open the libraries*/
 if (libraries == 0) exit(1);
 init();                       /*initialise display*/ 
 level=1;
 filehandle=(struct FileHandle *) Output(); /*find output file handle*/
 readfile("loadpic",&PicBitMap,WVP,0);      /*read IFF file*/
 /*copy picture on to screen*/
 BltBitMap(&PicBitMap,0,0,&ScreenBitMap,0,0,320,256,0xc0,0xff,0);
 FreeBitMap(&PicBitMap);                    /*free buffer*/
 MoveScreen(CustScr,0,-270);                /*move screen into place*/
 Handle_IDCMP();                            /*main polling loop*/
 cleanup();                                 /*free memory*/  
 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,5,320,256);  /*set up a bitmap */
 for(i=0;i<5;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*/
 }
 CustScr= (struct Screen *) OpenScreen(&NewCustScr);
 if (CustScr == 0) exit(1);                 /*open the screen*/
 NewWdw.FirstGadget=&lGadget1;              /*attach the first gadget*/
 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*/
}

/*close windows and screens, free rasters,close file*/
void cleanup()
{
 int i;                               /*close window and screen*/
 CloseWindow(Wdw);
 CloseScreen(CustScr);
 for (i=0;i<5;i++)                   /*free the screen memory*/
 {
  FreeRaster(ScreenBitMap.Planes[i],320,256);
 }
}

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

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

void program1()                        /*run viewports program*/
{                                      /*code clinic May 91 */ 
 Execute("viewports");
}
void program2()                        /*run windows program*/                        
{                                      /*code clinic March 91*/ 
 Execute("screensandwindows");
}
void fade()
{
fadedown(WVP);                         /*fade colours*/ 
fadeup(WVP);                           /*bring back colours*/
}

/*Handle keys for load screen*/
void HandleKey()            /* keys ESC 1 2 3 4 duplicate the gadgets*/
{
 code=code&255;             /*internal key code*/ 
 if (code==0x01) {program1();code=0;return;}
 if (code==0x03) {program2();code=0;return;}
 if (code==0x02) {fade();return;}
 if (code==0x45) {quit();code=0;return;}
}

/*handle gadgets for load screen*/
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)))
           {                       /*joystick is read if no message in port*/     
          joyvalue = playsig();    /*information interpreted*/
          if (joyvalue !=0 ) 
             {
          if ((joyvalue & 1)==1) Write(filehandle,"r",1);
          if ((joyvalue & 2)==2) Write(filehandle,"l",1);
          if ((joyvalue & 4)==4) Write(filehandle,"u",1);
          if ((joyvalue & 8)==8) Write(filehandle,"d",1);   
          if ((joyvalue & 16)==16) Write(filehandle,"f",1);
             }
           }
         }
      class = message->Class;    
      code = message->Code;    
      object=message->IAddress;
      ReplyMsg((struct Message *) message );   

      switch ( class )
         {
         case GADGETUP:             /*gadget pressed */
            HandleEvent(object);
            break;

         case GADGETDOWN:           /*gadget released*/
            HandleEvent(object);
            break;

         case RAWKEY:               /*key pressed*/
           HandleKey(code);
           break;

         case MOUSEBUTTONS:
		    break; 
         }                           /* end switch */
      }                              /* end forloop */
   }

