From 4cd8cf7f527553abad86c07c8ebbb2ba8b5d3699 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Mon, 11 Apr 2011 21:56:38 +0200 Subject: 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 --- src/sound.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 8 deletions(-) (limited to 'src/sound.cpp') 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(); + } } } -- cgit v1.2.3-60-g2f50