Sign in to follow this  

Application path problem

Recommended Posts

hi I'm trying to run my game from different folders but I get errors, it seems that the media files cannot be read, I use this syntax: Logo_Model.CreateSkinMesh ( "Artwork\\models\\logo.x" , Caps2 , Renderer.GetDevice () ); In debug mode there is no problem, but in release mode (I have to make my game in release mode finally!), when the path name is long I get error, for example: C:\dfsdf sdfdsfsdf\mygame\mygame.exe or C:\program files\mygame\mygame.exe I insert my media in Artwork\ folder what is the solution? how can work like microsoft in findmediapath? how can I use getmodulefilename?, how can I use these codes? char module_name[MAX_PATH]; GetModuleFileName(0, module_name, MAX_PATH); std::string path(module_name); path.erase(path.find_last_of('\\'), std::string::npos); thanks

Share this post

Link to post
Share on other sites
I may be wrong, but as long as your path looks like this...


Then in your code you can just use
"..\\Artwork\\MediaFiles\\FileName" etc (It could be just one . but I think it's two, I always forget.)

Does this work for you?

Share this post

Link to post
Share on other sites
The code snippet with GetModuleFileName gives you the path your exe resides in.

With this you build full paths every time you want to access an external file that's relative to your exe:

std::string FullPath = path + "\\Artwork\\models\\logo.x";

Logo_Model.CreateSkinMesh( FullPath.c_str(), Caps2, Renderer.GetDevice() );

You might want to create a helper function that gets the module path once and puts it in front of the passed in parameter:

std::string AppPath( const std::string& RelativePath )
static std::string ModulePath;

if ( ModulePath.empty() )
char module_name[MAX_PATH];

GetModuleFileName( 0, module_name, MAX_PATH );

ModulePath = module_name;
ModulePath.erase( ModulePath.find_last_of('\\'), std::string::npos );
return ModulePath + RelativePath;

Logo_Model.CreateSkinMesh( AppPath( "\\Artwork\\models\\logo.x" ).c_str(), Caps2, Renderer.GetDevice() );

Share this post

Link to post
Share on other sites
Try something like this. It will set the current directory. Then you can use relative paths.

char szFileName[ MAX_PATH ];
std::string szFilePath;
GetModuleFileName( NULL, szFileName, MAX_PATH );

std::string::size_type last = szFilePath.find_last_of( "\\/" );
if ( last == std::string::npos )
szFilePath = ".\\";
szFilePath = szFileName.substr( 0, last + 1 );

SetCurrentDirectory( szFilePath.c_str() );

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this