/*********************************************************/
/*********************************************************/
/****                                                 ****/
/**** WindChime V1.4. © Copyright, 1998, Owen Birnie. ****/
/****                                                 ****/
/**** This program was written using EasySound by     ****/
/**** Anders Bjerin of the Amiga C Club.              ****/
/****                                                 ****/
/**** This program is my own interpretation of an     ****/
/**** identically named program for Windoze machines. ****/
/****                                                 ****/
/**** To Do:-                                         ****/
/**** (*) Add switch to allow the use of one, two or  ****/
/****     four channel audio.                         ****/
/**** (*) Sort out the actual frequencies to use.     ****/
/**** (*) Reduce CPU usage.                           ****/
/**** (*) Turn into a commodity?                      ****/
/****                                                 ****/
/**** History:-                                       ****/
/**** V1.0. Concieved late one night and written the  ****/
/****       next day.                                 ****/
/**** V1.1. Removed bug that meant that it only       ****/
/****       worked when the Wind Chime window was     ****/
/****       active. This had the unfortunate effect   ****/
/****       of turning WindChime into a CPU hog.      ****/
/**** V1.2. Added prop gadgets for volume and         ****/
/****       windiness.                                ****/
/**** V1.3. Added Delay statment into main wait loop  ****/
/****       to reduce CPU usage.                      ****/
/**** V1.4. Added additional sample "Water"           ****/
/****                                                 ****/
/*********************************************************/
/*********************************************************/

#include <stdio.h>
#include <string.h>

#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <libraries/dos.h>
#include <exec/memory.h>

#include "EasySound.h"

STRPTR version="\0$VER: WindChime V1.4. © Copyright, Owen Birnie, 1998. All Rights Reserved.";
struct  TextAttr default_font=
{
  "topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT
};
struct  IntuiText text=
{
  1, 0, JAM1, 0, 0, &default_font, NULL, NULL
};
struct  IntuiText requester_text=
{
  0, 0, JAM1, 15, 5, &default_font, "PROGRAMMING ERROR!", NULL
};
struct  IntuiText cancel_text=
{
  1, 0, JAM1, 6, 3, &default_font, "OK", NULL
};
/**** Gadgets *********/

struct  Image windpropimage;
struct  PropInfo windpropinfo=
{
  FREEHORIZ|AUTOKNOB, 58000, NULL, 1310, NULL, 0, 0, 0, 0, 0, 0
};
struct  Gadget windpropgadget=
{
  NULL, 5, 30, 190, 8, GADGHBOX, RELVERIFY, PROPGADGET, (APTR)&windpropimage,
  NULL, NULL, NULL, (APTR)&windpropinfo, 0, NULL
};
struct  Image volpropimage;
struct  PropInfo volpropinfo=
{
  FREEHORIZ|AUTOKNOB, 40959, NULL, 1213, NULL, 0, 0, 0, 0, 0, 0
};
struct  Gadget volpropgadget=
{
  NULL, 5, 20, 190, 8, GADGHBOX, RELVERIFY, PROPGADGET, (APTR)&volpropimage,
  NULL, NULL, NULL, (APTR)&volpropinfo, 0, NULL
};
/**** End Gadgets *********/
struct  IntuitionBase *IntuitionBase;
struct  GfxBase *GfxBase;
struct  Window *window;
struct  NewWindow new_window=
{
  14, 14, 200, 45, 0, 1, CLOSEWINDOW|GADGETUP, WINDOWCLOSE|WINDOWDRAG|
  WINDOWDEPTH|SMART_REFRESH, NULL, NULL, "Wind Chimes", NULL, NULL,
  200, 15, 200, 15, WBENCHSCREEN
};
CPTR chime;
CPTR water;

VOID  clearup (UBYTE stage, STRPTR message)
{
  if (stage&8)
    {
      RemoveSound (chime);
      RemoveSound (water);
    }
  if (stage&4)
    CloseWindow (window);
  if (message!=NULL)
    {
      requester_text.IText=message;
      AutoRequest (NULL, &requester_text, NULL, &cancel_text, NULL, NULL, 320, 72);
    }
  if (stage&2)
    CloseLibrary (IntuitionBase);
  if (stage&1)
    CloseLibrary (GfxBase);
  exit (0);
}


VOID _main()
{
  BYTE channel=0, volume=volpropinfo.HorizPot/1213;
  UWORD wind=(windpropinfo.HorizPot/1310)+5;
  BOOL finish=FALSE;
  ULONG class;
/*  UWORD code; */
  APTR address;
  
  struct IntuiMessage *message;
  
  if ((IntuitionBase=(struct IntuitionBase *)OpenLibrary ("intuition.library", 0))==NULL)
    exit (!NULL);
  if ((GfxBase=(struct GfxBase *)OpenLibrary ("graphics.library", 0))==NULL)
    clearup (1, "Cannot open \"graphics library\".");
  if ((window=(struct Window *)OpenWindow (&new_window))==NULL)
    clearup (3, "Cannot open window.");
  volpropgadget.TopEdge=window->BorderTop+2;
  windpropgadget.TopEdge=window->BorderTop+12;
  SizeWindow (window, 0, (window->Height-(window->BorderTop+22+window->BorderBottom))*-1);
  AddGadget (window, &volpropgadget, 0);
  AddGadget (window, &windpropgadget, 0);
  RefreshGadgets (&windpropgadget, window, NULL);
  if ((chime=PrepareSound( "Chime" ))==NULL)
    if ((chime=PrepareSound( "PROGDIR:Chime" ))==NULL)
      clearup (7, "Cannot open sample \"Chime\".");
  if ((water=PrepareSound( "Water" ))==NULL)
    if ((water=PrepareSound( "PROGDIR:Water" ))==NULL)
      clearup (7, "Cannot open sample \"Water\".");
  srand (time (NULL));
  while (!finish)
    {
      while ((message=(struct IntuiMessage *)GetMsg (window->UserPort))==NULL)
        {
          if ((rand()%wind)<5)
	    {
              PlaySound (chime, volume+10, channel, ((rand()%9)-2)*100, 1);
              channel++;
	      if (channel==4)
	        channel=0;
	    }
          if ((rand()%wind)==0)
	    {
              PlaySound (water, volume, channel, 0, 1);
              channel++;
	      if (channel==4)
	        channel=0;
	    }
	  Delay (2);
	}
      class=message->Class;
/*      code=message->Code; */
      address=message->IAddress;
      ReplyMsg ((struct Message *)message);
      if (class==CLOSEWINDOW)
	finish=TRUE;
      else if (class==GADGETUP)
	{
	  if (address==(APTR)&volpropgadget)
	    volume=volpropinfo.HorizPot/1213;
	  else if (address==(APTR)&windpropgadget)
	    wind=(windpropinfo.HorizPot/1310)+5;
	}
    }
  StopSound (LEFT0);
  StopSound (LEFT1);
  StopSound (RIGHT0);
  StopSound (RIGHT1);
  clearup (15, NULL);
}
