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 | #ifndef __ParticleSystemManager_H__
|
---|
26 | #define __ParticleSystemManager_H__
|
---|
27 |
|
---|
28 |
|
---|
29 | #include "OgrePrerequisites.h"
|
---|
30 | #include "OgreParticleSystem.h"
|
---|
31 | #include "OgreFrameListener.h"
|
---|
32 | #include "OgreSingleton.h"
|
---|
33 | #include "OgreIteratorWrappers.h"
|
---|
34 | #include "OgreScriptLoader.h"
|
---|
35 | #include "OgreResourceGroupManager.h"
|
---|
36 |
|
---|
37 | namespace Ogre {
|
---|
38 |
|
---|
39 | // Forward decl
|
---|
40 | class ParticleSystemFactory;
|
---|
41 |
|
---|
42 | /** Manages particle systems, particle system scripts (templates) and the
|
---|
43 | available emitter & affector factories.
|
---|
44 | @remarks
|
---|
45 | This singleton class is responsible for creating and managing particle
|
---|
46 | systems. All particle systems must be created and destroyed using this
|
---|
47 | object, although the user interface to creating them is via
|
---|
48 | SceneManager. Remember that like all other MovableObject
|
---|
49 | subclasses, ParticleSystems do not get rendered until they are
|
---|
50 | attached to a SceneNode object.
|
---|
51 | @par
|
---|
52 | This class also manages factories for ParticleEmitter and
|
---|
53 | ParticleAffector classes. To enable easy extensions to the types of
|
---|
54 | emitters (particle sources) and affectors (particle modifiers), the
|
---|
55 | ParticleSystemManager lets plugins or applications register factory
|
---|
56 | classes which submit new subclasses to ParticleEmitter and
|
---|
57 | ParticleAffector. Ogre comes with a number of them already provided,
|
---|
58 | such as cone, sphere and box-shaped emitters, and simple affectors such
|
---|
59 | as constant directional force and colour faders. However using this
|
---|
60 | registration process, a plugin can create any behaviour required.
|
---|
61 | @par
|
---|
62 | This class also manages the loading and parsing of particle system
|
---|
63 | scripts, which are text files describing named particle system
|
---|
64 | templates. Instances of particle systems using these templates can
|
---|
65 | then be created easily through the createParticleSystem method.
|
---|
66 | */
|
---|
67 | class _OgreExport ParticleSystemManager:
|
---|
68 | public Singleton<ParticleSystemManager>, public ScriptLoader
|
---|
69 | {
|
---|
70 | friend class ParticleSystemFactory;
|
---|
71 | public:
|
---|
72 | typedef std::map<String, ParticleSystem*> ParticleTemplateMap;
|
---|
73 | typedef std::map<String, ParticleAffectorFactory*> ParticleAffectorFactoryMap;
|
---|
74 | typedef std::map<String, ParticleEmitterFactory*> ParticleEmitterFactoryMap;
|
---|
75 | typedef std::map<String, ParticleSystemRendererFactory*> ParticleSystemRendererFactoryMap;
|
---|
76 | protected:
|
---|
77 | /// Templates based on scripts
|
---|
78 | ParticleTemplateMap mSystemTemplates;
|
---|
79 |
|
---|
80 | /// Factories for named emitter types (can be extended using plugins)
|
---|
81 | ParticleEmitterFactoryMap mEmitterFactories;
|
---|
82 |
|
---|
83 | /// Factories for named affector types (can be extended using plugins)
|
---|
84 | ParticleAffectorFactoryMap mAffectorFactories;
|
---|
85 |
|
---|
86 | /// Map of renderer types to factories
|
---|
87 | ParticleSystemRendererFactoryMap mRendererFactories;
|
---|
88 |
|
---|
89 | StringVector mScriptPatterns;
|
---|
90 |
|
---|
91 | // Factory instance
|
---|
92 | ParticleSystemFactory* mFactory;
|
---|
93 |
|
---|
94 | /** Internal script parsing method. */
|
---|
95 | void parseNewEmitter(const String& type, DataStreamPtr& chunk, ParticleSystem* sys);
|
---|
96 | /** Internal script parsing method. */
|
---|
97 | void parseNewAffector(const String& type, DataStreamPtr& chunk, ParticleSystem* sys);
|
---|
98 | /** Internal script parsing method. */
|
---|
99 | void parseAttrib(const String& line, ParticleSystem* sys);
|
---|
100 | /** Internal script parsing method. */
|
---|
101 | void parseEmitterAttrib(const String& line, ParticleEmitter* sys);
|
---|
102 | /** Internal script parsing method. */
|
---|
103 | void parseAffectorAttrib(const String& line, ParticleAffector* sys);
|
---|
104 | /** Internal script parsing method. */
|
---|
105 | void skipToNextCloseBrace(DataStreamPtr& chunk);
|
---|
106 | /** Internal script parsing method. */
|
---|
107 | void skipToNextOpenBrace(DataStreamPtr& chunk);
|
---|
108 |
|
---|
109 | /// Internal implementation of createSystem
|
---|
110 | ParticleSystem* createSystemImpl(const String& name, size_t quota,
|
---|
111 | const String& resourceGroup);
|
---|
112 | /// Internal implementation of createSystem
|
---|
113 | ParticleSystem* createSystemImpl(const String& name, const String& templateName);
|
---|
114 | /// Internal implementation of destroySystem
|
---|
115 | void destroySystemImpl(ParticleSystem* sys);
|
---|
116 |
|
---|
117 |
|
---|
118 | public:
|
---|
119 |
|
---|
120 | ParticleSystemManager();
|
---|
121 | virtual ~ParticleSystemManager();
|
---|
122 |
|
---|
123 | /** Adds a new 'factory' object for emitters to the list of available emitter types.
|
---|
124 | @remarks
|
---|
125 | This method allows plugins etc to add new particle emitter types to Ogre. Particle emitters
|
---|
126 | are sources of particles, and generate new particles with their start positions, colours and
|
---|
127 | momentums appropriately. Plugins would create new subclasses of ParticleEmitter which
|
---|
128 | emit particles a certain way, and register a subclass of ParticleEmitterFactory to create them (since multiple
|
---|
129 | emitters can be created for different particle systems).
|
---|
130 | @par
|
---|
131 | All particle emitter factories have an assigned name which is used to identify the emitter
|
---|
132 | type. This must be unique.
|
---|
133 | @par
|
---|
134 | Note that the object passed to this function will not be destroyed by the ParticleSystemManager,
|
---|
135 | since it may have been allocted on a different heap in the case of plugins. The caller must
|
---|
136 | destroy the object later on, probably on plugin shutdown.
|
---|
137 | @param
|
---|
138 | factory Pointer to a ParticleEmitterFactory subclass created by the plugin or application code.
|
---|
139 | */
|
---|
140 | void addEmitterFactory(ParticleEmitterFactory* factory);
|
---|
141 |
|
---|
142 | /** Adds a new 'factory' object for affectors to the list of available affector types.
|
---|
143 | @remarks
|
---|
144 | This method allows plugins etc to add new particle affector types to Ogre. Particle
|
---|
145 | affectors modify the particles in a system a certain way such as affecting their direction
|
---|
146 | or changing their colour, lifespan etc. Plugins would
|
---|
147 | create new subclasses of ParticleAffector which affect particles a certain way, and register
|
---|
148 | a subclass of ParticleAffectorFactory to create them.
|
---|
149 | @par
|
---|
150 | All particle affector factories have an assigned name which is used to identify the affector
|
---|
151 | type. This must be unique.
|
---|
152 | @par
|
---|
153 | Note that the object passed to this function will not be destroyed by the ParticleSystemManager,
|
---|
154 | since it may have been allocted on a different heap in the case of plugins. The caller must
|
---|
155 | destroy the object later on, probably on plugin shutdown.
|
---|
156 | @param
|
---|
157 | factory Pointer to a ParticleAffectorFactory subclass created by the plugin or application code.
|
---|
158 | */
|
---|
159 | void addAffectorFactory(ParticleAffectorFactory* factory);
|
---|
160 |
|
---|
161 | /** Registers a factory class for creating ParticleSystemRenderer instances.
|
---|
162 | @par
|
---|
163 | Note that the object passed to this function will not be destroyed by the ParticleSystemManager,
|
---|
164 | since it may have been allocted on a different heap in the case of plugins. The caller must
|
---|
165 | destroy the object later on, probably on plugin shutdown.
|
---|
166 | @param
|
---|
167 | factory Pointer to a ParticleSystemRendererFactory subclass created by the plugin or application code.
|
---|
168 | */
|
---|
169 | void addRendererFactory(ParticleSystemRendererFactory* factory);
|
---|
170 |
|
---|
171 | /** Adds a new particle system template to the list of available templates.
|
---|
172 | @remarks
|
---|
173 | Instances of particle systems in a scene are not normally unique - often you want to place the
|
---|
174 | same effect in many places. This method allows you to register a ParticleSystem as a named template,
|
---|
175 | which can subsequently be used to create instances using the createSystem method.
|
---|
176 | @par
|
---|
177 | Note that particle system templates can either be created programmatically by an application
|
---|
178 | and registered using this method, or they can be defined in a script file (*.particle) which is
|
---|
179 | loaded by the engine at startup, very much like Material scripts.
|
---|
180 | @param
|
---|
181 | name The name of the template. Must be unique across all templates.
|
---|
182 | @param
|
---|
183 | sysTemplate A pointer to a particle system to be used as a template. The manager
|
---|
184 | will take over ownership of this pointer.
|
---|
185 |
|
---|
186 | */
|
---|
187 | void addTemplate(const String& name, ParticleSystem* sysTemplate);
|
---|
188 |
|
---|
189 | /** Create a new particle system template.
|
---|
190 | @remarks
|
---|
191 | This method is similar to the addTemplate method, except this just creates a new template
|
---|
192 | and returns a pointer to it to be populated. Use this when you don't already have a system
|
---|
193 | to add as a template and just want to create a new template which you will build up in-place.
|
---|
194 | @param
|
---|
195 | name The name of the template. Must be unique across all templates.
|
---|
196 | @param
|
---|
197 | resourceGroup The name of the resource group which will be used to
|
---|
198 | load any dependent resources.
|
---|
199 |
|
---|
200 | */
|
---|
201 | ParticleSystem* createTemplate(const String& name, const String& resourceGroup);
|
---|
202 |
|
---|
203 | /** Retrieves a particle system template for possible modification.
|
---|
204 | @remarks
|
---|
205 | Modifying a template does not affect the settings on any ParticleSystems already created
|
---|
206 | from this template.
|
---|
207 | */
|
---|
208 | ParticleSystem* getTemplate(const String& name);
|
---|
209 |
|
---|
210 | /** Internal method for creating a new emitter from a factory.
|
---|
211 | @remarks
|
---|
212 | Used internally by the engine to create new ParticleEmitter instances from named
|
---|
213 | factories. Applications should use the ParticleSystem::addEmitter method instead,
|
---|
214 | which calls this method to create an instance.
|
---|
215 | @param
|
---|
216 | emitterType String name of the emitter type to be created. A factory of this type must have been registered.
|
---|
217 | @param
|
---|
218 | psys The particle system this is being created for
|
---|
219 | */
|
---|
220 | ParticleEmitter* _createEmitter(const String& emitterType, ParticleSystem* psys);
|
---|
221 |
|
---|
222 | /** Internal method for destroying an emitter.
|
---|
223 | @remarks
|
---|
224 | Because emitters are created by factories which may allocate memory from separate heaps,
|
---|
225 | the memory allocated must be freed from the same place. This method is used to ask the factory
|
---|
226 | to destroy the instance passed in as a pointer.
|
---|
227 | @param
|
---|
228 | emitter Pointer to emitter to be destroyed. On return this pointer will point to invalid (freed) memory.
|
---|
229 | */
|
---|
230 | void _destroyEmitter(ParticleEmitter* emitter);
|
---|
231 |
|
---|
232 | /** Internal method for creating a new affector from a factory.
|
---|
233 | @remarks
|
---|
234 | Used internally by the engine to create new ParticleAffector instances from named
|
---|
235 | factories. Applications should use the ParticleSystem::addAffector method instead,
|
---|
236 | which calls this method to create an instance.
|
---|
237 | @param
|
---|
238 | effectorType String name of the affector type to be created. A factory of this type must have been registered.
|
---|
239 | @param
|
---|
240 | psys The particle system it is being created for
|
---|
241 | */
|
---|
242 | ParticleAffector* _createAffector(const String& affectorType, ParticleSystem* psys);
|
---|
243 |
|
---|
244 | /** Internal method for destroying an affector.
|
---|
245 | @remarks
|
---|
246 | Because affectors are created by factories which may allocate memory from separate heaps,
|
---|
247 | the memory allocated must be freed from the same place. This method is used to ask the factory
|
---|
248 | to destroy the instance passed in as a pointer.
|
---|
249 | @param
|
---|
250 | affector Pointer to affector to be destroyed. On return this pointer will point to invalid (freed) memory.
|
---|
251 | */
|
---|
252 | void _destroyAffector(ParticleAffector* affector);
|
---|
253 |
|
---|
254 | /** Internal method for creating a new renderer from a factory.
|
---|
255 | @remarks
|
---|
256 | Used internally by the engine to create new ParticleSystemRenderer instances from named
|
---|
257 | factories. Applications should use the ParticleSystem::setRenderer method instead,
|
---|
258 | which calls this method to create an instance.
|
---|
259 | @param
|
---|
260 | rendererType String name of the renderer type to be created. A factory of this type must have been registered.
|
---|
261 | */
|
---|
262 | ParticleSystemRenderer* _createRenderer(const String& rendererType);
|
---|
263 |
|
---|
264 | /** Internal method for destroying a renderer.
|
---|
265 | @remarks
|
---|
266 | Because renderer are created by factories which may allocate memory from separate heaps,
|
---|
267 | the memory allocated must be freed from the same place. This method is used to ask the factory
|
---|
268 | to destroy the instance passed in as a pointer.
|
---|
269 | @param
|
---|
270 | renderer Pointer to renderer to be destroyed. On return this pointer will point to invalid (freed) memory.
|
---|
271 | */
|
---|
272 | void _destroyRenderer(ParticleSystemRenderer* renderer);
|
---|
273 |
|
---|
274 | /** Frame event */
|
---|
275 | bool frameStarted(const FrameEvent &evt);
|
---|
276 |
|
---|
277 | /** Frame event */
|
---|
278 | bool frameEnded(const FrameEvent &evt);
|
---|
279 |
|
---|
280 | /** Init method to be called by OGRE system.
|
---|
281 | @remarks
|
---|
282 | Due to dependencies between various objects certain initialisation tasks cannot be done
|
---|
283 | on construction. OGRE will call this method when the rendering subsystem is initialised.
|
---|
284 | */
|
---|
285 | void _initialise(void);
|
---|
286 |
|
---|
287 | /// @copydoc ScriptLoader::getScriptPatterns
|
---|
288 | const StringVector& getScriptPatterns(void) const;
|
---|
289 | /// @copydoc ScriptLoader::parseScript
|
---|
290 | void parseScript(DataStreamPtr& stream, const String& groupName);
|
---|
291 | /// @copydoc ScriptLoader::getLoadingOrder
|
---|
292 | Real getLoadingOrder(void) const;
|
---|
293 |
|
---|
294 | typedef MapIterator<ParticleAffectorFactoryMap> ParticleAffectorFactoryIterator;
|
---|
295 | typedef MapIterator<ParticleEmitterFactoryMap> ParticleEmitterFactoryIterator;
|
---|
296 | typedef MapIterator<ParticleSystemRendererFactoryMap> ParticleRendererFactoryIterator;
|
---|
297 | /** Return an iterator over the affector factories currently registered */
|
---|
298 | ParticleAffectorFactoryIterator getAffectorFactoryIterator(void);
|
---|
299 | /** Return an iterator over the emitter factories currently registered */
|
---|
300 | ParticleEmitterFactoryIterator getEmitterFactoryIterator(void);
|
---|
301 | /** Return an iterator over the renderer factories currently registered */
|
---|
302 | ParticleRendererFactoryIterator getRendererFactoryIterator(void);
|
---|
303 |
|
---|
304 |
|
---|
305 | typedef MapIterator<ParticleTemplateMap> ParticleSystemTemplateIterator;
|
---|
306 | /** Gets an iterator over the list of particle system templates. */
|
---|
307 | ParticleSystemTemplateIterator getTemplateIterator(void)
|
---|
308 | {
|
---|
309 | return ParticleSystemTemplateIterator(
|
---|
310 | mSystemTemplates.begin(), mSystemTemplates.end());
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** Get an instance of ParticleSystemFactory (internal use). */
|
---|
314 | ParticleSystemFactory* _getFactory(void) { return mFactory; }
|
---|
315 |
|
---|
316 | /** Override standard Singleton retrieval.
|
---|
317 | @remarks
|
---|
318 | Why do we do this? Well, it's because the Singleton
|
---|
319 | implementation is in a .h file, which means it gets compiled
|
---|
320 | into anybody who includes it. This is needed for the
|
---|
321 | Singleton template to work, but we actually only want it
|
---|
322 | compiled into the implementation of the class based on the
|
---|
323 | Singleton, not all of them. If we don't change this, we get
|
---|
324 | link errors when trying to use the Singleton-based class from
|
---|
325 | an outside dll.
|
---|
326 | @par
|
---|
327 | This method just delegates to the template version anyway,
|
---|
328 | but the implementation stays in this single compilation unit,
|
---|
329 | preventing link errors.
|
---|
330 | */
|
---|
331 | static ParticleSystemManager& getSingleton(void);
|
---|
332 | /** Override standard Singleton retrieval.
|
---|
333 | @remarks
|
---|
334 | Why do we do this? Well, it's because the Singleton
|
---|
335 | implementation is in a .h file, which means it gets compiled
|
---|
336 | into anybody who includes it. This is needed for the
|
---|
337 | Singleton template to work, but we actually only want it
|
---|
338 | compiled into the implementation of the class based on the
|
---|
339 | Singleton, not all of them. If we don't change this, we get
|
---|
340 | link errors when trying to use the Singleton-based class from
|
---|
341 | an outside dll.
|
---|
342 | @par
|
---|
343 | This method just delegates to the template version anyway,
|
---|
344 | but the implementation stays in this single compilation unit,
|
---|
345 | preventing link errors.
|
---|
346 | */
|
---|
347 | static ParticleSystemManager* getSingletonPtr(void);
|
---|
348 |
|
---|
349 | };
|
---|
350 |
|
---|
351 | /** Factory object for creating ParticleSystem instances */
|
---|
352 | class _OgreExport ParticleSystemFactory : public MovableObjectFactory
|
---|
353 | {
|
---|
354 | protected:
|
---|
355 | MovableObject* createInstanceImpl(const String& name, const NameValuePairList* params);
|
---|
356 | public:
|
---|
357 | ParticleSystemFactory() {}
|
---|
358 | ~ParticleSystemFactory() {}
|
---|
359 |
|
---|
360 | static String FACTORY_TYPE_NAME;
|
---|
361 |
|
---|
362 | const String& getType(void) const;
|
---|
363 | void destroyInstance( MovableObject* obj);
|
---|
364 |
|
---|
365 | };
|
---|
366 |
|
---|
367 | }
|
---|
368 |
|
---|
369 | #endif
|
---|
370 |
|
---|