wip: more thread safety
[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
054d658f
PG
24#include "Entities/Entity.h"
25#include "Entities/Particle.h"
26#include "Entities/PhysicsEntity.h"
27
0ac1dc80 28#include "collisionManager.h"
a823a800 29#include "effectManager.h"
54fe85c5 30
ad9f1fb6 31/// ***** Private Method Headers *****
617dcc71 32
ad9f1fb6
PG
33void updateParticles(float);
34void updatePhysics(float);
35
36/// ***** Private Variables *****
617dcc71 37
f72f4a59 38typedef std::set<Particle*> setPart;
ad9f1fb6
PG
39setPart particles_To_Add;
40setPart active_Particles;
41setPart particles_To_Remove;
ad9f1fb6 42
f72f4a59 43typedef std::set<PhysicsEntity*> setPhys;
ad9f1fb6
PG
44setPhys physics_To_Add;
45setPhys active_Physics;
46setPhys physics_To_Remove;
ad9f1fb6 47
5829cb66
PG
48SDL_mutex* particleSetLock = NULL;
49SDL_mutex* physicsEntitySetLock = NULL;
50
617dcc71 51/// ***** Initializers/Cleaners *****
ad9f1fb6 52
d388f0ec 53void manager::init()
ad9f1fb6 54{
5829cb66
PG
55 particleSetLock = SDL_CreateMutex();
56 physicsEntitySetLock = SDL_CreateMutex();
57
54fe85c5 58 collision::init();
ad9f1fb6 59}
d388f0ec 60void manager::clean()
ad9f1fb6 61{
54fe85c5 62 collision::clean();
5829cb66
PG
63
64 SDL_DestroyMutex(particleSetLock);
65 particleSetLock = NULL;
66
67 SDL_DestroyMutex(physicsEntitySetLock);
68 physicsEntitySetLock = NULL;
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{
5829cb66
PG
131 SDL_mutexP( particleSetLock );
132 // update active Particle*s
133 for( setPart::iterator it = active_Particles.begin();
134 it != active_Particles.end();
135 it++ )
136 {
137 (*it)->draw();
138 }
139 SDL_mutexV( particleSetLock );
ad9f1fb6 140
5829cb66
PG
141 SDL_mutexP( physicsEntitySetLock );
142 // update active PhysicsEntity*s
143 for( setPhys::iterator it = active_Physics.begin();
144 it != active_Physics.end();
145 it++ )
146 {
147 (*it)->draw();
148 }
149 SDL_mutexV( physicsEntitySetLock );
ad9f1fb6
PG
150}
151
152/// ***** Private Methods *****
617dcc71 153
ad9f1fb6
PG
154void updateParticles(float time_step)
155{
5829cb66
PG
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();
160 it++ )
161 {
162 active_Particles.insert(*it);
163 }
164 particles_To_Add.clear();
ad9f1fb6 165
5829cb66
PG
166 // remove dead Particle*s from Active
167 for( setPart::iterator it = particles_To_Remove.begin();
168 it != particles_To_Remove.end();
169 it++ )
170 {
171 active_Particles.erase(*it);
172 }
173 particles_To_Remove.clear();
174 SDL_mutexV( particleSetLock );
ad9f1fb6
PG
175
176 // update active Particle*s
177 for( setPart::iterator it = active_Particles.begin();
178 it != active_Particles.end();
179 it++ )
180 {
181 (*it)->update(time_step);
182 }
183}
184void updatePhysics(float time_step)
185{
5829cb66
PG
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();
190 it++ )
191 {
192 active_Physics.insert(*it);
193 }
194 physics_To_Add.clear();
ad9f1fb6 195
5829cb66
PG
196 // remove dead PhysicsEntity*s from Active
197 for( setPhys::iterator it = physics_To_Remove.begin();
198 it != physics_To_Remove.end();
199 it++ )
200 {
201 active_Physics.erase(*it);
202 }
203 physics_To_Remove.clear();
204 SDL_mutexV( physicsEntitySetLock );
ad9f1fb6 205
54fe85c5 206 // apply collision math
2869e2e8 207 collision::update(active_Physics);
54fe85c5 208
ad9f1fb6
PG
209 // update active PhysicsEntity*s
210 for( setPhys::iterator it = active_Physics.begin();
211 it != active_Physics.end();
212 it++ )
213 {
214 (*it)->update(time_step);
215 }
216}