/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef SOUNDSCENE_H
#define SOUNDSCENE_H

//##############################################################################
// C Includes
//##############################################################################

#include "MathLibrary.h"

//##############################################################################
// Forward references
//##############################################################################

class	GLWindow;
class	Sound;
class	SoundSystem;
struct	IDirectSound8;
struct	IDirectSoundBuffer8;

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   SoundScene
// Purpose:      A SoundScene maintains a scene of various sound sources and
//               handles the playing and positioning of those sources. One of the
//               subclasses of the combined superclass, GameScene
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class SoundScene
{
 public:

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------

        // Constructor

        SoundScene();

        // Destructor

        ~SoundScene();

        // Updates all sounds in the scene and kills off sounds that are gone:

        void updateSounds();

        // Pauses all sounds

        void pauseAllSounds();

        // Resumes all sounds

        void resumeAllSounds();

        // Sets the position of the listener: expects a 3 component position vector

        void setListenerPosition( float * position );

        // Sets the orientation of the listener: expects a 3x3 orientaiton matrix

        void setListenerOrientation( float * orientation );

        // Adds a sound into the scene: returns the sound ID which is 0 on failure to play

        unsigned addSound
        (
            const Sound *   sound,          // sound to play
            float           source_x,       // sound location: x
            float           source_y,       // sound location: y
            float           source_z,       // sound location: z
            float           distance,       // distance from which the sound can be heard
            float           frequency,      // frequency the sound will play at, 1 is normal, 0 is the min and 2 is the max
            bool            looping         // loop the sound or not ?
        );

        // Trys to delete a given sound from the sound scene

        void deleteSound( unsigned soundID );

        // Tells if a given sound exists in the sound scene:

        bool soundExists( unsigned soundID );

        // Update a given sound's position

        void updateSoundPosition
        (
            unsigned    soundID,
            float       positionX,
            float       positionY,
            float       positionZ
        );

        // Update a given sound's velocity

        void updateSoundFrequency( unsigned soundID, float frequency );


//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

 private:

        //----------------------------------------------------------------------
        // Structs
        //----------------------------------------------------------------------

        // Structure which contains information on a sound source

        struct SoundSource
        {
            float pos_x;        // position in the world: x
            float pos_y;        // position in the world: y
            float pos_z;        // position in the world: z

            // Distance for which the sound can be heard

            float distance;

            // Play this sound looping ?

            bool looping;

            // Pointer to the sound buffer for this source:

            IDirectSoundBuffer8 * buffer;

            // Sound source id: used for sound retrieval- never 0, this is reserved for sounds that arent playing

            unsigned id;
        };

        //----------------------------------------------------------------------
        // Constants
        //----------------------------------------------------------------------

        // Maximum number of sounds allowed at any time

        static const unsigned MAX_SOURCES = 512;

        //----------------------------------------------------------------------
        // Functions:
        //----------------------------------------------------------------------

        // Updates a sounds volume and panning to taking into account current listener position:

        void updateSound( SoundSource & sound );

        //----------------------------------------------------------------------
        // Variables
        //----------------------------------------------------------------------

        // The direct sound object:

        IDirectSound8 * directSound;

        // An array of sound sources:

        SoundSource sounds[MAX_SOURCES];

        // Listener inverse position and inverse orientation (3x3 matrix) :

        float listenerInvPosition[3];
        float listenerInvOrientation[9];

        // The number of currently playing sounds:

        unsigned numSounds;

        // True if all the sounds in the scene are paused:

        bool soundsPaused;

}; // end of: class SoundScene{}

//##############################################################################
// C++ Includes
//##############################################################################

#include "GLWindow.h"
#include "Sound.h"
#include "SoundSystem.h"
#include "GameScene.h"
#include "GameObjectScene.h"
#include "GameObject.h"
#include "GameObjectHook.h"

#endif // end of: ifndef SOUNDSCENE_H
