From 907e27ef4e7a4220142e9e7ccfe711ae86dd6a4e Mon Sep 17 00:00:00 2001 From: Pablo Marcos Oltra Date: Sun, 29 Jul 2018 13:09:30 +0200 Subject: Fix Physac examples to be run without creating new thread --- src/physac.h | 64 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) (limited to 'src') 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 // 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 #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 // Required for: timespec - #include // Required for: clock_gettime() -#elif defined(__APPLE__) // macOS also defines __MACH__ +#elif defined(__APPLE__) // macOS also defines __MACH__ #include // 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 -- cgit v1.2.3