summaryrefslogtreecommitdiffstats
path: root/src/sound/oalsound
diff options
context:
space:
mode:
Diffstat (limited to 'src/sound/oalsound')
-rw-r--r--src/sound/oalsound/alsound.cpp38
-rw-r--r--src/sound/oalsound/alsound.h2
2 files changed, 31 insertions, 9 deletions
diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp
index 48fcc15..50564e9 100644
--- a/src/sound/oalsound/alsound.cpp
+++ b/src/sound/oalsound/alsound.cpp
@@ -52,6 +52,10 @@ void ALSound::CleanUp()
delete item.second;
}
+ for (auto item : mMusic) {
+ delete item.second;
+ }
+
mEnabled = false;
mCurrentMusic->FreeBuffer();
@@ -160,6 +164,18 @@ bool ALSound::Cache(Sound sound, std::string filename)
return false;
}
+bool ALSound::CacheMusic(std::string filename)
+{
+ Buffer *buffer = new Buffer();
+ std::stringstream file;
+ file << m_soundPath << "/" << filename;
+ if (buffer->LoadFromFile(file.str(), static_cast<Sound>(-1))) {
+ mMusic[filename] = buffer;
+ return true;
+ }
+ return false;
+}
+
int ALSound::GetPriority(Sound sound)
{
@@ -555,16 +571,20 @@ bool ALSound::PlayMusic(std::string filename, bool bRepeat)
std::stringstream file;
file << m_soundPath << "/" << filename;
- if (!boost::filesystem::exists(file.str())) {
- GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str());
- return false;
+ // check if we have music in cache
+ if (mMusic.find(filename) == mMusic.end()) {
+ GetLogger()->Warn("Music %s was not cached!\n", filename.c_str());
+ if (!boost::filesystem::exists(file.str())) {
+ GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str());
+ return false;
+ }
+ Buffer *buffer = new Buffer();
+ buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
+ mCurrentMusic->SetBuffer(buffer);
+ } else {
+ GetLogger()->Debug("Music loaded from cache\n");
+ mCurrentMusic->SetBuffer(mMusic[filename]);
}
-
- // 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 7e0503a..b8afd4d 100644
--- a/src/sound/oalsound/alsound.h
+++ b/src/sound/oalsound/alsound.h
@@ -40,6 +40,7 @@ class ALSound : public CSoundInterface
bool Create(bool b3D);
bool Cache(Sound, std::string);
+ bool CacheMusic(std::string);
bool GetEnable();
@@ -91,6 +92,7 @@ class ALSound : public CSoundInterface
ALCdevice* mDevice;
ALCcontext* mContext;
std::map<Sound, Buffer*> mSounds;
+ std::map<std::string, Buffer*> mMusic;
std::map<int, Channel*> mChannels;
Channel *mCurrentMusic;
Math::Vector mEye;