Merge pull request #609 from pamarcos/fix_physac_examples
[physac] Fix Physac examples to be run without creating new thread
This commit is contained in:
commit
d4bb444fe5
6 changed files with 54 additions and 35 deletions
|
@ -17,6 +17,7 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
|
#define PHYSAC_NO_THREADS
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -54,6 +55,8 @@ int main()
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Delay initialization of variables due to physics reset async
|
// Delay initialization of variables due to physics reset async
|
||||||
|
RunPhysicsStep();
|
||||||
|
|
||||||
if (needsReset)
|
if (needsReset)
|
||||||
{
|
{
|
||||||
floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
|
floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
|
||||||
|
@ -61,6 +64,8 @@ int main()
|
||||||
|
|
||||||
circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
|
circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
|
||||||
circle->enabled = false;
|
circle->enabled = false;
|
||||||
|
|
||||||
|
needsReset = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset physics input
|
// Reset physics input
|
||||||
|
@ -134,4 +139,3 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
|
#define PHYSAC_NO_THREADS
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -71,6 +72,8 @@ int main()
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
RunPhysicsStep();
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
{
|
{
|
||||||
// Reset dynamic physics bodies position, velocity and rotation
|
// Reset dynamic physics bodies position, velocity and rotation
|
||||||
|
@ -141,4 +144,3 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
|
#define PHYSAC_NO_THREADS
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
#define VELOCITY 0.5f
|
#define VELOCITY 0.5f
|
||||||
|
@ -64,6 +65,8 @@ int main()
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
RunPhysicsStep();
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
{
|
{
|
||||||
// Reset movement physics body position, velocity and rotation
|
// Reset movement physics body position, velocity and rotation
|
||||||
|
@ -127,4 +130,3 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
|
#define PHYSAC_NO_THREADS
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -57,6 +58,8 @@ int main()
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
RunPhysicsStep();
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
{
|
{
|
||||||
// Reset circles physics bodies position and velocity
|
// Reset circles physics bodies position and velocity
|
||||||
|
@ -120,4 +123,3 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#include "physac.h"
|
#define PHYSAC_NO_THREADS
|
||||||
|
#include "physac.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -48,12 +49,15 @@ int main()
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
|
RunPhysicsStep();
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Delay initialization of variables due to physics reset asynchronous
|
// Delay initialization of variables due to physics reset asynchronous
|
||||||
if (needsReset)
|
if (needsReset)
|
||||||
{
|
{
|
||||||
// Create random polygon physics body to shatter
|
// Create random polygon physics body to shatter
|
||||||
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
|
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
|
||||||
|
needsReset = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
|
@ -118,4 +122,3 @@ int main()
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
64
src/physac.h
64
src/physac.h
|
@ -196,6 +196,7 @@ extern "C" { // Prevents name mangling of fun
|
||||||
// Module Functions Declaration
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
PHYSACDEF void InitPhysics(void); // Initializes physics values, pointers and creates physics loop thread
|
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 bool IsPhysicsEnabled(void); // Returns true if physics thread is currently enabled
|
||||||
PHYSACDEF void SetPhysicsGravity(float x, float y); // Sets physics global gravity force
|
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
|
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
|
#endif
|
||||||
|
|
||||||
// Time management functionality
|
// Time management functionality
|
||||||
|
#include <time.h> // Required for: time(), clock_gettime()
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
// Functions required to query time on Windows
|
// Functions required to query time on Windows
|
||||||
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
|
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
|
||||||
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
|
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
|
||||||
#include <time.h>
|
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
#if _POSIX_C_SOURCE < 199309L
|
#if _POSIX_C_SOURCE < 199309L
|
||||||
#undef _POSIX_C_SOURCE
|
#undef _POSIX_C_SOURCE
|
||||||
#define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
|
#define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
|
||||||
#endif
|
#endif
|
||||||
#include <sys/time.h> // Required for: timespec
|
#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()
|
#include <mach/mach_time.h> // Required for: mach_absolute_time()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -356,7 +356,10 @@ PHYSACDEF void InitPhysics(void)
|
||||||
// Create physics thread using POSIXS thread libraries
|
// Create physics thread using POSIXS thread libraries
|
||||||
pthread_create(&physicsThreadId, NULL, &PhysicsLoop, NULL);
|
pthread_create(&physicsThreadId, NULL, &PhysicsLoop, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Initialize high resolution timer
|
||||||
|
InitTimer();
|
||||||
|
|
||||||
#if defined(PHYSAC_DEBUG)
|
#if defined(PHYSAC_DEBUG)
|
||||||
printf("[PHYSAC] physics module initialized successfully\n");
|
printf("[PHYSAC] physics module initialized successfully\n");
|
||||||
#endif
|
#endif
|
||||||
|
@ -1010,33 +1013,10 @@ static void *PhysicsLoop(void *arg)
|
||||||
physicsThreadEnabled = true;
|
physicsThreadEnabled = true;
|
||||||
accumulator = 0;
|
accumulator = 0;
|
||||||
|
|
||||||
// Initialize high resolution timer
|
|
||||||
InitTimer();
|
|
||||||
|
|
||||||
// Physics update loop
|
// Physics update loop
|
||||||
while (physicsThreadEnabled)
|
while (physicsThreadEnabled)
|
||||||
{
|
{
|
||||||
// Calculate current time
|
RunPhysicsStep();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unitialize physics manifolds dynamic memory allocations
|
// 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
|
// Finds a valid index for a new manifold initialization
|
||||||
static int FindAvailableManifoldIndex()
|
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 };
|
return (Vector2){ matrix.m00*vector.x + matrix.m01*vector.y, matrix.m10*vector.x + matrix.m11*vector.y };
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // PHYSAC_IMPLEMENTATION
|
#endif // PHYSAC_IMPLEMENTATION
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue