summaryrefslogtreecommitdiff
path: root/src/sound.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sound.cpp')
-rw-r--r--src/sound.cpp103
1 files changed, 38 insertions, 65 deletions
diff --git a/src/sound.cpp b/src/sound.cpp
index a573458ef..fb6958e25 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -2,7 +2,7 @@
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
- * Copyright (C) 2011 The ManaPlus Developers
+ * Copyright (C) 2011-2012 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
@@ -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();
}
}
}
@@ -349,9 +315,13 @@ void Sound::playGuiSfx(const std::string &path)
if (!mInstalled || path.empty() || !mPlayGui)
return;
+ std::string tmpPath;
+ if (!path.compare(0, 4, "sfx/"))
+ tmpPath = path;
+ else
+ tmpPath = paths.getValue("sfx", "sfx/") + path;
ResourceManager *resman = ResourceManager::getInstance();
- SoundEffect *sample = resman->getSoundEffect(
- paths.getStringValue("sfx") + path);
+ SoundEffect *sample = resman->getSoundEffect(tmpPath);
if (sample)
{
logger->log("Sound::playGuiSfx() Playing: %s", path.c_str());
@@ -379,8 +349,11 @@ void Sound::haltMusic()
return;
Mix_HaltMusic();
- Mix_FreeMusic(mMusic);
- mMusic = nullptr;
+ if (mMusic)
+ {
+ mMusic->decRef();
+ mMusic = nullptr;
+ }
}
void Sound::changeAudio()