summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-04-11 21:56:38 +0200
committerAndrei Karas <akaras@inbox.ru>2011-04-28 02:41:43 +0300
commit4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699 (patch)
tree2fce080e3001a53cbabc8468b98c84b4afd84444
parent24662442f04d3c70aef4b0502545e346b6c63f67 (diff)
downloadplus-4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699.tar.gz
plus-4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699.tar.bz2
plus-4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699.tar.xz
plus-4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699.zip
Implemented a simple non-blocking fadeOutAndPlay system.
It's currently used when changing maps. Resolves: TMW-Mantis: #750. Reviewed-by: Thorbjorn. Conflicts: src/game.cpp src/sound.cpp src/sound.h
-rw-r--r--src/client.cpp2
-rw-r--r--src/game.cpp7
-rw-r--r--src/sound.cpp59
-rw-r--r--src/sound.h28
4 files changed, 84 insertions, 12 deletions
diff --git a/src/client.cpp b/src/client.cpp
index 51e9155e7..1ac4be9f4 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -675,6 +675,8 @@ int Client::exec()
if (game)
game->logic();
+ sound.logic();
+
++lastTickTime;
}
diff --git a/src/game.cpp b/src/game.cpp
index d5ac15ef3..3c3067bbf 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1430,7 +1430,12 @@ void Game::changeMap(const std::string &mapPath)
std::string oldMusic = mCurrentMap ? mCurrentMap->getMusicFile() : "";
std::string newMusic = newMap ? newMap->getMusicFile() : "";
if (newMusic != oldMusic)
- sound.playMusic(newMusic);
+ {
+ if (newMusic.empty())
+ sound.fadeOutMusic();
+ else
+ sound.fadeOutAndPlayMusic(newMusic);
+ }
if (mCurrentMap)
mCurrentMap->saveExtraLayer();
diff --git a/src/sound.cpp b/src/sound.cpp
index d490898c8..5a0959804 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -30,7 +30,20 @@
#include "resources/resourcemanager.h"
#include "resources/soundeffect.h"
-#include "configuration.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),
@@ -41,6 +54,10 @@ Sound::Sound():
mPlayGui(false),
mPlayMusic(false)
{
+ // This set up our callback function used to
+ // handle fade outs endings.
+ sFadingOutEnded = false;
+ Mix_HookMusicFinished(fadeOutCallBack);
}
Sound::~Sound()
@@ -58,6 +75,8 @@ void Sound::optionChanged(const std::string &value)
mPlayGui = config.getBoolValue("playGuiSound");
else if (value == "playMusic")
mPlayMusic = config.getBoolValue("playMusic");
+ // Unlink the callback function.
+ Mix_HookMusicFinished(NULL);
}
void Sound::init()
@@ -145,11 +164,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;
@@ -258,8 +272,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();
+ }
}
}
diff --git a/src/sound.h b/src/sound.h
index 0de9f0636..6a509a574 100644
--- a/src/sound.h
+++ b/src/sound.h
@@ -73,16 +73,25 @@ class Sound : public ConfigListener
* @param path The full path to the music file.
* @param ms Duration of fade-in effect (ms)
*/
- void fadeInMusic(const std::string &path, int ms = 2000);
+ void fadeInMusic(const std::string &path, int ms = 1000);
/**
* Fades out currently running background music track.
*
* @param ms Duration of fade-out effect (ms)
*/
- void fadeOutMusic(int ms);
+ void fadeOutMusic(int ms = 1000);
- int getMaxVolume() const;
+ /**
+ * Fades out a background music and play a new one.
+ *
+ * @param path The full path to the fade in music file.
+ * @param ms Duration of fade-out effect (ms)
+ */
+ void fadeOutAndPlayMusic(const std::string &path, int ms = 1000);
+
+ int getMaxVolume() const
+ { return MIX_MAX_VOLUME; }
void setMusicVolume(int volume);
void setSfxVolume(int volume);
@@ -110,6 +119,13 @@ class Sound : public ConfigListener
std::string getCurrentMusicFile()
{ return mCurrentMusicFile; }
+ /**
+ * The sound logic.
+ * Currently used to check whether the music file can be freed after
+ * a fade out, and whether new music has to be played.
+ */
+ void logic();
+
private:
/** Logs various info about sound device. */
void info();
@@ -117,6 +133,12 @@ class Sound : public ConfigListener
/** Halts and frees currently playing music. */
void haltMusic();
+ /**
+ * When calling fadeOutAndPlayMusic(),
+ * the music file below will then be played
+ */
+ std::string mNextMusicPath;
+
bool mInstalled;
int mSfxVolume;