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

Improve how the base dir is retrieved

This commit is contained in:
Mathieu Schroeter 2017-02-11 18:10:32 +01:00
parent 4134b1bfae
commit 6af7270020
4 changed files with 25 additions and 57 deletions

View File

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#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

View File

@ -3,6 +3,7 @@
#pragma once
#include <string>
#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);

View File

@ -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);

View File

@ -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 ());