Application path problem

Started by
2 comments, last by dustin10 14 years, 11 months ago
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
Visit galaxyroad.com which soon will have english contents too
Advertisement
I may be wrong, but as long as your path looks like this...

ReleaseFolder\Executable.exe
ReleaseFolder\Artwork\MediaFiles

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

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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 = ".\\";else    szFilePath = szFileName.substr( 0, last + 1 );SetCurrentDirectory( szFilePath.c_str() );

This topic is closed to new replies.

Advertisement