Planeshift
manager.h
Go to the documentation of this file.
1 /*
2  * manager.h
3  *
4  * Copyright (C) 2001-2010 Atomic Blue (info@planeshift.it, http://www.planeshift.it)
5  *
6  * Credits : Saul Leite <leite@engineer.com>
7  * Mathias 'AgY' Voeroes <agy@operswithoutlife.net>
8  * and all past and present planeshift coders
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation (version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 
24 #ifndef _SOUND_MANAGER_H_
25 #define _SOUND_MANAGER_H_
26 
27 
28 //====================================================================================
29 // Crystal Space Includes
30 //====================================================================================
31 #include <cssysdef.h>
32 #include <iutil/objreg.h>
33 #include <csgeom/vector3.h>
34 #include <csutil/hash.h>
35 #include <iutil/timer.h>
36 #include <csutil/timer.h>
37 #include <csutil/randomgen.h>
38 
39 //====================================================================================
40 // Project Includes
41 //====================================================================================
42 #include "util/singleton.h"
43 
44 //------------------------------------------------------------------------------------
45 // Forward Declarations
46 //------------------------------------------------------------------------------------
47 class SoundSystem;
48 class SoundDataCache;
49 class SoundHandle;
50 class SoundControl;
51 
52 enum
53 {
54  LOOP = true,
55  DONT_LOOP = false
56 };
57 
58 
59 #define VOLUME_ZERO 0.0f
60 #define VOLUME_MIN 0.0f
61 #define VOLUME_NORM 1.0f
62 #define VOLUME_MAX 2.0f
63 
64 #define DEFAULT_SNDSYS_UPDATE_TIME 100
65 #define DEFAULT_SPEED_OF_SOUND 331
66 #define DEFAULT_DOPPLER_FACTOR 0.0
67 #define DEFAULT_SOUNDLIB_PATH "/planeshift/art/soundlib.xml"
68 
80 class SoundSystemManager: public Singleton<SoundSystemManager>
81 {
82 public:
83  bool Initialised;
85  csRef<iEventTimer> eventTimer;
86 
91 
99 
107  bool Initialize(iObjectRegistry* objectReg);
108 
113  bool IsHandleValid(uint handleID) const;
114 
119  void UpdateSound();
120 
133  bool Play2DSound(const char* name, bool loop, size_t loopstart,
134  size_t loopend, float volume_preset,
135  SoundControl* &sndCtrl, SoundHandle* &handle);
136 
154  bool Play3DSound(const char* name, bool loop, size_t loopstart,
155  size_t loopend, float volume_preset,
156  SoundControl* &sndCtrl, csVector3 pos, csVector3 dir,
157  float mindist, float maxdist, float rad, int type3d,
158  SoundHandle* &handle, bool dopplerEffect = true);
159 
165  bool StopSound(uint handleID);
166 
173  bool SetSoundSource(uint handleID, csVector3 position);
174 
179  void SetPlayerPosition(csVector3& pos);
180 
185  csVector3& GetPlayerPosition();
186 
191  void SetPlayerVelocity(csVector3 vel);
192 
199  void UpdateListener(csVector3 v, csVector3 f, csVector3 t);
200 
205  csVector3 GetListenerPos() const;
206 
212  void Update();
213 
220  SoundControl* AddSoundControl(uint sndCtrlID);
221  void RemoveSoundControl(uint sndCtrlID);
222  SoundControl* GetSoundControl(uint sndCtrlID) const;
223 
224  SoundSystem* GetSoundSystem() const { return soundSystem; }
225  SoundDataCache* GetSoundDataCache() const { return soundDataCache; }
226 
227 private:
228  SoundSystem* soundSystem;
229  SoundDataCache* soundDataCache;
230  csHash<SoundHandle*, uint> soundHandles;
231  csHash<SoundControl*, uint> soundControllers;
232 
233  uint updateTime;
234  csTicks SndTime;
235  csTicks LastUpdateTime;
236 
237  csVector3 playerPosition;
238  csVector3 playerVelocity;
239 
240  uint speedOfSound;
241  float dopplerFactor;
242 
247  uint FindHandleID();
248 
254  void RemoveHandle(uint handleID);
255 
263  void ChangePlayRate(SoundHandle* handle);
264 
279  bool InitSoundHandle(const char* name, bool loop, size_t loopstart,
280  size_t loopend, float volume_preset, int type3d,
281  SoundControl* &sndCtrl, SoundHandle* &handle, bool dopplerEffect);
282 };
283 
284 #endif /*_SOUND_MANAGER_H_*/
Definition: manager.h:39
~SoundSystemManager()
Destructor will remove everything this SoundManager created.
SoundDataCache * GetSoundDataCache() const
Definition: manager.h:225
bool Play3DSound(const char *name, bool loop, size_t loopstart, size_t loopend, float volume_preset, SoundControl *&sndCtrl, csVector3 pos, csVector3 dir, float mindist, float maxdist, float rad, int type3d, SoundHandle *&handle)
Plays a 3D sound.
csVector3 GetListenerPos() const
Gets the listener position.
bool Initialize(iObjectRegistry *objectReg)
Initializes this SoundSystemManager.
SoundSystemManager()
Constructor.
void RemoveSoundControl(uint sndCtrlID)
void StopSound(SoundHandle *handle)
Stops and removes a SoundHandle.
void SetPlayerPosition(csVector3 &pos)
Sets the current player&#39;s position.
SoundSystem * GetSoundSystem() const
Definition: manager.h:224
bool Play2DSound(const char *name, bool loop, size_t loopstart, size_t loopend, float volume_preset, SoundControl *&sndCtrl, SoundHandle *&handle)
Plays a 2D sound (Cannot be converted to 3D).
void UpdateSound()
Checks all SoundHandles alters Volume, does fading and removes them if Unmanaged (Autoremove true)...
SoundDataCache is the data-keeper of SoundSystemManager.
Definition: data.h:102
void UpdateListener(csVector3 v, csVector3 f, csVector3 t)
Update listener position.
A Volume and Sound control class.
Definition: control.h:40
void Update()
Updates this SoundManager and everything its driving.
SoundControl * GetSoundControl()
Returns a NEW SoundControl.
bool SetSoundSource(uint handleID, csVector3 position)
Set the position of the sound source of the handle with the given ID.
SoundControl * mainSndCtrl
sound control for this manager
Definition: manager.h:66
bool Initialised
is initialized ?
Definition: manager.h:65
csRef< iEventTimer > eventTimer
timer event used by all the sound handle to play after a delay
Definition: manager.h:85
SoundControl * AddSoundControl(uint sndCtrlID)
Returns a NEW SoundControl.
This Manager Object is used to play all sounds.
Definition: manager.h:62
csVector3 & GetPlayerPosition()
Gets the current player&#39;s position.
void SetPlayerVelocity(csVector3 vel)
Sets the player velocity.
bool IsHandleValid(uint handleID) const
Check if the given handle ID exists.
This is an Interface Class to the Crystalspace Soundrenderer.
Definition: system.h:37