core_delta_time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - delta time
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Robin (@RobinsAviary)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time");
  28. int currentFps = 60;
  29. // Store the position for the both of the circles
  30. Vector2 deltaCircle = { 0, (float)screenHeight/3.0f };
  31. Vector2 frameCircle = { 0, (float)screenHeight*(2.0f/3.0f) };
  32. // The speed applied to both circles
  33. const float speed = 10.0f;
  34. const float circleRadius = 32.0f;
  35. SetTargetFPS(currentFps);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. // Adjust the FPS target based on the mouse wheel
  43. float mouseWheel = GetMouseWheelMove();
  44. if (mouseWheel != 0)
  45. {
  46. currentFps += (int)mouseWheel;
  47. if (currentFps < 0) currentFps = 0;
  48. SetTargetFPS(currentFps);
  49. }
  50. // GetFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time)
  51. // Uses the delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS
  52. // Multiply by 6.0 (an arbitrary value) in order to make the speed
  53. // visually closer to the other circle (at 60 fps), for comparison
  54. deltaCircle.x += GetFrameTime()*6.0f*speed;
  55. // This circle can move faster or slower visually depending on the FPS
  56. frameCircle.x += 0.1f*speed;
  57. // If either circle is off the screen, reset it back to the start
  58. if (deltaCircle.x > screenWidth) deltaCircle.x = 0;
  59. if (frameCircle.x > screenWidth) frameCircle.x = 0;
  60. // Reset both circles positions
  61. if (IsKeyPressed(KEY_R))
  62. {
  63. deltaCircle.x = 0;
  64. frameCircle.x = 0;
  65. }
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. // Draw both circles to the screen
  72. DrawCircleV(deltaCircle, circleRadius, RED);
  73. DrawCircleV(frameCircle, circleRadius, BLUE);
  74. // Draw the help text
  75. // Determine what help text to show depending on the current FPS target
  76. const char *fpsText = 0;
  77. if (currentFps <= 0) fpsText = TextFormat("FPS: unlimited (%i)", GetFPS());
  78. else fpsText = TextFormat("FPS: %i (target: %i)", GetFPS(), currentFps);
  79. DrawText(fpsText, 10, 10, 20, DARKGRAY);
  80. DrawText(TextFormat("Frame time: %02.02f ms", GetFrameTime()), 10, 30, 20, DARKGRAY);
  81. DrawText("Use the scroll wheel to change the fps limit, r to reset", 10, 50, 20, DARKGRAY);
  82. // Draw the text above the circles
  83. DrawText("FUNC: x += GetFrameTime()*speed", 10, 90, 20, RED);
  84. DrawText("FUNC: x += speed", 10, 240, 20, BLUE);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }