delete is now thread safe
[physics.git] / src / entityManager.cpp
index bae30ae..e091b51 100644 (file)
 #include "debug.h"
 
 #include <set>
+#include <SDL/SDL.h>
+
+#include "locks/Mutex.h"
+#include "locks/Autolock.h"
 
 #include "Entities/Entity.h"
 #include "Entities/Particle.h"
@@ -31,6 +35,8 @@
 
 void updateParticles(float);
 void updatePhysics(float);
+void clearUpParticles();
+void clearUpPhysics();
 
 /// ***** Private Variables *****
 
@@ -44,15 +50,24 @@ setPhys physics_To_Add;
 setPhys active_Physics;
 setPhys physics_To_Remove;
 
+Mutex particleSetLock;
+Mutex physicsEntitySetLock;
+
 /// ***** Initializers/Cleaners *****
 
 void manager::init()
 {
+    particleSetLock.init();
+    physicsEntitySetLock.init();
+
     collision::init();
 }
 void manager::clean()
 {
     collision::clean();
+
+    physicsEntitySetLock.clean();
+    particleSetLock.clean();
 }
 
 /// ***** Public Methods *****
@@ -82,6 +97,7 @@ void manager::add(Entity* e)
 void manager::remove(Entity* e)
 {
     {
+        Autolock lock( particleSetLock );
         Particle* p = dynamic_cast<Particle*>(e);
         if( p != 0 )
         {
@@ -91,6 +107,7 @@ void manager::remove(Entity* e)
     }
 
     {
+        Autolock lock( physicsEntitySetLock );
         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
         if( p != 0 )
         {
@@ -115,32 +132,78 @@ void manager::update(float time_step)
 }
 void manager::draw()
 {
+    clearUpParticles();
+    {
+        Autolock lock( particleSetLock );
+        DPF(0, "Particle Draw Start");
+
+        // update active Particle*s
+        for( setPart::iterator it = active_Particles.begin();
+             it != active_Particles.end();
+             it++ )
+        {
+            (*it)->draw();
+        }
+
+        DPF(0, "Particle Draw End");
+    }
+
+    clearUpPhysics();
+    {
+        Autolock lock( physicsEntitySetLock );
+        DPF(0, "Physics Draw Start");
+
+        // update active PhysicsEntity*s
+        for( setPhys::iterator it = active_Physics.begin();
+             it != active_Physics.end();
+             it++ )
+        {
+            (*it)->draw();
+        }
+
+        DPF(0, "Physics Draw End");
+    }
+}
+
+/// ***** Private Methods *****
+
+void updateParticles(float time_step)
+{
+    clearUpParticles();
+
     // update active Particle*s
     for( setPart::iterator it = active_Particles.begin();
          it != active_Particles.end();
          it++ )
     {
-        (*it)->draw();
+        (*it)->update(time_step);
     }
+}
+void updatePhysics(float time_step)
+{
+    clearUpPhysics();
+
+    // apply collision math
+    collision::update(active_Physics);
 
     // update active PhysicsEntity*s
     for( setPhys::iterator it = active_Physics.begin();
          it != active_Physics.end();
          it++ )
     {
-        (*it)->draw();
+        (*it)->update(time_step);
     }
 }
-
-/// ***** Private Methods *****
-
-void updateParticles(float time_step)
+void clearUpParticles()
 {
+    Autolock lock( particleSetLock );
+
     // add new Particle*s to Active
     for( setPart::iterator it = particles_To_Add.begin();
          it != particles_To_Add.end();
          it++ )
     {
+        DPF(0, "Particle Insert");
         active_Particles.insert(*it);
     }
     particles_To_Add.clear();
@@ -150,25 +213,21 @@ void updateParticles(float time_step)
          it != particles_To_Remove.end();
          it++ )
     {
+        DPF(0, "Particle Delete");
         active_Particles.erase(*it);
     }
     particles_To_Remove.clear();
-
-    // update active Particle*s
-    for( setPart::iterator it = active_Particles.begin();
-         it != active_Particles.end();
-         it++ )
-    {
-        (*it)->update(time_step);
-    }
 }
-void updatePhysics(float time_step)
+void clearUpPhysics()
 {
+    Autolock lock( physicsEntitySetLock );
+
     // add new PhysicsEntity*s to Active
     for( setPhys::iterator it = physics_To_Add.begin();
          it != physics_To_Add.end();
          it++ )
     {
+        DPF(0, "Physics Insert");
         active_Physics.insert(*it);
     }
     physics_To_Add.clear();
@@ -178,18 +237,8 @@ void updatePhysics(float time_step)
          it != physics_To_Remove.end();
          it++ )
     {
+        DPF(0, "Physics Delete");
         active_Physics.erase(*it);
     }
     physics_To_Remove.clear();
-
-    // apply collision math
-    collision::update(active_Physics);
-
-    // update active PhysicsEntity*s
-    for( setPhys::iterator it = active_Physics.begin();
-         it != active_Physics.end();
-         it++ )
-    {
-        (*it)->update(time_step);
-    }
 }