Changeset 161 for code/branches/main_reto_vs05/src/ogre_control.cc
- Timestamp:
- Nov 4, 2007, 5:00:41 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/main_reto_vs05/src/ogre_control.cc
r159 r161 26 26 */ 27 27 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 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 36 37 37 38 #include "ogre_control.h" 38 39 39 40 40 /** 41 * Provide support for mac users. 42 */ 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); 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 66 223 } 67 #endif68 69 70 /**71 * Constructor that determines the resource path platform dependant.72 */73 OgreControl::OgreControl() : root_(0)74 {75 // Provide a nice cross platform solution for locating the configuration76 // files. On windows files are searched for in the current working77 // directory, on OS X however you must provide the full path, the helper78 // function macBundlePath does this for us.79 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE80 resourcePath_ = macBundlePath() + "/Contents/Resources/";81 #else82 resourcePath_ = "";83 #endif84 }85 86 87 /**88 * Standard Destructor.89 */90 OgreControl::~OgreControl()91 {92 if (root_)93 delete root_;94 }95 96 97 /* Sets up Ogre.98 * First, the Root object is being created, then the resources are defined99 * (not loaded!). And last but not least, the render settings (like resolution100 * or AA level) are prompted to the user.101 */102 bool OgreControl::initialise(void)103 {104 String pluginsPath;105 // only use plugins.cfg if not static106 #ifndef OGRE_STATIC_LIB107 pluginsPath = resourcePath_ + "plugins.cfg";108 #endif109 110 root_ = new Root(pluginsPath,111 resourcePath_ + "ogre.cfg", resourcePath_ + "Ogre.log");112 113 setupResources();114 115 if (!configure())116 return false;117 118 return true;119 }120 121 122 /**123 * Defines the source of the resources.124 */125 void OgreControl::setupResources(void)126 {127 // Load resource paths from config file128 ConfigFile cf;129 cf.load(resourcePath_ + "resources.cfg");130 131 // Go through all sections & settings in the file132 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_APPLE145 // 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 provide147 // the loading with it's own bundle path location148 ResourceGroupManager::getSingleton().addResourceLocation(149 String(macBundlePath() + "/" + archName), typeName, secName);150 #else151 ResourceGroupManager::getSingleton().addResourceLocation(152 archName, typeName, secName);153 #endif154 }155 }156 }157 158 159 /**160 * Prompts a setting window for the render engine if that has not already161 * been done.162 * The method also calls the root initialiser in order to get a render window.163 */164 bool OgreControl::configure(void)165 {166 // Show the configuration dialog and initialise the system167 // You can skip this and use root.restoreConfig() to load configuration168 // settings if you were sure there are valid ones saved in ogre.cfg169 if(!root_->restoreConfig() && !root_->showConfigDialog())170 return false;171 172 // user clicked OK so initialise173 // Here we choose to let the system create a default174 // rendering window by passing 'true'175 root_->saveConfig();176 window_ = root_->initialise(true);177 return true;178 }179 180 181 /**182 * Returns the root object.183 * @return Root object.184 */185 Root* OgreControl::getRoot(void)186 {187 return root_;188 }189 190 191 /**192 * Returns the render window.193 * @return Render window.194 */195 RenderWindow* OgreControl::getRenderWindow(void)196 {197 return window_;198 }199 200 201 /**202 * Returns the resource path.203 * @return Resource path.204 */205 Ogre::String OgreControl::getResourcePath(void)206 {207 return resourcePath_;208 }
Note: See TracChangeset
for help on using the changeset viewer.