[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 | |
---|
[159] | 28 | /** |
---|
| 29 | * Ogre control class. |
---|
| 30 | * This is merely a convenient way to handle Ogre. It only holds the Root |
---|
| 31 | * object and the render Window. These are the objects, that are independant |
---|
| 32 | * of the game state (playing, menu browsing, loading, etc.). |
---|
| 33 | * This class could easily be merged into the Orxnox class. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | |
---|
[157] | 37 | #include "ogre_control.h" |
---|
| 38 | |
---|
| 39 | |
---|
[159] | 40 | /** |
---|
| 41 | * Provide support for mac users. |
---|
| 42 | */ |
---|
[157] | 43 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 44 | // This function will locate the path to our application on OS X, |
---|
| 45 | // unlike windows you can not rely on the curent working directory |
---|
| 46 | // for locating your configuration files and resources. |
---|
| 47 | std::string macBundlePath() |
---|
| 48 | { |
---|
| 49 | char path[1024]; |
---|
| 50 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
| 51 | assert(mainBundle); |
---|
| 52 | |
---|
| 53 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
| 54 | assert(mainBundleURL); |
---|
| 55 | |
---|
| 56 | CFStringRef cfStringRef = |
---|
| 57 | CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
| 58 | assert(cfStringRef); |
---|
| 59 | |
---|
| 60 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
| 61 | |
---|
| 62 | CFRelease(mainBundleURL); |
---|
| 63 | CFRelease(cfStringRef); |
---|
| 64 | |
---|
| 65 | return std::string(path); |
---|
| 66 | } |
---|
| 67 | #endif |
---|
| 68 | |
---|
| 69 | |
---|
[159] | 70 | /** |
---|
| 71 | * Constructor that determines the resource path platform dependant. |
---|
| 72 | */ |
---|
| 73 | OgreControl::OgreControl() : root_(0) |
---|
[157] | 74 | { |
---|
| 75 | // Provide a nice cross platform solution for locating the configuration |
---|
| 76 | // files. On windows files are searched for in the current working |
---|
| 77 | // directory, on OS X however you must provide the full path, the helper |
---|
| 78 | // function macBundlePath does this for us. |
---|
| 79 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
[159] | 80 | resourcePath_ = macBundlePath() + "/Contents/Resources/"; |
---|
[157] | 81 | #else |
---|
[159] | 82 | resourcePath_ = ""; |
---|
[157] | 83 | #endif |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[159] | 87 | /** |
---|
| 88 | * Standard Destructor. |
---|
| 89 | */ |
---|
[157] | 90 | OgreControl::~OgreControl() |
---|
| 91 | { |
---|
[159] | 92 | if (root_) |
---|
| 93 | delete root_; |
---|
[157] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
[159] | 97 | /* Sets up Ogre. |
---|
| 98 | * First, the Root object is being created, then the resources are defined |
---|
| 99 | * (not loaded!). And last but not least, the render settings (like resolution |
---|
| 100 | * or AA level) are prompted to the user. |
---|
| 101 | */ |
---|
[157] | 102 | bool OgreControl::initialise(void) |
---|
| 103 | { |
---|
| 104 | String pluginsPath; |
---|
| 105 | // only use plugins.cfg if not static |
---|
| 106 | #ifndef OGRE_STATIC_LIB |
---|
[159] | 107 | pluginsPath = resourcePath_ + "plugins.cfg"; |
---|
[157] | 108 | #endif |
---|
| 109 | |
---|
[159] | 110 | root_ = new Root(pluginsPath, |
---|
| 111 | resourcePath_ + "ogre.cfg", resourcePath_ + "Ogre.log"); |
---|
[157] | 112 | |
---|
| 113 | setupResources(); |
---|
| 114 | |
---|
| 115 | if (!configure()) |
---|
| 116 | return false; |
---|
| 117 | |
---|
| 118 | return true; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[159] | 122 | /** |
---|
| 123 | * Defines the source of the resources. |
---|
| 124 | */ |
---|
[157] | 125 | void OgreControl::setupResources(void) |
---|
| 126 | { |
---|
| 127 | // Load resource paths from config file |
---|
| 128 | ConfigFile cf; |
---|
[159] | 129 | cf.load(resourcePath_ + "resources.cfg"); |
---|
[157] | 130 | |
---|
| 131 | // Go through all sections & settings in the file |
---|
| 132 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 133 | |
---|
| 134 | String secName, typeName, archName; |
---|
| 135 | while (seci.hasMoreElements()) |
---|
| 136 | { |
---|
| 137 | secName = seci.peekNextKey(); |
---|
| 138 | ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 139 | ConfigFile::SettingsMultiMap::iterator i; |
---|
| 140 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 141 | { |
---|
| 142 | typeName = i->first; |
---|
| 143 | archName = i->second; |
---|
| 144 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 145 | // OS X does not set the working directory relative to the app, |
---|
| 146 | // In order to make things portable on OS X we need to provide |
---|
| 147 | // the loading with it's own bundle path location |
---|
| 148 | ResourceGroupManager::getSingleton().addResourceLocation( |
---|
| 149 | String(macBundlePath() + "/" + archName), typeName, secName); |
---|
| 150 | #else |
---|
| 151 | ResourceGroupManager::getSingleton().addResourceLocation( |
---|
| 152 | archName, typeName, secName); |
---|
| 153 | #endif |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
[159] | 159 | /** |
---|
| 160 | * Prompts a setting window for the render engine if that has not already |
---|
| 161 | * been done. |
---|
| 162 | * The method also calls the root initialiser in order to get a render window. |
---|
| 163 | */ |
---|
[157] | 164 | bool OgreControl::configure(void) |
---|
| 165 | { |
---|
| 166 | // Show the configuration dialog and initialise the system |
---|
| 167 | // You can skip this and use root.restoreConfig() to load configuration |
---|
| 168 | // settings if you were sure there are valid ones saved in ogre.cfg |
---|
[159] | 169 | if(!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
[157] | 170 | return false; |
---|
| 171 | |
---|
| 172 | // user clicked OK so initialise |
---|
| 173 | // Here we choose to let the system create a default |
---|
| 174 | // rendering window by passing 'true' |
---|
[159] | 175 | root_->saveConfig(); |
---|
| 176 | window_ = root_->initialise(true); |
---|
[157] | 177 | return true; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
[159] | 181 | /** |
---|
| 182 | * Returns the root object. |
---|
| 183 | * @return Root object. |
---|
| 184 | */ |
---|
[157] | 185 | Root* OgreControl::getRoot(void) |
---|
| 186 | { |
---|
[159] | 187 | return root_; |
---|
[157] | 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
[159] | 191 | /** |
---|
| 192 | * Returns the render window. |
---|
| 193 | * @return Render window. |
---|
| 194 | */ |
---|
[157] | 195 | RenderWindow* OgreControl::getRenderWindow(void) |
---|
| 196 | { |
---|
[159] | 197 | return window_; |
---|
[157] | 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
[159] | 201 | /** |
---|
| 202 | * Returns the resource path. |
---|
| 203 | * @return Resource path. |
---|
| 204 | */ |
---|
[157] | 205 | Ogre::String OgreControl::getResourcePath(void) |
---|
| 206 | { |
---|
[159] | 207 | return resourcePath_; |
---|
[157] | 208 | } |
---|