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