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_PARTICLE |
---|
17 | |
---|
18 | #include "particle_engine.h" |
---|
19 | |
---|
20 | #include "particle_system.h" |
---|
21 | #include "particle_emitter.h" |
---|
22 | |
---|
23 | #include "list.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | /** |
---|
28 | \brief standard constructor |
---|
29 | */ |
---|
30 | ParticleEngine::ParticleEngine () |
---|
31 | { |
---|
32 | this->setClassName ("ParticleEngine"); |
---|
33 | |
---|
34 | this->systemList = new tList<ParticleSystem>; |
---|
35 | this->emitterList = new tList<ParticleEmitter>; |
---|
36 | this->connectionList = new tList<ParticleConnection>; |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | \brief the singleton reference to this class |
---|
41 | */ |
---|
42 | ParticleEngine* ParticleEngine::singletonRef = NULL; |
---|
43 | |
---|
44 | /** |
---|
45 | \returns a Pointer to this Class |
---|
46 | */ |
---|
47 | ParticleEngine* ParticleEngine::getInstance(void) |
---|
48 | { |
---|
49 | if (!ParticleEngine::singletonRef) |
---|
50 | ParticleEngine::singletonRef = new ParticleEngine(); |
---|
51 | return ParticleEngine::singletonRef; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | \brief deletes all the system, emitters, connections and Lists |
---|
56 | */ |
---|
57 | ParticleEngine::~ParticleEngine () |
---|
58 | { |
---|
59 | // delete all remaining systems |
---|
60 | tIterator<ParticleSystem>* sysIt = this->systemList->getIterator(); |
---|
61 | ParticleSystem* tmpSys = sysIt->nextElement(); |
---|
62 | while(tmpSys) |
---|
63 | { |
---|
64 | delete tmpSys; |
---|
65 | tmpSys = sysIt->nextElement(); |
---|
66 | } |
---|
67 | delete sysIt; |
---|
68 | delete this->systemList; |
---|
69 | |
---|
70 | // delete all remaining emitters |
---|
71 | tIterator<ParticleEmitter>* emitIt = this->emitterList->getIterator(); |
---|
72 | ParticleEmitter* tmpEmit = emitIt->nextElement(); |
---|
73 | while(tmpEmit) |
---|
74 | { |
---|
75 | delete tmpEmit; |
---|
76 | tmpEmit = emitIt->nextElement(); |
---|
77 | } |
---|
78 | delete emitIt; |
---|
79 | delete this->emitterList; |
---|
80 | |
---|
81 | // there should be no more Connections |
---|
82 | if (this->connectionList->getSize() == 0) |
---|
83 | delete this->connectionList; |
---|
84 | else |
---|
85 | PRINTF(2)("The Connection List is not empty. This should not happen.\n"); |
---|
86 | |
---|
87 | ParticleEngine::singletonRef = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | \brief Adds a System to the System list. |
---|
92 | |
---|
93 | this is done automatically when creating a ParticleSystem |
---|
94 | */ |
---|
95 | void ParticleEngine::addSystem(ParticleSystem* system) |
---|
96 | { |
---|
97 | this->systemList->add(system); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | \brief Adds an emitter to the emitterList |
---|
102 | |
---|
103 | this is done automatically when creating a ParticleEmitter |
---|
104 | */ |
---|
105 | void ParticleEngine::addEmitter(ParticleEmitter* emitter) |
---|
106 | { |
---|
107 | this->emitterList->add(emitter); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | \brief Connects a ParticleSystem to a ParticleSystem thus emitting Particles. |
---|
112 | \param emitter the Emitter to connect to the System |
---|
113 | \param system the System to connect to the Emitter |
---|
114 | */ |
---|
115 | void ParticleEngine::addConnection(ParticleEmitter* emitter, ParticleSystem* system) |
---|
116 | { |
---|
117 | // look, if we have already added this connection |
---|
118 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
119 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
120 | while(tmpConnection) |
---|
121 | { |
---|
122 | if (tmpConnection->emitter == emitter && tmpConnection->system == system) |
---|
123 | { |
---|
124 | PRINTF(2)("Connection between Emitter and System already added\n"); |
---|
125 | delete tmpConIt; |
---|
126 | return; |
---|
127 | } |
---|
128 | |
---|
129 | tmpConnection = tmpConIt->nextElement(); |
---|
130 | } |
---|
131 | delete tmpConIt; |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | ParticleConnection* tmpCon = new ParticleConnection; |
---|
136 | tmpCon->emitter = emitter; |
---|
137 | tmpCon->system = system; |
---|
138 | |
---|
139 | this->connectionList->add(tmpCon); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief Removes a system from the systemList and also removes all Connections to the System |
---|
144 | \param system The ParticleSystem to delete |
---|
145 | */ |
---|
146 | bool ParticleEngine::removeSystem(ParticleSystem* system) |
---|
147 | { |
---|
148 | // remove any connections, that have this system within |
---|
149 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
150 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
151 | while(tmpConnection) |
---|
152 | { |
---|
153 | if (tmpConnection->system == system) |
---|
154 | this->breakConnection(tmpConnection); |
---|
155 | tmpConnection = tmpConIt->nextElement(); |
---|
156 | } |
---|
157 | delete tmpConIt; |
---|
158 | |
---|
159 | // remove the System from the systemList. |
---|
160 | this->systemList->remove(system); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | \brief removes an emitter from the emitterList and also from all Connections it is attached to. |
---|
165 | \param emitter the ParticleEmitter to remove. |
---|
166 | */ |
---|
167 | bool ParticleEngine::removeEmitter(ParticleEmitter* emitter) |
---|
168 | { |
---|
169 | // remove any connections, that have this emitter within |
---|
170 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
171 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
172 | while(tmpConnection) |
---|
173 | { |
---|
174 | if (tmpConnection->emitter == emitter) |
---|
175 | this->breakConnection(tmpConnection); |
---|
176 | tmpConnection = tmpConIt->nextElement(); |
---|
177 | } |
---|
178 | delete tmpConIt; |
---|
179 | |
---|
180 | // remove the emitter from the emitterList |
---|
181 | this->emitterList->remove(emitter); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | \brief removes a Connection between an Emitter and a System |
---|
186 | \param emitter The emitter of the connection to remove |
---|
187 | \param system The system of the connection to remove |
---|
188 | \returns true, if the connection was broken, false if the conntection was not found |
---|
189 | |
---|
190 | only if both system and emitter are in the connection the Connection will be broken |
---|
191 | */ |
---|
192 | bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system) |
---|
193 | { |
---|
194 | // look, if we have already added this connection |
---|
195 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
196 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
197 | while(tmpConnection) |
---|
198 | { |
---|
199 | if (tmpConnection->emitter == emitter && tmpConnection->system == system) |
---|
200 | { |
---|
201 | this->breakConnection(tmpConnection); |
---|
202 | delete tmpConIt; |
---|
203 | return true; |
---|
204 | } |
---|
205 | tmpConnection = tmpConIt->nextElement(); |
---|
206 | } |
---|
207 | delete tmpConIt; |
---|
208 | return false; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | \brief removes a Connection between an Emitter and a System |
---|
213 | \param connection the connection to remove |
---|
214 | |
---|
215 | \see bool ParticleEngine::breakConnection(ParticleEmitter* emitter, ParticleSystem* system) |
---|
216 | */ |
---|
217 | bool ParticleEngine::breakConnection(ParticleConnection* connection) |
---|
218 | { |
---|
219 | this->connectionList->remove(connection); |
---|
220 | return true; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | \brief this function ticks all the ParticleSystems, so an animation will flow |
---|
226 | \param dt passed since last tick |
---|
227 | */ |
---|
228 | void ParticleEngine::tick(float dt) |
---|
229 | { |
---|
230 | // ticks all the ParticleSystems |
---|
231 | tIterator<ParticleSystem>* tmpIt = systemList->getIterator(); |
---|
232 | ParticleSystem* tmpSys = tmpIt->nextElement(); |
---|
233 | while(tmpSys) |
---|
234 | { |
---|
235 | tmpSys->tick(dt); |
---|
236 | tmpSys = tmpIt->nextElement(); |
---|
237 | } |
---|
238 | delete tmpIt; |
---|
239 | |
---|
240 | // add new Particles to each System connected to an Emitter. |
---|
241 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
242 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
243 | while(tmpConnection) |
---|
244 | { |
---|
245 | tmpConnection->emitter->tick(dt, tmpConnection->system); |
---|
246 | tmpConnection = tmpConIt->nextElement(); |
---|
247 | } |
---|
248 | delete tmpConIt; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | \brief draws all the systems and their Particles. |
---|
253 | \param dt the time passed in seconds (since the last Frame) |
---|
254 | */ |
---|
255 | void ParticleEngine::draw(float dt) |
---|
256 | { |
---|
257 | tIterator<ParticleSystem>* tmpIt = systemList->getIterator(); |
---|
258 | ParticleSystem* tmpSys = tmpIt->nextElement(); |
---|
259 | while(tmpSys) |
---|
260 | { |
---|
261 | tmpSys->draw(dt); |
---|
262 | tmpSys = tmpIt->nextElement(); |
---|
263 | } |
---|
264 | delete tmpIt; |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | \brief outputs some nice debug information |
---|
270 | */ |
---|
271 | void ParticleEngine::debug(void) |
---|
272 | { |
---|
273 | PRINT(0)("+-----------------------------------+\n"); |
---|
274 | PRINT(0)("+ PARTICLE-ENGINE DEBUG INFORMATION +\n"); |
---|
275 | PRINT(0)("+-----------------------------------+\n"); |
---|
276 | PRINT(0)(" Reference: %p\n", ParticleEngine::singletonRef); |
---|
277 | PRINT(0)(" Count: Emitters: %d; Systems: %d, Connections: %d\n", |
---|
278 | this->emitterList->getSize(), this->systemList->getSize(), this->connectionList->getSize()); |
---|
279 | if (this->connectionList->getSize() > 0) |
---|
280 | { |
---|
281 | PRINT(0)(" Connections:\n"); |
---|
282 | PRINT(0)(" -----------------------------------\n"); |
---|
283 | |
---|
284 | tIterator<ParticleConnection>* tmpConIt = connectionList->getIterator(); |
---|
285 | ParticleConnection* tmpConnection = tmpConIt->nextElement(); |
---|
286 | while(tmpConnection) |
---|
287 | { |
---|
288 | PRINT(0)(" Emitter '%s' emitts into System '%s'\n", tmpConnection->emitter->getName(), tmpConnection->system->getName()); |
---|
289 | tmpConnection = tmpConIt->nextElement(); |
---|
290 | } |
---|
291 | delete tmpConIt; |
---|
292 | } |
---|
293 | if (this->systemList->getSize() > 0) |
---|
294 | { |
---|
295 | tIterator<ParticleSystem>* tmpIt = systemList->getIterator(); |
---|
296 | ParticleSystem* tmpSys = tmpIt->nextElement(); |
---|
297 | while(tmpSys) |
---|
298 | { |
---|
299 | tmpSys->debug(); |
---|
300 | tmpSys = tmpIt->nextElement(); |
---|
301 | } |
---|
302 | delete tmpIt; |
---|
303 | } |
---|
304 | |
---|
305 | PRINT(0)("+--------------------------------PE-+\n"); |
---|
306 | |
---|
307 | } |
---|