summaryrefslogtreecommitdiff
path: root/src/sound.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sound.cpp')
-rw-r--r--src/sound.cpp93
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()