Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6603 was 5654, checked in by bensch, 19 years ago

orxonox/trunk: cycle-loading of LoadParam works…

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
311  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]312  ParticleSystem* tmpSys = tmpIt->firstElement();
[4176]313  while(tmpSys)
314    {
315      tmpSys->tick(dt);
316      tmpSys = tmpIt->nextElement();
317    }
318  delete tmpIt;
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{
336  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]337  ParticleSystem* tmpSys = tmpIt->firstElement();
[3932]338  while(tmpSys)
339    {
[4349]340      tmpSys->draw();
[3932]341      tmpSys = tmpIt->nextElement();
342    }
343  delete tmpIt;
344
345}
[3944]346
347/**
[4836]348 * @param number the n-th system to return
349 * @returns the system called by number or NULL if not found
[4338]350*/
351ParticleSystem* ParticleEngine::getSystemByNumber(unsigned int number) const
352{
353  int count = 0;
354  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]355  ParticleSystem* tmpSys = tmpIt->firstElement();
[4338]356  while(tmpSys)
357    {
358      count++;
359      if ( count == number)
[4597]360        {
361          delete tmpIt;
362          return tmpSys;
363        }
[4338]364      tmpSys = tmpIt->nextElement();
365    }
366  delete tmpIt;
367  return NULL;
368}
369
370/**
[4836]371 * @param number the n-th emitter to return
372 * @returns the emitter called by number or NULL if not found
[4338]373*/
374ParticleEmitter* ParticleEngine::getEmitterByNumber(unsigned int number) const
375{
376  int count = 0;
377  tIterator<ParticleEmitter>* tmpIt = emitterList->getIterator();
[5115]378  ParticleEmitter* tmpEmit = tmpIt->firstElement();
[4338]379  while(tmpEmit)
380    {
381      count++;
382      if ( count == number)
[4597]383        {
384          delete tmpIt;
385          return tmpEmit;
386        }
[4338]387      tmpEmit = tmpIt->nextElement();
388    }
389  delete tmpIt;
390  return NULL;
391}
392
393/**
[4836]394 *  outputs some nice debug information
[3944]395*/
[4746]396void ParticleEngine::debug()
[3944]397{
398  PRINT(0)("+-----------------------------------+\n");
399  PRINT(0)("+ PARTICLE-ENGINE DEBUG INFORMATION +\n");
400  PRINT(0)("+-----------------------------------+\n");
401  PRINT(0)(" Reference: %p\n", ParticleEngine::singletonRef);
402  PRINT(0)(" Count: Emitters: %d; Systems: %d, Connections: %d\n",
[4639]403  this->emitterList->getSize(), this->systemList->getSize(), this->connectionList->getSize());
404
[3944]405  if (this->connectionList->getSize() > 0)
[4639]406  {
407    PRINT(0)(" Connections:\n");
408    PRINT(0)(" -----------------------------------\n");
409
410    tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
[5115]411    ParticleConnection* tmpConnection = tmpConIt->firstElement();
[4639]412    while(tmpConnection)
[3944]413    {
[4639]414      PRINT(0)(" Emitter '%s' emitts into System '%s'\n", tmpConnection->emitter->getName(), tmpConnection->system->getName());
415      tmpConnection = tmpConIt->nextElement();
416    }
417    delete tmpConIt;
418  }
[3944]419
420  if (this->systemList->getSize() > 0)
[4639]421  {
422    tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
[5115]423    ParticleSystem* tmpSys = tmpIt->firstElement();
[4639]424    while(tmpSys)
[3944]425    {
[4639]426      tmpSys->debug();
427      tmpSys = tmpIt->nextElement();
[3944]428    }
[4639]429    delete tmpIt;
430  }
431  else
432  {
433    PRINT(0)("NO SYSTEMS\n");
434  }
435  if (this->emitterList->getSize() > 0)
436  {
437    tIterator<ParticleEmitter>* tmpIt = emitterList->getIterator();
[5115]438    ParticleEmitter* tmpEmit = tmpIt->firstElement();
[4639]439    while(tmpEmit)
440    {
441      tmpEmit->debug();
442      tmpEmit = tmpIt->nextElement();
443    }
444    delete tmpIt;
445  }
446  else
447  {
448    PRINTF(0)("NO EMITTERS\n");
449  }
[3944]450
451  PRINT(0)("+--------------------------------PE-+\n");
452
453}
[5447]454
Note: See TracBrowser for help on using the repository browser.