1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Look music for user dir, fallback to the game dir

Then it's possible to overload the original musicis by providing yours in
the user directory (in music subdir).
This commit is contained in:
Mathieu Schroeter 2017-12-11 18:42:49 +01:00
parent d2bfd12934
commit 67ea4d0c2e
4 changed files with 49 additions and 18 deletions

View File

@ -3362,18 +3362,29 @@ CEvent::ChangePhase (Uint32 phase)
music = m_pDecor->GetMusic (); music = m_pDecor->GetMusic ();
if (music > 0) if (music > 0)
{ {
const std::string exts[] = {"ogg", "mid"}; static const std::string exts[] = {"ogg", "mid"};
static const Location locs[] = {LOCATION_USER, LOCATION_BASE};
std::string absolute;
filename = string_format ( // Look for music in the user directory, then in the game directory.
"music/music%.3d.%s", music - 1, for (size_t i = 0; i < countof (locs); ++i)
exts[g_restoreMidi ? 1 : 0].c_str ()); {
if (!FileExists (filename))
filename = string_format ( filename = string_format (
"music/music%.3d.%s", music - 1, "music/music%.3d.%s", music - 1,
exts[g_restoreMidi ? 0 : 1].c_str ()); exts[g_restoreMidi ? 1 : 0].c_str ());
if (!FileExists (filename, absolute, locs[i]))
filename = string_format (
"music/music%.3d.%s", music - 1,
exts[g_restoreMidi ? 0 : 1].c_str ());
if (FileExists (filename, absolute, locs[i]))
break;
absolute = "";
}
m_pSound->StopMusic (); m_pSound->StopMusic ();
m_pSound->PlayMusic (filename); m_pSound->PlayMusic (absolute);
} }
} }
} }
@ -4403,7 +4414,8 @@ CEvent::StartMovie (const std::string & pFilename)
if (!m_bMovie) if (!m_bMovie)
return false; return false;
if (!FileExists (pFilename)) std::string absolute;
if (!FileExists (pFilename, absolute))
return false; return false;
HideMouse (true); HideMouse (true);

View File

@ -157,12 +157,28 @@ AddUserPath (std::string & pFilename)
} }
bool bool
FileExists (const std::string & filename) FileExists (
const std::string & filename, std::string & absolute, enum Location location)
{ {
const auto path = GetBaseDir () + filename; absolute = filename;
FILE * file; FILE * file;
file = fopen (path.c_str (), "rb"); switch (location)
{
case LOCATION_BASE:
absolute = GetBaseDir () + filename;
break;
case LOCATION_USER:
AddUserPath (absolute);
break;
default:
case LOCATION_ABSOLUTE:
break;
}
file = fopen (absolute.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
return false; return false;

View File

@ -37,7 +37,11 @@ std::string GetBaseDir ();
std::string GetShareDir (); std::string GetShareDir ();
std::string GetLocale (); std::string GetLocale ();
extern void AddUserPath (std::string & pFilename); extern void AddUserPath (std::string & pFilename);
bool FileExists (const std::string & filename);
enum Location { LOCATION_ABSOLUTE, LOCATION_BASE, LOCATION_USER };
bool FileExists (
const std::string & filename, std::string & absolute,
Location location = LOCATION_BASE);
template <typename... Args> template <typename... Args>
std::string std::string

View File

@ -326,20 +326,19 @@ CSound::PlayImage (Sounds channel, Point pos, Sint32 rank)
bool bool
CSound::PlayMusic (const std::string & lpszMIDIFilename) CSound::PlayMusic (const std::string & lpszMIDIFilename)
{ {
std::string path = GetBaseDir ();
if (m_midiVolume == 0) if (m_midiVolume == 0)
return true; return true;
if (lpszMIDIFilename.empty ())
return false;
Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100); Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100);
m_lastMidiVolume = m_midiVolume; m_lastMidiVolume = m_midiVolume;
path += lpszMIDIFilename;
if (m_pMusic) if (m_pMusic)
Mix_FreeMusic (m_pMusic); Mix_FreeMusic (m_pMusic);
m_pMusic = Mix_LoadMUS (path.c_str ()); m_pMusic = Mix_LoadMUS (lpszMIDIFilename.c_str ());
if (!m_pMusic) if (!m_pMusic)
{ {
printf ("%s\n", Mix_GetError ()); printf ("%s\n", Mix_GetError ());