added better thread safty ... still broke
[physics.git] / src / entityManager.cpp
CommitLineData
e68f847b
PG
1/*
2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
054d658f 18#include "entityManager.h"
a483ed75
PG
19#include "debug.h"
20
33b8c69b 21#include <set>
5829cb66 22#include <SDL/SDL.h>
33b8c69b 23
3bccd1d7
PG
24#include "locks/Mutex.h"
25#include "locks/Autolock.h"
26
054d658f
PG
27#include "Entities/Entity.h"
28#include "Entities/Particle.h"
29#include "Entities/PhysicsEntity.h"
30
0ac1dc80 31#include "collisionManager.h"
a823a800 32#include "effectManager.h"
54fe85c5 33
ad9f1fb6 34/// ***** Private Method Headers *****
617dcc71 35
ad9f1fb6
PG
36void updateParticles(float);
37void updatePhysics(float);
38
39/// ***** Private Variables *****
617dcc71 40
f72f4a59 41typedef std::set<Particle*> setPart;
ad9f1fb6
PG
42setPart particles_To_Add;
43setPart active_Particles;
44setPart particles_To_Remove;
ad9f1fb6 45
f72f4a59 46typedef std::set<PhysicsEntity*> setPhys;
ad9f1fb6
PG
47setPhys physics_To_Add;
48setPhys active_Physics;
49setPhys physics_To_Remove;
ad9f1fb6 50
3bccd1d7
PG
51Mutex particleSetLock;
52Mutex physicsEntitySetLock;
5829cb66 53
617dcc71 54/// ***** Initializers/Cleaners *****
ad9f1fb6 55
d388f0ec 56void manager::init()
ad9f1fb6 57{
3bccd1d7
PG
58 particleSetLock.init();
59 physicsEntitySetLock.init();
5829cb66 60
54fe85c5 61 collision::init();
ad9f1fb6 62}
d388f0ec 63void manager::clean()
ad9f1fb6 64{
54fe85c5 65 collision::clean();
5829cb66 66
3bccd1d7
PG
67 physicsEntitySetLock.clean();
68 particleSetLock.clean();
ad9f1fb6
PG
69}
70
617dcc71
PG
71/// ***** Public Methods *****
72
d388f0ec 73void manager::add(Entity* e)
ad9f1fb6
PG
74{
75 {
d388f0ec
PG
76 Particle* p = dynamic_cast<Particle*>(e);
77 if( p != 0 )
78 {
79 particles_To_Add.insert(p);
80 return;
81 }
ad9f1fb6
PG
82 }
83
84 {
d388f0ec
PG
85 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
86 if( p != 0 )
87 {
88 physics_To_Add.insert(p);
89 return;
90 }
ad9f1fb6
PG
91 }
92
ca2d526e 93 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
ad9f1fb6 94}
d388f0ec 95void manager::remove(Entity* e)
ad9f1fb6
PG
96{
97 {
d388f0ec
PG
98 Particle* p = dynamic_cast<Particle*>(e);
99 if( p != 0 )
100 {
101 particles_To_Remove.insert(p);
102 return;
103 }
ad9f1fb6
PG
104 }
105
106 {
d388f0ec
PG
107 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
108 if( p != 0 )
109 {
110 physics_To_Remove.insert(p);
111 return;
112 }
ad9f1fb6
PG
113 }
114
ca2d526e 115 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
ad9f1fb6
PG
116}
117
d388f0ec 118void manager::handleInput()
ad9f1fb6 119{
a823a800 120 effect::handleInput();
ad9f1fb6 121}
d388f0ec 122void manager::update(float time_step)
ad9f1fb6 123{
a823a800
PG
124 effect::update(time_step);
125
ad9f1fb6
PG
126 updateParticles(time_step);
127 updatePhysics(time_step);
128}
d388f0ec 129void manager::draw()
ad9f1fb6 130{
3bccd1d7
PG
131 {
132 Autolock lock( particleSetLock );
133 DPF(0, "Particle Draw Start");
134
f463ae40
PG
135 // remove dead Particle*s from Active
136 for( setPart::iterator it = particles_To_Remove.begin();
137 it != particles_To_Remove.end();
138 it++ )
139 {
140 DPF(0, "Particle Delete");
141 active_Particles.erase(*it);
142 }
143 particles_To_Remove.clear();
144
5829cb66
PG
145 // update active Particle*s
146 for( setPart::iterator it = active_Particles.begin();
147 it != active_Particles.end();
148 it++ )
149 {
150 (*it)->draw();
151 }
ad9f1fb6 152
3bccd1d7
PG
153 DPF(0, "Particle Draw End");
154 }
155
156 {
157 Autolock lock( physicsEntitySetLock );
158 DPF(0, "Physics Draw Start");
159
f463ae40
PG
160 // remove dead PhysicsEntity*s from Active
161 for( setPhys::iterator it = physics_To_Remove.begin();
162 it != physics_To_Remove.end();
163 it++ )
164 {
165 DPF(0, "Physics Delete");
166 active_Physics.erase(*it);
167 }
168 physics_To_Remove.clear();
169
5829cb66
PG
170 // update active PhysicsEntity*s
171 for( setPhys::iterator it = active_Physics.begin();
172 it != active_Physics.end();
173 it++ )
174 {
175 (*it)->draw();
176 }
3bccd1d7
PG
177
178 DPF(0, "Physics Draw End");
179 }
ad9f1fb6
PG
180}
181
182/// ***** Private Methods *****
617dcc71 183
ad9f1fb6
PG
184void updateParticles(float time_step)
185{
3bccd1d7
PG
186 {
187 Autolock lock( particleSetLock );
188
5829cb66
PG
189 // add new Particle*s to Active
190 for( setPart::iterator it = particles_To_Add.begin();
191 it != particles_To_Add.end();
192 it++ )
193 {
3bccd1d7 194 DPF(0, "Particle Insert");
5829cb66
PG
195 active_Particles.insert(*it);
196 }
ad9f1fb6 197
5829cb66
PG
198 // remove dead Particle*s from Active
199 for( setPart::iterator it = particles_To_Remove.begin();
200 it != particles_To_Remove.end();
201 it++ )
202 {
3bccd1d7 203 DPF(0, "Particle Delete");
5829cb66
PG
204 active_Particles.erase(*it);
205 }
f463ae40 206 particles_To_Remove.clear();
3bccd1d7
PG
207 }
208
209 particles_To_Add.clear();
ad9f1fb6
PG
210
211 // update active Particle*s
212 for( setPart::iterator it = active_Particles.begin();
213 it != active_Particles.end();
214 it++ )
215 {
216 (*it)->update(time_step);
217 }
218}
219void updatePhysics(float time_step)
220{
3bccd1d7
PG
221 {
222 Autolock lock( physicsEntitySetLock );
223
5829cb66
PG
224 // add new PhysicsEntity*s to Active
225 for( setPhys::iterator it = physics_To_Add.begin();
226 it != physics_To_Add.end();
227 it++ )
228 {
3bccd1d7 229 DPF(0, "Physics Insert");
5829cb66
PG
230 active_Physics.insert(*it);
231 }
ad9f1fb6 232
5829cb66
PG
233 // remove dead PhysicsEntity*s from Active
234 for( setPhys::iterator it = physics_To_Remove.begin();
235 it != physics_To_Remove.end();
236 it++ )
237 {
3bccd1d7 238 DPF(0, "Physics Delete");
5829cb66
PG
239 active_Physics.erase(*it);
240 }
f463ae40 241 physics_To_Remove.clear();
3bccd1d7
PG
242 }
243
3bccd1d7 244 physics_To_Add.clear();
ad9f1fb6 245
54fe85c5 246 // apply collision math
2869e2e8 247 collision::update(active_Physics);
54fe85c5 248
ad9f1fb6
PG
249 // update active PhysicsEntity*s
250 for( setPhys::iterator it = active_Physics.begin();
251 it != active_Physics.end();
252 it++ )
253 {
254 (*it)->update(time_step);
255 }
256}