Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/tools/ParticleInterface.cc @ 3254

Last change on this file since 3254 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

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