65b82730954e99989a061174c6eafb11b0a53aa2
[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         // update active Particle*s
136         for( setPart::iterator it = active_Particles.begin();
137              it != active_Particles.end();
138              it++ )
139         {
140             (*it)->draw();
141         }
142
143         DPF(0, "Particle Draw End");
144     }
145
146     {
147         Autolock lock( physicsEntitySetLock );
148         DPF(0, "Physics Draw Start");
149
150         // update active PhysicsEntity*s
151         for( setPhys::iterator it = active_Physics.begin();
152              it != active_Physics.end();
153              it++ )
154         {
155             (*it)->draw();
156         }
157
158         DPF(0, "Physics Draw End");
159     }
160 }
161
162 /// ***** Private Methods *****
163
164 void updateParticles(float time_step)
165 {
166     {
167         Autolock lock( particleSetLock );
168
169         // add new Particle*s to Active
170         for( setPart::iterator it = particles_To_Add.begin();
171              it != particles_To_Add.end();
172              it++ )
173         {
174             DPF(0, "Particle Insert");
175             active_Particles.insert(*it);
176         }
177
178         // remove dead Particle*s from Active
179         for( setPart::iterator it = particles_To_Remove.begin();
180              it != particles_To_Remove.end();
181              it++ )
182         {
183             DPF(0, "Particle Delete");
184             active_Particles.erase(*it);
185         }
186     }
187
188     particles_To_Add.clear();
189     particles_To_Remove.clear();
190
191     // update active Particle*s
192     for( setPart::iterator it = active_Particles.begin();
193          it != active_Particles.end();
194          it++ )
195     {
196         (*it)->update(time_step);
197     }
198 }
199 void updatePhysics(float time_step)
200 {
201     {
202         Autolock lock( physicsEntitySetLock );
203
204         // add new PhysicsEntity*s to Active
205         for( setPhys::iterator it = physics_To_Add.begin();
206              it != physics_To_Add.end();
207              it++ )
208         {
209             DPF(0, "Physics Insert");
210             active_Physics.insert(*it);
211         }
212
213         // remove dead PhysicsEntity*s from Active
214         for( setPhys::iterator it = physics_To_Remove.begin();
215              it != physics_To_Remove.end();
216              it++ )
217         {
218             DPF(0, "Physics Delete");
219             active_Physics.erase(*it);
220         }
221     }
222
223     physics_To_Remove.clear();
224     physics_To_Add.clear();
225
226     // apply collision math
227     collision::update(active_Physics);
228
229     // update active PhysicsEntity*s
230     for( setPhys::iterator it = active_Physics.begin();
231          it != active_Physics.end();
232          it++ )
233     {
234         (*it)->update(time_step);
235     }
236 }