diff options
Diffstat (limited to 'src/sound.cpp')
-rw-r--r-- | src/sound.cpp | 95 |
1 files changed, 82 insertions, 13 deletions
diff --git a/src/sound.cpp b/src/sound.cpp index fa39e49b..4216c8fa 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -21,13 +21,29 @@ #include <SDL.h> +#include "configuration.h" +#include "game.h" +#include "localplayer.h" #include "log.h" #include "sound.h" #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), @@ -35,10 +51,16 @@ Sound::Sound(): 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 +140,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; @@ -142,7 +159,7 @@ void Sound::setSfxVolume(int volume) static Mix_Music *loadMusic(const std::string &filename) { ResourceManager *resman = ResourceManager::getInstance(); - std::string path = resman->getPath("music/" + filename); + std::string path = resman->getPath(paths.getStringValue("music") + filename); if (path.find(".zip/") != std::string::npos || path.find(".zip\\") != std::string::npos) @@ -152,7 +169,7 @@ static Mix_Music *loadMusic(const std::string &filename) logger->log("Loading music \"%s\" from temporary file tempMusic.ogg", path.c_str()); bool success = resman->copyFile( - paths.getValue("music", "music/") + paths.getStringValue("music") + filename, "tempMusic.ogg"); if (success) path = resman->getPath("tempMusic.ogg"); @@ -223,22 +240,74 @@ 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::playSfx(const std::string &path) +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(); + } + } +} + +void Sound::playSfx(const std::string &path, int x, int y) { if (!mInstalled || path.empty()) return; + std::string tmpPath; + if (!path.find("sfx/")) + tmpPath = path; + else + tmpPath = paths.getValue("sfx", "sfx/") + path; ResourceManager *resman = ResourceManager::getInstance(); - SoundEffect *sample = resman->getSoundEffect(path); + SoundEffect *sample = resman->getSoundEffect(tmpPath); if (sample) { logger->log("Sound::playSfx() Playing: %s", path.c_str()); - sample->play(0, 120); + int vol = 120; + if (player_node && x > 0 && y > 0) + { + Vector pos = player_node->getPosition(); + Map *map = Game::instance()->getCurrentMap(); + int dx = ((int)pos.x - x) / map->getTileWidth(); + int dy = ((int)pos.y - y) / map->getTileHeight(); + if (dx < 0) + dx = -dx; + if (dy < 0) + dy = -dy; + int dist = dx > dy ? dx : dy; + + // Check for negative values + if (dist * 8 > vol) + return; + vol -= dist * 8; + } + sample->play(0, vol); } } |