diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/gui/setup_audio.cpp | 14 | ||||
-rw-r--r-- | src/localplayer.cpp | 5 | ||||
-rw-r--r-- | src/sound.cpp | 121 | ||||
-rw-r--r-- | src/sound.h | 32 |
5 files changed, 89 insertions, 85 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 555c3f17..95f2cb1b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -132,7 +132,7 @@ bool Engine::changeMap(const std::string &mapPath) std::string newMusic = newMap->getProperty("music"); if (newMusic != oldMusic) - sound.playMusic(newMusic, -1); + sound.playMusic(newMusic); mCurrentMap = newMap; diff --git a/src/gui/setup_audio.cpp b/src/gui/setup_audio.cpp index 3f98c44f..05a9eaa2 100644 --- a/src/gui/setup_audio.cpp +++ b/src/gui/setup_audio.cpp @@ -31,8 +31,6 @@ #include "configuration.h" #include "log.h" #include "sound.h" -#include "map.h" -#include "engine.h" #include "utils/gettext.h" @@ -41,8 +39,8 @@ Setup_Audio::Setup_Audio(): mSfxVolume((int)config.getValue("sfxVolume", 100)), mSoundEnabled(config.getValue("sound", 0)), mSoundCheckBox(new CheckBox(_("Sound"), mSoundEnabled)), - mSfxSlider(new Slider(0, 128)), - mMusicSlider(new Slider(0, 128)) + mSfxSlider(new Slider(0, sound.getMaxVolume())), + mMusicSlider(new Slider(0, sound.getMaxVolume())) { setName(_("Audio")); setDimension(gcn::Rectangle(0, 0, 250, 200)); @@ -96,12 +94,6 @@ void Setup_Audio::apply() new OkDialog("Sound Engine", err); logger->log("Warning: %s", err); } - - if (engine) - { - Map *currentMap = engine->getCurrentMap(); - sound.playMusic(currentMap->getProperty("music"), -1); - } } else { @@ -134,6 +126,6 @@ void Setup_Audio::action(const gcn::ActionEvent &event) else if (event.getId() == "music") { config.setValue("musicVolume", (int) mMusicSlider->getValue()); - sound.setMusicVolume((int)mMusicSlider->getValue()); + sound.setMusicVolume((int) mMusicSlider->getValue()); } } diff --git a/src/localplayer.cpp b/src/localplayer.cpp index ecc6b25d..5748cd53 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -973,9 +973,8 @@ void LocalPlayer::handleStatusEffect(StatusEffect *effect, int effectId) { Being::handleStatusEffect(effect, effectId); - - - if (effect) { + if (effect) + { effect->deliverMessage(); effect->playSFX(); diff --git a/src/sound.cpp b/src/sound.cpp index 6d18b86c..3686d009 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -30,7 +30,8 @@ Sound::Sound(): mInstalled(false), mSfxVolume(100), - mMusicVolume(60) + mMusicVolume(60), + mMusic(NULL) { } @@ -46,7 +47,8 @@ void Sound::init() logger->log("Sound::init() Initializing sound..."); - if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) { + if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) + { logger->log("Sound::init() Failed to initialize audio subsystem"); return; } @@ -54,20 +56,24 @@ void Sound::init() const size_t audioBuffer = 4096; const int res = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, - 2, audioBuffer); - if (res >= 0) { - Mix_AllocateChannels(16); - } else { + MIX_DEFAULT_CHANNELS, audioBuffer); + if (res < 0) + { logger->log("Sound::init Could not initialize audio: %s", - Mix_GetError()); + Mix_GetError()); return; } - info(); + Mix_AllocateChannels(16); + Mix_VolumeMusic(mMusicVolume); + Mix_Volume(-1, mSfxVolume); - mMusic = NULL; + info(); mInstalled = true; + + if (!mCurrentMusicFile.empty()) + playMusic(mCurrentMusicFile); } void Sound::info() @@ -109,46 +115,56 @@ void Sound::info() logger->log("Sound::info() Channels: %i", channels); } -void Sound::setMusicVolume(int volume) +int Sound::getMaxVolume() const { - if (!mInstalled) - return; + return MIX_MAX_VOLUME; +} +void Sound::setMusicVolume(int volume) +{ mMusicVolume = volume; - Mix_VolumeMusic(volume); + + if (mInstalled) + Mix_VolumeMusic(mMusicVolume); } void Sound::setSfxVolume(int volume) { - if (!mInstalled) - return; - mSfxVolume = volume; - Mix_Volume(-1, volume); + + if (mInstalled) + Mix_Volume(-1, mSfxVolume); } -void Sound::playMusic(const std::string &filename, int loop) +static Mix_Music *loadMusic(const std::string &filename) { - if (!mInstalled) - return; - - if (mMusic) - stopMusic(); - ResourceManager *resman = ResourceManager::getInstance(); std::string path = resman->getPath("music/" + filename); - logger->log("Sound::startMusic() Playing \"%s\" %i times", path.c_str(), - loop); + logger->log("Loading music \"%s\"", path.c_str()); - mMusic = Mix_LoadMUS(path.c_str()); - if (mMusic) { - Mix_PlayMusic(mMusic, loop); - } - else { - logger->log("Sound::startMusic() Warning: error loading file: %s", - Mix_GetError()); + 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; +} + +void Sound::playMusic(const std::string &filename) +{ + mCurrentMusicFile = filename; + + if (!mInstalled) + return; + + haltMusic(); + + if ((mMusic = loadMusic(filename))) + Mix_PlayMusic(mMusic, -1); // Loop forever } void Sound::stopMusic() @@ -165,29 +181,23 @@ void Sound::stopMusic() } } -void Sound::fadeInMusic(const std::string &path, int loop, int ms) +void Sound::fadeInMusic(const std::string &path, int ms) { + mCurrentMusicFile = path; + if (!mInstalled) return; - if (mMusic) - stopMusic(); - - logger->log("Sound::fadeInMusic() Fading \"%s\" %i times (%i ms)", - path.c_str(), - loop, ms); + haltMusic(); - mMusic = Mix_LoadMUS(path.c_str()); - if (mMusic) { - Mix_FadeInMusic(mMusic, loop, ms); - } - else { - logger->log("Sound::fadeInMusic() Warning: error loading file."); - } + if ((mMusic = loadMusic(path.c_str()))) + Mix_FadeInMusic(mMusic, -1, ms); // Loop forever } void Sound::fadeOutMusic(int ms) { + mCurrentMusicFile.clear(); + if (!mInstalled) return; @@ -215,9 +225,22 @@ void Sound::playSfx(const std::string &path) void Sound::close() { - stopMusic(); + if (!mInstalled) + return; - mInstalled = false; + haltMusic(); logger->log("Sound::close() Shutting down sound..."); Mix_CloseAudio(); + + mInstalled = false; +} + +void Sound::haltMusic() +{ + if (!mMusic) + return; + + Mix_HaltMusic(); + Mix_FreeMusic(mMusic); + mMusic = NULL; } diff --git a/src/sound.h b/src/sound.h index 2e43ef37..3fce5447 100644 --- a/src/sound.h +++ b/src/sound.h @@ -46,11 +46,6 @@ class Sound void init(); /** - * Logs various info about sound device. - */ - void info(); - - /** * Removes all sound functionalities. */ void close(); @@ -59,9 +54,8 @@ class Sound * Starts background music. * * @param path The full path to the music file. - * @param loop The number of times the song is played (-1 = infinite) */ - void playMusic(const std::string &path, int loop = -1); + void playMusic(const std::string &path); /** * Stops currently running background music track. @@ -72,11 +66,9 @@ class Sound * Fades in background music. * * @param path The full path to the music file. - * @param loop The number of times the song is played (-1 = infinite) * @param ms Duration of fade-in effect (ms) */ - void fadeInMusic(const std::string &path, int loop = -1, - int ms = 2000); + void fadeInMusic(const std::string &path, int ms = 2000); /** * Fades out currently running background music track. @@ -85,18 +77,9 @@ class Sound */ void fadeOutMusic(int ms); - /** - * Sets music volume. - * - * @param volume Volume value - */ - void setMusicVolume(int volume); + int getMaxVolume() const; - /** - * Sets sfx volume. - * - * @param volume Volume value - */ + void setMusicVolume(int volume); void setSfxVolume(int volume); /** @@ -107,11 +90,18 @@ class Sound void playSfx(const std::string &path); private: + /** Logs various info about sound device. */ + void info(); + + /** Halts and frees currently playing music. */ + void haltMusic(); + bool mInstalled; int mSfxVolume; int mMusicVolume; + std::string mCurrentMusicFile; Mix_Music *mMusic; }; |