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 "ogre_control.h" |
---|
29 | |
---|
30 | |
---|
31 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
32 | // This function will locate the path to our application on OS X, |
---|
33 | // unlike windows you can not rely on the curent working directory |
---|
34 | // for locating your configuration files and resources. |
---|
35 | std::string macBundlePath() |
---|
36 | { |
---|
37 | char path[1024]; |
---|
38 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
39 | assert(mainBundle); |
---|
40 | |
---|
41 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
42 | assert(mainBundleURL); |
---|
43 | |
---|
44 | CFStringRef cfStringRef = |
---|
45 | CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
46 | assert(cfStringRef); |
---|
47 | |
---|
48 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
49 | |
---|
50 | CFRelease(mainBundleURL); |
---|
51 | CFRelease(cfStringRef); |
---|
52 | |
---|
53 | return std::string(path); |
---|
54 | } |
---|
55 | #endif |
---|
56 | |
---|
57 | |
---|
58 | OgreControl::OgreControl() |
---|
59 | { |
---|
60 | mRoot = 0; |
---|
61 | // Provide a nice cross platform solution for locating the configuration |
---|
62 | // files. On windows files are searched for in the current working |
---|
63 | // directory, on OS X however you must provide the full path, the helper |
---|
64 | // function macBundlePath does this for us. |
---|
65 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
66 | mResourcePath = macBundlePath() + "/Contents/Resources/"; |
---|
67 | #else |
---|
68 | mResourcePath = ""; |
---|
69 | #endif |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | // standard destructor |
---|
74 | OgreControl::~OgreControl() |
---|
75 | { |
---|
76 | if (mRoot) |
---|
77 | delete mRoot; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /**------------- SETTING UP OGRE --------------**/ |
---|
82 | |
---|
83 | bool OgreControl::initialise(void) |
---|
84 | { |
---|
85 | String pluginsPath; |
---|
86 | // only use plugins.cfg if not static |
---|
87 | #ifndef OGRE_STATIC_LIB |
---|
88 | pluginsPath = mResourcePath + "plugins.cfg"; |
---|
89 | #endif |
---|
90 | |
---|
91 | mRoot = new Root(pluginsPath, |
---|
92 | mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log"); |
---|
93 | |
---|
94 | setupResources(); |
---|
95 | |
---|
96 | if (!configure()) |
---|
97 | return false; |
---|
98 | |
---|
99 | return true; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | // Method which will define the source of resources |
---|
104 | // (other than current folder) |
---|
105 | void OgreControl::setupResources(void) |
---|
106 | { |
---|
107 | // Load resource paths from config file |
---|
108 | ConfigFile cf; |
---|
109 | cf.load(mResourcePath + "resources.cfg"); |
---|
110 | |
---|
111 | // Go through all sections & settings in the file |
---|
112 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
113 | |
---|
114 | String secName, typeName, archName; |
---|
115 | while (seci.hasMoreElements()) |
---|
116 | { |
---|
117 | secName = seci.peekNextKey(); |
---|
118 | ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
119 | ConfigFile::SettingsMultiMap::iterator i; |
---|
120 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
121 | { |
---|
122 | typeName = i->first; |
---|
123 | archName = i->second; |
---|
124 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
125 | // OS X does not set the working directory relative to the app, |
---|
126 | // In order to make things portable on OS X we need to provide |
---|
127 | // the loading with it's own bundle path location |
---|
128 | ResourceGroupManager::getSingleton().addResourceLocation( |
---|
129 | String(macBundlePath() + "/" + archName), typeName, secName); |
---|
130 | #else |
---|
131 | ResourceGroupManager::getSingleton().addResourceLocation( |
---|
132 | archName, typeName, secName); |
---|
133 | #endif |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | bool OgreControl::configure(void) |
---|
140 | { |
---|
141 | // Show the configuration dialog and initialise the system |
---|
142 | // You can skip this and use root.restoreConfig() to load configuration |
---|
143 | // settings if you were sure there are valid ones saved in ogre.cfg |
---|
144 | if(!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
145 | return false; |
---|
146 | |
---|
147 | // user clicked OK so initialise |
---|
148 | // Here we choose to let the system create a default |
---|
149 | // rendering window by passing 'true' |
---|
150 | mWindow = mRoot->initialise(true); |
---|
151 | mRoot->saveConfig(); |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | Root* OgreControl::getRoot(void) |
---|
157 | { |
---|
158 | return mRoot; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | RenderWindow* OgreControl::getRenderWindow(void) |
---|
163 | { |
---|
164 | return mWindow; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | Ogre::String OgreControl::getResourcePath(void) |
---|
169 | { |
---|
170 | return mResourcePath; |
---|
171 | } |
---|