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