Review struct typedef to avoid pointers for users
This commit is contained in:
parent
03cc031d00
commit
c9e30f7754
4 changed files with 28 additions and 27 deletions
|
@ -30,26 +30,26 @@ int main()
|
||||||
bool isDebug = false;
|
bool isDebug = false;
|
||||||
|
|
||||||
// Create rectangle physic object
|
// Create rectangle physic object
|
||||||
PhysicObject *rectangle = CreatePhysicObject((Vector2){ screenWidth*0.25f, screenHeight/2 }, 0.0f, (Vector2){ 75, 50 });
|
PhysicObject rectangle = CreatePhysicObject((Vector2){ screenWidth*0.25f, screenHeight/2 }, 0.0f, (Vector2){ 75, 50 });
|
||||||
rectangle->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
|
rectangle->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
|
||||||
rectangle->rigidbody.applyGravity = true;
|
rectangle->rigidbody.applyGravity = true;
|
||||||
rectangle->rigidbody.friction = 0.1f;
|
rectangle->rigidbody.friction = 0.1f;
|
||||||
rectangle->rigidbody.bounciness = 6.0f;
|
rectangle->rigidbody.bounciness = 6.0f;
|
||||||
|
|
||||||
// Create square physic object
|
// Create square physic object
|
||||||
PhysicObject *square = CreatePhysicObject((Vector2){ screenWidth*0.75f, screenHeight/2 }, 0.0f, (Vector2){ 50, 50 });
|
PhysicObject square = CreatePhysicObject((Vector2){ screenWidth*0.75f, screenHeight/2 }, 0.0f, (Vector2){ 50, 50 });
|
||||||
square->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
|
square->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
|
||||||
square->rigidbody.applyGravity = true;
|
square->rigidbody.applyGravity = true;
|
||||||
square->rigidbody.friction = 0.1f;
|
square->rigidbody.friction = 0.1f;
|
||||||
|
|
||||||
// Create walls physic objects
|
// Create walls physic objects
|
||||||
PhysicObject *floor = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.95f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
|
PhysicObject floor = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.95f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
|
||||||
PhysicObject *leftWall = CreatePhysicObject((Vector2){ 0.0f, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
|
PhysicObject leftWall = CreatePhysicObject((Vector2){ 0.0f, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
|
||||||
PhysicObject *rightWall = CreatePhysicObject((Vector2){ screenWidth, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
|
PhysicObject rightWall = CreatePhysicObject((Vector2){ screenWidth, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
|
||||||
PhysicObject *roof = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.05f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
|
PhysicObject roof = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.05f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
|
||||||
|
|
||||||
// Create pplatform physic object
|
// Create pplatform physic object
|
||||||
PhysicObject *platform = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
|
PhysicObject platform = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -114,7 +114,8 @@ int main()
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
ClosePhysics(); // Unitialize physics module
|
ClosePhysics(); // Unitialize physics (including all loaded objects)
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#define LINE_LENGTH 75
|
#define LINE_LENGTH 75
|
||||||
#define TRIANGLE_LENGTH 12
|
#define TRIANGLE_LENGTH 12
|
||||||
|
|
||||||
void DrawRigidbodyCircle(PhysicObject *obj, Color color);
|
void DrawRigidbodyCircle(PhysicObject obj, Color color);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -36,7 +36,7 @@ int main()
|
||||||
bool isDebug = false;
|
bool isDebug = false;
|
||||||
|
|
||||||
// Create rectangle physic objects
|
// Create rectangle physic objects
|
||||||
PhysicObject *rectangles[3];
|
PhysicObject rectangles[3];
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
rectangles[i] = CreatePhysicObject((Vector2){ screenWidth/4*(i+1), (((i % 2) == 0) ? (screenHeight/3) : (screenHeight/1.5f)) }, 0.0f, (Vector2){ 50, 50 });
|
rectangles[i] = CreatePhysicObject((Vector2){ screenWidth/4*(i+1), (((i % 2) == 0) ? (screenHeight/3) : (screenHeight/1.5f)) }, 0.0f, (Vector2){ 50, 50 });
|
||||||
|
@ -46,7 +46,7 @@ int main()
|
||||||
|
|
||||||
// Create circles physic objects
|
// Create circles physic objects
|
||||||
// NOTE: when creating circle physic objects, transform.scale must be { 0, 0 } and object radius must be defined in collider.radius and use this value to draw the circle.
|
// NOTE: when creating circle physic objects, transform.scale must be { 0, 0 } and object radius must be defined in collider.radius and use this value to draw the circle.
|
||||||
PhysicObject *circles[3];
|
PhysicObject circles[3];
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
circles[i] = CreatePhysicObject((Vector2){ screenWidth/4*(i+1), (((i % 2) == 0) ? (screenHeight/1.5f) : (screenHeight/4)) }, 0.0f, (Vector2){ 0, 0 });
|
circles[i] = CreatePhysicObject((Vector2){ screenWidth/4*(i+1), (((i % 2) == 0) ? (screenHeight/1.5f) : (screenHeight/4)) }, 0.0f, (Vector2){ 0, 0 });
|
||||||
|
@ -57,10 +57,10 @@ int main()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create walls physic objects
|
// Create walls physic objects
|
||||||
PhysicObject *leftWall = CreatePhysicObject((Vector2){ -25, screenHeight/2 }, 0.0f, (Vector2){ 50, screenHeight });
|
PhysicObject leftWall = CreatePhysicObject((Vector2){ -25, screenHeight/2 }, 0.0f, (Vector2){ 50, screenHeight });
|
||||||
PhysicObject *rightWall = CreatePhysicObject((Vector2){ screenWidth + 25, screenHeight/2 }, 0.0f, (Vector2){ 50, screenHeight });
|
PhysicObject rightWall = CreatePhysicObject((Vector2){ screenWidth + 25, screenHeight/2 }, 0.0f, (Vector2){ 50, screenHeight });
|
||||||
PhysicObject *topWall = CreatePhysicObject((Vector2){ screenWidth/2, -25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
PhysicObject topWall = CreatePhysicObject((Vector2){ screenWidth/2, -25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
||||||
PhysicObject *bottomWall = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight + 25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
PhysicObject bottomWall = CreatePhysicObject((Vector2){ screenWidth/2, screenHeight + 25 }, 0.0f, (Vector2){ screenWidth, 50 });
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
12
src/physac.c
12
src/physac.c
|
@ -49,7 +49,7 @@
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
static PhysicObject *physicObjects[MAX_PHYSIC_OBJECTS]; // Physic objects pool
|
static PhysicObject physicObjects[MAX_PHYSIC_OBJECTS]; // Physic objects pool
|
||||||
static int physicObjectsCount; // Counts current enabled physic objects
|
static int physicObjectsCount; // Counts current enabled physic objects
|
||||||
static Vector2 gravityForce; // Gravity force
|
static Vector2 gravityForce; // Gravity force
|
||||||
|
|
||||||
|
@ -463,10 +463,10 @@ void ClosePhysics()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new physic object dinamically, initialize it and add to pool
|
// Create a new physic object dinamically, initialize it and add to pool
|
||||||
PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale)
|
PhysicObject CreatePhysicObject(Vector2 position, float rotation, Vector2 scale)
|
||||||
{
|
{
|
||||||
// Allocate dynamic memory
|
// Allocate dynamic memory
|
||||||
PhysicObject *obj = (PhysicObject *)malloc(sizeof(PhysicObject));
|
PhysicObject obj = (PhysicObject)malloc(sizeof(PhysicObjectData));
|
||||||
|
|
||||||
// Initialize physic object values with generic values
|
// Initialize physic object values with generic values
|
||||||
obj->id = physicObjectsCount;
|
obj->id = physicObjectsCount;
|
||||||
|
@ -498,7 +498,7 @@ PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy a specific physic object and take it out of the list
|
// Destroy a specific physic object and take it out of the list
|
||||||
void DestroyPhysicObject(PhysicObject *pObj)
|
void DestroyPhysicObject(PhysicObject pObj)
|
||||||
{
|
{
|
||||||
// Free dynamic memory allocation
|
// Free dynamic memory allocation
|
||||||
free(physicObjects[pObj->id]);
|
free(physicObjects[pObj->id]);
|
||||||
|
@ -520,7 +520,7 @@ void DestroyPhysicObject(PhysicObject *pObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply directional force to a physic object
|
// Apply directional force to a physic object
|
||||||
void ApplyForce(PhysicObject *pObj, Vector2 force)
|
void ApplyForce(PhysicObject pObj, Vector2 force)
|
||||||
{
|
{
|
||||||
if (pObj->rigidbody.enabled)
|
if (pObj->rigidbody.enabled)
|
||||||
{
|
{
|
||||||
|
@ -571,7 +571,7 @@ Rectangle TransformToRectangle(Transform transform)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw physic object information at screen position
|
// Draw physic object information at screen position
|
||||||
void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize)
|
void DrawPhysicObjectInfo(PhysicObject pObj, Vector2 position, int fontSize)
|
||||||
{
|
{
|
||||||
// Draw physic object ID
|
// Draw physic object ID
|
||||||
DrawText(FormatText("PhysicObject ID: %i - Enabled: %i", pObj->id, pObj->enabled), position.x, position.y, fontSize, BLACK);
|
DrawText(FormatText("PhysicObject ID: %i - Enabled: %i", pObj->id, pObj->enabled), position.x, position.y, fontSize, BLACK);
|
||||||
|
|
12
src/raylib.h
12
src/raylib.h
|
@ -535,13 +535,13 @@ typedef struct Collider {
|
||||||
int radius; // Used for COLLIDER_CIRCLE
|
int radius; // Used for COLLIDER_CIRCLE
|
||||||
} Collider;
|
} Collider;
|
||||||
|
|
||||||
typedef struct PhysicObject {
|
typedef struct PhysicObjectData {
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
Transform transform;
|
Transform transform;
|
||||||
Rigidbody rigidbody;
|
Rigidbody rigidbody;
|
||||||
Collider collider;
|
Collider collider;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
} PhysicObject;
|
} PhysicObjectData, *PhysicObject;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" { // Prevents name mangling of functions
|
extern "C" { // Prevents name mangling of functions
|
||||||
|
@ -856,14 +856,14 @@ void InitPhysics(Vector2 gravity);
|
||||||
void UpdatePhysics(); // Update physic objects, calculating physic behaviours and collisions detection
|
void UpdatePhysics(); // Update physic objects, calculating physic behaviours and collisions detection
|
||||||
void ClosePhysics(); // Unitialize all physic objects and empty the objects pool
|
void ClosePhysics(); // Unitialize all physic objects and empty the objects pool
|
||||||
|
|
||||||
PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool
|
PhysicObject CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool
|
||||||
void DestroyPhysicObject(PhysicObject *pObj); // Destroy a specific physic object and take it out of the list
|
void DestroyPhysicObject(PhysicObject pObj); // Destroy a specific physic object and take it out of the list
|
||||||
|
|
||||||
void ApplyForce(PhysicObject *pObj, Vector2 force); // Apply directional force to a physic object
|
void ApplyForce(PhysicObject pObj, Vector2 force); // Apply directional force to a physic object
|
||||||
void ApplyForceAtPosition(Vector2 position, float force, float radius); // Apply radial force to all physic objects in range
|
void ApplyForceAtPosition(Vector2 position, float force, float radius); // Apply radial force to all physic objects in range
|
||||||
|
|
||||||
Rectangle TransformToRectangle(Transform transform); // Convert Transform data type to Rectangle (position and scale)
|
Rectangle TransformToRectangle(Transform transform); // Convert Transform data type to Rectangle (position and scale)
|
||||||
void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize); // Draw physic object information at screen position
|
void DrawPhysicObjectInfo(PhysicObject pObj, Vector2 position, int fontSize); // Draw physic object information at screen position
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Audio Loading and Playing Functions (Module: audio)
|
// Audio Loading and Playing Functions (Module: audio)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue