core_custom_frame_control.c 5.7 KB

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