Coverage Report

Created: 2026-05-30 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/src/OgreParticleSystemManager.cpp
Line
Count
Source
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-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#include "OgreStableHeaders.h"
29
30
#include "OgreParticleEmitterFactory.h"
31
#include "OgreParticleAffectorFactory.h"
32
#include "OgreParticleSystemRenderer.h"
33
#include "OgreBillboardParticleRenderer.h"
34
#include "OgreParticleSystem.h"
35
36
namespace Ogre {
37
    //-----------------------------------------------------------------------
38
    // Shortcut to set up billboard particle renderer
39
    BillboardParticleRendererFactory* mBillboardRendererFactory = 0;
40
    //-----------------------------------------------------------------------
41
    template<> ParticleSystemManager* Singleton<ParticleSystemManager>::msSingleton = 0;
42
    ParticleSystemManager* ParticleSystemManager::getSingletonPtr(void)
43
0
    {
44
0
        return msSingleton;
45
0
    }
46
    ParticleSystemManager& ParticleSystemManager::getSingleton(void)
47
0
    {  
48
0
        assert( msSingleton );  return ( *msSingleton );  
49
0
    }
50
    //-----------------------------------------------------------------------
51
    ParticleSystemManager::ParticleSystemManager()
52
1
    {
53
1
        OGRE_LOCK_AUTO_MUTEX;
54
1
        mFactory = OGRE_NEW ParticleSystemFactory();
55
1
        Root::getSingleton().addMovableObjectFactory(mFactory);
56
1
    }
57
    //-----------------------------------------------------------------------
58
    ParticleSystemManager::~ParticleSystemManager()
59
1
    {
60
1
        removeAllTemplates(true); // Destroy all templates
61
1
        OGRE_LOCK_AUTO_MUTEX;
62
        // delete billboard factory
63
1
        if (mBillboardRendererFactory)
64
0
        {
65
0
            OGRE_DELETE mBillboardRendererFactory;
66
0
            mBillboardRendererFactory = 0;
67
0
        }
68
69
1
        if (mFactory)
70
1
        {
71
            // delete particle system factory
72
1
            Root::getSingleton().removeMovableObjectFactory(mFactory);
73
1
            OGRE_DELETE mFactory;
74
1
            mFactory = 0;
75
1
        }
76
77
1
    }
78
    //-----------------------------------------------------------------------
79
    void ParticleSystemManager::addEmitterFactory(ParticleEmitterFactory* factory)
80
0
    {
81
0
        OGRE_LOCK_AUTO_MUTEX;
82
0
        String name = factory->getName();
83
0
        mEmitterFactories[name] = factory;
84
0
        LogManager::getSingleton().logMessage("Particle Emitter Type '" + name + "' registered");
85
0
    }
86
    //-----------------------------------------------------------------------
87
    void ParticleSystemManager::addAffectorFactory(ParticleAffectorFactory* factory)
88
0
    {
89
0
        OGRE_LOCK_AUTO_MUTEX;
90
0
        String name = factory->getName();
91
0
        mAffectorFactories[name] = factory;
92
0
        LogManager::getSingleton().logMessage("Particle Affector Type '" + name + "' registered");
93
0
    }
94
    //-----------------------------------------------------------------------
95
    void ParticleSystemManager::addRendererFactory(ParticleSystemRendererFactory* factory)
96
0
    {
97
0
        OGRE_LOCK_AUTO_MUTEX ;
98
0
        String name = factory->getType();
99
0
        mRendererFactories[name] = factory;
100
0
        LogManager::getSingleton().logMessage("Particle Renderer Type '" + name + "' registered");
101
0
    }
102
    //-----------------------------------------------------------------------
103
    void ParticleSystemManager::addTemplate(const String& name, ParticleSystem* sysTemplate)
104
0
    {
105
0
        OGRE_LOCK_AUTO_MUTEX;
106
        // check name
107
0
        if (mSystemTemplates.find(name) != mSystemTemplates.end())
108
0
        {
109
0
            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, 
110
0
                "ParticleSystem template with name '" + name + "' already exists.", 
111
0
                "ParticleSystemManager::addTemplate");
112
0
        }
113
114
0
        mSystemTemplates[name] = sysTemplate;
115
0
    }
