File SoundMgrSample.cpp         FMOD SoundManager - sample
#include "stdafx.h"
#include "OgreFrameListener.h"
#include "OgreEventListeners.h"
#include "OgreKeyEvent.h"
#include "SoundManager.h"

#if _DEBUG
#pragma comment(lib, "OgreMain_d.lib")
#else
#pragma comment(lib, "OgreMain.lib")
#endif

#pragma comment(lib, "C:/Program Files/FMOD SoundSystem/FMOD Programmers API/api/lib/fmodex_vc.lib")

using namespace Ogre;

// Bad form to use these globals, but this is just a sample app.
SoundManager * soundMgr;
SceneNode *    cameraNode;
SceneNode *    ff06Node;
int            gunSoundIndex;
int            gunSoundChannel;

const int   flightRadius = 200.0;   // radius of the circle the plane flies in
const float flightSpeed = 1.0;      // multiplier for the plane speed
const float fireGunInterval = 3.0;  // how often to fire the guns

// Move the plane in a circle around the camera.
void MoveFF06(SceneNode *sceneNode, Real timeElapsed)
   {
   Vector3     newPostion;
   static Real currentRadians = 3.1;

   currentRadians += timeElapsed * flightSpeed;
   if (currentRadians >= 6.2832)
      currentRadians = 0;

   newPostion.x = -((float)cos(currentRadians) * flightRadius);
   newPostion.z = ((float)sin(currentRadians) * flightRadius);
   newPostion.y = 150.0;
   sceneNode->setPosition(newPostion);
   }

class CSimpleFrameListener : public FrameListener
   {
public:
   CSimpleFrameListener(InputReader* inputReader)
      {
      m_InputReader = inputReader;
      }

   bool frameStarted(const FrameEvent& evt)
      {
      static float timeToFireGuns = 0;

      m_InputReader->capture();

      // exit application if ESC key pressed
      if (m_InputReader->isKeyDown(KC_ESCAPE))
         return false;

      // Move the plane in a circle around our camera.
      MoveFF06(ff06Node, evt.timeSinceLastFrame);

      if (timeToFireGuns <= 0)
         {
         // Fire the guns! (just plays sound, no visuals in this demo)
         soundMgr->PlaySound(gunSoundIndex, ff06Node, &gunSoundChannel);
         // Allow the plane's gunfire to be heard farther away than normal sounds.
         soundMgr->Set3DMinMaxDistance(gunSoundChannel, 100.0, 500.0);
         timeToFireGuns = fireGunInterval;
         }
      else
         {
         timeToFireGuns -= evt.timeSinceLastFrame;
         }

      // Allow SoundManager to update sound positions, velocities
      soundMgr->FrameStarted(cameraNode, evt.timeSinceLastFrame);

      return true;
      }

   bool frameEnded(const FrameEvent& evt)
      {
      return true;
      }

private:
   InputReader* m_InputReader;
   };

class CSimpleKeyListener : public KeyListener
   { 
public:
   void keyClicked(KeyEvent* e)
      {
      }

   void keyPressed(KeyEvent* e)
      {
      }

   void keyReleased(KeyEvent* e)
      {
      }
   };

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
   {
   int      ff06SoundIndex;
   int      ff06SoundChannel;
   Entity * ent;
   Root *   root;

   root = new Root;
   if (!root->showConfigDialog())
      return false;                                 // Exit the application on cancel
   root->initialise(true, "FMOD SoundManager Sample App");

   ConfigFile cf;
   cf.load("resources.cfg");

   // Go through all sections & settings in the file
   ConfigFile::SectionIterator seci = cf.getSectionIterator();

   String secName, typeName, archName;
   while (seci.hasMoreElements())
      {
      secName = seci.peekNextKey();
      ConfigFile::SettingsMultiMap *settings = seci.getNext();
      ConfigFile::SettingsMultiMap::iterator i;
      for (i = settings->begin(); i != settings->end(); ++i)
         {
         typeName = i->first;
         archName = i->second;
         ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
         }
      }

   ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

   SceneManager* sceneMgr = root->getSceneManager(ST_GENERIC);
   sceneMgr->setAmbientLight(ColourValue(1, 1, 1));

   Camera* camera = sceneMgr->createCamera("SimpleCamera");
   camera->setPosition(Vector3(0.0f, 0.0f, 0.0f));
   camera->lookAt(Vector3(50.0f, 50.0f, 0.0f));
   camera->setNearClipDistance(2.0f);
   camera->setFarClipDistance(5000.0f);
   cameraNode = sceneMgr->getRootSceneNode()->createChildSceneNode("CameraNode", Vector3(0.0f, 0.0f, 0.0f));
   cameraNode->attachObject(camera);

   Viewport* viewPort = root->getAutoCreatedWindow()->addViewport(camera);
   viewPort->setBackgroundColour(ColourValue(0.4f, 0.45f, 0.93f));

   EventProcessor* eventProcessor = new EventProcessor();
   eventProcessor->initialise(root->getAutoCreatedWindow());
   eventProcessor->startProcessingEvents();

   CSimpleKeyListener* keyListener = new CSimpleKeyListener();
   eventProcessor->addKeyListener(keyListener);
   CSimpleFrameListener* frameListener = new CSimpleFrameListener(eventProcessor->getInputReader());
   root->addFrameListener(frameListener);

   soundMgr = new SoundManager;
   soundMgr->Initialize();

   // Create a 3D, looped sound for the plane's engine noise
   ff06SoundIndex = soundMgr->CreateLoopedSound(Ogre::String("Sound050.ogg"));
   // or you can use the generic CreateSound, specifying the sound type directly:
//   ff06SoundIndex = soundMgr->CreateSound(Ogre::String("Sound050.ogg"), SOUND_TYPE_3D_SOUND_LOOPED);
   ff06SoundChannel = INVALID_SOUND_CHANNEL;

   // Create a 3D, single-shot sound for the plane's gunfire noise
   gunSoundIndex = soundMgr->CreateSound(Ogre::String("Sound035.ogg"));
   gunSoundChannel = INVALID_SOUND_CHANNEL;

   ent = sceneMgr->createEntity("FF06", "FF06.mesh");
   ff06Node = sceneMgr->getRootSceneNode()->createChildSceneNode("FF06Node", Vector3(0.0f, 0.0f, 0.0f));
   ff06Node->attachObject(ent);
   ff06Node->setScale(1.0f, 1.0f, 1.0f);

   // Start playing the plane's engine noise
   soundMgr->PlaySound(ff06SoundIndex, ff06Node, &ff06SoundChannel);
   // Allow the plane's engine to be heard farther away than normal sounds.
   soundMgr->Set3DMinMaxDistance(ff06SoundChannel, 100.0, 500.0);

   // blocks until a frame listener returns false. eg from pressing escape in this example
   root->startRendering();

   delete soundMgr;
   delete frameListener;
   delete keyListener;
   delete eventProcessor;
   delete root;
   return 0;
   }