2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
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.
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.
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/>.
18 #include "entityManager.h"
24 #include "Entities/Entity.h"
25 #include "Entities/Particle.h"
26 #include "Entities/PhysicsEntity.h"
28 #include "collisionManager.h"
29 #include "effectManager.h"
31 /// ***** Private Method Headers *****
33 void updateParticles(float);
34 void updatePhysics(float);
36 /// ***** Private Variables *****
38 typedef std::set<Particle*> setPart;
39 setPart particles_To_Add;
40 setPart active_Particles;
41 setPart particles_To_Remove;
43 typedef std::set<PhysicsEntity*> setPhys;
44 setPhys physics_To_Add;
45 setPhys active_Physics;
46 setPhys physics_To_Remove;
48 SDL_mutex* particleSetLock = NULL;
49 SDL_mutex* physicsEntitySetLock = NULL;
51 /// ***** Initializers/Cleaners *****
55 particleSetLock = SDL_CreateMutex();
56 physicsEntitySetLock = SDL_CreateMutex();
64 SDL_DestroyMutex(particleSetLock);
65 particleSetLock = NULL;
67 SDL_DestroyMutex(physicsEntitySetLock);
68 physicsEntitySetLock = NULL;
71 /// ***** Public Methods *****
73 void manager::add(Entity* e)
76 Particle* p = dynamic_cast<Particle*>(e);
79 particles_To_Add.insert(p);
85 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
88 physics_To_Add.insert(p);
93 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
95 void manager::remove(Entity* e)
98 Particle* p = dynamic_cast<Particle*>(e);
101 particles_To_Remove.insert(p);
107 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
110 physics_To_Remove.insert(p);
115 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
118 void manager::handleInput()
120 effect::handleInput();
122 void manager::update(float time_step)
124 effect::update(time_step);
126 updateParticles(time_step);
127 updatePhysics(time_step);
131 SDL_mutexP( particleSetLock );
132 // update active Particle*s
133 for( setPart::iterator it = active_Particles.begin();
134 it != active_Particles.end();
139 SDL_mutexV( particleSetLock );
141 SDL_mutexP( physicsEntitySetLock );
142 // update active PhysicsEntity*s
143 for( setPhys::iterator it = active_Physics.begin();
144 it != active_Physics.end();
149 SDL_mutexV( physicsEntitySetLock );
152 /// ***** Private Methods *****
154 void updateParticles(float time_step)
156 SDL_mutexP( particleSetLock );
157 // add new Particle*s to Active
158 for( setPart::iterator it = particles_To_Add.begin();
159 it != particles_To_Add.end();
162 active_Particles.insert(*it);
164 particles_To_Add.clear();
166 // remove dead Particle*s from Active
167 for( setPart::iterator it = particles_To_Remove.begin();
168 it != particles_To_Remove.end();
171 active_Particles.erase(*it);
173 particles_To_Remove.clear();
174 SDL_mutexV( particleSetLock );
176 // update active Particle*s
177 for( setPart::iterator it = active_Particles.begin();
178 it != active_Particles.end();
181 (*it)->update(time_step);
184 void updatePhysics(float time_step)
186 SDL_mutexP( physicsEntitySetLock );
187 // add new PhysicsEntity*s to Active
188 for( setPhys::iterator it = physics_To_Add.begin();
189 it != physics_To_Add.end();
192 active_Physics.insert(*it);
194 physics_To_Add.clear();
196 // remove dead PhysicsEntity*s from Active
197 for( setPhys::iterator it = physics_To_Remove.begin();
198 it != physics_To_Remove.end();
201 active_Physics.erase(*it);
203 physics_To_Remove.clear();
204 SDL_mutexV( physicsEntitySetLock );
206 // apply collision math
207 collision::update(active_Physics);
209 // update active PhysicsEntity*s
210 for( setPhys::iterator it = active_Physics.begin();
211 it != active_Physics.end();
214 (*it)->update(time_step);