116
    //-----------------------------------------------------------------------
117
    void ParticleSystemManager::removeTemplate(const String& name, bool deleteTemplate)
118
0
    {
119
0
        OGRE_LOCK_AUTO_MUTEX;
120
0
        ParticleTemplateMap::iterator itr = mSystemTemplates.find(name);
121
0
        if (itr == mSystemTemplates.end())
122
0
            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
123
0
                "ParticleSystem template with name '" + name + "' cannot be found.",
124
0
                "ParticleSystemManager::removeTemplate");
125
126
0
        if (deleteTemplate)
127
0
            OGRE_DELETE itr->second;
128
129
0
        mSystemTemplates.erase(itr);
130
0
    }
131
    //-----------------------------------------------------------------------
132
    void ParticleSystemManager::removeAllTemplates(bool deleteTemplate)
133
1
    {
134
1
        OGRE_LOCK_AUTO_MUTEX;
135
1
        if (deleteTemplate)
136
1
        {
137
1
            ParticleTemplateMap::iterator itr;
138
1
            for (itr = mSystemTemplates.begin(); itr != mSystemTemplates.end(); ++itr)
139
1
                OGRE_DELETE itr->second;
140
1
        }
141
142
1
        mSystemTemplates.clear();
143
1
    }
144
    //-----------------------------------------------------------------------
145
    void ParticleSystemManager::removeTemplatesByResourceGroup(const String& resourceGroup)
146
0
    {
147
0
        OGRE_LOCK_AUTO_MUTEX;
148
        
149
0
        ParticleTemplateMap::iterator i = mSystemTemplates.begin();
150
0
        while (i != mSystemTemplates.end())
151
0
        {
152
0
            ParticleTemplateMap::iterator icur = i++;
153
154
0
            if(icur->second->getResourceGroupName() == resourceGroup)
155
0
            {
156
0
                delete icur->second;
157
0
                mSystemTemplates.erase(icur);
158
0
            }
159
0
        }    
160
0
    }
161
    //-----------------------------------------------------------------------
162
    ParticleSystem* ParticleSystemManager::createTemplate(const String& name, 
163
        const String& resourceGroup)
164
0
    {
165
0
        OGRE_LOCK_AUTO_MUTEX;
166
        // check name
167
0
        if (mSystemTemplates.find(name) != mSystemTemplates.end())
168
0
        {
169
#if OGRE_PLATFORM == OGRE_PLATFORM_WINRT
170
            LogManager::getSingleton().logMessage("ParticleSystem template with name '" + name + "' already exists.");
171
            return NULL;
172
#else
173
0
            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, 
174
0
                "ParticleSystem template with name '" + name + "' already exists.", 
175
0
                "ParticleSystemManager::createTemplate");
176
0
#endif
177
0
        }
178
179
0
        ParticleSystem* tpl = OGRE_NEW ParticleSystem(name, resourceGroup);
180
0
        addTemplate(name, tpl);
181
0
        return tpl;
182
183
0
    }
184
    //-----------------------------------------------------------------------
185
    ParticleSystem* ParticleSystemManager::getTemplate(const String& name)
186
0
    {
187
0
        OGRE_LOCK_AUTO_MUTEX;
188
0
        ParticleTemplateMap::iterator i = mSystemTemplates.find(name);
189
0
        if (i != mSystemTemplates.end())
190
0
        {
191
0
            return i->second;
192
0
        }
193
0
        else
194
0
        {
195
0
            return 0;
196
0
        }
197
0
    }
198
    //-----------------------------------------------------------------------
199
    ParticleSystem* ParticleSystemManager::createSystemImpl(const String& name,
200
        size_t quota, const String& resourceGroup)
201
0
    {
202
0
        ParticleSystem* sys = OGRE_NEW ParticleSystem(name, resourceGroup);
203
0
        sys->setParticleQuota(quota);
204
0
        return sys;
205
0
    }
206
    //-----------------------------------------------------------------------
207
    ParticleSystem* ParticleSystemManager::createSystemImpl(const String& name, 
208
        const String& templateName)
