1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 | #include "OgreSceneManagerEnumerator.h"
|
---|
27 |
|
---|
28 | #include "OgreDynLibManager.h"
|
---|
29 | #include "OgreDynLib.h"
|
---|
30 | #include "OgreConfigFile.h"
|
---|
31 | #include "OgreMaterial.h"
|
---|
32 | #include "OgreException.h"
|
---|
33 | #include "OgreRoot.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | namespace Ogre {
|
---|
37 |
|
---|
38 | //-----------------------------------------------------------------------
|
---|
39 | template<> SceneManagerEnumerator* Singleton<SceneManagerEnumerator>::ms_Singleton = 0;
|
---|
40 | SceneManagerEnumerator* SceneManagerEnumerator::getSingletonPtr(void)
|
---|
41 | {
|
---|
42 | return ms_Singleton;
|
---|
43 | }
|
---|
44 | SceneManagerEnumerator& SceneManagerEnumerator::getSingleton(void)
|
---|
45 | {
|
---|
46 | assert( ms_Singleton ); return ( *ms_Singleton );
|
---|
47 | }
|
---|
48 |
|
---|
49 | //-----------------------------------------------------------------------
|
---|
50 | SceneManagerEnumerator::SceneManagerEnumerator()
|
---|
51 | : mInstanceCreateCount(0), mCurrentRenderSystem(0)
|
---|
52 | {
|
---|
53 | addFactory(&mDefaultFactory);
|
---|
54 |
|
---|
55 | }
|
---|
56 | //-----------------------------------------------------------------------
|
---|
57 | SceneManagerEnumerator::~SceneManagerEnumerator()
|
---|
58 | {
|
---|
59 | // Destroy all remaining instances
|
---|
60 | // Really should have shutdown and unregistered by now, but catch here in case
|
---|
61 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i)
|
---|
62 | {
|
---|
63 | // destroy instances
|
---|
64 | for(Factories::iterator f = mFactories.begin(); f != mFactories.end(); ++f)
|
---|
65 | {
|
---|
66 | if ((*f)->getMetaData().typeName == i->second->getTypeName())
|
---|
67 | {
|
---|
68 | (*f)->destroyInstance(i->second);
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 | mInstances.clear();
|
---|
75 |
|
---|
76 | }
|
---|
77 | //-----------------------------------------------------------------------
|
---|
78 | void SceneManagerEnumerator::addFactory(SceneManagerFactory* fact)
|
---|
79 | {
|
---|
80 | mFactories.push_back(fact);
|
---|
81 | // add to metadata
|
---|
82 | mMetaDataList.push_back(&fact->getMetaData());
|
---|
83 | }
|
---|
84 | //-----------------------------------------------------------------------
|
---|
85 | void SceneManagerEnumerator::removeFactory(SceneManagerFactory* fact)
|
---|
86 | {
|
---|
87 | // destroy all instances for this factory
|
---|
88 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); )
|
---|
89 | {
|
---|
90 | SceneManager* instance = i->second;
|
---|
91 | if (instance->getTypeName() == fact->getMetaData().typeName)
|
---|
92 | {
|
---|
93 | fact->destroyInstance(instance);
|
---|
94 | Instances::iterator deli = i++;
|
---|
95 | mInstances.erase(deli);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | ++i;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | // remove from metadata
|
---|
103 | for (MetaDataList::iterator m = mMetaDataList.begin(); m != mMetaDataList.end(); ++m)
|
---|
104 | {
|
---|
105 | if(*m == &(fact->getMetaData()))
|
---|
106 | {
|
---|
107 | mMetaDataList.erase(m);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | mFactories.remove(fact);
|
---|
112 | }
|
---|
113 | //-----------------------------------------------------------------------
|
---|
114 | const SceneManagerMetaData* SceneManagerEnumerator::getMetaData(const String& typeName) const
|
---|
115 | {
|
---|
116 | for (MetaDataList::const_iterator i = mMetaDataList.begin();
|
---|
117 | i != mMetaDataList.end(); ++i)
|
---|
118 | {
|
---|
119 | if (typeName == (*i)->typeName)
|
---|
120 | {
|
---|
121 | return *i;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
126 | "No metadata found for scene manager of type '" + typeName + "'",
|
---|
127 | "SceneManagerEnumerator::createSceneManager");
|
---|
128 |
|
---|
129 | }
|
---|
130 | //-----------------------------------------------------------------------
|
---|
131 | SceneManagerEnumerator::MetaDataIterator
|
---|
132 | SceneManagerEnumerator::getMetaDataIterator(void) const
|
---|
133 | {
|
---|
134 | return MetaDataIterator(mMetaDataList.begin(), mMetaDataList.end());
|
---|
135 |
|
---|
136 | }
|
---|
137 | //-----------------------------------------------------------------------
|
---|
138 | SceneManager* SceneManagerEnumerator::createSceneManager(
|
---|
139 | const String& typeName, const String& instanceName)
|
---|
140 | {
|
---|
141 | if (mInstances.find(instanceName) != mInstances.end())
|
---|
142 | {
|
---|
143 | OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,
|
---|
144 | "SceneManager instance called '" + instanceName + "' already exists",
|
---|
145 | "SceneManagerEnumerator::createSceneManager");
|
---|
146 | }
|
---|
147 |
|
---|
148 | SceneManager* inst = 0;
|
---|
149 | for(Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i)
|
---|
150 | {
|
---|
151 | if ((*i)->getMetaData().typeName == typeName)
|
---|
152 | {
|
---|
153 | if (instanceName.empty())
|
---|
154 | {
|
---|
155 | // generate a name
|
---|
156 | StringUtil::StrStreamType s;
|
---|
157 | s << "SceneManagerInstance" << ++mInstanceCreateCount;
|
---|
158 | inst = (*i)->createInstance(s.str());
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | inst = (*i)->createInstance(instanceName);
|
---|
163 | }
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (!inst)
|
---|
169 | {
|
---|
170 | // Error!
|
---|
171 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
172 | "No factory found for scene manager of type '" + typeName + "'",
|
---|
173 | "SceneManagerEnumerator::createSceneManager");
|
---|
174 | }
|
---|
175 |
|
---|
176 | /// assign rs if already configured
|
---|
177 | if (mCurrentRenderSystem)
|
---|
178 | inst->_setDestinationRenderSystem(mCurrentRenderSystem);
|
---|
179 |
|
---|
180 | mInstances[inst->getName()] = inst;
|
---|
181 |
|
---|
182 | return inst;
|
---|
183 |
|
---|
184 |
|
---|
185 | }
|
---|
186 | //-----------------------------------------------------------------------
|
---|
187 | SceneManager* SceneManagerEnumerator::createSceneManager(
|
---|
188 | SceneTypeMask typeMask, const String& instanceName)
|
---|
189 | {
|
---|
190 | if (mInstances.find(instanceName) != mInstances.end())
|
---|
191 | {
|
---|
192 | OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,
|
---|
193 | "SceneManager instance called '" + instanceName + "' already exists",
|
---|
194 | "SceneManagerEnumerator::createSceneManager");
|
---|
195 | }
|
---|
196 |
|
---|
197 | SceneManager* inst = 0;
|
---|
198 | String name = instanceName;
|
---|
199 | if (name.empty())
|
---|
200 | {
|
---|
201 | // generate a name
|
---|
202 | StringUtil::StrStreamType s;
|
---|
203 | s << "SceneManagerInstance" << ++mInstanceCreateCount;
|
---|
204 | name = s.str();
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Iterate backwards to find the matching factory registered last
|
---|
208 | for(Factories::reverse_iterator i = mFactories.rbegin(); i != mFactories.rend(); ++i)
|
---|
209 | {
|
---|
210 | if ((*i)->getMetaData().sceneTypeMask & typeMask)
|
---|
211 | {
|
---|
212 | inst = (*i)->createInstance(name);
|
---|
213 | break;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | // use default factory if none
|
---|
218 | if (!inst)
|
---|
219 | inst = mDefaultFactory.createInstance(name);
|
---|
220 |
|
---|
221 | /// assign rs if already configured
|
---|
222 | if (mCurrentRenderSystem)
|
---|
223 | inst->_setDestinationRenderSystem(mCurrentRenderSystem);
|
---|
224 |
|
---|
225 | mInstances[inst->getName()] = inst;
|
---|
226 |
|
---|
227 | return inst;
|
---|
228 |
|
---|
229 | }
|
---|
230 | //-----------------------------------------------------------------------
|
---|
231 | void SceneManagerEnumerator::destroySceneManager(SceneManager* sm)
|
---|
232 | {
|
---|
233 | // Erase instance from map
|
---|
234 | mInstances.erase(sm->getName());
|
---|
235 |
|
---|
236 | // Find factory to destroy
|
---|
237 | for(Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i)
|
---|
238 | {
|
---|
239 | if ((*i)->getMetaData().typeName == sm->getTypeName())
|
---|
240 | {
|
---|
241 | (*i)->destroyInstance(sm);
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | }
|
---|
247 | //-----------------------------------------------------------------------
|
---|
248 | SceneManager* SceneManagerEnumerator::getSceneManager(const String& instanceName) const
|
---|
249 | {
|
---|
250 | Instances::const_iterator i = mInstances.find(instanceName);
|
---|
251 | if(i != mInstances.end())
|
---|
252 | {
|
---|
253 | return i->second;
|
---|
254 | }
|
---|
255 | else
|
---|
256 | {
|
---|
257 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
258 | "SceneManager instance with name '" + instanceName + "' not found.",
|
---|
259 | "SceneManagerEnumerator::getSceneManager");
|
---|
260 | }
|
---|
261 |
|
---|
262 | }
|
---|
263 | //-----------------------------------------------------------------------
|
---|
264 | SceneManagerEnumerator::SceneManagerIterator
|
---|
265 | SceneManagerEnumerator::getSceneManagerIterator(void)
|
---|
266 | {
|
---|
267 | return SceneManagerIterator(mInstances.begin(), mInstances.end());
|
---|
268 |
|
---|
269 | }
|
---|
270 | //-----------------------------------------------------------------------
|
---|
271 | void SceneManagerEnumerator::setRenderSystem(RenderSystem* rs)
|
---|
272 | {
|
---|
273 | mCurrentRenderSystem = rs;
|
---|
274 |
|
---|
275 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i)
|
---|
276 | {
|
---|
277 | i->second->_setDestinationRenderSystem(rs);
|
---|
278 | }
|
---|
279 |
|
---|
280 | }
|
---|
281 | //-----------------------------------------------------------------------
|
---|
282 | void SceneManagerEnumerator::shutdownAll(void)
|
---|
283 | {
|
---|
284 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i)
|
---|
285 | {
|
---|
286 | // shutdown instances (clear scene)
|
---|
287 | i->second->clearScene();
|
---|
288 | }
|
---|
289 |
|
---|
290 | }
|
---|
291 | //-----------------------------------------------------------------------
|
---|
292 | const String DefaultSceneManagerFactory::FACTORY_TYPE_NAME = "DefaultSceneManager";
|
---|
293 | //-----------------------------------------------------------------------
|
---|
294 | void DefaultSceneManagerFactory::initMetaData(void) const
|
---|
295 | {
|
---|
296 | mMetaData.typeName = FACTORY_TYPE_NAME;
|
---|
297 | mMetaData.description = "The default scene manager";
|
---|
298 | mMetaData.sceneTypeMask = ST_GENERIC;
|
---|
299 | mMetaData.worldGeometrySupported = false;
|
---|
300 | }
|
---|
301 | //-----------------------------------------------------------------------
|
---|
302 | SceneManager* DefaultSceneManagerFactory::createInstance(
|
---|
303 | const String& instanceName)
|
---|
304 | {
|
---|
305 | return new DefaultSceneManager(instanceName);
|
---|
306 | }
|
---|
307 | //-----------------------------------------------------------------------
|
---|
308 | void DefaultSceneManagerFactory::destroyInstance(SceneManager* instance)
|
---|
309 | {
|
---|
310 | delete instance;
|
---|
311 | }
|
---|
312 | //-----------------------------------------------------------------------
|
---|
313 | //-----------------------------------------------------------------------
|
---|
314 | DefaultSceneManager::DefaultSceneManager(const String& name)
|
---|
315 | : SceneManager(name)
|
---|
316 | {
|
---|
317 | }
|
---|
318 | //-----------------------------------------------------------------------
|
---|
319 | DefaultSceneManager::~DefaultSceneManager()
|
---|
320 | {
|
---|
321 | }
|
---|
322 | //-----------------------------------------------------------------------
|
---|
323 | const String& DefaultSceneManager::getTypeName(void) const
|
---|
324 | {
|
---|
325 | return DefaultSceneManagerFactory::FACTORY_TYPE_NAME;
|
---|
326 | }
|
---|
327 | //-----------------------------------------------------------------------
|
---|
328 |
|
---|
329 |
|
---|
330 | }
|
---|