created locks abstraction
[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
5829cb66
PG
135 // update active Particle*s
136 for( setPart::iterator it = active_Particles.begin();
137 it != active_Particles.end();
138 it++ )
139 {
140 (*it)->draw();
141 }
ad9f1fb6 142
3bccd1d7
PG
143 DPF(0, "Particle Draw End");
144 }
145
146 {
147 Autolock lock( physicsEntitySetLock );
148 DPF(0, "Physics Draw Start");
149
5829cb66
PG
150 // update active PhysicsEntity*s
151 for( setPhys::iterator it = active_Physics.begin();
152 it != active_Physics.end();
153 it++ )
154 {
155 (*it)->draw();
156 }
3bccd1d7
PG
157
158 DPF(0, "Physics Draw End");
159 }
ad9f1fb6
PG
160}
161
162/// ***** Private Methods *****
617dcc71 163
ad9f1fb6
PG
164void updateParticles(float time_step)
165{
3bccd1d7
PG
166 {
167 Autolock lock( particleSetLock );
168
5829cb66
PG
169 // add new Particle*s to Active
170 for( setPart::iterator it = particles_To_Add.begin();
171 it != particles_To_Add.end();
172 it++ )
173 {
3bccd1d7 174 DPF(0, "Particle Insert");
5829cb66
PG
175 active_Particles.insert(*it);
176 }
ad9f1fb6 177
5829cb66
PG
178 // remove dead Particle*s from Active
179 for( setPart::iterator it = particles_To_Remove.begin();
180 it != particles_To_Remove.end();
181 it++ )
182 {
3bccd1d7 183 DPF(0, "Particle Delete");
5829cb66
PG
184 active_Particles.erase(*it);
185 }
3bccd1d7
PG
186 }
187
188 particles_To_Add.clear();
189 particles_To_Remove.clear();
ad9f1fb6
PG
190
191 // update active Particle*s
192 for( setPart::iterator it = active_Particles.begin();
193 it != active_Particles.end();
194 it++ )
195 {
196 (*it)->update(time_step);
197 }
198}
199void updatePhysics(float time_step)
200{
3bccd1d7
PG
201 {
202 Autolock lock( physicsEntitySetLock );
203
5829cb66
PG
204 // add new PhysicsEntity*s to Active
205 for( setPhys::iterator it = physics_To_Add.begin();
206 it != physics_To_Add.end();
207 it++ )
208 {
3bccd1d7 209 DPF(0, "Physics Insert");
5829cb66
PG
210 active_Physics.insert(*it);
211 }
ad9f1fb6 212
5829cb66
PG
213 // remove dead PhysicsEntity*s from Active
214 for( setPhys::iterator it = physics_To_Remove.begin();
215 it != physics_To_Remove.end();
216 it++ )
217 {
3bccd1d7 218 DPF(0, "Physics Delete");
5829cb66
PG
219 active_Physics.erase(*it);
220 }
3bccd1d7
PG
221 }
222
223 physics_To_Remove.clear();
224 physics_To_Add.clear();
ad9f1fb6 225
54fe85c5 226 // apply collision math
2869e2e8 227 collision::update(active_Physics);
54fe85c5 228
ad9f1fb6
PG
229 // update active PhysicsEntity*s
230 for( setPhys::iterator it = active_Physics.begin();
231 it != active_Physics.end();
232 it++ )
233 {
234 (*it)->update(time_step);
235 }
236}