209
0
    {
210
        // Look up template
211
0
        ParticleSystem* pTemplate = getTemplate(templateName);
212
0
        if (!pTemplate)
213
0
        {
214
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find required template '" + templateName + "'", "ParticleSystemManager::createSystem");
215
0
        }
216
217
0
        ParticleSystem* sys = createSystemImpl(name, pTemplate->getParticleQuota(), 
218
0
            pTemplate->getResourceGroupName());
219
        // Copy template settings
220
0
        *sys = *pTemplate;
221
0
        return sys;
222
        
223
0
    }
224
    //-----------------------------------------------------------------------
225
    ParticleEmitter* ParticleSystemManager::_createEmitter(
226
        const String& emitterType, ParticleSystem* psys)
227
0
    {
228
0
        OGRE_LOCK_AUTO_MUTEX;
229
        // Locate emitter type
230
0
        ParticleEmitterFactoryMap::iterator pFact = mEmitterFactories.find(emitterType);
231
232
0
        if (pFact == mEmitterFactories.end())
233
0
        {
234
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find emitter type '" + emitterType + "'");
235
0
        }
236
237
0
        return pFact->second->createEmitter(psys);
238
0
    }
239
    //-----------------------------------------------------------------------
240
    void ParticleSystemManager::_destroyEmitter(ParticleEmitter* emitter)
241
0
    {
242
0
        if(!emitter)
243
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot destroy a null ParticleEmitter.", "ParticleSystemManager::_destroyEmitter");
244
245
0
        OGRE_LOCK_AUTO_MUTEX;
246
        // Destroy using the factory which created it
247
0
        ParticleEmitterFactoryMap::iterator pFact = mEmitterFactories.find(emitter->getType());
248
249
0
        if (pFact == mEmitterFactories.end())
250
0
        {
251
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find emitter factory to destroy emitter.", 
252
0
                "ParticleSystemManager::_destroyEmitter");
253
0
        }
254
255
0
        pFact->second->destroyEmitter(emitter);
256
0
    }
257
    //-----------------------------------------------------------------------
258
    ParticleAffector* ParticleSystemManager::_createAffector(
259
        const String& affectorType, ParticleSystem* psys)
260
0
    {
261
0
        OGRE_LOCK_AUTO_MUTEX;
262
        // Locate affector type
263
0
        ParticleAffectorFactoryMap::iterator pFact = mAffectorFactories.find(affectorType);
264
265
0
        if (pFact == mAffectorFactories.end())
266
0
        {
267
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find affector type '" + affectorType + "'");
268
0
        }
269
270
0
        return pFact->second->createAffector(psys);
271
272
0
    }
273
    //-----------------------------------------------------------------------
274
    void ParticleSystemManager::_destroyAffector(ParticleAffector* affector)
275
0
    {
276
0
        if(!affector)
277
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot destroy a null ParticleAffector.", "ParticleSystemManager::_destroyAffector");
278
279
0
        OGRE_LOCK_AUTO_MUTEX;
280
        // Destroy using the factory which created it
281
0
        ParticleAffectorFactoryMap::iterator pFact = mAffectorFactories.find(affector->getType());
282
283
0
        if (pFact == mAffectorFactories.end())
284
0
        {
285
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find affector factory to destroy affector.", 
286
0
                "ParticleSystemManager::_destroyAffector");
287
0
        }
288
289
0
        pFact->second->destroyAffector(affector);
290
0
    }
291
    //-----------------------------------------------------------------------
292
    ParticleSystemRenderer* ParticleSystemManager::_createRenderer(const String& rendererType)
293
0
    {
294
0
        OGRE_LOCK_AUTO_MUTEX;
295
        // Locate affector type
296
0
        ParticleSystemRendererFactoryMap::iterator pFact = mRendererFactories.find(rendererType);
297
298
0
        if (pFact == mRendererFactories.end())
299
0
        {
300
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find requested renderer type.", 
301
0
                "ParticleSystemManager::_createRenderer");
302
0
        }
303
304
0
        return pFact->second->createInstance(rendererType);
305
0
    }
306
    //-----------------------------------------------------------------------
