summaryrefslogtreecommitdiff
path: root/src/sound.cpp
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-04-11 21:56:38 +0200
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-04-11 21:56:38 +0200
commit5957475d12c7739fbe69e569316a33588c621c51 (patch)
tree542fb61c4034d477ee8bfe111efca3589471cef1 /src/sound.cpp
parentd30a53bf394079f2286fcf1faa8932d0becc2980 (diff)
downloadmana-5957475d12c7739fbe69e569316a33588c621c51.tar.gz
mana-5957475d12c7739fbe69e569316a33588c621c51.tar.bz2
mana-5957475d12c7739fbe69e569316a33588c621c51.tar.xz
mana-5957475d12c7739fbe69e569316a33588c621c51.zip
Implemented a simple non-blocking fadeOutAndPlay system.
It's currently used when changing maps. Resolves: TMW-Mantis: #750. Reviewed-by: Thorbjorn.
Diffstat (limited to 'src/sound.cpp')
-rw-r--r--src/sound.cpp59
1 files changed, 52 insertions, 7 deletions
diff --git a/src/sound.cpp b/src/sound.cpp
index c64e10d8..636da7c7 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -29,16 +29,37 @@
#include "resources/resourcemanager.h"
#include "resources/soundeffect.h"
+/**
+ * This will be set to true, when a music can be freed after a fade out
+ * Currently used by fadeOutCallBack()
+ */
+static bool sFadingOutEnded = false;
+
+/**
+ * Callback used at end of fadeout.
+ * It is called by Mix_MusicFadeFinished().
+ */
+static void fadeOutCallBack()
+{
+ sFadingOutEnded = true;
+}
+
Sound::Sound():
mInstalled(false),
mSfxVolume(100),
mMusicVolume(60),
mMusic(NULL)
{
+ // This set up our callback function used to
+ // handle fade outs endings.
+ sFadingOutEnded = false;
+ Mix_HookMusicFinished(fadeOutCallBack);
}
Sound::~Sound()
{
+ // Unlink the callback function.
+ Mix_HookMusicFinished(NULL);
}
void Sound::init()
@@ -118,11 +139,6 @@ void Sound::info()
logger->log("Sound::info() Channels: %i", channels);
}
-int Sound::getMaxVolume() const
-{
- return MIX_MAX_VOLUME;
-}
-
void Sound::setMusicVolume(int volume)
{
mMusicVolume = volume;
@@ -223,8 +239,37 @@ void Sound::fadeOutMusic(int ms)
if (mMusic)
{
Mix_FadeOutMusic(ms);
- Mix_FreeMusic(mMusic);
- mMusic = NULL;
+ // Note: The fadeOutCallBack handler will take care about freeing
+ // the music file at fade out ending.
+ }
+ else
+ {
+ sFadingOutEnded = true;
+ }
+}
+
+void Sound::fadeOutAndPlayMusic(const std::string &path, int ms)
+{
+ mNextMusicPath = path;
+ fadeOutMusic(ms);
+}
+
+void Sound::logic()
+{
+ if (sFadingOutEnded)
+ {
+ if (mMusic)
+ {
+ Mix_FreeMusic(mMusic);
+ mMusic = NULL;
+ }
+ sFadingOutEnded = false;
+
+ if (!mNextMusicPath.empty())
+ {
+ playMusic(mNextMusicPath);
+ mNextMusicPath.clear();
+ }
}
}