ecf7955c5598e10d1a4bf1d9e1e5fcd2b53822d5
[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 "Entities/Entity.h"
25 #include "Entities/Particle.h"
26 #include "Entities/PhysicsEntity.h"
27
28 #include "collisionManager.h"
29 #include "effectManager.h"
30
31 /// ***** Private Method Headers *****
32
33 void updateParticles(float);
34 void updatePhysics(float);
35
36 /// ***** Private Variables *****
37
38 typedef std::set<Particle*> setPart;
39 setPart particles_To_Add;
40 setPart active_Particles;
41 setPart particles_To_Remove;
42
43 typedef std::set<PhysicsEntity*> setPhys;
44 setPhys physics_To_Add;
45 setPhys active_Physics;
46 setPhys physics_To_Remove;
47
48 SDL_mutex* particleSetLock      = NULL;
49 SDL_mutex* physicsEntitySetLock = NULL;
50
51 /// ***** Initializers/Cleaners *****
52
53 void manager::init()
54 {
55     particleSetLock = SDL_CreateMutex();
56     physicsEntitySetLock = SDL_CreateMutex();
57
58     collision::init();
59 }
60 void manager::clean()
61 {
62     collision::clean();
63
64     SDL_DestroyMutex(particleSetLock);
65     particleSetLock = NULL;
66
67     SDL_DestroyMutex(physicsEntitySetLock);
68     physicsEntitySetLock = NULL;
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     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 );
140
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 );
150 }
151
152 /// ***** Private Methods *****
153
154 void updateParticles(float time_step)
155 {
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();
165
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 );
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 }
184 void updatePhysics(float time_step)
185 {
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();
195
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 );
205
206     // apply collision math
207     collision::update(active_Physics);
208
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 }