summaryrefslogtreecommitdiffstats
path: root/src/sound
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2013-04-13 16:06:35 +0200
committerkrzys-h <krzys_h@interia.pl>2013-04-13 16:34:52 +0200
commitcdba398d29b78256ca12d665ccc4c31c00ec2007 (patch)
tree78baa11a65bd67246d7c6ef5d1704ba4e027b15b /src/sound
parente3b92fb9d842af19f40777dc6764204100c926da (diff)
downloadcolobot-cdba398d29b78256ca12d665ccc4c31c00ec2007.tar.gz
colobot-cdba398d29b78256ca12d665ccc4c31c00ec2007.tar.bz2
colobot-cdba398d29b78256ca12d665ccc4c31c00ec2007.zip
Added music files cache
Diffstat (limited to 'src/sound')
-rw-r--r--src/sound/oalsound/alsound.cpp38
-rw-r--r--src/sound/oalsound/alsound.h2
-rw-r--r--src/sound/sound.h9
3 files changed, 40 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;
diff --git a/src/sound/sound.h b/src/sound/sound.h
index f101518..9d0e9a0 100644
--- a/src/sound/sound.h
+++ b/src/sound/sound.h
@@ -178,6 +178,8 @@ class CSoundInterface
/** Function called to add all music files to list */
inline void AddMusicFiles(std::string path) {
m_soundPath = path;
+ CacheMusic("sound010.ogg");
+ CacheMusic("sound011.ogg");
};
/** Function called to cache sound effect file.
@@ -188,6 +190,13 @@ class CSoundInterface
*/
inline virtual bool Cache(Sound bSound, std::string bFile) { return true; };
+ /** Function called to cache music file.
+ * This function is called by CRobotMain for each file used in the mission.
+ * \param bFile - file to load
+ * \return return true on success
+ */
+ inline virtual bool CacheMusic(std::string bFile) { return true; };
+
/** Return if plugin is enabled
* \return return true if plugin is enabled
*/