参考ogre wiki:
获取执行路径—-GetExecutablePath
示例程序—-Basic+Ogre+Application
基础教程6—Basic+Tutorial+6
MAC
Mac osx的启动路径和windows/linux不一致,wiki推荐用如下函数:
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include <CoreFoundation/CoreFoundation.h>
// This function will locate the path to our application on OS X,
// unlike windows you cannot rely on the current working directory
// for locating your configuration files and resources.
std::string macBundlePath()
{
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);
}
#endif
然后在启动时
int main() {
// content ...
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
resourcePath = Ogre::macBundlePath() + "/Contents/Resources/";
#else
resourcePath = "";
#endif
Ogre::Root* root = new Ogre::Root(resourcePath + "plugins.cfg", resourcePath + "ogre.cfg", "Ogre.log");
// reset content ...
}
如果出现undefined _CFURLGetFileSystemRepresentation
、_CFBundleCopyResourcesDirectoryURL
、undefined _CFRelease
等错误提示,给编译好的库添加-framework CoreFoundation
,
如果是cmake的话,可使用
target_link_libraries(program stdc++ "-framework Foundation" "-framework Cocoa" ...)