307
    void ParticleSystemManager::_destroyRenderer(ParticleSystemRenderer* renderer)
308
0
    {
309
0
        if(!renderer)
310
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot destroy a null ParticleSystemRenderer.", "ParticleSystemManager::_destroyRenderer");
311
312
0
        OGRE_LOCK_AUTO_MUTEX;
313
        // Destroy using the factory which created it
314
0
        ParticleSystemRendererFactoryMap::iterator pFact = mRendererFactories.find(renderer->getType());
315
316
0
        if (pFact == mRendererFactories.end())
317
0
        {
318
0
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot find renderer factory to destroy renderer.", 
319
0
                "ParticleSystemManager::_destroyRenderer");
320
0
        }
321
322
0
        pFact->second->destroyInstance(renderer);
323
0
    }
324
    //-----------------------------------------------------------------------
325
    void ParticleSystemManager::_initialise(void)
326
0
    {
327
0
        OGRE_LOCK_AUTO_MUTEX;
328
        // Create Billboard renderer factory
329
0
        mBillboardRendererFactory = OGRE_NEW BillboardParticleRendererFactory();
330
0
        addRendererFactory(mBillboardRendererFactory);
331
332
0
    }
333
    //-----------------------------------------------------------------------
334
    ParticleSystemManager::ParticleAffectorFactoryIterator 
335
    ParticleSystemManager::getAffectorFactoryIterator(void)
336
0
    {
337
0
        return ParticleAffectorFactoryIterator(
338
0
            mAffectorFactories.begin(), mAffectorFactories.end());
339
0
    }
340
    //-----------------------------------------------------------------------
341
    ParticleSystemManager::ParticleEmitterFactoryIterator 
342
    ParticleSystemManager::getEmitterFactoryIterator(void)
343
0
    {
344
0
        return ParticleEmitterFactoryIterator(
345
0
            mEmitterFactories.begin(), mEmitterFactories.end());
346
0
    }
347
    //-----------------------------------------------------------------------
348
    ParticleSystemManager::ParticleRendererFactoryIterator 
349
    ParticleSystemManager::getRendererFactoryIterator(void)
350
0
    {
351
0
        return ParticleRendererFactoryIterator(
352
0
            mRendererFactories.begin(), mRendererFactories.end());
353
0
    }
354
    //-----------------------------------------------------------------------
355
    //-----------------------------------------------------------------------
356
    //-----------------------------------------------------------------------
357
    const String MOT_PARTICLE_SYSTEM = "ParticleSystem";
358
    //-----------------------------------------------------------------------
359
    MovableObject* ParticleSystemFactory::createInstanceImpl( const String& name, 
360
            const NameValuePairList* params)
361
0
    {
362
0
        if (params != 0)
363
0
        {
364
0
            NameValuePairList::const_iterator ni = params->find("templateName");
365
0
            if (ni != params->end())
366
0
            {
367
0
                String templateName = ni->second;
368
                // create using manager
369
0
                return ParticleSystemManager::getSingleton().createSystemImpl(
370
0
                        name, templateName);
371
0
            }
372
0
        }
373
        // Not template based, look for quota & resource name
374
0
        size_t quota = 500;
375
0
        String resourceGroup = ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
376
0
        if (params != 0)
377
0
        {
378
0
            NameValuePairList::const_iterator ni = params->find("quota");
379
0
            if (ni != params->end())
380
0
            {
381
0
                quota = StringConverter::parseUnsignedInt(ni->second);
382
0
            }
383
0
            ni = params->find("resourceGroup");
384
0
            if (ni != params->end())
385
0
            {
386
0
                resourceGroup = ni->second;
387
0
            }
388
0
        }
389
        // create using manager
390
0
        return ParticleSystemManager::getSingleton().createSystemImpl(
391
0
                name, quota, resourceGroup);
392
                
393
394
0
    }
395
    //-----------------------------------------------------------------------
396
    const String& ParticleSystemFactory::getType(void) const
397
4
    {
398
4
        return MOT_PARTICLE_SYSTEM;
399
4
    }
400
    //-----------------------------------------------------------------------
401
}