/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: ... co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE #include "particle_engine.h" #include "particle_system.h" #include "particle_emitter.h" #include "list.h" using namespace std; /** \brief standard constructor */ ParticleEngine::ParticleEngine () { this->setClassName ("ParticleEngine"); this->systemList = new tList; this->emitterList = new tList; this->connectionList = new tList; } /** \brief the singleton reference to this class */ ParticleEngine* ParticleEngine::singletonRef = NULL; /** \returns a Pointer to this Class */ ParticleEngine* ParticleEngine::getInstance(void) { if (!ParticleEngine::singletonRef) ParticleEngine::singletonRef = new ParticleEngine(); return ParticleEngine::singletonRef; } /** \brief standard destructor */ ParticleEngine::~ParticleEngine () { delete this->systemList; delete this->connectionList; ParticleEngine::singletonRef = NULL; } void ParticleEngine::addSystem(ParticleSystem* system) { this->systemList->add(system); } void ParticleEngine::addEmitter(ParticleEmitter* emitter) { this->emitterList->add(emitter); } /** \brief \todo header, check for double connections */ void ParticleEngine::addConnection(ParticleEmitter* emitter, ParticleSystem* system) { // look, if we have already added this connection tIterator* tmpConIt = connectionList->getIterator(); ParticleConnection* tmpConnection = tmpConIt->nextElement(); while(tmpConnection) { if (tmpConnection->emitter == emitter && tmpConnection->system == system) { PRINTF(2)("Connection between Emitter and System already added\n"); delete tmpConIt; return; } tmpConnection = tmpConIt->nextElement(); } delete tmpConIt; ParticleConnection* tmpCon = new ParticleConnection; tmpCon->emitter = emitter; tmpCon->system = system; this->connectionList->add(tmpCon); } bool ParticleEngine::removeSystem(ParticleSystem* system) { // remove any connections, that have this system within tIterator* tmpConIt = connectionList->getIterator(); ParticleConnection* tmpConnection = tmpConIt->nextElement(); while(tmpConnection) { if (tmpConnection->system == system) this->breakConnection(tmpConnection); tmpConnection = tmpConIt->nextElement(); } delete tmpConIt; // remove the System from the systemList. this->systemList->remove(system); } bool ParticleEngine::removeEmitter(ParticleEmitter* emitter) { // remove any connections, that have this emitter within tIterator* tmpConIt = connectionList->getIterator(); ParticleConnection* tmpConnection = tmpConIt->nextElement(); while(tmpConnection) { if (tmpConnection->emitter == emitter) this->breakConnection(tmpConnection); tmpConnection = tmpConIt->nextElement(); } delete tmpConIt; // remove the emitter from the emitterList this->emitterList->remove(emitter); } bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system) { // look, if we have already added this connection tIterator* tmpConIt = connectionList->getIterator(); ParticleConnection* tmpConnection = tmpConIt->nextElement(); while(tmpConnection) { if (tmpConnection->emitter == emitter && tmpConnection->system == system) this->breakConnection(tmpConnection); tmpConnection = tmpConIt->nextElement(); } delete tmpConIt; } bool ParticleEngine::breakConnection(ParticleConnection* connection) { this->connectionList->remove(connection); } /** \brief this function ticks all the ParticleSystems, so an animation will flow \param dt passed since last tick */ void ParticleEngine::tick(float dt) { // add new Particles to each System they are connected to. tIterator* tmpConIt = connectionList->getIterator(); ParticleConnection* tmpConnection = tmpConIt->nextElement(); while(tmpConnection) { tmpConnection->emitter->tick(dt, tmpConnection->system); tmpConnection = tmpConIt->nextElement(); } delete tmpConIt; // ticks all the ParticleSystems tIterator* tmpIt = systemList->getIterator(); ParticleSystem* tmpSys = tmpIt->nextElement(); while(tmpSys) { tmpSys->tick(dt); tmpSys = tmpIt->nextElement(); } delete tmpIt; } void ParticleEngine::draw(void) { tIterator* tmpIt = systemList->getIterator(); ParticleSystem* tmpSys = tmpIt->nextElement(); while(tmpSys) { tmpSys->draw(); tmpSys = tmpIt->nextElement(); } delete tmpIt; }