#include "OgreControl.h" #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE // This function will locate the path to our application on OS X, // unlike windows you can not rely on the curent 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 OgreControl::OgreControl() { mRoot = 0; // Provide a nice cross platform solution for locating the configuration files // On windows files are searched for in the current working directory, on OS X however // you must provide the full path, the helper function macBundlePath does this for us. #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE mResourcePath = macBundlePath() + "/Contents/Resources/"; #else mResourcePath = ""; #endif } // standard destructor OgreControl::~OgreControl() { if (mRoot) delete mRoot; } /**------------- SETTING UP OGRE --------------**/ bool OgreControl::initialise(void) { String pluginsPath; // only use plugins.cfg if not static #ifndef OGRE_STATIC_LIB pluginsPath = mResourcePath + "plugins.cfg"; #endif mRoot = new Root(pluginsPath, mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log"); setupResources(); if (!configure()) return false; return true; } /// Method which will define the source of resources (other than current folder) void OgreControl::setupResources(void) { // Load resource paths from config file ConfigFile cf; cf.load(mResourcePath + "resources.cfg"); // Go through all sections & settings in the file ConfigFile::SectionIterator seci = cf.getSectionIterator(); String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); ConfigFile::SettingsMultiMap *settings = seci.getNext(); ConfigFile::SettingsMultiMap::iterator i; for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; archName = i->second; #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE // OS X does not set the working directory relative to the app, // In order to make things portable on OS X we need to provide // the loading with it's own bundle path location ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); #else ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); #endif } } } bool OgreControl::configure(void) { // Show the configuration dialog and initialise the system // You can skip this and use root.restoreConfig() to load configuration // settings if you were sure there are valid ones saved in ogre.cfg if(!mRoot->restoreConfig() && !mRoot->showConfigDialog()) return false; // user clicked OK so initialise // Here we choose to let the system create a default rendering window by passing 'true' mWindow = mRoot->initialise(true); mRoot->saveConfig(); return true; } Root* OgreControl::getRoot(void) { return mRoot; } RenderWindow* OgreControl::getRenderWindow(void) { return mWindow; } Ogre::String OgreControl::getResourcePath(void) { return mResourcePath; }