changed src so the libpg headers are now used
[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"
f32a9b7c
PG
19
20#include <pg/debug.h>
21#include <pg/Mutex.h>
22#include <pg/Autolock.h>
a483ed75 23
33b8c69b 24#include <set>
5829cb66 25#include <SDL/SDL.h>
33b8c69b 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
b6ef7e06
PG
36static void updateParticles(float);
37static void updatePhysics(float);
38static void addOrRemoveParticles();
39static void addOrRemovePhysics();
ad9f1fb6
PG
40
41/// ***** Private Variables *****
617dcc71 42
f72f4a59 43typedef std::set<Particle*> setPart;
b6ef7e06
PG
44static setPart particles_To_Add;
45static setPart active_Particles;
46static setPart particles_To_Remove;
ad9f1fb6 47
f72f4a59 48typedef std::set<PhysicsEntity*> setPhys;
b6ef7e06
PG
49static setPhys physics_To_Add;
50static setPhys active_Physics;
51static setPhys physics_To_Remove;
ad9f1fb6 52
b6ef7e06
PG
53static Mutex muSetPart;
54static Mutex muSetPhys;
5829cb66 55
617dcc71 56/// ***** Initializers/Cleaners *****
ad9f1fb6 57
d388f0ec 58void manager::init()
ad9f1fb6 59{
b6ef7e06
PG
60 muSetPart.init();
61 muSetPhys.init();
5829cb66 62
54fe85c5 63 collision::init();
ad9f1fb6 64}
d388f0ec 65void manager::clean()
ad9f1fb6 66{
54fe85c5 67 collision::clean();
5829cb66 68
b6ef7e06
PG
69 muSetPhys.clean();
70 muSetPart.clean();
ad9f1fb6
PG
71}
72
617dcc71
PG
73/// ***** Public Methods *****
74
b6ef7e06 75void manager::add(Entity* pe)
ad9f1fb6 76{
b6ef7e06
PG
77 DASSERT(pe != NULL);
78
ad9f1fb6 79 {
b6ef7e06
PG
80 Particle* pp = dynamic_cast<Particle*>(pe);
81 if( pp != NULL )
d388f0ec 82 {
b6ef7e06 83 particles_To_Add.insert(pp);
d388f0ec
PG
84 return;
85 }
ad9f1fb6
PG
86 }
87
88 {
b6ef7e06
PG
89 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
90 if( ppe != NULL )
d388f0ec 91 {
b6ef7e06 92 physics_To_Add.insert(ppe);
d388f0ec
PG
93 return;
94 }
ad9f1fb6
PG
95 }
96
ca2d526e 97 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
ad9f1fb6 98}
b6ef7e06 99void manager::remove(Entity* pe)
ad9f1fb6 100{
b6ef7e06
PG
101 DASSERT(pe != NULL);
102
ad9f1fb6 103 {
b6ef7e06
PG
104 Autolock lock( muSetPart );
105 Particle* p = dynamic_cast<Particle*>(pe);
106 if( p != NULL )
d388f0ec
PG
107 {
108 particles_To_Remove.insert(p);
109 return;
110 }
ad9f1fb6
PG
111 }
112
113 {
b6ef7e06
PG
114 Autolock lock( muSetPhys );
115 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
116 if( ppe != NULL )
d388f0ec 117 {
b6ef7e06 118 physics_To_Remove.insert(ppe);
d388f0ec
PG
119 return;
120 }
ad9f1fb6
PG
121 }
122
ca2d526e 123 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
ad9f1fb6
PG
124}
125
d388f0ec 126void manager::handleInput()
ad9f1fb6 127{
a823a800 128 effect::handleInput();
ad9f1fb6 129}
d388f0ec 130void manager::update(float time_step)
ad9f1fb6 131{
a823a800
PG
132 effect::update(time_step);
133
ad9f1fb6
PG
134 updateParticles(time_step);
135 updatePhysics(time_step);
136}
d388f0ec 137void manager::draw()
ad9f1fb6 138{
3bccd1d7 139 {
b6ef7e06 140 Autolock lock( muSetPart );
3bccd1d7 141
b6ef7e06
PG
142 addOrRemoveParticles();
143
144 // draw active Particle*s
5829cb66
PG
145 for( setPart::iterator it = active_Particles.begin();
146 it != active_Particles.end();
147 it++ )
148 {
149 (*it)->draw();
150 }
3bccd1d7
PG
151 }
152
153 {
b6ef7e06
PG
154 Autolock lock( muSetPhys );
155
156 addOrRemovePhysics();
3bccd1d7 157
b6ef7e06 158 // draw active PhysicsEntity*s
5829cb66
PG
159 for( setPhys::iterator it = active_Physics.begin();
160 it != active_Physics.end();
161 it++ )
162 {
163 (*it)->draw();
164 }
3bccd1d7 165 }
ad9f1fb6
PG
166}
167
168/// ***** Private Methods *****
617dcc71 169
ad9f1fb6
PG
170void updateParticles(float time_step)
171{
b6ef7e06 172 addOrRemoveParticles();
ad9f1fb6
PG
173
174 // update active Particle*s
175 for( setPart::iterator it = active_Particles.begin();
176 it != active_Particles.end();
177 it++ )
178 {
179 (*it)->update(time_step);
180 }
181}
182void updatePhysics(float time_step)
183{
b6ef7e06 184 addOrRemovePhysics();
ad9f1fb6 185
54fe85c5 186 // apply collision math
2869e2e8 187 collision::update(active_Physics);
54fe85c5 188
ad9f1fb6
PG
189 // update active PhysicsEntity*s
190 for( setPhys::iterator it = active_Physics.begin();
191 it != active_Physics.end();
192 it++ )
193 {
194 (*it)->update(time_step);
195 }
196}
b6ef7e06 197void addOrRemoveParticles()
f342ff31 198{
b6ef7e06 199 Autolock lock( muSetPart );
f342ff31
PG
200
201 // add new Particle*s to Active
202 for( setPart::iterator it = particles_To_Add.begin();
203 it != particles_To_Add.end();
204 it++ )
205 {
f342ff31
PG
206 active_Particles.insert(*it);
207 }
208 particles_To_Add.clear();
209
210 // remove dead Particle*s from Active
211 for( setPart::iterator it = particles_To_Remove.begin();
212 it != particles_To_Remove.end();
213 it++ )
214 {
f342ff31
PG
215 active_Particles.erase(*it);
216 }
217 particles_To_Remove.clear();
218}
b6ef7e06 219void addOrRemovePhysics()
f342ff31 220{
b6ef7e06 221 Autolock lock( muSetPhys );
f342ff31
PG
222
223 // add new PhysicsEntity*s to Active
224 for( setPhys::iterator it = physics_To_Add.begin();
225 it != physics_To_Add.end();
226 it++ )
227 {
f342ff31
PG
228 active_Physics.insert(*it);
229 }
230 physics_To_Add.clear();
231
232 // remove dead PhysicsEntity*s from Active
233 for( setPhys::iterator it = physics_To_Remove.begin();
234 it != physics_To_Remove.end();
235 it++ )
236 {
f342ff31
PG
237 active_Physics.erase(*it);
238 }
239 physics_To_Remove.clear();
240}