25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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