| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | * | 
|---|
| 4 | * | 
|---|
| 5 | *   License notice: | 
|---|
| 6 | * | 
|---|
| 7 | *   This program is free software: you can redistribute it and/or modify | 
|---|
| 8 | *   it under the terms of the GNU General Public License as published by | 
|---|
| 9 | *   the Free Software Foundation, either version 3 of the License, or | 
|---|
| 10 | *   (at your option) any later version. | 
|---|
| 11 | * | 
|---|
| 12 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 13 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 15 | *   GNU General Public License for more details. | 
|---|
| 16 | * | 
|---|
| 17 | *   You should have received a copy of the GNU General Public License | 
|---|
| 18 | *   along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 19 | * | 
|---|
| 20 | * | 
|---|
| 21 | *   Author: | 
|---|
| 22 | *      Reto Grieder | 
|---|
| 23 | *   Co-authors: | 
|---|
| 24 | *      ... | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | #include "OgrePlatform.h" | 
|---|
| 29 | #include "OgreRoot.h" | 
|---|
| 30 | #include "OgreRenderWindow.h" | 
|---|
| 31 | #include "OgreString.h" | 
|---|
| 32 | #include "OgreConfigFile.h" | 
|---|
| 33 |  | 
|---|
| 34 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 35 | #include <CoreFoundation/CoreFoundation.h> | 
|---|
| 36 | #endif | 
|---|
| 37 |  | 
|---|
| 38 | #include "ogre_control.h" | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox { | 
|---|
| 42 | using namespace Ogre; | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | * Ogre control class. | 
|---|
| 46 | * This is merely a convenient way to handle Ogre. It only holds the Root | 
|---|
| 47 | * object and the render Window. These are the objects, that are independant | 
|---|
| 48 | * of the game state (playing, menu browsing, loading, etc.). | 
|---|
| 49 | * This class could easily be merged into the Orxnox class. | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | /** | 
|---|
| 54 | * Provide support for mac users. | 
|---|
| 55 | */ | 
|---|
| 56 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 57 | // This function will locate the path to our application on OS X, | 
|---|
| 58 | // unlike windows you can not rely on the curent working directory | 
|---|
| 59 | // for locating your configuration files and resources. | 
|---|
| 60 | std::string macBundlePath() | 
|---|
| 61 | { | 
|---|
| 62 | char path[1024]; | 
|---|
| 63 | CFBundleRef mainBundle = CFBundleGetMainBundle(); | 
|---|
| 64 | assert(mainBundle); | 
|---|
| 65 |  | 
|---|
| 66 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); | 
|---|
| 67 | assert(mainBundleURL); | 
|---|
| 68 |  | 
|---|
| 69 | CFStringRef cfStringRef = | 
|---|
| 70 | CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); | 
|---|
| 71 | assert(cfStringRef); | 
|---|
| 72 |  | 
|---|
| 73 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); | 
|---|
| 74 |  | 
|---|
| 75 | CFRelease(mainBundleURL); | 
|---|
| 76 | CFRelease(cfStringRef); | 
|---|
| 77 |  | 
|---|
| 78 | return std::string(path); | 
|---|
| 79 | } | 
|---|
| 80 | #endif | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | * Constructor that determines the resource path platform dependant. | 
|---|
| 85 | */ | 
|---|
| 86 | OgreControl::OgreControl() : root_(0) | 
|---|
| 87 | { | 
|---|
| 88 | // Provide a nice cross platform solution for locating the configuration | 
|---|
| 89 | // files. On windows files are searched for in the current working | 
|---|
| 90 | // directory, on OS X however you must provide the full path, the helper | 
|---|
| 91 | // function macBundlePath does this for us. | 
|---|
| 92 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 93 | resourcePath_ = macBundlePath() + "/Contents/Resources/"; | 
|---|
| 94 | #else | 
|---|
| 95 | resourcePath_ = ""; | 
|---|
| 96 | #endif | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * Standard Destructor. | 
|---|
| 102 | */ | 
|---|
| 103 | OgreControl::~OgreControl() | 
|---|
| 104 | { | 
|---|
| 105 | if (root_) | 
|---|
| 106 | delete root_; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | /* Sets up Ogre. | 
|---|
| 111 | * First, the Root object is being created, then the resources are defined | 
|---|
| 112 | * (not loaded!). And last but not least, the render settings (like resolution | 
|---|
| 113 | * or AA level) are prompted to the user. | 
|---|
| 114 | */ | 
|---|
| 115 | bool OgreControl::initialise(void) | 
|---|
| 116 | { | 
|---|
| 117 | String pluginsPath; | 
|---|
| 118 | // only use plugins.cfg if not static | 
|---|
| 119 | #ifndef OGRE_STATIC_LIB | 
|---|
| 120 | pluginsPath = resourcePath_ + "plugins.cfg"; | 
|---|
| 121 | #endif | 
|---|
| 122 |  | 
|---|
| 123 | root_ = new Root(pluginsPath, | 
|---|
| 124 | resourcePath_ + "ogre.cfg", resourcePath_ + "Ogre.log"); | 
|---|
| 125 |  | 
|---|
| 126 | setupResources(); | 
|---|
| 127 |  | 
|---|
| 128 | if (!configure()) | 
|---|
| 129 | return false; | 
|---|
| 130 |  | 
|---|
| 131 | return true; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 | /** | 
|---|
| 136 | * Defines the source of the resources. | 
|---|
| 137 | */ | 
|---|
| 138 | void OgreControl::setupResources(void) | 
|---|
| 139 | { | 
|---|
| 140 | // Load resource paths from config file | 
|---|
| 141 | ConfigFile cf; | 
|---|
| 142 | cf.load(resourcePath_ + "resources.cfg"); | 
|---|
| 143 |  | 
|---|
| 144 | // Go through all sections & settings in the file | 
|---|
| 145 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); | 
|---|
| 146 |  | 
|---|
| 147 | String secName, typeName, archName; | 
|---|
| 148 | while (seci.hasMoreElements()) | 
|---|
| 149 | { | 
|---|
| 150 | secName = seci.peekNextKey(); | 
|---|
| 151 | ConfigFile::SettingsMultiMap *settings = seci.getNext(); | 
|---|
| 152 | ConfigFile::SettingsMultiMap::iterator i; | 
|---|
| 153 | for (i = settings->begin(); i != settings->end(); ++i) | 
|---|
| 154 | { | 
|---|
| 155 | typeName = i->first; | 
|---|
| 156 | archName = i->second; | 
|---|
| 157 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 158 | // OS X does not set the working directory relative to the app, | 
|---|
| 159 | // In order to make things portable on OS X we need to provide | 
|---|
| 160 | // the loading with it's own bundle path location | 
|---|
| 161 | ResourceGroupManager::getSingleton().addResourceLocation( | 
|---|
| 162 | String(macBundlePath() + "/" + archName), typeName, secName); | 
|---|
| 163 | #else | 
|---|
| 164 | ResourceGroupManager::getSingleton().addResourceLocation( | 
|---|
| 165 | archName, typeName, secName); | 
|---|
| 166 | #endif | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | /** | 
|---|
| 173 | * Prompts a setting window for the render engine if that has not already | 
|---|
| 174 | * been done. | 
|---|
| 175 | * The method also calls the root initialiser in order to get a render window. | 
|---|
| 176 | */ | 
|---|
| 177 | bool OgreControl::configure(void) | 
|---|
| 178 | { | 
|---|
| 179 | // Show the configuration dialog and initialise the system | 
|---|
| 180 | // You can skip this and use root.restoreConfig() to load configuration | 
|---|
| 181 | // settings if you were sure there are valid ones saved in ogre.cfg | 
|---|
| 182 | if(!root_->restoreConfig() && !root_->showConfigDialog()) | 
|---|
| 183 | return false; | 
|---|
| 184 |  | 
|---|
| 185 | // user clicked OK so initialise | 
|---|
| 186 | // Here we choose to let the system create a default | 
|---|
| 187 | // rendering window by passing 'true' | 
|---|
| 188 | root_->saveConfig(); | 
|---|
| 189 | window_ = root_->initialise(true); | 
|---|
| 190 | return true; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 |  | 
|---|
| 194 | /** | 
|---|
| 195 | * Returns the root object. | 
|---|
| 196 | * @return Root object. | 
|---|
| 197 | */ | 
|---|
| 198 | Root* OgreControl::getRoot(void) | 
|---|
| 199 | { | 
|---|
| 200 | return root_; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | /** | 
|---|
| 205 | * Returns the render window. | 
|---|
| 206 | * @return Render window. | 
|---|
| 207 | */ | 
|---|
| 208 | RenderWindow* OgreControl::getRenderWindow(void) | 
|---|
| 209 | { | 
|---|
| 210 | return window_; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 | /** | 
|---|
| 215 | * Returns the resource path. | 
|---|
| 216 | * @return Resource path. | 
|---|
| 217 | */ | 
|---|
| 218 | Ogre::String OgreControl::getResourcePath(void) | 
|---|
| 219 | { | 
|---|
| 220 | return resourcePath_; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | } | 
|---|