Fix Physac examples to be run without creating new thread
This commit is contained in:
parent
e82505b873
commit
907e27ef4e
@ -17,6 +17,7 @@
|
||||
#include "raylib.h"
|
||||
|
||||
#define PHYSAC_IMPLEMENTATION
|
||||
#define PHYSAC_NO_THREADS
|
||||
#include "physac.h"
|
||||
|
||||
int main()
|
||||
@ -54,6 +55,8 @@ int main()
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Delay initialization of variables due to physics reset async
|
||||
RunPhysicsStep();
|
||||
|
||||
if (needsReset)
|
||||
{
|
||||
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->enabled = false;
|
||||
|
||||
needsReset = false;
|
||||
}
|
||||
|
||||
// Reset physics input
|
||||
@ -134,4 +139,3 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "raylib.h"
|
||||
|
||||
#define PHYSAC_IMPLEMENTATION
|
||||
#define PHYSAC_NO_THREADS
|
||||
#include "physac.h"
|
||||
|
||||
int main()
|
||||
@ -71,6 +72,8 @@ int main()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
RunPhysicsStep();
|
||||
|
||||
if (IsKeyPressed('R')) // Reset physics input
|
||||
{
|
||||
// Reset dynamic physics bodies position, velocity and rotation
|
||||
@ -141,4 +144,3 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "raylib.h"
|
||||
|
||||
#define PHYSAC_IMPLEMENTATION
|
||||
#define PHYSAC_NO_THREADS
|
||||
#include "physac.h"
|
||||
|
||||
#define VELOCITY 0.5f
|
||||
@ -64,6 +65,8 @@ int main()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
RunPhysicsStep();
|
||||
|
||||
if (IsKeyPressed('R')) // Reset physics input
|
||||
{
|
||||
// Reset movement physics body position, velocity and rotation
|
||||
@ -127,4 +130,3 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "raylib.h"
|
||||
|
||||
#define PHYSAC_IMPLEMENTATION
|
||||
#define PHYSAC_NO_THREADS
|
||||
#include "physac.h"
|
||||
|
||||
int main()
|
||||
@ -57,6 +58,8 @@ int main()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
RunPhysicsStep();
|
||||
|
||||
if (IsKeyPressed('R')) // Reset physics input
|
||||
{
|
||||
// Reset circles physics bodies position and velocity
|
||||
@ -120,4 +123,3 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,8 @@
|
||||
#include "raylib.h"
|
||||
|
||||
#define PHYSAC_IMPLEMENTATION
|
||||
#include "physac.h"
|
||||
#define PHYSAC_NO_THREADS
|
||||
#include "physac.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -48,12 +49,15 @@ int main()
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
RunPhysicsStep();
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Delay initialization of variables due to physics reset asynchronous
|
||||
if (needsReset)
|
||||
{
|
||||
// Create random polygon physics body to shatter
|
||||
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
|
||||
needsReset = false;
|
||||
}
|
||||
|
||||
if (IsKeyPressed('R')) // Reset physics input
|
||||
@ -118,4 +122,3 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
64
src/physac.h
64
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
|
||||
#endif // PHYSAC_IMPLEMENTATION
|
||||
|
Loading…
Reference in New Issue
Block a user