aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPablo Marcos Oltra <pablo.marcos.oltra@gmail.com>2018-07-29 13:09:30 +0200
committerPablo Marcos Oltra <pablo.marcos.oltra@gmail.com>2018-07-29 21:32:39 +0200
commit907e27ef4e7a4220142e9e7ccfe711ae86dd6a4e (patch)
tree1829443c7f811914d75a626aa1b60c657a4b76a4 /src
parente82505b873370b8f3a914a079062c21e64353210 (diff)
downloadraylib-907e27ef4e7a4220142e9e7ccfe711ae86dd6a4e.tar.gz
raylib-907e27ef4e7a4220142e9e7ccfe711ae86dd6a4e.zip
Fix Physac examples to be run without creating new thread
Diffstat (limited to 'src')
-rw-r--r--src/physac.h64
1 files changed, 35 insertions, 29 deletions
diff --git a/src/physac.h b/src/physac.h
index 6b78fcc6..38789a6d 100644
--- a/src/physac.h
+++ b/src/physac.h
@@ -196,6 +196,7 @@ extern "C" { // Prevents name mangling of fun
// Module Functions Declaration
//----------------------------------------------------------------------------------
PHYSACDEF void InitPhysics(void); // Initializes physics values, pointers and creates physics loop thread
+PHYSACDEF void RunPhysicsStep(void); // Run physics step, to be used if PHYSICS_NO_THREADS is set in your main loop
PHYSACDEF bool IsPhysicsEnabled(void); // Returns true if physics thread is currently enabled
PHYSACDEF void SetPhysicsGravity(float x, float y); // Sets physics global gravity force
PHYSACDEF PhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float density); // Creates a new circle physics body with generic parameters
@@ -245,19 +246,18 @@ PHYSACDEF void ClosePhysics(void);
#endif
// Time management functionality
+#include <time.h> // Required for: time(), clock_gettime()
#if defined(_WIN32)
// Functions required to query time on Windows
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
- #include <time.h>
#elif defined(__linux__)
#if _POSIX_C_SOURCE < 199309L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
#endif
#include <sys/time.h> // Required for: timespec
- #include <time.h> // Required for: clock_gettime()
-#elif defined(__APPLE__) // macOS also defines __MACH__
+#elif defined(__APPLE__) // macOS also defines __MACH__
#include <mach/mach_time.h> // Required for: mach_absolute_time()
#endif
@@ -356,7 +356,10 @@ PHYSACDEF void InitPhysics(void)
// Create physics thread using POSIXS thread libraries
pthread_create(&physicsThreadId, NULL, &PhysicsLoop, NULL);
#endif
-
+
+ // Initialize high resolution timer
+ InitTimer();
+
#if defined(PHYSAC_DEBUG)
printf("[PHYSAC] physics module initialized successfully\n");
#endif
@@ -1010,33 +1013,10 @@ static void *PhysicsLoop(void *arg)
physicsThreadEnabled = true;
accumulator = 0;
- // Initialize high resolution timer
- InitTimer();
-
// Physics update loop
while (physicsThreadEnabled)
{
- // Calculate current time
- currentTime = GetCurrentTime();
-
- // Calculate current delta time
- deltaTime = currentTime - startTime;
-
- // Store the time elapsed since the last frame began
- accumulator += deltaTime;
-
- // Clamp accumulator to max time step to avoid bad performance
- MathClamp(&accumulator, 0.0, PHYSAC_MAX_TIMESTEP);
-
- // Fixed time stepping loop
- while (accumulator >= PHYSAC_DESIRED_DELTATIME)
- {
- PhysicsStep();
- accumulator -= deltaTime;
- }
-
- // Record the starting of this frame
- startTime = currentTime;
+ RunPhysicsStep();
}
// Unitialize physics manifolds dynamic memory allocations
@@ -1160,6 +1140,32 @@ static void PhysicsStep(void)
}
}
+// Wrapper to ensure PhysicsStep is run with at a fixed time step
+PHYSACDEF void RunPhysicsStep(void)
+{
+ // Calculate current time
+ currentTime = GetCurrentTime();
+
+ // Calculate current delta time
+ deltaTime = currentTime - startTime;
+
+ // Store the time elapsed since the last frame began
+ accumulator += deltaTime;
+
+ // Clamp accumulator to max time step to avoid bad performance
+ MathClamp(&accumulator, 0.0, PHYSAC_MAX_TIMESTEP);
+
+ // Fixed time stepping loop
+ while (accumulator >= PHYSAC_DESIRED_DELTATIME)
+ {
+ PhysicsStep();
+ accumulator -= deltaTime;
+ }
+
+ // Record the starting of this frame
+ startTime = currentTime;
+}
+
// Finds a valid index for a new manifold initialization
static int FindAvailableManifoldIndex()
{
@@ -2048,4 +2054,4 @@ static inline Vector2 Mat2MultiplyVector2(Mat2 matrix, Vector2 vector)
return (Vector2){ matrix.m00*vector.x + matrix.m01*vector.y, matrix.m10*vector.x + matrix.m11*vector.y };
}
-#endif // PHYSAC_IMPLEMENTATION \ No newline at end of file
+#endif // PHYSAC_IMPLEMENTATION