[157] | 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 | |
---|
[161] | 28 | #include "OgrePlatform.h" |
---|
| 29 | #include "OgreRoot.h" |
---|
| 30 | #include "OgreRenderWindow.h" |
---|
| 31 | #include "OgreString.h" |
---|
| 32 | #include "OgreConfigFile.h" |
---|
[159] | 33 | |
---|
[161] | 34 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 35 | #include <CoreFoundation/CoreFoundation.h> |
---|
| 36 | #endif |
---|
[159] | 37 | |
---|
[157] | 38 | #include "ogre_control.h" |
---|
| 39 | |
---|
| 40 | |
---|
[169] | 41 | namespace orxonox { |
---|
[161] | 42 | using namespace Ogre; |
---|
[157] | 43 | |
---|
[161] | 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 | */ |
---|
[157] | 51 | |
---|
| 52 | |
---|
[161] | 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); |
---|
[157] | 65 | |
---|
[161] | 66 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
| 67 | assert(mainBundleURL); |
---|
[157] | 68 | |
---|
[161] | 69 | CFStringRef cfStringRef = |
---|
| 70 | CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
| 71 | assert(cfStringRef); |
---|
[157] | 72 | |
---|
[161] | 73 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
[157] | 74 | |
---|
[161] | 75 | CFRelease(mainBundleURL); |
---|
| 76 | CFRelease(cfStringRef); |
---|
[157] | 77 | |
---|
[161] | 78 | return std::string(path); |
---|
| 79 | } |
---|
| 80 | #endif |
---|
[157] | 81 | |
---|
| 82 | |
---|
[161] | 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 | } |
---|
[157] | 98 | |
---|
| 99 | |
---|
[161] | 100 | /** |
---|
| 101 | * Standard Destructor. |
---|
| 102 | */ |
---|
| 103 | OgreControl::~OgreControl() |
---|
| 104 | { |
---|
| 105 | if (root_) |
---|
| 106 | delete root_; |
---|
| 107 | } |
---|
[157] | 108 | |
---|
| 109 | |
---|
[161] | 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 |
---|
[157] | 122 | |
---|
[161] | 123 | root_ = new Root(pluginsPath, |
---|
| 124 | resourcePath_ + "ogre.cfg", resourcePath_ + "Ogre.log"); |
---|
[157] | 125 | |
---|
[161] | 126 | setupResources(); |
---|
[157] | 127 | |
---|
[161] | 128 | if (!configure()) |
---|
| 129 | return false; |
---|
[157] | 130 | |
---|
[161] | 131 | return true; |
---|
| 132 | } |
---|
[157] | 133 | |
---|
| 134 | |
---|
[161] | 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"); |
---|
[157] | 143 | |
---|
[161] | 144 | // Go through all sections & settings in the file |
---|
| 145 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
[157] | 146 | |
---|
[161] | 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 | } |
---|
[157] | 170 | |
---|
| 171 | |
---|
[161] | 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; |
---|
[157] | 184 | |
---|
[161] | 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 | } |
---|
[157] | 192 | |
---|
| 193 | |
---|
[161] | 194 | /** |
---|
| 195 | * Returns the root object. |
---|
| 196 | * @return Root object. |
---|
| 197 | */ |
---|
| 198 | Root* OgreControl::getRoot(void) |
---|
| 199 | { |
---|
| 200 | return root_; |
---|
| 201 | } |
---|
[157] | 202 | |
---|
[161] | 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 | |
---|
[157] | 223 | } |
---|