Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/tools/ParticleInterface.cc @ 2406

Last change on this file since 2406 was 2406, checked in by landauf, 15 years ago

Added TimeFactorListener to properly handle changes of the global time factor (makes the game faster or slower). Currently supported in Backlight, ParticleInterface and Timer, which were all critical classes I could think of (all other classes are already covered by the adjustment of dt in tick(dt)). It seems to work well, both with small values (0.1, 0.01) and great values (10, 100). Even pausing the game (0) works fine.

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30* @file
31* @brief class to control praticle effects
32*/
33
34#include "OrxonoxStableHeaders.h"
35#include "ParticleInterface.h"
36
37#include <OgreParticleSystem.h>
38#include <OgreParticleEmitter.h>
39#include <OgreSceneManager.h>
40#include <cassert>
41
42#include "GraphicsEngine.h"
43#include "core/Core.h"
44#include "core/CoreIncludes.h"
45#include "util/Convert.h"
46
47namespace orxonox
48{
49    unsigned int ParticleInterface::counter_s = 0;
50    ParticleInterface* ParticleInterface::currentParticleInterface_s = 0;
51
52    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::LOD detaillevel)
53    {
54        RegisterObject(ParticleInterface);
55
56        assert(scenemanager);
57
58        this->scenemanager_ = scenemanager;
59        this->sceneNode_ = 0;
60        this->particleSystem_ = 0;
61
62        this->bEnabled_ = true;
63        this->bVisible_ = true;
64        this->bAllowedByLOD_ = true;
65
66        if (Core::showsGraphics())
67        {
68            try
69            {
70                this->particleSystem_ = this->scenemanager_->createParticleSystem("particles" + getConvertedValue<unsigned int, std::string>(ParticleInterface::counter_s++), templateName);
71                this->setSpeedFactor(1.0f);
72            }
73            catch (...)
74            {
75                COUT(1) << "Error: Couln't load particle system \"" << templateName << "\"" << std::endl;
76                this->particleSystem_ = 0;
77            }
78        }
79
80        this->setDetailLevel((unsigned int)detaillevel);
81    }
82
83    ParticleInterface::~ParticleInterface()
84    {
85        if (this->particleSystem_)
86        {
87            this->particleSystem_->removeAllEmitters();
88            this->detachFromSceneNode();
89            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
90        }
91    }
92
93    void ParticleInterface::addToSceneNode(Ogre::SceneNode* sceneNode)
94    {
95        if (this->sceneNode_)
96            this->detachFromSceneNode();
97
98        if (this->particleSystem_)
99        {
100            this->sceneNode_ = sceneNode;
101            this->sceneNode_->attachObject(this->particleSystem_);
102        }
103    }
104
105    void ParticleInterface::detachFromSceneNode()
106    {
107        if (this->sceneNode_)
108        {
109            if (this->particleSystem_)
110                this->sceneNode_->detachObject(this->particleSystem_);
111            this->sceneNode_ = 0;
112        }
113    }
114
115    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
116    {
117        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
118        {
119            Ogre::ParticleEmitter* newemitter = this->particleSystem_->addEmitter(this->particleSystem_->getEmitter(0)->getType());
120            this->particleSystem_->getEmitter(0)->copyParametersTo(newemitter);
121            return newemitter;
122        }
123        else
124            return 0;
125    }
126    Ogre::ParticleEmitter* ParticleInterface::getEmitter(unsigned int emitterNr) const
127    {
128        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
129            return this->particleSystem_->getEmitter(emitterNr);
130        else
131            return 0;
132    }
133    void ParticleInterface::removeEmitter(unsigned int emitterNr)
134    {
135        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
136            this->particleSystem_->removeEmitter(emitterNr);
137    }
138    void ParticleInterface::removeAllEmitters()
139    {
140        if (this->particleSystem_)
141            this->particleSystem_->removeAllEmitters();
142    }
143    unsigned int ParticleInterface::getNumEmitters() const
144    {
145        if (this->particleSystem_)
146            return this->particleSystem_->getNumEmitters();
147        else
148            return 0;
149    }
150
151    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
152    {
153        if (this->particleSystem_)
154            return this->particleSystem_->addAffector(name);
155        else
156            return 0;
157    }
158    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr) const
159    {
160        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
161            return this->particleSystem_->getAffector(affectorNr);
162        else
163            return 0;
164    }
165    void ParticleInterface::removeAffector(unsigned int affectorNr)
166    {
167        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
168            this->particleSystem_->removeAffector(affectorNr);
169    }
170    void ParticleInterface::removeAllAffectors()
171    {
172        if (this->particleSystem_)
173            this->particleSystem_->removeAllAffectors();
174    }
175    unsigned int ParticleInterface::getNumAffectors() const
176    {
177        if (this->particleSystem_)
178            return this->particleSystem_->getNumAffectors();
179        else
180            return 0;
181    }
182
183    void ParticleInterface::setEnabled(bool enable)
184    {
185        this->bEnabled_ = enable;
186
187        if (this->particleSystem_)
188            for (unsigned int i = 0; i < this->particleSystem_->getNumEmitters(); i++)
189                this->particleSystem_->getEmitter(i)->setEnabled(this->bEnabled_ && this->bAllowedByLOD_);
190    }
191
192    void ParticleInterface::setVisible(bool visible)
193    {
194        this->bVisible_ = visible;
195
196        if (this->particleSystem_)
197            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
198    }
199
200    void ParticleInterface::setDetailLevel(unsigned int level)
201    {
202        this->detaillevel_ = level;
203        if (GraphicsEngine::getInstancePtr())
204            this->detailLevelChanged(GraphicsEngine::getInstance().getDetailLevelParticle());
205    }
206
207    void ParticleInterface::detailLevelChanged(unsigned int newlevel)
208    {
209        if (newlevel >= (unsigned int)this->detaillevel_)
210            this->bAllowedByLOD_ = true;
211        else
212            this->bAllowedByLOD_ = false;
213
214        this->updateVisibility();
215    }
216
217    void ParticleInterface::updateVisibility()
218    {
219        this->setEnabled(this->isEnabled());
220        this->setVisible(this->isVisible());
221    }
222
223    void ParticleInterface::setSpeedFactor(float factor)
224    {
225        this->speedFactor_ = factor;
226
227        if (this->particleSystem_)
228            this->particleSystem_->setSpeedFactor(factor * this->getTimeFactor());
229    }
230    void ParticleInterface::changedTimeFactor(float factor_new, float factor_old)
231    {
232        this->setSpeedFactor(this->speedFactor_);
233    }
234
235    bool ParticleInterface::getKeepParticlesInLocalSpace() const
236    {
237        if (this->particleSystem_)
238            return this->particleSystem_->getKeepParticlesInLocalSpace();
239        else
240            return false;
241    }
242    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
243    {
244        if (this->particleSystem_)
245            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
246    }
247}
Note: See TracBrowser for help on using the repository browser.