changed src so the libpg headers are now used
[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
20#include <pg/debug.h>
21#include <pg/Mutex.h>
22#include <pg/Autolock.h>
23
24#include <set>
25#include <SDL/SDL.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
36static void updateParticles(float);
37static void updatePhysics(float);
38static void addOrRemoveParticles();
39static void addOrRemovePhysics();
40
41/// ***** Private Variables *****
42
43typedef std::set<Particle*> setPart;
44static setPart particles_To_Add;
45static setPart active_Particles;
46static setPart particles_To_Remove;
47
48typedef std::set<PhysicsEntity*> setPhys;
49static setPhys physics_To_Add;
50static setPhys active_Physics;
51static setPhys physics_To_Remove;
52
53static Mutex muSetPart;
54static Mutex muSetPhys;
55
56/// ***** Initializers/Cleaners *****
57
58void manager::init()
59{
60 muSetPart.init();
61 muSetPhys.init();
62
63 collision::init();
64}
65void manager::clean()
66{
67 collision::clean();
68
69 muSetPhys.clean();
70 muSetPart.clean();
71}
72
73/// ***** Public Methods *****
74
75void manager::add(Entity* pe)
76{
77 DASSERT(pe != NULL);
78
79 {
80 Particle* pp = dynamic_cast<Particle*>(pe);
81 if( pp != NULL )
82 {
83 particles_To_Add.insert(pp);
84 return;
85 }
86 }
87
88 {
89 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
90 if( ppe != NULL )
91 {
92 physics_To_Add.insert(ppe);
93 return;
94 }
95 }
96
97 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
98}
99void manager::remove(Entity* pe)
100{
101 DASSERT(pe != NULL);
102
103 {
104 Autolock lock( muSetPart );
105 Particle* p = dynamic_cast<Particle*>(pe);
106 if( p != NULL )
107 {
108 particles_To_Remove.insert(p);
109 return;
110 }
111 }
112
113 {
114 Autolock lock( muSetPhys );
115 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
116 if( ppe != NULL )
117 {
118 physics_To_Remove.insert(ppe);
119 return;
120 }
121 }
122
123 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
124}
125
126void manager::handleInput()
127{
128 effect::handleInput();
129}
130void manager::update(float time_step)
131{
132 effect::update(time_step);
133
134 updateParticles(time_step);
135 updatePhysics(time_step);
136}
137void manager::draw()
138{
139 {
140 Autolock lock( muSetPart );
141
142 addOrRemoveParticles();
143
144 // draw active Particle*s
145 for( setPart::iterator it = active_Particles.begin();
146 it != active_Particles.end();
147 it++ )
148 {
149 (*it)->draw();
150 }
151 }
152
153 {
154 Autolock lock( muSetPhys );
155
156 addOrRemovePhysics();
157
158 // draw active PhysicsEntity*s
159 for( setPhys::iterator it = active_Physics.begin();
160 it != active_Physics.end();
161 it++ )
162 {
163 (*it)->draw();
164 }
165 }
166}
167
168/// ***** Private Methods *****
169
170void updateParticles(float time_step)
171{
172 addOrRemoveParticles();
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{
184 addOrRemovePhysics();
185
186 // apply collision math
187 collision::update(active_Physics);
188
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}
197void addOrRemoveParticles()
198{
199 Autolock lock( muSetPart );
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 {
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 {
215 active_Particles.erase(*it);
216 }
217 particles_To_Remove.clear();
218}
219void addOrRemovePhysics()
220{
221 Autolock lock( muSetPhys );
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 {
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 {
237 active_Physics.erase(*it);
238 }
239 physics_To_Remove.clear();
240}