Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/main_reto_vs05/src/ogre_control.cc @ 159

Last change on this file since 159 was 159, checked in by rgrieder, 16 years ago
File size: 5.3 KB
Line 
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/**
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
37#include "ogre_control.h"
38
39
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.
47std::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
70/**
71* Constructor that determines the resource path platform dependant.
72*/
73OgreControl::OgreControl() : root_(0)
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
80        resourcePath_ = macBundlePath() + "/Contents/Resources/";
81#else
82        resourcePath_ = "";
83#endif
84}
85
86
87/**
88* Standard Destructor.
89*/
90OgreControl::~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 defined
99* (not loaded!). And last but not least, the render settings (like resolution
100* or AA level) are prompted to the user.
101*/
102bool OgreControl::initialise(void)
103{
104        String pluginsPath;
105        // only use plugins.cfg if not static
106#ifndef OGRE_STATIC_LIB
107        pluginsPath = resourcePath_ + "plugins.cfg";
108#endif
109
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*/
125void OgreControl::setupResources(void)
126{
127        // Load resource paths from config file
128        ConfigFile cf;
129        cf.load(resourcePath_ + "resources.cfg");
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/**
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*/
164bool 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
169        if(!root_->restoreConfig() && !root_->showConfigDialog())
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'
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*/
185Root* OgreControl::getRoot(void)
186{
187        return root_;
188}
189
190
191/**
192* Returns the render window.
193* @return Render window.
194*/
195RenderWindow* OgreControl::getRenderWindow(void)
196{
197        return window_;
198}
199
200
201/**
202* Returns the resource path.
203* @return Resource path.
204*/
205Ogre::String OgreControl::getResourcePath(void)
206{
207        return resourcePath_;
208}
Note: See TracBrowser for help on using the repository browser.