您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

142 行
5.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics demo
  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] - Physics demo");
  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. // Create floor rectangle physics body
  36. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  37. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  38. // Create obstacle circle physics body
  39. PhysicsBody circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  40. circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. // Delay initialization of variables due to physics reset async
  49. RunPhysicsStep();
  50. if (needsReset)
  51. {
  52. floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  53. floor->enabled = false;
  54. circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  55. circle->enabled = false;
  56. needsReset = false;
  57. }
  58. // Reset physics input
  59. if (IsKeyPressed('R'))
  60. {
  61. ResetPhysics();
  62. needsReset = true;
  63. }
  64. // Physics body creation inputs
  65. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
  66. else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
  67. // Destroy falling physics bodies
  68. int bodiesCount = GetPhysicsBodiesCount();
  69. for (int i = bodiesCount - 1; i >= 0; i--)
  70. {
  71. PhysicsBody body = GetPhysicsBody(i);
  72. if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
  73. }
  74. //----------------------------------------------------------------------------------
  75. // Draw
  76. //----------------------------------------------------------------------------------
  77. BeginDrawing();
  78. ClearBackground(BLACK);
  79. DrawFPS(screenWidth - 90, screenHeight - 30);
  80. // Draw created physics bodies
  81. bodiesCount = GetPhysicsBodiesCount();
  82. for (int i = 0; i < bodiesCount; i++)
  83. {
  84. PhysicsBody body = GetPhysicsBody(i);
  85. if (body != NULL)
  86. {
  87. int vertexCount = GetPhysicsShapeVerticesCount(i);
  88. for (int j = 0; j < vertexCount; j++)
  89. {
  90. // Get physics bodies shape vertices to draw lines
  91. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  92. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  93. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  94. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  95. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  96. }
  97. }
  98. }
  99. DrawText("Left mouse button to create a polygon", 10, 10, 10, WHITE);
  100. DrawText("Right mouse button to create a circle", 10, 25, 10, WHITE);
  101. DrawText("Press 'R' to reset example", 10, 40, 10, WHITE);
  102. DrawText("Physac", logoX, logoY, 30, WHITE);
  103. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. ClosePhysics(); // Unitialize physics
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }