diff options
Diffstat (limited to 'src/sound')
-rw-r--r-- | src/sound/oalsound/alsound.cpp | 48 | ||||
-rw-r--r-- | src/sound/oalsound/alsound.h | 2 | ||||
-rw-r--r-- | src/sound/sound.h | 16 |
3 files changed, 28 insertions, 38 deletions
diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 3ce975b..48fcc15 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -539,44 +539,32 @@ void ALSound::SetListener(Math::Vector eye, Math::Vector lookat) } } - bool ALSound::PlayMusic(int rank, bool bRepeat) { + std::stringstream filename; + filename << "music" << std::setfill('0') << std::setw(3) << rank << ".ogg"; + return PlayMusic(filename.str(), bRepeat); +} + +bool ALSound::PlayMusic(std::string filename, bool bRepeat) +{ if (!mEnabled) { return false; } - - if (static_cast<int>(mCurrentMusic->GetSoundType()) != rank) { - // check if we have music in cache - for (auto music : mMusicCache) { - if (static_cast<int>(music->GetSoundType()) == rank) { - GetLogger()->Debug("Music loaded from cache\n"); - mCurrentMusic->SetBuffer(music); - - mCurrentMusic->SetVolume(mMusicVolume); - mCurrentMusic->SetLoop(bRepeat); - mCurrentMusic->Play(); - return true; - } - } - - // we cache only 3 music files - if (mMusicCache.size() == 3) { - mCurrentMusic->FreeBuffer(); - mMusicCache.pop_back(); - } - if (mMusic.find(rank) == mMusic.end()) { - GetLogger()->Info("Requested music %d was not found.\n", rank); - return false; - } + std::stringstream file; + file << m_soundPath << "/" << filename; - Buffer *buffer = new Buffer(); - mMusicCache.push_front(buffer); - buffer->LoadFromFile(mMusic.at(rank), static_cast<Sound>(rank)); - mCurrentMusic->SetBuffer(buffer); - mMusicCache[rank] = buffer; + if (!boost::filesystem::exists(file.str())) { + GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str()); + return false; } + + // TODO: Cache + + Buffer *buffer = new Buffer(); + buffer->LoadFromFile(file.str(), static_cast<Sound>(-1)); + mCurrentMusic->SetBuffer(buffer); mCurrentMusic->SetVolume(mMusicVolume); mCurrentMusic->SetLoop(bRepeat); diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 5701997..7e0503a 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -66,6 +66,7 @@ class ALSound : public CSoundInterface bool MuteAll(bool bMute); bool PlayMusic(int rank, bool bRepeat); + bool PlayMusic(std::string filename, bool bRepeat); bool RestartMusic(); void SuspendMusic(); void StopMusic(); @@ -91,7 +92,6 @@ class ALSound : public CSoundInterface ALCcontext* mContext; std::map<Sound, Buffer*> mSounds; std::map<int, Channel*> mChannels; - std::deque<Buffer*> mMusicCache; Channel *mCurrentMusic; Math::Vector mEye; Math::Vector mLookat; diff --git a/src/sound/sound.h b/src/sound/sound.h index 70139ea..f101518 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -177,12 +177,7 @@ class CSoundInterface /** Function called to add all music files to list */ inline void AddMusicFiles(std::string path) { - for ( int i = 1; i <= 12; i++ ) { - std::stringstream filename; - filename << path << "/music" << std::setfill('0') << std::setw(3) << i << ".ogg"; - if (boost::filesystem::exists(filename.str())) - mMusic[i] = filename.str(); - } + m_soundPath = path; }; /** Function called to cache sound effect file. @@ -317,6 +312,13 @@ class CSoundInterface */ inline virtual bool PlayMusic(int rank, bool bRepeat) {return true;}; + /** Start playing music + * \param filename - name of file to play + * \param bRepeat - repeat playing + * \return return true on success + */ + inline virtual bool PlayMusic(std::string filename, bool bRepeat) {return true;}; + /** Restart music * @return return true on success */ @@ -338,6 +340,6 @@ class CSoundInterface inline virtual bool IsPlayingMusic() {return true;}; protected: - std::map<int, std::string> mMusic; + std::string m_soundPath; }; |