Ver Fonte

Fixed memory leaks in physics step operations

victorfisac há 7 anos atrás
pai
commit
3201bad65a
3 ficheiros alterados com 223 adições e 222 exclusões
  1. 10 3
      examples/physac/physics_demo.c
  2. 9 3
      examples/physac/physics_shatter.c
  3. 204 216
      src/physac.h

+ 10 - 3
examples/physac/physics_demo.c

@@ -32,6 +32,7 @@ int main()
     // Physac logo drawing position
     int logoX = screenWidth - MeasureText("Physac", 30) - 10;
     int logoY = 15;
+    bool needsReset = false;
 
     // Initialize physics and default physics bodies
     InitPhysics();
@@ -52,10 +53,9 @@ int main()
     {
         // Update
         //----------------------------------------------------------------------------------
-        if (IsKeyPressed('R'))    // Reset physics input
+        // Delay initialization of variables due to physics reset async
+        if (needsReset)
         {
-            ResetPhysics();
-
             floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
             floor->enabled = false;
 
@@ -63,6 +63,13 @@ int main()
             circle->enabled = false;
         }
 
+        // Reset physics input
+        if (IsKeyPressed('R'))
+        {
+            ResetPhysics();
+            needsReset = true;
+        }
+
         // Physics body creation inputs
         if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
         else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);

+ 9 - 3
examples/physac/physics_shatter.c

@@ -32,6 +32,7 @@ int main()
     // Physac logo drawing position
     int logoX = screenWidth - MeasureText("Physac", 30) - 10;
     int logoY = 15;
+    bool needsReset = false;
 
     // Initialize physics and default physics bodies
     InitPhysics();
@@ -48,14 +49,19 @@ int main()
     {
         // Update
         //----------------------------------------------------------------------------------
-        if (IsKeyPressed('R'))    // Reset physics input
+        // Delay initialization of variables due to physics reset asynchronous
+        if (needsReset)
         {
-            ResetPhysics();
-
             // Create random polygon physics body to shatter
             body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
         }
 
+        if (IsKeyPressed('R'))    // Reset physics input
+        {
+            ResetPhysics();
+            needsReset = true;
+        }
+
         if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))    // Physics shatter input
         {
             // Note: some values need to be stored in variables due to asynchronous changes during main thread

Diff do ficheiro suprimidas por serem muito extensas
+ 204 - 216
src/physac.h


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff