core_custom_frame_control.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - custom frame control
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * NOTE: WARNING: This is an example for advanced users willing to have full control over
  8. * the frame processes. By default, EndDrawing() calls the following processes:
  9. * 1. Draw remaining batch data: rlDrawRenderBatchActive()
  10. * 2. SwapScreenBuffer()
  11. * 3. Frame time control: WaitTime()
  12. * 4. PollInputEvents()
  13. *
  14. * To avoid steps 2, 3 and 4, flag SUPPORT_CUSTOM_FRAME_CONTROL can be enabled in
  15. * config.h (it requires recompiling raylib). This way those steps are up to the user.
  16. *
  17. * Note that enabling this flag invalidates some functions:
  18. * - GetFrameTime()
  19. * - SetTargetFPS()
  20. * - GetFPS()
  21. *
  22. * Example originally created with raylib 4.0, last time updated with raylib 4.0
  23. *
  24. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  25. * BSD-like license that allows static linking with closed source software
  26. *
  27. * Copyright (c) 2021-2025 Ramon Santamaria (@raysan5)
  28. *
  29. ********************************************************************************************/
  30. #include "raylib.h"
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom frame control");
  41. // Custom timming variables
  42. double previousTime = GetTime(); // Previous time measure
  43. double currentTime = 0.0; // Current time measure
  44. double updateDrawTime = 0.0; // Update + Draw time
  45. double waitTime = 0.0; // Wait time (if target fps required)
  46. float deltaTime = 0.0f; // Frame time (Update + Draw + Wait time)
  47. float timeCounter = 0.0f; // Accumulative time counter (seconds)
  48. float position = 0.0f; // Circle position
  49. bool pause = false; // Pause control flag
  50. int targetFPS = 60; // Our initial target fps
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. #ifndef PLATFORM_WEB // NOTE: On non web platforms the PollInputEvents just works before the inputs checks
  58. PollInputEvents(); // Poll input events (SUPPORT_CUSTOM_FRAME_CONTROL)
  59. #endif
  60. if (IsKeyPressed(KEY_SPACE)) pause = !pause;
  61. if (IsKeyPressed(KEY_UP)) targetFPS += 20;
  62. else if (IsKeyPressed(KEY_DOWN)) targetFPS -= 20;
  63. if (targetFPS < 0) targetFPS = 0;
  64. if (!pause)
  65. {
  66. position += 200*deltaTime; // We move at 200 pixels per second
  67. if (position >= GetScreenWidth()) position = 0;
  68. timeCounter += deltaTime; // We count time (seconds)
  69. }
  70. #ifdef PLATFORM_WEB // NOTE: On web platform for some reason the PollInputEvents only works after the inputs check, so just call it after check all your inputs (on web)
  71. PollInputEvents(); // Poll input events (SUPPORT_CUSTOM_FRAME_CONTROL)
  72. #endif
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. for (int i = 0; i < GetScreenWidth()/200; i++) DrawRectangle(200*i, 0, 1, GetScreenHeight(), SKYBLUE);
  79. DrawCircle((int)position, GetScreenHeight()/2 - 25, 50, RED);
  80. DrawText(TextFormat("%03.0f ms", timeCounter*1000.0f), (int)position - 40, GetScreenHeight()/2 - 100, 20, MAROON);
  81. DrawText(TextFormat("PosX: %03.0f", position), (int)position - 50, GetScreenHeight()/2 + 40, 20, BLACK);
  82. DrawText("Circle is moving at a constant 200 pixels/sec,\nindependently of the frame rate.", 10, 10, 20, DARKGRAY);
  83. DrawText("PRESS SPACE to PAUSE MOVEMENT", 10, GetScreenHeight() - 60, 20, GRAY);
  84. DrawText("PRESS UP | DOWN to CHANGE TARGET FPS", 10, GetScreenHeight() - 30, 20, GRAY);
  85. DrawText(TextFormat("TARGET FPS: %i", targetFPS), GetScreenWidth() - 220, 10, 20, LIME);
  86. if (deltaTime != 0)
  87. {
  88. DrawText(TextFormat("CURRENT FPS: %i", (int)(1.0f/deltaTime)), GetScreenWidth() - 220, 40, 20, GREEN);
  89. }
  90. EndDrawing();
  91. // NOTE: In case raylib is configured to SUPPORT_CUSTOM_FRAME_CONTROL,
  92. // Events polling, screen buffer swap and frame time control must be managed by the user
  93. SwapScreenBuffer(); // Flip the back buffer to screen (front buffer)
  94. currentTime = GetTime();
  95. updateDrawTime = currentTime - previousTime;
  96. if (targetFPS > 0) // We want a fixed frame rate
  97. {
  98. waitTime = (1.0f/(float)targetFPS) - updateDrawTime;
  99. if (waitTime > 0.0)
  100. {
  101. WaitTime((float)waitTime);
  102. currentTime = GetTime();
  103. deltaTime = (float)(currentTime - previousTime);
  104. }
  105. }
  106. else deltaTime = (float)updateDrawTime; // Framerate could be variable
  107. previousTime = currentTime;
  108. //----------------------------------------------------------------------------------
  109. }
  110. // De-Initialization
  111. //--------------------------------------------------------------------------------------
  112. CloseWindow(); // Close window and OpenGL context
  113. //--------------------------------------------------------------------------------------
  114. return 0;
  115. }