diff --git a/src/misc.cpp b/src/misc.cpp index 13e6b97..a6fe87f 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "misc.h" #include "blupi.h" #include "def.h" @@ -64,33 +65,24 @@ int Random(int min, int max) // Retourne le nom de dossier en cours. -void GetCurrentDir(char *pName, size_t lg) +std::string GetBaseDir () { - char *basePath = SDL_GetBasePath (); - strncpy(pName, basePath, lg-1); - pName[lg-1] = 0; + char *sdlBasePath = nullptr; + static std::string basePath; - lg = strlen(pName); - if (lg == 0) - goto out; - - while ( lg > 0 ) + if (!basePath.size ()) { - lg --; - if ( pName[lg] == '\\' ) - { - pName[lg+1] = 0; - break; - } + sdlBasePath = SDL_GetBasePath (); + sdlBasePath[strlen (sdlBasePath) - 1] = '\0'; } - if ( lg > 6 && strcmp(pName+lg-6, "\\Debug\\") == 0 ) - { - pName[lg-5] = 0; // ignore le dossier \debug ! - } + std::string path = sdlBasePath; + path = path.substr (0, path.find_last_of ("\\/") + 1); -out: - SDL_free (basePath); + if (sdlBasePath) + SDL_free (sdlBasePath); + + return path; } // Ajoute le chemin permettant de lire un fichier diff --git a/src/misc.h b/src/misc.h index 56f81b4..359f114 100644 --- a/src/misc.h +++ b/src/misc.h @@ -3,6 +3,7 @@ #pragma once +#include #include "blupi.h" extern void OutputDebug(char *pMessage); @@ -12,5 +13,5 @@ extern POINT ConvLongToPos(LPARAM lParam); extern void InitRandom(); extern int Random(int min, int max); -extern void GetCurrentDir(char *pName, size_t lg); +std::string GetBaseDir(); extern void AddUserPath(char *pFilename); diff --git a/src/movie.cpp b/src/movie.cpp index 11c8629..547b774 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -79,24 +79,15 @@ void CMovie::fileCloseMovie() bool CMovie::fileOpenMovie(RECT rect, char *pFilename) { - char string[MAX_PATH]; - - if ( pFilename[1] == ':' ) // nom complet "D:\REP..." ? - { - strcpy(string, pFilename); - } - else - { - GetCurrentDir(string, MAX_PATH-30); - strcat(string, pFilename); - } + std::string path = GetBaseDir (); + path += pFilename; // we got a filename, now close any old movie and open the new one. */ - if ( m_fMovieOpen ) fileCloseMovie(); + if ( m_fMovieOpen ) fileCloseMovie(); // Open up the sourcefile. // This can be a local file, network url, ... - m_movie = Kit_CreateSourceFromUrl (string); + m_movie = Kit_CreateSourceFromUrl (path.c_str ()); if (m_movie) { // Create the player @@ -204,20 +195,12 @@ bool CMovie::GetEnable() bool CMovie::IsExist(char *pFilename) { - char string[MAX_PATH]; + std::string path = GetBaseDir (); FILE* file; - if ( pFilename[1] == ':' ) // nom complet "D:\REP..." ? - { - strcpy(string, pFilename); - } - else - { - GetCurrentDir(string, MAX_PATH-30); - strcat(string, pFilename); - } + path += pFilename; - file = fopen(string, "rb"); + file = fopen(path.c_str (), "rb"); if ( file == nullptr ) return false; fclose(file); diff --git a/src/sound.cpp b/src/sound.cpp index 0faa9c1..0aa059d 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -267,7 +267,7 @@ bool CSound::PlayImage(int channel, POINT pos, int rank) bool CSound::PlayMusic(const char *lpszMIDIFilename) { - char string[MAX_PATH]; + std::string path = GetBaseDir (); if ( !m_bEnable ) return true; if ( m_midiVolume == 0 ) return true; @@ -275,17 +275,9 @@ bool CSound::PlayMusic(const char *lpszMIDIFilename) Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100); m_lastMidiVolume = m_midiVolume; - if ( lpszMIDIFilename[1] == ':' ) // nom complet "D:\REP..." ? - { - strcpy(string, lpszMIDIFilename); - } - else - { - GetCurrentDir(string, MAX_PATH-30); - strcat(string, lpszMIDIFilename); - } + path += lpszMIDIFilename; - m_pMusic = Mix_LoadMUS (string); + m_pMusic = Mix_LoadMUS (path.c_str ()); if (!m_pMusic) { printf ("%s\n", Mix_GetError ());