diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-01-24 19:14:24 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-01-24 19:15:07 +0100 |
commit | b856e8b47ab2dfd393e3c2720c5647eb66393931 (patch) | |
tree | 3709cc05792d977aa36ddaa5e3874552aed9d2e4 | |
parent | 59c5d1ef260736225ba3ba486f40532949cc293b (diff) | |
download | mana-b856e8b47ab2dfd393e3c2720c5647eb66393931.tar.gz mana-b856e8b47ab2dfd393e3c2720c5647eb66393931.tar.bz2 mana-b856e8b47ab2dfd393e3c2720c5647eb66393931.tar.xz mana-b856e8b47ab2dfd393e3c2720c5647eb66393931.zip |
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.
Also cleaned up some initialization of configuration defaults.
Reviewed-by: Yohann Ferreira
-rw-r--r-- | src/client.cpp | 11 | ||||
-rw-r--r-- | src/defaults.cpp | 2 | ||||
-rw-r--r-- | src/resources/music.cpp | 50 | ||||
-rw-r--r-- | src/resources/music.h | 17 | ||||
-rw-r--r-- | src/sound.cpp | 72 | ||||
-rw-r--r-- | src/sound.h | 22 |
6 files changed, 59 insertions, 115 deletions
diff --git a/src/client.cpp b/src/client.cpp index 058c113b..ab9557fe 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -202,11 +202,15 @@ Client::Client(const Options &options): logger = new Logger; + // Set default values for configuration files + branding.setDefaultValues(getBrandingDefaults()); + paths.setDefaultValues(getPathsDefaults()); + config.setDefaultValues(getConfigDefaults()); + // Load branding information if (!options.brandingPath.empty()) { branding.init(options.brandingPath); - branding.setDefaultValues(getBrandingDefaults()); } initRootDir(); @@ -393,8 +397,7 @@ Client::Client(const Options &options): userPalette = new UserPalette; setupWindow = new Setup; - sound.playMusic(branding.getValue("loginMusic", - "music/system/Magick - Real.ogg")); + sound.playMusic(branding.getStringValue("loginMusic")); // Initialize default server mCurrentServer.hostname = options.serverName; @@ -754,7 +757,6 @@ int Client::exec() // Read default paths file 'data/paths.xml' paths.init("paths.xml", true); - paths.setDefaultValues(getPathsDefaults()); Event::trigger(Event::ClientChannel, Event::LoadingDatabases); @@ -1257,7 +1259,6 @@ void Client::initConfiguration() { fclose(configFile); config.init(configPath); - config.setDefaultValues(getConfigDefaults()); } } diff --git a/src/defaults.cpp b/src/defaults.cpp index 80273faa..100b7f97 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -131,7 +131,7 @@ DefaultsData* getBrandingDefaults() AddDEF(brandingData, "wallpaperFile", ""); AddDEF(brandingData, "appName", "Mana"); AddDEF(brandingData, "appIcon", "icons/mana"); - AddDEF(brandingData, "loginMusic", "Magick - Real.ogg"); + AddDEF(brandingData, "loginMusic", "system/Magick - Real.ogg"); AddDEF(brandingData, "defaultServer", ""); AddDEF(brandingData, "defaultPort", DEFAULT_PORT); AddDEF(brandingData, "defaultServerType", "tmwathena"); diff --git a/src/resources/music.cpp b/src/resources/music.cpp index 2154fb5f..677838b6 100644 --- a/src/resources/music.cpp +++ b/src/resources/music.cpp @@ -23,61 +23,33 @@ #include "log.h" -Music::Music(Mix_Chunk *music): - mChunk(music), - mChannel(-1) +Music::Music(Mix_Music *music): + mMusic(music) { } Music::~Music() { - //Mix_FreeMusic(music); - Mix_FreeChunk(mChunk); + Mix_FreeMusic(mMusic); } Resource *Music::load(SDL_RWops *rw) { - // Use Mix_LoadMUS to load the raw music data - //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted - Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1); - - if (tmpMusic) + if (Mix_Music *music = Mix_LoadMUS_RW(rw)) { - return new Music(tmpMusic); + return new Music(music); } else { logger->log("Error, failed to load music: %s", Mix_GetError()); - return NULL; + return 0; } } -bool Music::play(int loops) -{ - /* - * Warning: loops should be always set to -1 (infinite) with current - * implementation to avoid halting the playback of other samples - */ - - /*if (Mix_PlayMusic(music, loops)) - return true;*/ - Mix_VolumeChunk(mChunk, 120); - mChannel = Mix_PlayChannel(-1, mChunk, loops); - - return mChannel != -1; -} - -void Music::stop() +bool Music::play(int loops, int fadeIn) { - /* - * Warning: very dungerous trick, it could try to stop channels occupied - * by samples rather than the current music file - */ - - //Mix_HaltMusic(); - - if (mChannel != -1) - { - Mix_HaltChannel(mChannel); - } + if (fadeIn > 0) + return Mix_FadeInMusic(mMusic, loops, fadeIn); + else + return Mix_PlayMusic(mMusic, loops); } diff --git a/src/resources/music.h b/src/resources/music.h index 6a4cabd5..f744fcd4 100644 --- a/src/resources/music.h +++ b/src/resources/music.h @@ -51,24 +51,19 @@ class Music : public Resource /** * Plays the music. * - * @param loops Number of times to repeat the playback. + * @param loops Number of times to repeat the playback (-1 means + * forever). + * @param fadeIn Duration in milliseconds to fade in the music. * * @return <code>true</code> if the playback started properly * <code>false</code> otherwise. */ - virtual bool play(int loops); - - /** - * Stops the music. - */ - virtual void stop(); + bool play(int loops = -1, int fadeIn = 0); protected: - Music(Mix_Chunk *music); + Music(Mix_Music *music); - //Mix_Music *music; - Mix_Chunk *mChunk; - int mChannel; + Mix_Music *mMusic; }; #endif diff --git a/src/sound.cpp b/src/sound.cpp index 468f48c0..12ffbb38 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -27,6 +27,7 @@ #include "log.h" #include "sound.h" +#include "resources/music.h" #include "resources/resourcemanager.h" #include "resources/soundeffect.h" @@ -156,53 +157,25 @@ 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 NULL; - } - else - { - logger->log("Loading music \"%s\"", path.c_str()); - } - - 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) return; haltMusic(); - if ((mMusic = loadMusic(filename))) - Mix_PlayMusic(mMusic, -1); // Loop forever + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(); } void Sound::stopMusic() @@ -215,17 +188,18 @@ void Sound::stopMusic() 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) return; haltMusic(); - if ((mMusic = loadMusic(path.c_str()))) - Mix_FadeInMusic(mMusic, -1, ms); // Loop forever + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(-1, ms); } void Sound::fadeOutMusic(int ms) @@ -249,9 +223,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); } @@ -261,15 +235,15 @@ void Sound::logic() { if (mMusic) { - Mix_FreeMusic(mMusic); - mMusic = NULL; + mMusic->decRef(); + mMusic = 0; } sFadingOutEnded = false; - if (!mNextMusicPath.empty()) + if (!mNextMusicFile.empty()) { - playMusic(mNextMusicPath); - mNextMusicPath.clear(); + playMusic(mNextMusicFile); + mNextMusicFile.clear(); } } } @@ -325,6 +299,6 @@ void Sound::haltMusic() return; Mix_HaltMusic(); - Mix_FreeMusic(mMusic); - mMusic = NULL; + mMusic->decRef(); + mMusic = 0; } diff --git a/src/sound.h b/src/sound.h index dbec3fc9..05caa1ed 100644 --- a/src/sound.h +++ b/src/sound.h @@ -30,6 +30,8 @@ #include <string> +class Music; + /** Sound engine * * \ingroup CORE @@ -53,9 +55,9 @@ class Sound /** * Starts background music. * - * @param path The full path to the music file. + * @param fileName The name of the music file. */ - void playMusic(const std::string &path); + void playMusic(const std::string &fileName); /** * Stops currently running background music track. @@ -65,10 +67,10 @@ class Sound /** * Fades in background music. * - * @param path The full path to the music file. - * @param ms Duration of fade-in effect (ms) + * @param fileName The name of the music file. + * @param ms Duration of fade-in effect (ms) */ - void fadeInMusic(const std::string &path, int ms = 1000); + void fadeInMusic(const std::string &fileName, int ms = 1000); /** * Fades out currently running background music track. @@ -80,10 +82,10 @@ class Sound /** * 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) + * @param fileName The name of the music file. + * @param ms Duration of fade-out effect (ms) */ - void fadeOutAndPlayMusic(const std::string &path, int ms = 1000); + void fadeOutAndPlayMusic(const std::string &fileName, int ms = 1000); int getMaxVolume() const { return MIX_MAX_VOLUME; } @@ -118,7 +120,7 @@ class Sound * When calling fadeOutAndPlayMusic(), * the music file below will then be played */ - std::string mNextMusicPath; + std::string mNextMusicFile; bool mInstalled; @@ -126,7 +128,7 @@ class Sound int mMusicVolume; std::string mCurrentMusicFile; - Mix_Music *mMusic; + Music *mMusic; }; extern Sound sound; |