You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * Physac - Body shatter
  4. *
  5. * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
  6. *
  7. * Use the following code to compile (-static -lpthread):
  8. * gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread
  9. * -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
  10. *
  11. * Copyright (c) 2017 Victor Fisac
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #define PHYSAC_IMPLEMENTATION
  16. #include "../src/physac.h"
  17. int main()
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. int screenWidth = 800;
  22. int screenHeight = 450;
  23. SetConfigFlags(FLAG_MSAA_4X_HINT);
  24. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Body shatter");
  25. SetTargetFPS(60);
  26. // Physac logo drawing position
  27. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  28. int logoY = 15;
  29. // Initialize physics and default physics bodies
  30. InitPhysics();
  31. SetPhysicsGravity(0, 0);
  32. // Create random polygon physics body to shatter
  33. PhysicsBody body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. if (IsKeyPressed('R')) // Reset physics input
  41. {
  42. ResetPhysics();
  43. // Create random polygon physics body to shatter
  44. body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  45. }
  46. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
  47. {
  48. // Note: some values need to be stored in variables due to asynchronous changes during main thread
  49. int count = GetPhysicsBodiesCount();
  50. for (int i = count - 1; i >= 0; i--)
  51. {
  52. PhysicsBody currentBody = GetPhysicsBody(i);
  53. if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
  54. }
  55. }
  56. //----------------------------------------------------------------------------------
  57. // Draw
  58. //----------------------------------------------------------------------------------
  59. BeginDrawing();
  60. ClearBackground(BLACK);
  61. // Draw created physics bodies
  62. int bodiesCount = GetPhysicsBodiesCount();
  63. for (int i = 0; i < bodiesCount; i++)
  64. {
  65. PhysicsBody currentBody = GetPhysicsBody(i);
  66. int vertexCount = GetPhysicsShapeVerticesCount(i);
  67. for (int j = 0; j < vertexCount; j++)
  68. {
  69. // Get physics bodies shape vertices to draw lines
  70. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  71. Vector2 vertexA = GetPhysicsShapeVertex(currentBody, j);
  72. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  73. Vector2 vertexB = GetPhysicsShapeVertex(currentBody, jj);
  74. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  75. }
  76. }
  77. DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, WHITE);
  78. DrawText("Physac", logoX, logoY, 30, WHITE);
  79. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. ClosePhysics(); // Unitialize physics
  86. CloseWindow(); // Close window and OpenGL context
  87. //--------------------------------------------------------------------------------------
  88. return 0;
  89. }