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.

121 lines
4.7 KiB

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