Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_FS18/src/libraries/tools/ParticleInterface.cc @ 11820

Last change on this file since 11820 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.5 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 "ParticleInterface.h"
35
36#include <cassert>
37#include <string>
38#include <OgreParticleSystem.h>
39#include <OgreParticleEmitter.h>
40#include <OgreSceneManager.h>
41
42#include "util/Convert.h"
43#include "util/Math.h"
44#include "core/CoreIncludes.h"
45#include "core/config/ConfigValueIncludes.h"
46#include "core/GameMode.h"
47
48namespace orxonox
49{
50    unsigned int ParticleInterface::counter_s = 0;
51
52    RegisterAbstractClass(ParticleInterface).inheritsFrom<TimeFactorListener>();
53
54    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::Value detaillevel)
55    {
56        RegisterObject(ParticleInterface);
57
58        assert(scenemanager);
59
60        this->scenemanager_ = scenemanager;
61        this->particleSystem_ = nullptr;
62
63        this->bEnabled_ = true;
64        this->bVisible_ = true;
65        this->bAllowedByLOD_ = true;
66        this->speedFactor_ = 1.0f;
67
68        this->setDetailLevel(static_cast<unsigned int>(detaillevel));
69
70        this->setConfigValues();
71
72        if (GameMode::showsGraphics())
73        {
74            try
75            {
76                this->particleSystem_ = this->scenemanager_->createParticleSystem("particles" + multi_cast<std::string>(ParticleInterface::counter_s++), templateName);
77                this->setSpeedFactor(1.0f);
78            }
79            catch (...)
80            {
81                orxout(internal_error) << "Couldn't load particle system \"" << templateName << '"' << endl;
82                this->particleSystem_ = nullptr;
83            }
84        }
85    }
86
87    ParticleInterface::~ParticleInterface()
88    {
89        if (this->particleSystem_)
90        {
91            this->particleSystem_->removeAllEmitters();
92            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
93        }
94    }
95
96    void ParticleInterface::setConfigValues()
97    {
98        SetConfigValueExternal(globalDetailLevel_, "GraphicsSettings", "particlesDetailLevel", 2)
99            .description("O: off, 1: low, 2: normal, 3: high")
100            .callback(this, &ParticleInterface::detailLevelChanged);
101    }
102
103    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
104    {
105        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
106        {
107            Ogre::ParticleEmitter* newemitter = this->particleSystem_->addEmitter(this->particleSystem_->getEmitter(0)->getType());
108            this->particleSystem_->getEmitter(0)->copyParametersTo(newemitter);
109            return newemitter;
110        }
111        else
112            return nullptr;
113    }
114    Ogre::ParticleEmitter* ParticleInterface::getEmitter(unsigned int emitterNr) const
115    {
116        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
117            return this->particleSystem_->getEmitter(emitterNr);
118        else
119            return nullptr;
120    }
121    void ParticleInterface::removeEmitter(unsigned int emitterNr)
122    {
123        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
124            this->particleSystem_->removeEmitter(emitterNr);
125    }
126    void ParticleInterface::removeAllEmitters()
127    {
128        if (this->particleSystem_)
129            this->particleSystem_->removeAllEmitters();
130    }
131    unsigned int ParticleInterface::getNumEmitters() const
132    {
133        if (this->particleSystem_)
134            return this->particleSystem_->getNumEmitters();
135        else
136            return 0;
137    }
138
139    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
140    {
141        if (this->particleSystem_)
142            return this->particleSystem_->addAffector(name);
143        else
144            return nullptr;
145    }
146    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr)
147    {
148        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
149            return this->particleSystem_->getAffector(affectorNr);
150        else
151            return nullptr;
152    }
153    void ParticleInterface::removeAffector(unsigned int affectorNr)
154    {
155        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
156            this->particleSystem_->removeAffector(affectorNr);
157    }
158    void ParticleInterface::removeAllAffectors()
159    {
160        if (this->particleSystem_)
161            this->particleSystem_->removeAllAffectors();
162    }
163    unsigned int ParticleInterface::getNumAffectors() const
164    {
165        if (this->particleSystem_)
166            return this->particleSystem_->getNumAffectors();
167        else
168            return 0;
169    }
170
171    void ParticleInterface::setEnabled(bool enable)
172    {
173        this->bEnabled_ = enable;
174
175        if (this->particleSystem_)
176            for (unsigned int i = 0; i < this->particleSystem_->getNumEmitters(); i++)
177                this->particleSystem_->getEmitter(i)->setEnabled(this->bEnabled_ && this->bAllowedByLOD_);
178    }
179
180    void ParticleInterface::setVisible(bool visible)
181    {
182        this->bVisible_ = visible;
183
184        if (this->particleSystem_)
185            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
186    }
187
188    void ParticleInterface::setDetailLevel(unsigned int level)
189    {
190        this->detaillevel_ = level;
191        if (GameMode::showsGraphics())
192            this->detailLevelChanged();
193    }
194
195    void ParticleInterface::detailLevelChanged()
196    {
197        if (this->globalDetailLevel_ >= this->detaillevel_)
198            this->bAllowedByLOD_ = true;
199        else
200            this->bAllowedByLOD_ = false;
201
202        this->updateVisibility();
203    }
204
205    void ParticleInterface::updateVisibility()
206    {
207        this->setEnabled(this->isEnabled());
208        this->setVisible(this->isVisible());
209    }
210
211    void ParticleInterface::setSpeedFactor(float factor)
212    {
213        this->speedFactor_ = factor;
214
215        if (this->particleSystem_)
216            this->particleSystem_->setSpeedFactor(factor * this->getTimeFactor());
217    }
218    void ParticleInterface::changedTimeFactor(float factor_new, float factor_old)
219    {
220        this->setSpeedFactor(this->speedFactor_);
221    }
222
223    bool ParticleInterface::getKeepParticlesInLocalSpace() const
224    {
225        if (this->particleSystem_)
226            return this->particleSystem_->getKeepParticlesInLocalSpace();
227        else
228            return false;
229    }
230    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
231    {
232        if (this->particleSystem_)
233            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
234    }
235
236    void ParticleInterface::setDimensions(float scale)
237    {
238        this->particleSystem_->setDefaultDimensions(scale * this->particleSystem_->getDefaultWidth(), scale * this->particleSystem_->getDefaultHeight());
239    }
240}
Note: See TracBrowser for help on using the repository browser.