瀏覽代碼

Fixed bug in delta time calculation...

and added PHYSAC_NO_THREADS define. Improved physac example drawing
frames per second in screen.
victorfisac 9 年之前
父節點
當前提交
54537e8f0b
共有 3 個文件被更改,包括 21 次插入11 次删除
  1. 2 0
      examples/physics_basic_rigidbody.c
  2. 3 1
      examples/physics_forces.c
  3. 16 10
      src/physac.h

+ 2 - 0
examples/physics_basic_rigidbody.c

@@ -110,6 +110,8 @@ int main()
             // Draw help message
             // Draw help message
             DrawText("Use WASD to move rectangle and ARROWS to move square", screenWidth/2 - MeasureText("Use WASD to move rectangle and ARROWS to move square", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
             DrawText("Use WASD to move rectangle and ARROWS to move square", screenWidth/2 - MeasureText("Use WASD to move rectangle and ARROWS to move square", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
 
 
+            DrawFPS(10, 10);
+            
         EndDrawing();
         EndDrawing();
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
     }
     }

+ 3 - 1
examples/physics_forces.c

@@ -164,7 +164,9 @@ int main()
             
             
             // Draw help messages
             // Draw help messages
             DrawText("Use LEFT MOUSE BUTTON to apply a force", screenWidth/2 - MeasureText("Use LEFT MOUSE BUTTON to apply a force", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
             DrawText("Use LEFT MOUSE BUTTON to apply a force", screenWidth/2 - MeasureText("Use LEFT MOUSE BUTTON to apply a force", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
-            DrawText("Use R to reset objects position", screenWidth/2 - MeasureText("Use R to reset objects position", 20)/2, screenHeight*0.875f, 20, GRAY);    
+            DrawText("Use R to reset objects position", screenWidth/2 - MeasureText("Use R to reset objects position", 20)/2, screenHeight*0.875f, 20, GRAY);
+            
+            DrawFPS(10, 10);
 
 
         EndDrawing();
         EndDrawing();
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------

+ 16 - 10
src/physac.h

@@ -178,8 +178,10 @@ PHYSACDEF Rectangle TransformToRectangle(Transform transform);
 
 
 #include <math.h>           // Required for: cos(), sin(), abs(), fminf()
 #include <math.h>           // Required for: cos(), sin(), abs(), fminf()
 #include <stdint.h>         // Required for typedef unsigned long long int uint64_t, used by hi-res timer
 #include <stdint.h>         // Required for typedef unsigned long long int uint64_t, used by hi-res timer
-#include <pthread.h>        // Required for: pthread_create()
-#include "utils.h"          // Required for: TraceLog()
+
+#ifndef PHYSAC_NO_THREADS
+    #include <pthread.h>        // Required for: pthread_create()
+#endif
 
 
 #if defined(PLATFORM_DESKTOP)
 #if defined(PLATFORM_DESKTOP)
     // Functions required to query time on Windows
     // Functions required to query time on Windows
@@ -234,9 +236,11 @@ PHYSACDEF void InitPhysics(Vector2 gravity)
     physicBodiesCount = 0;
     physicBodiesCount = 0;
     gravityForce = gravity;
     gravityForce = gravity;
     
     
-    // Create physics thread
-    pthread_t tid;
-    pthread_create(&tid, NULL, &PhysicsThread, NULL);
+    #ifndef PHYSAC_NO_THREADS       // NOTE: if defined, user will need to create a thread for PhysicsThread function manually
+        // Create physics thread
+        pthread_t tid;
+        pthread_create(&tid, NULL, &PhysicsThread, NULL);
+    #endif
 }
 }
 
 
 // Update physic objects, calculating physic behaviours and collisions detection
 // Update physic objects, calculating physic behaviours and collisions detection
@@ -768,7 +772,6 @@ static void InitTimer(void)
     {
     {
         baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec;
         baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec;
     }
     }
-    else TraceLog(WARNING, "No hi-resolution timer available");
 #endif
 #endif
 
 
     previousTime = GetCurrentTime();       // Get time as double
     previousTime = GetCurrentTime();       // Get time as double
@@ -777,22 +780,25 @@ static void InitTimer(void)
 // Time measure returned are microseconds
 // Time measure returned are microseconds
 static double GetCurrentTime(void)
 static double GetCurrentTime(void)
 {
 {
+    double time;
+    
 #if defined(PLATFORM_DESKTOP)
 #if defined(PLATFORM_DESKTOP)
     unsigned long long int clockFrequency, currentTime;
     unsigned long long int clockFrequency, currentTime;
     
     
     QueryPerformanceFrequency(&clockFrequency);
     QueryPerformanceFrequency(&clockFrequency);
     QueryPerformanceCounter(&currentTime);
     QueryPerformanceCounter(&currentTime);
-
-    return (double)(currentTime/clockFrequency);
+    
 #endif
 #endif
 
 
 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
     struct timespec ts;
     struct timespec ts;
     clock_gettime(CLOCK_MONOTONIC, &ts);
     clock_gettime(CLOCK_MONOTONIC, &ts);
-    uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
+    uint64_t temp = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
 
 
-    return (double)(time - baseTime)*1e-9;
+    time = (double)(temp - baseTime)*1e-9;
 #endif
 #endif
+
+    return time;
 }
 }
 
 
 // Returns the dot product of two Vector2
 // Returns the dot product of two Vector2