/*sound 3 -c source code */
/*Amiga Computing  March 92 issue*/
/*M.Stanger  Nov 1991*/

#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*/

 /*standard full size lo-res screen*/
static  struct NewScreen NewCustScr =
{
0,0,
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|RAWKEY|MOUSEBUTTONS,
SUPER_BITMAP | ACTIVATE
 | BORDERLESS | WINDOWCLOSE,
NULL,
NULL,
NULL,
NULL,
&ScreenBitMap,
0,0,
0,0,
CUSTOMSCREEN
};

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


void main()
{
 libraries= openlibraries();   /*open the libraries*/
 if (libraries == 0) exit(1);
 init();                       /*initialise display*/ 
 initsound();                  /* initialise sound device and structures*/
 Handle_IDCMP();               /*main polling loop */
 clearsound();                 /*close sound device*/
 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.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);
}



/*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)))
           {                       /*other processing*/     
           }
         }
      class = message->Class;    
      code = message->Code;    
      object=message->IAddress;
      ReplyMsg((struct Message *) message );   

      switch ( class )
         {

         case CLOSEWINDOW:           /*window closed*/
            quit_flag=TRUE;
            break;

         case RAWKEY:               /*key pressed*/
           if (code==1) bell();     /*sound when key 1 pressed */
           if (code==2) sploit();   /*sound when key 2 pressed */
           if (code==3) quit_flag=TRUE; /* quit when key 3 pressed */
           break;

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

