Introduction
Applications are built differently on Mac and on Win/Linux.
On Win/Linux, you have your binary file on top with your Media folder and even config files :
|MyApp.exe |Media| |materials | |... |ogre.cfg |...
On Mac, it's a bundle system, so that everything is packed into the bundle (*.app). Of course you can choose not to respect that and make your project like on Win/Linux. But the cleaner way (and the usual way for OSX users) is the bundle :
|MyApp(.app)| |Contents| | |MacOS| | | |MyApp (binary file) | | |Resources| | | | *** here goes all your stuff : medias, plugins, ... ***
How Can I get the path to my application ?
This very useful information is provided by a small method (given in the Ogre Mac release) :
char path[1024]; CFBundleRef mainBundle = CFBundleGetMainBundle(); assert( mainBundle ); CFURLRef mainBundleURL = CFBundleCopyBundleURL( mainBundle); assert( mainBundleURL); CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); assert( cfStringRef); CFStringGetCString( cfStringRef, path, 1024, kCFStringEncodingASCII); CFRelease( mainBundleURL); CFRelease( cfStringRef); return std::string( path);
On Windows, you can use the method outlined in GetExecutablePath. For Linux, if you want to release your project using any of the major packaging systems such as rpm or deb it is generally preferred to hardcode the path or prefix at compile time. If you want to provide relocatable releases for Linux the Autopackage project provides a tool called Binreloc which allows for this.
And what about cross platform projects ?
Yes yes, we're getting there...
You can make it usable only on Mac with guard :
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
So, a good way of getting you application path on every system is :
std::string bundlePath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE char path[1024]; CFBundleRef mainBundle = CFBundleGetMainBundle(); assert( mainBundle ); CFURLRef mainBundleURL = CFBundleCopyBundleURL( mainBundle); assert( mainBundleURL); CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); assert( cfStringRef); CFStringGetCString( cfStringRef, path, 1024, kCFStringEncodingASCII); CFRelease( mainBundleURL); CFRelease( cfStringRef); return std::string( path); #else return "" ; #endif }
Then, in your code, just call the bundlePath() to get the bundle/application folder.
And if I want my media folder then ?
The thing couldn't be easier... Just define another function to get your Media folder :
std::string mediaPath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE return bundlePath() + "/Contents/Resources/Media/" ; #else return bundlePath() + "/Media/" ; #endif }
And in the code ? Isn't a bit messy ?
Not at all... Create a file (Paths.h for example) and put into it all your necessary functions for paths. Then in your, include this file, and just call the methods your need. The good point is that in the code, you don't have to worry anymore about the plateform you are on !
Example => I created a logsPath function :
std::string logsPath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE return bundlePath() + "/Contents/Resources/Logs/" ; #else return bundlePath() + "/Logs/" ; #endif }
Then in my custom Log class, I just call :
string filePath = logsPath() + filename ;
Conclusion
It's not really hard to have thing well set up for cross plateform project. Just do it well at start and it will make your code easier to understand (and to make too). Moreover, this concept make your code more flexible with paths.
If you have any remarks, please pm fp12 on the forum ๐
Sample file
#ifndef Paths_h #define Paths_h #include <OGRE/OgrePlatform.h> std::string bundlePath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE char path[1024]; CFBundleRef mainBundle = CFBundleGetMainBundle(); assert( mainBundle ); CFURLRef mainBundleURL = CFBundleCopyBundleURL( mainBundle); assert( mainBundleURL); CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); assert( cfStringRef); CFStringGetCString( cfStringRef, path, 1024, kCFStringEncodingASCII); CFRelease( mainBundleURL); CFRelease( cfStringRef); return std::string( path); #else return "" ; #endif } std::string logsPath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE return bundlePath() + "/Contents/Resources/Logs/" ; #else return bundlePath() + "/Logs/" ; #endif } std::string configPath() { #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE return bundlePath() + "/Contents/Resources/" ; #else return bundlePath() ; #endif } #endif