Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/particles/engine/particle_engine.cc @ 6694

Last change on this file since 6694 was 6619, checked in by bensch, 19 years ago

orxonox/trunk: rendering without the ParticleEngine, so now we are FAST :)

File size: 12.6 KB
RevLine 
[4597]1/*
[3655]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: ...
13   co-programmer: ...
14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[3655]17
[3924]18#include "particle_engine.h"
[3655]19
[5447]20#include "class_list.h"
[3931]21
[3930]22#include "list.h"
[4381]23#include "debug.h"
24#include "stdlibincl.h"
[4726]25#include "load_param.h"
[3930]26
[3655]27using namespace std;
28
29/**
[4836]30 *  standard constructor
[3655]31*/
[4597]32ParticleEngine::ParticleEngine ()
[3655]33{
[4320]34   this->setClassID(CL_PARTICLE_ENGINE, "ParticleEngine");
[4597]35   this->setName("ParticleEngine");
[3655]36
[3931]37   this->systemList = new tList<ParticleSystem>;
[3935]38   this->emitterList = new tList<ParticleEmitter>;
[3932]39   this->connectionList = new tList<ParticleConnection>;
[3655]40}
41
42/**
[4836]43 *  the singleton reference to this class
[3655]44*/
[3923]45ParticleEngine* ParticleEngine::singletonRef = NULL;
[3655]46
47/**
[4836]48 *  deletes all the system, emitters, connections and Lists
[3655]49*/
[4597]50ParticleEngine::~ParticleEngine ()
[3655]51{
[5445]52  /// @todo we must not do this, because PNoe does it for us
53  /// or we do this with help from ClassList, which essentially makes much more sense
54
[3945]55  // delete all remaining systems
[5115]56//   tIterator<ParticleSystem>* sysIt = this->systemList->getIterator();
57//   ParticleSystem* tmpSys = sysIt->firstElement();
58//   while(tmpSys)
59//     {
60//       delete tmpSys;
61//       tmpSys = sysIt->nextElement();
62//     }
63//   delete sysIt;
64   delete this->systemList;
65//
[5445]66   // delete all remaining emitters
67   tIterator<ParticleEmitter>* emitIt = this->emitterList->getIterator();
68   ParticleEmitter* tmpEmit = emitIt->firstElement();
69   while(tmpEmit)
70     {
71       delete tmpEmit;
72       tmpEmit = emitIt->nextElement();
73     }
74   delete emitIt;
[5115]75   delete this->emitterList;
[3930]76
[3945]77  // there should be no more Connections
78  if (this->connectionList->getSize() == 0)
79    delete this->connectionList;
80  else
81    PRINTF(2)("The Connection List is not empty. This should not happen.\n");
82
[3923]83  ParticleEngine::singletonRef = NULL;
[3655]84}
[3931]85
[3945]86/**
[4726]87  \brief loads the ParticleEngines settings and connections between particles and emitters
[4836]88* @param root the XML-element to load this from.
[4726]89 */
90void ParticleEngine::loadParams(const TiXmlElement* root)
91{
[5654]92  LOAD_PARAM_START_CYCLE(root, element);
[4726]93  {
[5654]94    LoadParam_CYCLE(element, "connect", this, ParticleEngine, addConnection)
[4726]95        .describe("connects an Emitter to a System (emitterName, systemName)");
96  }
[5654]97  LOAD_PARAM_END_CYCLE(element);
[4726]98}
99
100/**
[4836]101 *  Adds a System to the System list.
[3945]102
103   this is done automatically when creating a ParticleSystem
104*/
[3932]105void ParticleEngine::addSystem(ParticleSystem* system)
106{
107  this->systemList->add(system);
108}
109
[3945]110/**
[4836]111 *  Adds an emitter to the emitterList
[3945]112
113   this is done automatically when creating a ParticleEmitter
114*/
[3935]115void ParticleEngine::addEmitter(ParticleEmitter* emitter)
116{
117  this->emitterList->add(emitter);
118}
[3932]119
[3931]120/**
[5439]121* @brief Connects a ParticleSystem to a ParticleSystem thus emitting Particles.
[4836]122* @param emitter the Emitter to connect to the System
123* @param system the System to connect to the Emitter
[4726]124*/
125void ParticleEngine::addConnection(const char* emitter, const char* system)
126{
[5445]127  ParticleEmitter* tmpEmit = dynamic_cast<ParticleEmitter*>(ClassList::getObject(emitter, CL_PARTICLE_EMITTER));//this->getEmitterByName(emitter);
128  ParticleSystem* tmpSys = dynamic_cast<ParticleSystem*>(ClassList::getObject(system, CL_PARTICLE_SYSTEM));//this->getSystemByName(system);
[4726]129
130  if (tmpEmit != NULL && tmpSys != NULL)
131    this->addConnection(tmpEmit, tmpSys);
132  else
133  {
134    if (tmpEmit == NULL)
135      PRINTF(2)("Emitter %s not found in the List of emitters, not connecting to %s\n", emitter, system);
136    if (tmpEmit == NULL)
137      PRINTF(2)("System %s not found in the List of emitters, not connecting to %s\n", system, emitter);
138  }
139}
140
141/**
[4836]142 *  Connects a ParticleSystem to a ParticleSystem thus emitting Particles.
143 * @param emitter the Emitter to connect to the System
144 * @param system the System to connect to the Emitter
[3932]145*/
146void ParticleEngine::addConnection(ParticleEmitter* emitter, ParticleSystem* system)
147{
[3937]148  // look, if we have already added this connection
149  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]150  ParticleConnection* tmpConnection = tmpConIt->firstElement();
[3937]151  while(tmpConnection)
152    {
153      if (tmpConnection->emitter == emitter && tmpConnection->system == system)
[4597]154        {
[5445]155          PRINTF(2)("Connection between Emitter and System already exists.\n");
[4597]156          delete tmpConIt;
157          return;
158        }
159
[3937]160      tmpConnection = tmpConIt->nextElement();
161    }
162  delete tmpConIt;
163
164
[4597]165
[3932]166  ParticleConnection* tmpCon = new ParticleConnection;
167  tmpCon->emitter = emitter;
168  tmpCon->system = system;
169
170  this->connectionList->add(tmpCon);
171}
172
[3945]173/**
[4836]174 *  Removes a system from the systemList and also removes all Connections to the System
175 * @param system The ParticleSystem to delete
[3945]176*/
[3935]177bool ParticleEngine::removeSystem(ParticleSystem* system)
178{
[3943]179  // remove any connections, that have this system within
180  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]181  ParticleConnection* tmpConnection = tmpConIt->firstElement();
[5445]182  while(tmpConnection != NULL)
[3943]183    {
184      if (tmpConnection->system == system)
[4597]185        this->breakConnection(tmpConnection);
[3943]186      tmpConnection = tmpConIt->nextElement();
187    }
188  delete tmpConIt;
189
190  // remove the System from the systemList.
[3935]191  this->systemList->remove(system);
192}
193
[3945]194/**
[4836]195 *  removes an emitter from the emitterList and also from all Connections it is attached to.
196 * @param emitter the ParticleEmitter to remove.
[3945]197*/
[3935]198bool ParticleEngine::removeEmitter(ParticleEmitter* emitter)
199{
[3943]200  // remove any connections, that have this emitter within
201  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]202  ParticleConnection* tmpConnection = tmpConIt->firstElement();
[5445]203  while(tmpConnection != NULL)
[3943]204    {
205      if (tmpConnection->emitter == emitter)
[4597]206        this->breakConnection(tmpConnection);
[3943]207      tmpConnection = tmpConIt->nextElement();
208    }
209  delete tmpConIt;
210
211  // remove the emitter from the emitterList
[3935]212  this->emitterList->remove(emitter);
213}
214
[5447]215
[3945]216/**
[4836]217 *  removes a Connection between an Emitter and a System
[5447]218 * @param connection the connection to remove
219 *
220 * \see bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system)
221 */
222bool ParticleEngine::breakConnection(ParticleConnection* connection)
223{
224  this->connectionList->remove(connection);
225  return true;
226}
227
228/**
229 *  removes a Connection between an Emitter and a System
[4836]230 * @param emitter The emitter of the connection to remove
231 * @param system The system of the connection to remove
232 * @returns true, if the connection was broken, false if the conntection was not found
[5447]233 *
234 * only if both system and emitter are in the connection the Connection will be broken
[3945]235*/
[3943]236bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system)
237{
238  // look, if we have already added this connection
239  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]240  ParticleConnection* tmpConnection = tmpConIt->firstElement();
[3943]241  while(tmpConnection)
242    {
[3945]243    if (tmpConnection->emitter == emitter && tmpConnection->system == system)
244      {
[4597]245        this->breakConnection(tmpConnection);
246        delete tmpConIt;
247        return true;
[3945]248      }
249    tmpConnection = tmpConIt->nextElement();
[3943]250    }
[3945]251  delete tmpConIt;
252  return false;
[3943]253}
254
[3945]255/**
[4836]256 *  removes a Connection between an Emitter and a System
[5447]257 * @param emitter The emitter of the connections to remove
258 * @returns the count of connections that were broken, 0 if no conntection was not found
259 */
260unsigned int ParticleEngine::breakConnections(ParticleEmitter* emitter)
[3943]261{
[5447]262  unsigned int retVal = 0;
263  // look, if we have already added this connection
264  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
265  ParticleConnection* tmpConnection = tmpConIt->firstElement();
266  while(tmpConnection)
267  {
268    if (tmpConnection->emitter == emitter)
269    {
270      this->breakConnection(tmpConnection);
271      retVal++;
272    }
273    tmpConnection = tmpConIt->nextElement();
274  }
275  delete tmpConIt;
276  return retVal;
[3943]277}
278
279
[3932]280/**
[5447]281 *  removes a Connection between an Emitter and a System
282 * @param system The system of the connections to remove
283 * @returns the count of connections that were broken, 0 if no conntection was not found
284 */
285unsigned int ParticleEngine::breakConnections(ParticleSystem* system)
286{
287  unsigned int retVal = 0;
288  // look, if we have already added this connection
289  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
290  ParticleConnection* tmpConnection = tmpConIt->firstElement();
291  while(tmpConnection)
292  {
293    if (tmpConnection->system == system)
294    {
295      this->breakConnection(tmpConnection);
296      retVal++;
297    }
298    tmpConnection = tmpConIt->nextElement();
299  }
300  delete tmpConIt;
301  return retVal;
302}
303
304/**
[4836]305 *  this function ticks all the ParticleSystems, so an animation will flow
306 * @param dt passed since last tick
[3931]307*/
308void ParticleEngine::tick(float dt)
309{
[4176]310  // ticks all the ParticleSystems
[6612]311//   tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
312//   ParticleSystem* tmpSys = tmpIt->firstElement();
313//   while(tmpSys)
314//     {
315//       tmpSys->tick(dt);
316//       tmpSys = tmpIt->nextElement();
317//     }
318//   delete tmpIt;
[4176]319
[3945]320  // add new Particles to each System connected to an Emitter.
[3932]321  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]322  ParticleConnection* tmpConnection = tmpConIt->firstElement();
[3932]323  while(tmpConnection)
324    {
325      tmpConnection->emitter->tick(dt, tmpConnection->system);
326      tmpConnection = tmpConIt->nextElement();
327    }
328  delete tmpConIt;
[3931]329}
[3932]330
[3945]331/**
[4836]332 *  draws all the systems and their Particles.
[3945]333*/
[4349]334void ParticleEngine::draw() const
[3932]335{
[6612]336  /*
[3932]337  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]338  ParticleSystem* tmpSys = tmpIt->firstElement();
[3932]339  while(tmpSys)
340    {
[4349]341      tmpSys->draw();
[3932]342      tmpSys = tmpIt->nextElement();
343    }
[6612]344  delete tmpIt;*/
[3932]345
346}
[3944]347
348/**
[4836]349 * @param number the n-th system to return
350 * @returns the system called by number or NULL if not found
[4338]351*/
352ParticleSystem* ParticleEngine::getSystemByNumber(unsigned int number) const
353{
354  int count = 0;
355  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]356  ParticleSystem* tmpSys = tmpIt->firstElement();
[4338]357  while(tmpSys)
358    {
359      count++;
360      if ( count == number)
[4597]361        {
362          delete tmpIt;
363          return tmpSys;
364        }
[4338]365      tmpSys = tmpIt->nextElement();
366    }
367  delete tmpIt;
368  return NULL;
369}
370
371/**
[4836]372 * @param number the n-th emitter to return
373 * @returns the emitter called by number or NULL if not found
[4338]374*/
375ParticleEmitter* ParticleEngine::getEmitterByNumber(unsigned int number) const
376{
377  int count = 0;
378  tIterator<ParticleEmitter>* tmpIt = emitterList->getIterator();
[5115]379  ParticleEmitter* tmpEmit = tmpIt->firstElement();
[4338]380  while(tmpEmit)
381    {
382      count++;
383      if ( count == number)
[4597]384        {
385          delete tmpIt;
386          return tmpEmit;
387        }
[4338]388      tmpEmit = tmpIt->nextElement();
389    }
390  delete tmpIt;
391  return NULL;
392}
393
394/**
[4836]395 *  outputs some nice debug information
[3944]396*/
[4746]397void ParticleEngine::debug()
[3944]398{
399  PRINT(0)("+-----------------------------------+\n");
400  PRINT(0)("+ PARTICLE-ENGINE DEBUG INFORMATION +\n");
401  PRINT(0)("+-----------------------------------+\n");
402  PRINT(0)(" Reference: %p\n", ParticleEngine::singletonRef);
403  PRINT(0)(" Count: Emitters: %d; Systems: %d, Connections: %d\n",
[4639]404  this->emitterList->getSize(), this->systemList->getSize(), this->connectionList->getSize());
405
[3944]406  if (this->connectionList->getSize() > 0)
[4639]407  {
408    PRINT(0)(" Connections:\n");
409    PRINT(0)(" -----------------------------------\n");
410
411    tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]412    ParticleConnection* tmpConnection = tmpConIt->firstElement();
[4639]413    while(tmpConnection)
[3944]414    {
[4639]415      PRINT(0)(" Emitter '%s' emitts into System '%s'\n", tmpConnection->emitter->getName(), tmpConnection->system->getName());
416      tmpConnection = tmpConIt->nextElement();
417    }
418    delete tmpConIt;
419  }
[3944]420
421  if (this->systemList->getSize() > 0)
[4639]422  {
423    tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]424    ParticleSystem* tmpSys = tmpIt->firstElement();
[4639]425    while(tmpSys)
[3944]426    {
[4639]427      tmpSys->debug();
428      tmpSys = tmpIt->nextElement();
[3944]429    }
[4639]430    delete tmpIt;
431  }
432  else
433  {
434    PRINT(0)("NO SYSTEMS\n");
435  }
436  if (this->emitterList->getSize() > 0)
437  {
438    tIterator<ParticleEmitter>* tmpIt = emitterList->getIterator();
[5115]439    ParticleEmitter* tmpEmit = tmpIt->firstElement();
[4639]440    while(tmpEmit)
441    {
442      tmpEmit->debug();
443      tmpEmit = tmpIt->nextElement();
444    }
445    delete tmpIt;
446  }
447  else
448  {
449    PRINTF(0)("NO EMITTERS\n");
450  }
[3944]451
452  PRINT(0)("+--------------------------------PE-+\n");
453
454}
[5447]455
Note: See TracBrowser for help on using the repository browser.