Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6621 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
Line 
1/*
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
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "particle_engine.h"
19
20#include "class_list.h"
21
22#include "list.h"
23#include "debug.h"
24#include "stdlibincl.h"
25#include "load_param.h"
26
27using namespace std;
28
29/**
30 *  standard constructor
31*/
32ParticleEngine::ParticleEngine ()
33{
34   this->setClassID(CL_PARTICLE_ENGINE, "ParticleEngine");
35   this->setName("ParticleEngine");
36
37   this->systemList = new tList<ParticleSystem>;
38   this->emitterList = new tList<ParticleEmitter>;
39   this->connectionList = new tList<ParticleConnection>;
40}
41
42/**
43 *  the singleton reference to this class
44*/
45ParticleEngine* ParticleEngine::singletonRef = NULL;
46
47/**
48 *  deletes all the system, emitters, connections and Lists
49*/
50ParticleEngine::~ParticleEngine ()
51{
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
55  // delete all remaining systems
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//
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;
75   delete this->emitterList;
76
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
83  ParticleEngine::singletonRef = NULL;
84}
85
86/**
87  \brief loads the ParticleEngines settings and connections between particles and emitters
88* @param root the XML-element to load this from.
89 */
90void ParticleEngine::loadParams(const TiXmlElement* root)
91{
92  LOAD_PARAM_START_CYCLE(root, element);
93  {
94    LoadParam_CYCLE(element, "connect", this, ParticleEngine, addConnection)
95        .describe("connects an Emitter to a System (emitterName, systemName)");
96  }
97  LOAD_PARAM_END_CYCLE(element);
98}
99
100/**
101 *  Adds a System to the System list.
102
103   this is done automatically when creating a ParticleSystem
104*/
105void ParticleEngine::addSystem(ParticleSystem* system)
106{
107  this->systemList->add(system);
108}
109
110/**
111 *  Adds an emitter to the emitterList
112
113   this is done automatically when creating a ParticleEmitter
114*/
115void ParticleEngine::addEmitter(ParticleEmitter* emitter)
116{
117  this->emitterList->add(emitter);
118}
119
120/**
121* @brief Connects a ParticleSystem to a ParticleSystem thus emitting Particles.
122* @param emitter the Emitter to connect to the System
123* @param system the System to connect to the Emitter
124*/
125void ParticleEngine::addConnection(const char* emitter, const char* system)
126{
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);
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/**
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
145*/
146void ParticleEngine::addConnection(ParticleEmitter* emitter, ParticleSystem* system)
147{
148  // look, if we have already added this connection
149  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
150  ParticleConnection* tmpConnection = tmpConIt->firstElement();
151  while(tmpConnection)
152    {
153      if (tmpConnection->emitter == emitter && tmpConnection->system == system)
154        {
155          PRINTF(2)("Connection between Emitter and System already exists.\n");
156          delete tmpConIt;
157          return;
158        }
159
160      tmpConnection = tmpConIt->nextElement();
161    }
162  delete tmpConIt;
163
164
165
166  ParticleConnection* tmpCon = new ParticleConnection;
167  tmpCon->emitter = emitter;
168  tmpCon->system = system;
169
170  this->connectionList->add(tmpCon);
171}
172
173/**
174 *  Removes a system from the systemList and also removes all Connections to the System
175 * @param system The ParticleSystem to delete
176*/
177bool ParticleEngine::removeSystem(ParticleSystem* system)
178{
179  // remove any connections, that have this system within
180  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
181  ParticleConnection* tmpConnection = tmpConIt->firstElement();
182  while(tmpConnection != NULL)
183    {
184      if (tmpConnection->system == system)
185        this->breakConnection(tmpConnection);
186      tmpConnection = tmpConIt->nextElement();
187    }
188  delete tmpConIt;
189
190  // remove the System from the systemList.
191  this->systemList->remove(system);
192}
193
194/**
195 *  removes an emitter from the emitterList and also from all Connections it is attached to.
196 * @param emitter the ParticleEmitter to remove.
197*/
198bool ParticleEngine::removeEmitter(ParticleEmitter* emitter)
199{
200  // remove any connections, that have this emitter within
201  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
202  ParticleConnection* tmpConnection = tmpConIt->firstElement();
203  while(tmpConnection != NULL)
204    {
205      if (tmpConnection->emitter == emitter)
206        this->breakConnection(tmpConnection);
207      tmpConnection = tmpConIt->nextElement();
208    }
209  delete tmpConIt;
210
211  // remove the emitter from the emitterList
212  this->emitterList->remove(emitter);
213}
214
215
216/**
217 *  removes a Connection between an Emitter and a System
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
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
233 *
234 * only if both system and emitter are in the connection the Connection will be broken
235*/
236bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system)
237{
238  // look, if we have already added this connection
239  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
240  ParticleConnection* tmpConnection = tmpConIt->firstElement();
241  while(tmpConnection)
242    {
243    if (tmpConnection->emitter == emitter && tmpConnection->system == system)
244      {
245        this->breakConnection(tmpConnection);
246        delete tmpConIt;
247        return true;
248      }
249    tmpConnection = tmpConIt->nextElement();
250    }
251  delete tmpConIt;
252  return false;
253}
254
255/**
256 *  removes a Connection between an Emitter and a System
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)
261{
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;
277}
278
279
280/**
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/**
305 *  this function ticks all the ParticleSystems, so an animation will flow
306 * @param dt passed since last tick
307*/
308void ParticleEngine::tick(float dt)
309{
310  // ticks all the ParticleSystems
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;
319
320  // add new Particles to each System connected to an Emitter.
321  tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
322  ParticleConnection* tmpConnection = tmpConIt->firstElement();
323  while(tmpConnection)
324    {
325      tmpConnection->emitter->tick(dt, tmpConnection->system);
326      tmpConnection = tmpConIt->nextElement();
327    }
328  delete tmpConIt;
329}
330
331/**
332 *  draws all the systems and their Particles.
333*/
334void ParticleEngine::draw() const
335{
336  /*
337  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
338  ParticleSystem* tmpSys = tmpIt->firstElement();
339  while(tmpSys)
340    {
341      tmpSys->draw();
342      tmpSys = tmpIt->nextElement();
343    }
344  delete tmpIt;*/
345
346}
347
348/**
349 * @param number the n-th system to return
350 * @returns the system called by number or NULL if not found
351*/
352ParticleSystem* ParticleEngine::getSystemByNumber(unsigned int number) const
353{
354  int count = 0;
355  tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
356  ParticleSystem* tmpSys = tmpIt->firstElement();
357  while(tmpSys)
358    {
359      count++;
360      if ( count == number)
361        {
362          delete tmpIt;
363          return tmpSys;
364        }
365      tmpSys = tmpIt->nextElement();
366    }
367  delete tmpIt;
368  return NULL;
369}
370
371/**
372 * @param number the n-th emitter to return
373 * @returns the emitter called by number or NULL if not found
374*/
375ParticleEmitter* ParticleEngine::getEmitterByNumber(unsigned int number) const
376{
377  int count = 0;
378  tIterator<ParticleEmitter>* tmpIt = emitterList->getIterator();
379  ParticleEmitter* tmpEmit = tmpIt->firstElement();
380  while(tmpEmit)
381    {
382      count++;
383      if ( count == number)
384        {
385          delete tmpIt;
386          return tmpEmit;
387        }
388      tmpEmit = tmpIt->nextElement();
389    }
390  delete tmpIt;
391  return NULL;
392}
393
394/**
395 *  outputs some nice debug information
396*/
397void ParticleEngine::debug()
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",
404  this->emitterList->getSize(), this->systemList->getSize(), this->connectionList->getSize());
405
406  if (this->connectionList->getSize() > 0)
407  {
408    PRINT(0)(" Connections:\n");
409    PRINT(0)(" -----------------------------------\n");
410
411    tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator();
412    ParticleConnection* tmpConnection = tmpConIt->firstElement();
413    while(tmpConnection)
414    {
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  }
420
421  if (this->systemList->getSize() > 0)
422  {
423    tIterator<ParticleSystem>* tmpIt = systemList->getIterator();
424    ParticleSystem* tmpSys = tmpIt->firstElement();
425    while(tmpSys)
426    {
427      tmpSys->debug();
428      tmpSys = tmpIt->nextElement();
429    }
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();
439    ParticleEmitter* tmpEmit = tmpIt->firstElement();
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  }
451
452  PRINT(0)("+--------------------------------PE-+\n");
453
454}
455
Note: See TracBrowser for help on using the repository browser.