LandscapeSoundManager.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <landscape/LandscapeSoundManager.h>
00022 #include <landscapedef/LandscapeInclude.h>
00023 #include <landscapedef/LandscapeTex.h>
00024 #include <landscapedef/LandscapeDefn.h>
00025 #include <landscapedef/LandscapeSound.h>
00026 #include <landscapemap/LandscapeMaps.h>
00027 #include <client/ScorchedClient.h>
00028 #include <graph/OptionsDisplay.h>
00029 #include <common/Defines.h>
00030 #include <sound/Sound.h>
00031 
00032 LandscapeSoundManager *LandscapeSoundManager::instance_(0);
00033 
00034 LandscapeSoundManager *LandscapeSoundManager::instance()
00035 {
00036         if (!instance_) instance_ = new LandscapeSoundManager();
00037         return instance_;
00038 }
00039 
00040 LandscapeSoundManager::LandscapeSoundManager() : 
00041         lastTime_(0), haveSound_(false)
00042 {
00043 }
00044 
00045 LandscapeSoundManager::~LandscapeSoundManager()
00046 {
00047 }
00048 
00049 void LandscapeSoundManager::cleanUp()
00050 {
00051         haveSound_ = false;
00052         std::list<LandscapeSoundManagerEntry>::iterator itor;
00053         for (itor = entries_.begin();
00054                 itor != entries_.end();
00055                 itor++)
00056         {
00057                 LandscapeSoundManagerEntry &entry = (*itor);
00058                 delete entry.soundSource;
00059                 entry.soundSource = 0;
00060         }
00061         entries_.clear();
00062 }
00063 
00064 void LandscapeSoundManager::addSounds()
00065 {
00066         cleanUp();
00067 
00068         // Load in the per level sound
00069         LandscapeDefn *defn = 
00070                 ScorchedClient::instance()->getLandscapeMaps().getDefinitions().getDefn();
00071         LandscapeTex *tex = 
00072                 ScorchedClient::instance()->getLandscapeMaps().getDefinitions().getTex();
00073 
00074         haveSound_ = true;
00075         loadSound(tex->texDefn.includes);
00076         loadSound(defn->texDefn.includes);
00077 }
00078 
00079 void LandscapeSoundManager::loadSound(std::vector<LandscapeInclude *> &sounds)
00080 {
00081         std::vector<LandscapeInclude *>::iterator itor;
00082         for (itor = sounds.begin();
00083                 itor != sounds.end();
00084                 itor++)
00085         {
00086                 LandscapeInclude *sound = (*itor);
00087 
00088                 std::vector<LandscapeSoundType *>::iterator typeItor;
00089                 for (typeItor = sound->sounds.begin();
00090                         typeItor != sound->sounds.end();
00091                         typeItor ++)
00092                 {
00093                         LandscapeSoundType *soundType = (*typeItor);
00094                         for (int i=0; i<soundType->position->getInitCount(); i++)
00095                         {
00096                                 // Create the new sound entry
00097                                 LandscapeSoundManagerEntry entry;
00098                                 entry.soundType = soundType;
00099                                 entry.initData = soundType->position->getInitData(i);
00100                                 entry.timeLeft = soundType->timing->getNextEventTime();
00101                                 entry.soundSource = new VirtualSoundSource(
00102                                         VirtualSoundPriority::eEnvironment, 
00103                                         (entry.timeLeft < 0.0f), // Looping
00104                                         false);
00105                                 entries_.push_back(entry);
00106 
00107                                 // Start any looped sounds
00108                                 if (entry.timeLeft < 0.0f)
00109                                 {
00110                                         if (entry.soundType->position->setPosition(
00111                                                 entry.soundSource, entry.initData))
00112                                         {
00113                                                 entry.soundType->sound->play(entry.soundSource);
00114                                         }
00115                                         else
00116                                         {
00117                                                 entry.removed = true;
00118                                         }
00119                                 }
00120                         }
00121                 }
00122         }
00123 }
00124 
00125 void LandscapeSoundManager::simulate(float frameTime)
00126 {
00127         if (OptionsDisplay::instance()->getAmbientSoundVolume() == 0)
00128         {
00129                 if (haveSound_) cleanUp();
00130                 return;
00131         }
00132         else if (!haveSound_)
00133         {
00134                 addSounds();
00135         }
00136 
00137         lastTime_ += frameTime;
00138         if (lastTime_ < 0.1f) return;
00139 
00140         std::list<LandscapeSoundManagerEntry>::iterator itor;
00141         for (itor = entries_.begin();
00142                 itor != entries_.end();
00143                 itor++)
00144         {
00145                 LandscapeSoundManagerEntry &entry = (*itor);
00146 
00147                 // Check if this entry is still relevant
00148                 if (entry.removed) continue;
00149 
00150                 // Set this sounds position
00151                 if (!entry.soundType->position->setPosition(
00152                         entry.soundSource, entry.initData))
00153                 {
00154                         // The position set failed, stop this sound and remove it
00155                         entry.soundSource->stop();
00156                         entry.removed = true;
00157                 }
00158                 else
00159                 {
00160                         // Set the volume
00161                         if (entry.soundSource)
00162                         {
00163                                 entry.soundSource->setGain(
00164                                         (float)OptionsDisplay::instance()->getAmbientSoundVolume() / 128.0f);
00165                         }
00166 
00167                         // Check if looped
00168                         if (entry.timeLeft >= 0.0f) 
00169                         {
00170                                 // Not looped
00171                                 // Check if we play again
00172                                 entry.timeLeft -= lastTime_;
00173                                 if (entry.timeLeft < 0.0f)
00174                                 {
00175                                         // Play again
00176                                         entry.timeLeft = entry.soundType->timing->getNextEventTime();
00177                                         entry.soundType->sound->play(entry.soundSource);
00178                                 }
00179                         }
00180                 }
00181         }
00182 
00183         lastTime_ = 0.0f;
00184 }

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3