diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-01-24 22:59:25 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-01-24 22:59:25 +0300 |
commit | 53535919d3cf62f97bc9fee28ee31e4f577d2700 (patch) | |
tree | 98b8bc6b8da941b5d2c72c598756374623ad850a /src/sound.cpp | |
parent | b4e47fb41a19ca09c02bcd009b28cb7c3caa2256 (diff) | |
download | mv-53535919d3cf62f97bc9fee28ee31e4f577d2700.tar.gz mv-53535919d3cf62f97bc9fee28ee31e4f577d2700.tar.bz2 mv-53535919d3cf62f97bc9fee28ee31e4f577d2700.tar.xz mv-53535919d3cf62f97bc9fee28ee31e4f577d2700.zip |
Based on commit b856e8b47ab2dfd393e3c2720c5647eb66393931
Author: Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
Date: Tue Jan 24 19:14:24 2012 +0100
Stream music files directly from the archives
Use Mix_LoadMUS_RW to stream music files directly from PhysFS. I kept
around ResourceManager:copyFile for now, since it may have other uses.
Diffstat (limited to 'src/sound.cpp')
-rw-r--r-- | src/sound.cpp | 93 |
1 files changed, 31 insertions, 62 deletions
diff --git a/src/sound.cpp b/src/sound.cpp index 123a66567..fb6958e25 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -27,6 +27,7 @@ #include "logger.h" #include "sound.h" +#include "resources/music.h" #include "resources/resourcemanager.h" #include "resources/soundeffect.h" @@ -184,84 +185,49 @@ void Sound::setSfxVolume(int volume) Mix_Volume(-1, mSfxVolume); } -static Mix_Music *loadMusic(const std::string &filename) +static Music *loadMusic(const std::string &fileName) { ResourceManager *resman = ResourceManager::getInstance(); - std::string path = resman->getPath( - paths.getStringValue("music") + filename); - - if (path.find(".zip/") != std::string::npos || - path.find(".zip\\") != std::string::npos) - { - // Music file is a virtual file inside a zip archive - we have to copy - // it to a temporary physical file so that SDL_mixer can stream it. - logger->log("Loading music \"%s\" from temporary file tempMusic.ogg", - path.c_str()); - bool success = resman->copyFile(paths.getStringValue("music") - + filename, "tempMusic.ogg"); - if (success) - path = resman->getPath("tempMusic.ogg"); - else - return nullptr; - } - else - { - logger->log("Loading music \"%s\"", path.c_str()); - } - - if (path.empty()) - return nullptr; - - Mix_Music *music = Mix_LoadMUS(path.c_str()); - - if (!music) - { - logger->log("Mix_LoadMUS() Error loading '%s': %s", path.c_str(), - Mix_GetError()); - } - - return music; + return resman->getMusic(paths.getStringValue("music") + fileName); } -void Sound::playMusic(const std::string &filename) +void Sound::playMusic(const std::string &fileName) { - mCurrentMusicFile = filename; + mCurrentMusicFile = fileName; if (!mInstalled || !mPlayMusic) return; haltMusic(); - if (!filename.empty() && (mMusic = loadMusic(filename))) - Mix_PlayMusic(mMusic, -1); // Loop forever + if (!fileName.empty()) + { + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(); + } } void Sound::stopMusic() { - if (!mInstalled) - return; - - logger->log1("Sound::stopMusic()"); - - if (mMusic) - { - Mix_HaltMusic(); - Mix_FreeMusic(mMusic); - mMusic = nullptr; - } + haltMusic(); } -void Sound::fadeInMusic(const std::string &path, int ms) +void Sound::fadeInMusic(const std::string &fileName, int ms) { - mCurrentMusicFile = path; + mCurrentMusicFile = fileName; if (!mInstalled || !mPlayMusic) return; haltMusic(); - if ((mMusic = loadMusic(path.c_str()))) - Mix_FadeInMusic(mMusic, -1, ms); // Loop forever + if (!fileName.empty()) + { + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(-1, ms); + } } void Sound::fadeOutMusic(int ms) @@ -285,9 +251,9 @@ void Sound::fadeOutMusic(int ms) } } -void Sound::fadeOutAndPlayMusic(const std::string &path, int ms) +void Sound::fadeOutAndPlayMusic(const std::string &fileName, int ms) { - mNextMusicPath = path; + mNextMusicFile = fileName; fadeOutMusic(ms); } @@ -297,15 +263,15 @@ void Sound::logic() { if (mMusic) { - Mix_FreeMusic(mMusic); + mMusic->decRef(); mMusic = nullptr; } sFadingOutEnded = false; - if (!mNextMusicPath.empty()) + if (!mNextMusicFile.empty()) { - playMusic(mNextMusicPath); - mNextMusicPath.clear(); + playMusic(mNextMusicFile); + mNextMusicFile.clear(); } } } @@ -383,8 +349,11 @@ void Sound::haltMusic() return; Mix_HaltMusic(); - Mix_FreeMusic(mMusic); - mMusic = nullptr; + if (mMusic) + { + mMusic->decRef(); + mMusic = nullptr; + } } void Sound::changeAudio() |