| [123] | 1 | #include "OgreControl.h" |
|---|
| 2 | |
|---|
| 3 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 4 | // This function will locate the path to our application on OS X, |
|---|
| 5 | // unlike windows you can not rely on the curent working directory |
|---|
| 6 | // for locating your configuration files and resources. |
|---|
| 7 | std::string macBundlePath() |
|---|
| 8 | { |
|---|
| 9 | char path[1024]; |
|---|
| 10 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
|---|
| 11 | assert(mainBundle); |
|---|
| 12 | |
|---|
| 13 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
|---|
| 14 | assert(mainBundleURL); |
|---|
| 15 | |
|---|
| 16 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
|---|
| 17 | assert(cfStringRef); |
|---|
| 18 | |
|---|
| 19 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
|---|
| 20 | |
|---|
| 21 | CFRelease(mainBundleURL); |
|---|
| 22 | CFRelease(cfStringRef); |
|---|
| 23 | |
|---|
| 24 | return std::string(path); |
|---|
| 25 | } |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | OgreControl::OgreControl() |
|---|
| 30 | { |
|---|
| 31 | mRoot = 0; |
|---|
| 32 | // Provide a nice cross platform solution for locating the configuration files |
|---|
| 33 | // On windows files are searched for in the current working directory, on OS X however |
|---|
| 34 | // you must provide the full path, the helper function macBundlePath does this for us. |
|---|
| 35 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 36 | mResourcePath = macBundlePath() + "/Contents/Resources/"; |
|---|
| 37 | #else |
|---|
| 38 | mResourcePath = ""; |
|---|
| 39 | #endif |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | // standard destructor |
|---|
| 44 | OgreControl::~OgreControl() |
|---|
| 45 | { |
|---|
| 46 | if (mRoot) |
|---|
| 47 | delete mRoot; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /**------------- SETTING UP OGRE --------------**/ |
|---|
| 52 | |
|---|
| 53 | bool OgreControl::initialise(void) |
|---|
| 54 | { |
|---|
| 55 | String pluginsPath; |
|---|
| 56 | // only use plugins.cfg if not static |
|---|
| 57 | #ifndef OGRE_STATIC_LIB |
|---|
| 58 | pluginsPath = mResourcePath + "plugins.cfg"; |
|---|
| 59 | #endif |
|---|
| 60 | |
|---|
| 61 | mRoot = new Root(pluginsPath, |
|---|
| 62 | mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log"); |
|---|
| 63 | |
|---|
| 64 | setupResources(); |
|---|
| 65 | |
|---|
| 66 | if (!configure()) |
|---|
| 67 | return false; |
|---|
| 68 | |
|---|
| 69 | return true; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /// Method which will define the source of resources (other than current folder) |
|---|
| 74 | void OgreControl::setupResources(void) |
|---|
| 75 | { |
|---|
| 76 | // Load resource paths from config file |
|---|
| 77 | ConfigFile cf; |
|---|
| 78 | cf.load(mResourcePath + "resources.cfg"); |
|---|
| 79 | |
|---|
| 80 | // Go through all sections & settings in the file |
|---|
| 81 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
|---|
| 82 | |
|---|
| 83 | String secName, typeName, archName; |
|---|
| 84 | while (seci.hasMoreElements()) |
|---|
| 85 | { |
|---|
| 86 | secName = seci.peekNextKey(); |
|---|
| 87 | ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
|---|
| 88 | ConfigFile::SettingsMultiMap::iterator i; |
|---|
| 89 | for (i = settings->begin(); i != settings->end(); ++i) |
|---|
| 90 | { |
|---|
| 91 | typeName = i->first; |
|---|
| 92 | archName = i->second; |
|---|
| 93 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 94 | // OS X does not set the working directory relative to the app, |
|---|
| 95 | // In order to make things portable on OS X we need to provide |
|---|
| 96 | // the loading with it's own bundle path location |
|---|
| 97 | ResourceGroupManager::getSingleton().addResourceLocation( |
|---|
| 98 | String(macBundlePath() + "/" + archName), typeName, secName); |
|---|
| 99 | #else |
|---|
| 100 | ResourceGroupManager::getSingleton().addResourceLocation( |
|---|
| 101 | archName, typeName, secName); |
|---|
| 102 | #endif |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | bool OgreControl::configure(void) |
|---|
| 109 | { |
|---|
| 110 | // Show the configuration dialog and initialise the system |
|---|
| 111 | // You can skip this and use root.restoreConfig() to load configuration |
|---|
| 112 | // settings if you were sure there are valid ones saved in ogre.cfg |
|---|
| 113 | if(!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
|---|
| 114 | return false; |
|---|
| 115 | |
|---|
| 116 | // user clicked OK so initialise |
|---|
| 117 | // Here we choose to let the system create a default rendering window by passing 'true' |
|---|
| 118 | mWindow = mRoot->initialise(true); |
|---|
| 119 | mRoot->saveConfig(); |
|---|
| 120 | return true; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | Root* OgreControl::getRoot(void) |
|---|
| 125 | { |
|---|
| 126 | return mRoot; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | RenderWindow* OgreControl::getRenderWindow(void) |
|---|
| 131 | { |
|---|
| 132 | return mWindow; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | Ogre::String OgreControl::getResourcePath(void) |
|---|
| 137 | { |
|---|
| 138 | return mResourcePath; |
|---|
| 139 | } |
|---|