Selaa lähdekoodia

Review DEBUG trace log and custom allocators

Not exposing some data structures
pull/1097/head
raysan5 5 vuotta sitten
vanhempi
commit
884e868e92
1 muutettua tiedostoa jossa 109 lisäystä ja 93 poistoa
  1. +109
    -93
      src/physac.h

+ 109
- 93
src/physac.h Näytä tiedosto

@ -83,6 +83,14 @@
#endif #endif
#endif #endif
// Allow custom memory allocators
#ifndef PHYSAC_MALLOC
#define PHYSAC_MALLOC(size) malloc(size)
#endif
#ifndef PHYSAC_FREE
#define PHYSAC_FREE(ptr) free(ptr)
#endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -98,9 +106,6 @@
#define PHYSAC_PI 3.14159265358979323846 #define PHYSAC_PI 3.14159265358979323846
#define PHYSAC_DEG2RAD (PHYSAC_PI/180.0f) #define PHYSAC_DEG2RAD (PHYSAC_PI/180.0f)
#define PHYSAC_MALLOC(size) malloc(size)
#define PHYSAC_FREE(ptr) free(ptr)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
// NOTE: Below types are required for PHYSAC_STANDALONE usage // NOTE: Below types are required for PHYSAC_STANDALONE usage
@ -125,63 +130,6 @@ typedef enum PhysicsShapeType { PHYSICS_CIRCLE, PHYSICS_POLYGON } PhysicsShapeTy
// Previously defined to be used in PhysicsShape struct as circular dependencies // Previously defined to be used in PhysicsShape struct as circular dependencies
typedef struct PhysicsBodyData *PhysicsBody; typedef struct PhysicsBodyData *PhysicsBody;
// Matrix2x2 type (used for polygon shape rotation matrix)
typedef struct Matrix2x2 {
float m00;
float m01;
float m10;
float m11;
} Matrix2x2;
typedef struct PolygonData {
unsigned int vertexCount; // Current used vertex and normals count
Vector2 positions[PHYSAC_MAX_VERTICES]; // Polygon vertex positions vectors
Vector2 normals[PHYSAC_MAX_VERTICES]; // Polygon vertex normals vectors
} PolygonData;
typedef struct PhysicsShape {
PhysicsShapeType type; // Physics shape type (circle or polygon)
PhysicsBody body; // Shape physics body reference
float radius; // Circle shape radius (used for circle shapes)
Matrix2x2 transform; // Vertices transform matrix 2x2
PolygonData vertexData; // Polygon shape vertices position and normals data (just used for polygon shapes)
} PhysicsShape;
typedef struct PhysicsBodyData {
unsigned int id; // Reference unique identifier
bool enabled; // Enabled dynamics state (collisions are calculated anyway)
Vector2 position; // Physics body shape pivot
Vector2 velocity; // Current linear velocity applied to position
Vector2 force; // Current linear force (reset to 0 every step)
float angularVelocity; // Current angular velocity applied to orient
float torque; // Current angular force (reset to 0 every step)
float orient; // Rotation in radians
float inertia; // Moment of inertia
float inverseInertia; // Inverse value of inertia
float mass; // Physics body mass
float inverseMass; // Inverse value of mass
float staticFriction; // Friction when the body has not movement (0 to 1)
float dynamicFriction; // Friction when the body has movement (0 to 1)
float restitution; // Restitution coefficient of the body (0 to 1)
bool useGravity; // Apply gravity force to dynamics
bool isGrounded; // Physics grounded on other body state
bool freezeOrient; // Physics rotation constraint
PhysicsShape shape; // Physics body shape information (type, radius, vertices, normals)
} PhysicsBodyData;
typedef struct PhysicsManifoldData {
unsigned int id; // Reference unique identifier
PhysicsBody bodyA; // Manifold first physics body reference
PhysicsBody bodyB; // Manifold second physics body reference
float penetration; // Depth of penetration from collision
Vector2 normal; // Normal direction vector from 'a' to 'b'
Vector2 contacts[2]; // Points of contact during collision
unsigned int contactsCount; // Current collision number of contacts
float restitution; // Mixed restitution during collision
float dynamicFriction; // Mixed dynamic friction during collision
float staticFriction; // Mixed static friction during collision
} PhysicsManifoldData, *PhysicsManifold;
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
@ -228,13 +176,20 @@ PHYSACDEF void ClosePhysics(void);
#include <pthread.h> // Required for: pthread_t, pthread_create() #include <pthread.h> // Required for: pthread_t, pthread_create()
#endif #endif
#if defined(PHYSAC_DEBUG)
#endif
// Support TRACELOG macros
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
#include <stdio.h> // Required for: printf() #include <stdio.h> // Required for: printf()
#define TRACELOG(...) printf(__VA_ARGS__)
#else
#define TRACELOG(...) (void)0
#endif #endif
#include <stdlib.h> // Required for: malloc(), free(), srand(), rand() #include <stdlib.h> // Required for: malloc(), free(), srand(), rand()
#include <math.h> // Required for: cosf(), sinf(), fabs(), sqrtf() #include <math.h> // Required for: cosf(), sinf(), fabs(), sqrtf()
#include <stdint.h> // Required for: uint64_t
#if !defined(PHYSAC_STANDALONE) #if !defined(PHYSAC_STANDALONE)
#include "raymath.h" // Required for: Vector2Add(), Vector2Subtract() #include "raymath.h" // Required for: Vector2Add(), Vector2Subtract()
@ -266,6 +221,67 @@ PHYSACDEF void ClosePhysics(void);
#define PHYSAC_K 1.0f/3.0f #define PHYSAC_K 1.0f/3.0f
#define PHYSAC_VECTOR_ZERO (Vector2){ 0.0f, 0.0f } #define PHYSAC_VECTOR_ZERO (Vector2){ 0.0f, 0.0f }
//----------------------------------------------------------------------------------
// Data Types Structure Definition
//----------------------------------------------------------------------------------
// Matrix2x2 type (used for polygon shape rotation matrix)
typedef struct Matrix2x2 {
float m00;
float m01;
float m10;
float m11;
} Matrix2x2;
typedef struct PolygonData {
unsigned int vertexCount; // Current used vertex and normals count
Vector2 positions[PHYSAC_MAX_VERTICES]; // Polygon vertex positions vectors
Vector2 normals[PHYSAC_MAX_VERTICES]; // Polygon vertex normals vectors
} PolygonData;
typedef struct PhysicsShape {
PhysicsShapeType type; // Physics shape type (circle or polygon)
PhysicsBody body; // Shape physics body reference
float radius; // Circle shape radius (used for circle shapes)
Matrix2x2 transform; // Vertices transform matrix 2x2
PolygonData vertexData; // Polygon shape vertices position and normals data (just used for polygon shapes)
} PhysicsShape;
typedef struct PhysicsBodyData {
unsigned int id; // Reference unique identifier
bool enabled; // Enabled dynamics state (collisions are calculated anyway)
Vector2 position; // Physics body shape pivot
Vector2 velocity; // Current linear velocity applied to position
Vector2 force; // Current linear force (reset to 0 every step)
float angularVelocity; // Current angular velocity applied to orient
float torque; // Current angular force (reset to 0 every step)
float orient; // Rotation in radians
float inertia; // Moment of inertia
float inverseInertia; // Inverse value of inertia
float mass; // Physics body mass
float inverseMass; // Inverse value of mass
float staticFriction; // Friction when the body has not movement (0 to 1)
float dynamicFriction; // Friction when the body has movement (0 to 1)
float restitution; // Restitution coefficient of the body (0 to 1)
bool useGravity; // Apply gravity force to dynamics
bool isGrounded; // Physics grounded on other body state
bool freezeOrient; // Physics rotation constraint
PhysicsShape shape; // Physics body shape information (type, radius, vertices, normals)
} PhysicsBodyData;
typedef struct PhysicsManifoldData {
unsigned int id; // Reference unique identifier
PhysicsBody bodyA; // Manifold first physics body reference
PhysicsBody bodyB; // Manifold second physics body reference
float penetration; // Depth of penetration from collision
Vector2 normal; // Normal direction vector from 'a' to 'b'
Vector2 contacts[2]; // Points of contact during collision
unsigned int contactsCount; // Current collision number of contacts
float restitution; // Mixed restitution during collision
float dynamicFriction; // Mixed dynamic friction during collision
float staticFriction; // Mixed static friction during collision
} PhysicsManifoldData, *PhysicsManifold;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -278,7 +294,7 @@ static double baseTime = 0.0; // Offset time for M
static double startTime = 0.0; // Start time in milliseconds static double startTime = 0.0; // Start time in milliseconds
static double deltaTime = 1.0/60.0/10.0 * 1000; // Delta time used for physics steps, in milliseconds static double deltaTime = 1.0/60.0/10.0 * 1000; // Delta time used for physics steps, in milliseconds
static double currentTime = 0.0; // Current time in milliseconds static double currentTime = 0.0; // Current time in milliseconds
static n">uint64_t frequency = 0; // Hi-res clock frequency
static kt">unsigned long long int frequency = 0; // Hi-res clock frequency
static double accumulator = 0.0; // Physics time step delta time accumulator static double accumulator = 0.0; // Physics time step delta time accumulator
static unsigned int stepsCount = 0; // Total physics steps processed static unsigned int stepsCount = 0; // Total physics steps processed
@ -316,7 +332,7 @@ static bool BiasGreaterThan(float valueA, float valueB);
static Vector2 TriangleBarycenter(Vector2 v1, Vector2 v2, Vector2 v3); // Returns the barycenter of a triangle given by 3 points static Vector2 TriangleBarycenter(Vector2 v1, Vector2 v2, Vector2 v3); // Returns the barycenter of a triangle given by 3 points
static void InitTimer(void); // Initializes hi-resolution MONOTONIC timer static void InitTimer(void); // Initializes hi-resolution MONOTONIC timer
static n">uint64_t GetTimeCount(void); // Get hi-res MONOTONIC time measure in mseconds
static kt">unsigned long long int GetTimeCount(void); // Get hi-res MONOTONIC time measure in mseconds
static double GetCurrentTime(void); // Get current time measure in milliseconds static double GetCurrentTime(void); // Get current time measure in milliseconds
// Math functions // Math functions
@ -352,7 +368,7 @@ PHYSACDEF void InitPhysics(void)
InitTimer(); InitTimer();
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] physics module initialized successfully\n");
TRACELOG("[PHYSAC] physics module initialized successfully\n");
#endif #endif
accumulator = 0.0; accumulator = 0.0;
@ -455,11 +471,11 @@ PHYSACDEF PhysicsBody CreatePhysicsBodyRectangle(Vector2 pos, float width, float
physicsBodiesCount++; physicsBodiesCount++;
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] created polygon physics body id %i\n", newBody->id);
TRACELOG("[PHYSAC] created polygon physics body id %i\n", newBody->id);
#endif #endif
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] new physics body creation failed because there is any available id to use\n");
else TRACELOG("[PHYSAC] new physics body creation failed because there is any available id to use\n");
#endif #endif
return newBody; return newBody;
@ -541,11 +557,11 @@ PHYSACDEF PhysicsBody CreatePhysicsBodyPolygon(Vector2 pos, float radius, int si
physicsBodiesCount++; physicsBodiesCount++;
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] created polygon physics body id %i\n", newBody->id);
TRACELOG("[PHYSAC] created polygon physics body id %i\n", newBody->id);
#endif #endif
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] new physics body creation failed because there is any available id to use\n");
else TRACELOG("[PHYSAC] new physics body creation failed because there is any available id to use\n");
#endif #endif
return newBody; return newBody;
@ -600,7 +616,7 @@ PHYSACDEF void PhysicsShatter(PhysicsBody body, Vector2 position, float force)
{ {
int count = vertexData.vertexCount; int count = vertexData.vertexCount;
Vector2 bodyPos = body->position; Vector2 bodyPos = body->position;
Vector2 *vertices = (Vector2*)malloc(sizeof(Vector2) * count);
Vector2 *vertices = (Vector2 *)PHYSAC_MALLOC(sizeof(Vector2)*count);
Matrix2x2 trans = body->shape.transform; Matrix2x2 trans = body->shape.transform;
for (int i = 0; i < count; i++) vertices[i] = vertexData.positions[i]; for (int i = 0; i < count; i++) vertices[i] = vertexData.positions[i];
@ -693,12 +709,12 @@ PHYSACDEF void PhysicsShatter(PhysicsBody body, Vector2 position, float force)
PhysicsAddForce(newBody, forceDirection); PhysicsAddForce(newBody, forceDirection);
} }
free(vertices);
PHYSAC_FREE(vertices);
} }
} }
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error when trying to shatter a null reference physics body");
else TRACELOG("[PHYSAC] error when trying to shatter a null reference physics body");
#endif #endif
} }
@ -720,12 +736,12 @@ PHYSACDEF PhysicsBody GetPhysicsBody(int index)
if (body == NULL) if (body == NULL)
{ {
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] error when trying to get a null reference physics body");
TRACELOG("[PHYSAC] error when trying to get a null reference physics body");
#endif #endif
} }
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] physics body index is out of bounds");
else TRACELOG("[PHYSAC] physics body index is out of bounds");
#endif #endif
return body; return body;
@ -742,11 +758,11 @@ PHYSACDEF int GetPhysicsShapeType(int index)
if (body != NULL) result = body->shape.type; if (body != NULL) result = body->shape.type;
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error when trying to get a null reference physics body");
else TRACELOG("[PHYSAC] error when trying to get a null reference physics body");
#endif #endif
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] physics body index is out of bounds");
else TRACELOG("[PHYSAC] physics body index is out of bounds");
#endif #endif
return result; return result;
@ -771,11 +787,11 @@ PHYSACDEF int GetPhysicsShapeVerticesCount(int index)
} }
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error when trying to get a null reference physics body");
else TRACELOG("[PHYSAC] error when trying to get a null reference physics body");
#endif #endif
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] physics body index is out of bounds");
else TRACELOG("[PHYSAC] physics body index is out of bounds");
#endif #endif
return result; return result;
@ -804,7 +820,7 @@ PHYSACDEF Vector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex)
} }
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error when trying to get a null reference physics body");
else TRACELOG("[PHYSAC] error when trying to get a null reference physics body");
#endif #endif
return position; return position;
@ -841,7 +857,7 @@ PHYSACDEF void DestroyPhysicsBody(PhysicsBody body)
if (index == -1) if (index == -1)
{ {
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] Not possible to find body id %i in pointers array\n", id);
TRACELOG("[PHYSAC] Not possible to find body id %i in pointers array\n", id);
#endif #endif
return; // Prevent access to index -1 return; // Prevent access to index -1
} }
@ -861,11 +877,11 @@ PHYSACDEF void DestroyPhysicsBody(PhysicsBody body)
physicsBodiesCount--; physicsBodiesCount--;
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] destroyed physics body id %i\n", id);
TRACELOG("[PHYSAC] destroyed physics body id %i\n", id);
#endif #endif
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error trying to destroy a null referenced body\n");
else TRACELOG("[PHYSAC] error trying to destroy a null referenced body\n");
#endif #endif
} }
@ -903,7 +919,7 @@ PHYSACDEF void ResetPhysics(void)
physicsManifoldsCount = 0; physicsManifoldsCount = 0;
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] physics module reset successfully\n");
TRACELOG("[PHYSAC] physics module reset successfully\n");
#endif #endif
} }
@ -924,9 +940,9 @@ PHYSACDEF void ClosePhysics(void)
for (int i = physicsBodiesCount - 1; i >= 0; i--) DestroyPhysicsBody(bodies[i]); for (int i = physicsBodiesCount - 1; i >= 0; i--) DestroyPhysicsBody(bodies[i]);
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
if (physicsBodiesCount > 0 || usedMemory != 0) printf("[PHYSAC] physics module closed with %i still allocated bodies [MEMORY: %i bytes]\n", physicsBodiesCount, usedMemory);
else if (physicsManifoldsCount > 0 || usedMemory != 0) printf("[PHYSAC] physics module closed with %i still allocated manifolds [MEMORY: %i bytes]\n", physicsManifoldsCount, usedMemory);
else printf("[PHYSAC] physics module closed successfully\n");
if (physicsBodiesCount > 0 || usedMemory != 0) TRACELOG("[PHYSAC] physics module closed with %i still allocated bodies [MEMORY: %i bytes]\n", physicsBodiesCount, usedMemory);
else if (physicsManifoldsCount > 0 || usedMemory != 0) TRACELOG("[PHYSAC] physics module closed with %i still allocated manifolds [MEMORY: %i bytes]\n", physicsManifoldsCount, usedMemory);
else TRACELOG("[PHYSAC] physics module closed successfully\n");
#endif #endif
} }
@ -1018,7 +1034,7 @@ static void *PhysicsLoop(void *arg)
{ {
#if !defined(PHYSAC_NO_THREADS) #if !defined(PHYSAC_NO_THREADS)
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] physics thread created successfully\n");
TRACELOG("[PHYSAC] physics thread created successfully\n");
#endif #endif
// Initialize physics loop thread values // Initialize physics loop thread values
@ -1156,7 +1172,7 @@ PHYSACDEF void RunPhysicsStep(void)
while (accumulator >= deltaTime) while (accumulator >= deltaTime)
{ {
#ifdef PHYSAC_DEBUG #ifdef PHYSAC_DEBUG
//printf("currentTime %f, startTime %f, accumulator-pre %f, accumulator-post %f, delta %f, deltaTime %f\n",
//TRACELOG("currentTime %f, startTime %f, accumulator-pre %f, accumulator-post %f, delta %f, deltaTime %f\n",
// currentTime, startTime, accumulator, accumulator-deltaTime, delta, deltaTime); // currentTime, startTime, accumulator, accumulator-deltaTime, delta, deltaTime);
#endif #endif
PhysicsStep(); PhysicsStep();
@ -1228,7 +1244,7 @@ static PhysicsManifold CreatePhysicsManifold(PhysicsBody a, PhysicsBody b)
physicsManifoldsCount++; physicsManifoldsCount++;
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] new physics manifold creation failed because there is any available id to use\n");
else TRACELOG("[PHYSAC] new physics manifold creation failed because there is any available id to use\n");
#endif #endif
return newManifold; return newManifold;
@ -1254,7 +1270,7 @@ static void DestroyPhysicsManifold(PhysicsManifold manifold)
if (index == -1) if (index == -1)
{ {
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
printf("[PHYSAC] Not possible to manifold id %i in pointers array\n", id);
TRACELOG("[PHYSAC] Not possible to manifold id %i in pointers array\n", id);
#endif #endif
return; // Prevent access to index -1 return; // Prevent access to index -1
} }
@ -1274,7 +1290,7 @@ static void DestroyPhysicsManifold(PhysicsManifold manifold)
physicsManifoldsCount--; physicsManifoldsCount--;
} }
#if defined(PHYSAC_DEBUG) #if defined(PHYSAC_DEBUG)
else printf("[PHYSAC] error trying to destroy a null referenced manifold\n");
else TRACELOG("[PHYSAC] error trying to destroy a null referenced manifold\n");
#endif #endif
} }
@ -1923,9 +1939,9 @@ static void InitTimer(void)
} }
// Get hi-res MONOTONIC time measure in seconds // Get hi-res MONOTONIC time measure in seconds
static n">uint64_t GetTimeCount(void)
static kt">unsigned long long int GetTimeCount(void)
{ {
n">uint64_t value = 0;
kt">unsigned long long int value = 0;
#if defined(_WIN32) #if defined(_WIN32)
QueryPerformanceCounter((unsigned long long int *) &value); QueryPerformanceCounter((unsigned long long int *) &value);
@ -1934,7 +1950,7 @@ static uint64_t GetTimeCount(void)
#if defined(__linux__) #if defined(__linux__)
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
value = (n">uint64_t)now.tv_sec*(n">uint64_t)1000000000 + (n">uint64_t)now.tv_nsec;
value = (kt">unsigned long long int)now.tv_sec*(kt">unsigned long long int)1000000000 + (kt">unsigned long long int)now.tv_nsec;
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)

Ladataan…
Peruuta
Tallenna