From 59038bae96bf8e9ae96ce3432ddbde96590f4642 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 5 Mar 2017 21:04:07 +0100 Subject: [PATCH 1/4] Added function: DrawLineEx() Supports line thickness --- examples/core_basic_window.c | 22 +++++++++++++++++++++- src/raylib.h | 15 ++++++++------- src/shapes.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/examples/core_basic_window.c b/examples/core_basic_window.c index fb83400aa..888f34609 100644 --- a/examples/core_basic_window.c +++ b/examples/core_basic_window.c @@ -39,6 +39,11 @@ int main() // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here + float dx = 600 - 100; + float dy = 105 - 405; + + float d = sqrtf(dx*dx + dy*dy); + float angle = asinf(dy/d); //---------------------------------------------------------------------------------- // Draw @@ -47,7 +52,22 @@ int main() ClearBackground(RAYWHITE); - DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); + //DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); + + DrawRectangle(100, 400, 1, 10, BLACK); + DrawRectangle(96, 404, 10, 1, BLACK); + + DrawRectangle(600, 100, 1, 10, BLACK); + DrawRectangle(596, 104, 10, 1, BLACK); + + DrawLine(100, 405, 600, 105, RED); + + // Draw lines using textures + /* + DrawTexturePro(GetDefaultTexture(), (Rectangle){ 0, 0, GetDefaultTexture().width, GetDefaultTexture().height }, + (Rectangle){ 100, 405, (float)GetDefaultTexture().width*d, 1 }, + (Vector2){ 0, (float)GetDefaultTexture().height/2 }, -RAD2DEG*angle, BLUE); + */ EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/src/raylib.h b/src/raylib.h index beda833c5..b0f03bbe9 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -98,13 +98,13 @@ #define RAD2DEG (180.0f/PI) // raylib Config Flags -#define FLAG_SHOW_LOGO 1 // Set this flag to show raylib logo at startup -#define FLAG_FULLSCREEN_MODE 2 // Set this flag to run program in fullscreen -#define FLAG_WINDOW_RESIZABLE 4 // Set this flag to allow resizable window -#define FLAG_WINDOW_DECORATED 8 // Set this flag to show window decoration (frame and buttons) -#define FLAG_WINDOW_TRANSPARENT 16 // Set this flag to allow transparent window -#define FLAG_MSAA_4X_HINT 32 // Set this flag to try enabling MSAA 4X -#define FLAG_VSYNC_HINT 64 // Set this flag to try enabling V-Sync on GPU +#define FLAG_SHOW_LOGO 1 // Set to show raylib logo at startup +#define FLAG_FULLSCREEN_MODE 2 // Set to run program in fullscreen +#define FLAG_WINDOW_RESIZABLE 4 // Set to allow resizable window +#define FLAG_WINDOW_DECORATED 8 // Set to show window decoration (frame and buttons) +#define FLAG_WINDOW_TRANSPARENT 16 // Set to allow transparent window +#define FLAG_MSAA_4X_HINT 32 // Set to try enabling MSAA 4X +#define FLAG_VSYNC_HINT 64 // Set to try enabling V-Sync on GPU // Keyboard Function Keys #define KEY_SPACE 32 @@ -763,6 +763,7 @@ RLAPI void DrawPixel(int posX, int posY, Color color); RLAPI void DrawPixelV(Vector2 position, Color color); // Draw a pixel (Vector version) RLAPI void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (Vector version) +RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line defining thickness RLAPI void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle RLAPI void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2); // Draw a gradient-filled circle RLAPI void DrawCircleV(Vector2 center, float radius, Color color); // Draw a color-filled circle (Vector version) diff --git a/src/shapes.c b/src/shapes.c index a42b05510..9cbe1da4d 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -103,6 +103,36 @@ void DrawLineV(Vector2 startPos, Vector2 endPos, Color color) rlEnd(); } +// Draw a line defining thickness +void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color) +{ + float dx = endPos.x - startPos.x; + float dy = endPos.y - startPos.y; + + float d = sqrtf(dx*dx + dy*dy); + float angle = asinf(dy/d); + + rlEnableTexture(GetDefaultTexture().id); + + rlPushMatrix(); + rlTranslatef((float)startPos.x, (float)startPos.y, 0); + rlRotatef(-RAD2DEG*angle, 0, 0, 1); + rlTranslatef(0, -thick/2.0f, 0); + + rlBegin(RL_QUADS); + rlColor4ub(color.r, color.g, color.b, color.a); + rlNormal3f(0.0f, 0.0f, 1.0f); + + rlVertex2f(0.0f, 0.0f); + rlVertex2f(0.0f, thick); + rlVertex2f(d, thick); + rlVertex2f(d, 0.0f); + rlEnd(); + rlPopMatrix(); + + rlDisableTexture(); +} + // Draw a color-filled circle void DrawCircle(int centerX, int centerY, float radius, Color color) { From b734802743f2089c8d649b27aea48ab71fa653b3 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 5 Mar 2017 21:05:17 +0100 Subject: [PATCH 2/4] Revert test code... --- examples/core_basic_window.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/examples/core_basic_window.c b/examples/core_basic_window.c index 888f34609..fb83400aa 100644 --- a/examples/core_basic_window.c +++ b/examples/core_basic_window.c @@ -39,11 +39,6 @@ int main() // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here - float dx = 600 - 100; - float dy = 105 - 405; - - float d = sqrtf(dx*dx + dy*dy); - float angle = asinf(dy/d); //---------------------------------------------------------------------------------- // Draw @@ -52,22 +47,7 @@ int main() ClearBackground(RAYWHITE); - //DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); - - DrawRectangle(100, 400, 1, 10, BLACK); - DrawRectangle(96, 404, 10, 1, BLACK); - - DrawRectangle(600, 100, 1, 10, BLACK); - DrawRectangle(596, 104, 10, 1, BLACK); - - DrawLine(100, 405, 600, 105, RED); - - // Draw lines using textures - /* - DrawTexturePro(GetDefaultTexture(), (Rectangle){ 0, 0, GetDefaultTexture().width, GetDefaultTexture().height }, - (Rectangle){ 100, 405, (float)GetDefaultTexture().width*d, 1 }, - (Vector2){ 0, (float)GetDefaultTexture().height/2 }, -RAD2DEG*angle, BLUE); - */ + DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); EndDrawing(); //---------------------------------------------------------------------------------- From f88a94341891b1969ba58dd7ab88e6b3c69cabf2 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 6 Mar 2017 09:58:28 +0100 Subject: [PATCH 3/4] Fix bug in isGrounded state calculations --- src/physac.h | 80 ++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 68 deletions(-) diff --git a/src/physac.h b/src/physac.h index cb0e3f3c7..a6b529bc0 100644 --- a/src/physac.h +++ b/src/physac.h @@ -361,70 +361,6 @@ PHYSACDEF PhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float d { PhysicsBody newBody = CreatePhysicsBodyPolygon(pos, radius, PHYSAC_CIRCLE_VERTICES, density); return newBody; - - /*PhysicsBody newBody = (PhysicsBody)PHYSAC_MALLOC(sizeof(PhysicsBodyData)); - usedMemory += sizeof(PhysicsBodyData); - - int newId = -1; - for (int i = 0; i < PHYSAC_MAX_BODIES; i++) - { - int currentId = i; - - // Check if current id already exist in other physics body - for (int k = 0; k < physicsBodiesCount; k++) - { - if (bodies[k]->id == currentId) - { - currentId++; - break; - } - } - - // If it is not used, use it as new physics body id - if (currentId == i) - { - newId = i; - break; - } - } - - if (newId != -1) - { - // Initialize new body with generic values - newBody->id = newId; - newBody->enabled = true; - newBody->position = pos; - newBody->velocity = (Vector2){ 0 }; - newBody->force = (Vector2){ 0 }; - newBody->angularVelocity = 0; - newBody->torque = 0; - newBody->orient = 0; - newBody->mass = PHYSAC_PI*radius*radius*density; - newBody->inverseMass = ((newBody->mass != 0.0f) ? 1.0f/newBody->mass : 0.0f); - newBody->inertia = newBody->mass*radius*radius; - newBody->inverseInertia = ((newBody->inertia != 0.0f) ? 1.0f/newBody->inertia : 0.0f); - newBody->staticFriction = 0; - newBody->dynamicFriction = 0; - newBody->restitution = 0; - newBody->useGravity = true; - newBody->freezeOrient = false; - newBody->shape.type = PHYSICS_CIRCLE; - newBody->shape.body = newBody; - newBody->shape.radius = radius; - - // Add new body to bodies pointers array and update bodies count - bodies[physicsBodiesCount] = newBody; - physicsBodiesCount++; - - #if defined(PHYSAC_DEBUG) - printf("[PHYSAC] created circle physics body id %i\n", newBody->id); - #endif - } - #if defined(PHYSAC_DEBUG) - else printf("[PHYSAC] new physics body creation failed because there is any available id to use\n"); - #endif - - return newBody;*/ } // Creates a new rectangle physics body with generic parameters @@ -1130,6 +1066,7 @@ static void *PhysicsLoop(void *arg) // Physics steps calculations (dynamics, collisions and position corrections) static void PhysicsStep(void) { + // Update current steps count stepsCount++; // Clear previous generated collisions information @@ -1138,6 +1075,13 @@ static void PhysicsStep(void) PhysicsManifold manifold = contacts[i]; if (manifold != NULL) DestroyPhysicsManifold(manifold); } + + // Reset physics bodies grounded state + for (int i = 0; i < physicsBodiesCount; i++) + { + PhysicsBody body = bodies[i]; + body->isGrounded = false; + } // Generate new collision information for (int i = 0; i < physicsBodiesCount; i++) @@ -1347,9 +1291,9 @@ static void SolvePhysicsManifold(PhysicsManifold manifold) } break; default: break; } - - // Update physics body grounded state if normal direction is downside - manifold->bodyB->isGrounded = (manifold->normal.y < 0); + + // Update physics body grounded state if normal direction is down and grounded state is not set yet in previous manifolds + if (!manifold->bodyB->isGrounded) manifold->bodyB->isGrounded = (manifold->normal.y < 0); } // Solves collision between two circle shape physics bodies @@ -1388,7 +1332,7 @@ static void SolveCircleToCircle(PhysicsManifold manifold) } // Update physics body grounded state if normal direction is down - if (manifold->normal.y < 0) bodyA->isGrounded = true; + if (!bodyA->isGrounded) bodyA->isGrounded = (manifold->normal.y < 0); } // Solves collision between a circle to a polygon shape physics bodies From c964559bc966292a7d70f00559ea80c224aab96d Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 6 Mar 2017 22:57:33 +0100 Subject: [PATCH 4/4] Update physac source and examples with new changes --- examples/physics_demo.c | 7 +++++-- examples/physics_friction.c | 7 +++++-- examples/physics_movement.c | 7 +++++-- examples/physics_restitution.c | 7 +++++-- examples/physics_shatter.c | 7 +++++-- src/physac.h | 9 ++++++++- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/examples/physics_demo.c b/examples/physics_demo.c index de8d515ee..b12ac7088 100644 --- a/examples/physics_demo.c +++ b/examples/physics_demo.c @@ -3,9 +3,12 @@ * Physac - Physics demo * * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. -* The file pthreadGC2.dll is required to run the program; you can find it in 'src\external' * -* Copyright (c) 2016 Victor Fisac +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* +* Copyright (c) 2017 Victor Fisac * ********************************************************************************************/ diff --git a/examples/physics_friction.c b/examples/physics_friction.c index a4baad539..db1b5f4c1 100644 --- a/examples/physics_friction.c +++ b/examples/physics_friction.c @@ -3,9 +3,12 @@ * Physac - Physics friction * * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. -* The file pthreadGC2.dll is required to run the program; you can find it in 'src\external' * -* Copyright (c) 2016 Victor Fisac +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* +* Copyright (c) 2017 Victor Fisac * ********************************************************************************************/ diff --git a/examples/physics_movement.c b/examples/physics_movement.c index ee97845f4..3345404d6 100644 --- a/examples/physics_movement.c +++ b/examples/physics_movement.c @@ -3,9 +3,12 @@ * Physac - Physics movement * * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. -* The file pthreadGC2.dll is required to run the program; you can find it in 'src\external' * -* Copyright (c) 2016 Victor Fisac +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* +* Copyright (c) 2017 Victor Fisac * ********************************************************************************************/ diff --git a/examples/physics_restitution.c b/examples/physics_restitution.c index 378f6f24e..534d125e0 100644 --- a/examples/physics_restitution.c +++ b/examples/physics_restitution.c @@ -3,9 +3,12 @@ * Physac - Physics restitution * * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. -* The file pthreadGC2.dll is required to run the program; you can find it in 'src\external' * -* Copyright (c) 2016 Victor Fisac +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* +* Copyright (c) 2017 Victor Fisac * ********************************************************************************************/ diff --git a/examples/physics_shatter.c b/examples/physics_shatter.c index 637a163e5..fac907148 100644 --- a/examples/physics_shatter.c +++ b/examples/physics_shatter.c @@ -3,9 +3,12 @@ * Physac - Body shatter * * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. -* The file pthreadGC2.dll is required to run the program; you can find it in 'src\external' * -* Copyright (c) 2016 Victor Fisac +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* +* Copyright (c) 2017 Victor Fisac * ********************************************************************************************/ diff --git a/src/physac.h b/src/physac.h index a6b529bc0..ff56615d7 100644 --- a/src/physac.h +++ b/src/physac.h @@ -38,13 +38,20 @@ * You can define your own malloc/free implementation replacing stdlib.h malloc()/free() functions. * Otherwise it will include stdlib.h and use the C standard library malloc()/free() function. * +* +* NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. +* +* Use the following code to compile (-static -lpthread): +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread +* -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition +* * VERY THANKS TO: * Ramón Santamaria (@raysan5) * * * LICENSE: zlib/libpng * -* Copyright (c) 2016 Victor Fisac +* Copyright (c) 2017 Victor Fisac * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software.