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-2006 Torus Knot Software Ltd |
---|
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 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreStableHeaders.h" |
---|
30 | #include "OgreSceneManagerEnumerator.h" |
---|
31 | |
---|
32 | #include "OgreDynLibManager.h" |
---|
33 | #include "OgreDynLib.h" |
---|
34 | #include "OgreConfigFile.h" |
---|
35 | #include "OgreMaterial.h" |
---|
36 | #include "OgreException.h" |
---|
37 | #include "OgreRoot.h" |
---|
38 | #include "OgreLogManager.h" |
---|
39 | |
---|
40 | |
---|
41 | namespace Ogre { |
---|
42 | |
---|
43 | //----------------------------------------------------------------------- |
---|
44 | template<> SceneManagerEnumerator* Singleton<SceneManagerEnumerator>::ms_Singleton = 0; |
---|
45 | SceneManagerEnumerator* SceneManagerEnumerator::getSingletonPtr(void) |
---|
46 | { |
---|
47 | return ms_Singleton; |
---|
48 | } |
---|
49 | SceneManagerEnumerator& SceneManagerEnumerator::getSingleton(void) |
---|
50 | { |
---|
51 | assert( ms_Singleton ); return ( *ms_Singleton ); |
---|
52 | } |
---|
53 | |
---|
54 | //----------------------------------------------------------------------- |
---|
55 | SceneManagerEnumerator::SceneManagerEnumerator() |
---|
56 | : mInstanceCreateCount(0), mCurrentRenderSystem(0) |
---|
57 | { |
---|
58 | addFactory(&mDefaultFactory); |
---|
59 | |
---|
60 | } |
---|
61 | //----------------------------------------------------------------------- |
---|
62 | SceneManagerEnumerator::~SceneManagerEnumerator() |
---|
63 | { |
---|
64 | // Destroy all remaining instances |
---|
65 | // Really should have shutdown and unregistered by now, but catch here in case |
---|
66 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i) |
---|
67 | { |
---|
68 | // destroy instances |
---|
69 | for(Factories::iterator f = mFactories.begin(); f != mFactories.end(); ++f) |
---|
70 | { |
---|
71 | if ((*f)->getMetaData().typeName == i->second->getTypeName()) |
---|
72 | { |
---|
73 | (*f)->destroyInstance(i->second); |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | } |
---|
79 | mInstances.clear(); |
---|
80 | |
---|
81 | } |
---|
82 | //----------------------------------------------------------------------- |
---|
83 | void SceneManagerEnumerator::addFactory(SceneManagerFactory* fact) |
---|
84 | { |
---|
85 | mFactories.push_back(fact); |
---|
86 | // add to metadata |
---|
87 | mMetaDataList.push_back(&fact->getMetaData()); |
---|
88 | // Log |
---|
89 | LogManager::getSingleton().logMessage("SceneManagerFactory for type '" + |
---|
90 | fact->getMetaData().typeName + "' registered."); |
---|
91 | } |
---|
92 | //----------------------------------------------------------------------- |
---|
93 | void SceneManagerEnumerator::removeFactory(SceneManagerFactory* fact) |
---|
94 | { |
---|
95 | // destroy all instances for this factory |
---|
96 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ) |
---|
97 | { |
---|
98 | SceneManager* instance = i->second; |
---|
99 | if (instance->getTypeName() == fact->getMetaData().typeName) |
---|
100 | { |
---|
101 | fact->destroyInstance(instance); |
---|
102 | Instances::iterator deli = i++; |
---|
103 | mInstances.erase(deli); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | ++i; |
---|
108 | } |
---|
109 | } |
---|
110 | // remove from metadata |
---|
111 | for (MetaDataList::iterator m = mMetaDataList.begin(); m != mMetaDataList.end(); ++m) |
---|
112 | { |
---|
113 | if(*m == &(fact->getMetaData())) |
---|
114 | { |
---|
115 | mMetaDataList.erase(m); |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | mFactories.remove(fact); |
---|
120 | } |
---|
121 | //----------------------------------------------------------------------- |
---|
122 | const SceneManagerMetaData* SceneManagerEnumerator::getMetaData(const String& typeName) const |
---|
123 | { |
---|
124 | for (MetaDataList::const_iterator i = mMetaDataList.begin(); |
---|
125 | i != mMetaDataList.end(); ++i) |
---|
126 | { |
---|
127 | if (typeName == (*i)->typeName) |
---|
128 | { |
---|
129 | return *i; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
134 | "No metadata found for scene manager of type '" + typeName + "'", |
---|
135 | "SceneManagerEnumerator::createSceneManager"); |
---|
136 | |
---|
137 | } |
---|
138 | //----------------------------------------------------------------------- |
---|
139 | SceneManagerEnumerator::MetaDataIterator |
---|
140 | SceneManagerEnumerator::getMetaDataIterator(void) const |
---|
141 | { |
---|
142 | return MetaDataIterator(mMetaDataList.begin(), mMetaDataList.end()); |
---|
143 | |
---|
144 | } |
---|
145 | //----------------------------------------------------------------------- |
---|
146 | SceneManager* SceneManagerEnumerator::createSceneManager( |
---|
147 | const String& typeName, const String& instanceName) |
---|
148 | { |
---|
149 | if (mInstances.find(instanceName) != mInstances.end()) |
---|
150 | { |
---|
151 | OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, |
---|
152 | "SceneManager instance called '" + instanceName + "' already exists", |
---|
153 | "SceneManagerEnumerator::createSceneManager"); |
---|
154 | } |
---|
155 | |
---|
156 | SceneManager* inst = 0; |
---|
157 | for(Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i) |
---|
158 | { |
---|
159 | if ((*i)->getMetaData().typeName == typeName) |
---|
160 | { |
---|
161 | if (instanceName.empty()) |
---|
162 | { |
---|
163 | // generate a name |
---|
164 | StringUtil::StrStreamType s; |
---|
165 | s << "SceneManagerInstance" << ++mInstanceCreateCount; |
---|
166 | inst = (*i)->createInstance(s.str()); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | inst = (*i)->createInstance(instanceName); |
---|
171 | } |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | if (!inst) |
---|
177 | { |
---|
178 | // Error! |
---|
179 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
180 | "No factory found for scene manager of type '" + typeName + "'", |
---|
181 | "SceneManagerEnumerator::createSceneManager"); |
---|
182 | } |
---|
183 | |
---|
184 | /// assign rs if already configured |
---|
185 | if (mCurrentRenderSystem) |
---|
186 | inst->_setDestinationRenderSystem(mCurrentRenderSystem); |
---|
187 | |
---|
188 | mInstances[inst->getName()] = inst; |
---|
189 | |
---|
190 | return inst; |
---|
191 | |
---|
192 | |
---|
193 | } |
---|
194 | //----------------------------------------------------------------------- |
---|
195 | SceneManager* SceneManagerEnumerator::createSceneManager( |
---|
196 | SceneTypeMask typeMask, const String& instanceName) |
---|
197 | { |
---|
198 | if (mInstances.find(instanceName) != mInstances.end()) |
---|
199 | { |
---|
200 | OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, |
---|
201 | "SceneManager instance called '" + instanceName + "' already exists", |
---|
202 | "SceneManagerEnumerator::createSceneManager"); |
---|
203 | } |
---|
204 | |
---|
205 | SceneManager* inst = 0; |
---|
206 | String name = instanceName; |
---|
207 | if (name.empty()) |
---|
208 | { |
---|
209 | // generate a name |
---|
210 | StringUtil::StrStreamType s; |
---|
211 | s << "SceneManagerInstance" << ++mInstanceCreateCount; |
---|
212 | name = s.str(); |
---|
213 | } |
---|
214 | |
---|
215 | // Iterate backwards to find the matching factory registered last |
---|
216 | for(Factories::reverse_iterator i = mFactories.rbegin(); i != mFactories.rend(); ++i) |
---|
217 | { |
---|
218 | if ((*i)->getMetaData().sceneTypeMask & typeMask) |
---|
219 | { |
---|
220 | inst = (*i)->createInstance(name); |
---|
221 | break; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | // use default factory if none |
---|
226 | if (!inst) |
---|
227 | inst = mDefaultFactory.createInstance(name); |
---|
228 | |
---|
229 | /// assign rs if already configured |
---|
230 | if (mCurrentRenderSystem) |
---|
231 | inst->_setDestinationRenderSystem(mCurrentRenderSystem); |
---|
232 | |
---|
233 | mInstances[inst->getName()] = inst; |
---|
234 | |
---|
235 | return inst; |
---|
236 | |
---|
237 | } |
---|
238 | //----------------------------------------------------------------------- |
---|
239 | void SceneManagerEnumerator::destroySceneManager(SceneManager* sm) |
---|
240 | { |
---|
241 | // Erase instance from map |
---|
242 | mInstances.erase(sm->getName()); |
---|
243 | |
---|
244 | // Find factory to destroy |
---|
245 | for(Factories::iterator i = mFactories.begin(); i != mFactories.end(); ++i) |
---|
246 | { |
---|
247 | if ((*i)->getMetaData().typeName == sm->getTypeName()) |
---|
248 | { |
---|
249 | (*i)->destroyInstance(sm); |
---|
250 | break; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | } |
---|
255 | //----------------------------------------------------------------------- |
---|
256 | SceneManager* SceneManagerEnumerator::getSceneManager(const String& instanceName) const |
---|
257 | { |
---|
258 | Instances::const_iterator i = mInstances.find(instanceName); |
---|
259 | if(i != mInstances.end()) |
---|
260 | { |
---|
261 | return i->second; |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
266 | "SceneManager instance with name '" + instanceName + "' not found.", |
---|
267 | "SceneManagerEnumerator::getSceneManager"); |
---|
268 | } |
---|
269 | |
---|
270 | } |
---|
271 | //----------------------------------------------------------------------- |
---|
272 | SceneManagerEnumerator::SceneManagerIterator |
---|
273 | SceneManagerEnumerator::getSceneManagerIterator(void) |
---|
274 | { |
---|
275 | return SceneManagerIterator(mInstances.begin(), mInstances.end()); |
---|
276 | |
---|
277 | } |
---|
278 | //----------------------------------------------------------------------- |
---|
279 | void SceneManagerEnumerator::setRenderSystem(RenderSystem* rs) |
---|
280 | { |
---|
281 | mCurrentRenderSystem = rs; |
---|
282 | |
---|
283 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i) |
---|
284 | { |
---|
285 | i->second->_setDestinationRenderSystem(rs); |
---|
286 | } |
---|
287 | |
---|
288 | } |
---|
289 | //----------------------------------------------------------------------- |
---|
290 | void SceneManagerEnumerator::shutdownAll(void) |
---|
291 | { |
---|
292 | for (Instances::iterator i = mInstances.begin(); i != mInstances.end(); ++i) |
---|
293 | { |
---|
294 | // shutdown instances (clear scene) |
---|
295 | i->second->clearScene(); |
---|
296 | } |
---|
297 | |
---|
298 | } |
---|
299 | //----------------------------------------------------------------------- |
---|
300 | const String DefaultSceneManagerFactory::FACTORY_TYPE_NAME = "DefaultSceneManager"; |
---|
301 | //----------------------------------------------------------------------- |
---|
302 | void DefaultSceneManagerFactory::initMetaData(void) const |
---|
303 | { |
---|
304 | mMetaData.typeName = FACTORY_TYPE_NAME; |
---|
305 | mMetaData.description = "The default scene manager"; |
---|
306 | mMetaData.sceneTypeMask = ST_GENERIC; |
---|
307 | mMetaData.worldGeometrySupported = false; |
---|
308 | } |
---|
309 | //----------------------------------------------------------------------- |
---|
310 | SceneManager* DefaultSceneManagerFactory::createInstance( |
---|
311 | const String& instanceName) |
---|
312 | { |
---|
313 | return new DefaultSceneManager(instanceName); |
---|
314 | } |
---|
315 | //----------------------------------------------------------------------- |
---|
316 | void DefaultSceneManagerFactory::destroyInstance(SceneManager* instance) |
---|
317 | { |
---|
318 | delete instance; |
---|
319 | } |
---|
320 | //----------------------------------------------------------------------- |
---|
321 | //----------------------------------------------------------------------- |
---|
322 | DefaultSceneManager::DefaultSceneManager(const String& name) |
---|
323 | : SceneManager(name) |
---|
324 | { |
---|
325 | } |
---|
326 | //----------------------------------------------------------------------- |
---|
327 | DefaultSceneManager::~DefaultSceneManager() |
---|
328 | { |
---|
329 | } |
---|
330 | //----------------------------------------------------------------------- |
---|
331 | const String& DefaultSceneManager::getTypeName(void) const |
---|
332 | { |
---|
333 | return DefaultSceneManagerFactory::FACTORY_TYPE_NAME; |
---|
334 | } |
---|
335 | //----------------------------------------------------------------------- |
---|
336 | |
---|
337 | |
---|
338 | } |
---|