core_custom_frame_control.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - custom frame control
  4. *
  5. * 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. * This example has been created using raylib 3.8 (www.raylib.com)
  21. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  22. *
  23. * Copyright (c) 2021 Ramon Santamaria (@raysan5)
  24. *
  25. ********************************************************************************************/
  26. #include "raylib.h"
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom frame control");
  34. // Custom timming variables
  35. double previousTime = GetTime(); // Previous time measure
  36. double currentTime = 0.0; // Current time measure
  37. double updateDrawTime = 0.0; // Update + Draw time
  38. double waitTime = 0.0; // Wait time (if target fps required)
  39. float deltaTime = 0.0f; // Frame time (Update + Draw + Wait time)
  40. float timeCounter = 0.0f; // Accumulative time counter (seconds)
  41. float position = 0.0f; // Circle position
  42. bool pause = false; // Pause control flag
  43. int targetFPS = 60; // Our initial target fps
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. PollInputEvents(); // Poll input events (SUPPORT_CUSTOM_FRAME_CONTROL)
  51. if (IsKeyPressed(KEY_SPACE)) pause = !pause;
  52. if (IsKeyPressed(KEY_UP)) targetFPS += 20;
  53. else if (IsKeyPressed(KEY_DOWN)) targetFPS -= 20;
  54. if (targetFPS < 0) targetFPS = 0;
  55. if (!pause)
  56. {
  57. position += 200*deltaTime; // We move at 200 pixels per second
  58. if (position >= GetScreenWidth()) position = 0;
  59. timeCounter += deltaTime; // We count time (seconds)
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. for (int i = 0; i < GetScreenWidth()/200; i++) DrawRectangle(200*i, 0, 1, GetScreenHeight(), SKYBLUE);
  67. DrawCircle((int)position, GetScreenHeight()/2 - 25, 50, RED);
  68. DrawText(TextFormat("%03.0f ms", timeCounter*1000.0f), position - 40, GetScreenHeight()/2 - 100, 20, MAROON);
  69. DrawText(TextFormat("PosX: %03.0f", position), position - 50, GetScreenHeight()/2 + 40, 20, BLACK);
  70. DrawText("Circle is moving at a constant 200 pixels/sec,\nindependently of the frame rate.", 10, 10, 20, DARKGRAY);
  71. DrawText("PRESS SPACE to PAUSE MOVEMENT", 10, GetScreenHeight() - 60, 20, GRAY);
  72. DrawText("PRESS UP | DOWN to CHANGE TARGET FPS", 10, GetScreenHeight() - 30, 20, GRAY);
  73. DrawText(TextFormat("TARGET FPS: %i", targetFPS), GetScreenWidth() - 220, 10, 20, LIME);
  74. DrawText(TextFormat("CURRENT FPS: %i", (int)(1.0f/deltaTime)), GetScreenWidth() - 220, 40, 20, GREEN);
  75. EndDrawing();
  76. // NOTE: In case raylib is configured to SUPPORT_CUSTOM_FRAME_CONTROL,
  77. // Events polling, screen buffer swap and frame time control must be managed by the user
  78. SwapScreenBuffer(); // Flip the back buffer to screen (front buffer)
  79. currentTime = GetTime();
  80. updateDrawTime = currentTime - previousTime;
  81. if (targetFPS > 0) // We want a fixed frame rate
  82. {
  83. waitTime = (1.0f/(float)targetFPS) - updateDrawTime;
  84. if (waitTime > 0.0)
  85. {
  86. WaitTime((float)waitTime*1000.0f);
  87. currentTime = GetTime();
  88. deltaTime = (float)(currentTime - previousTime);
  89. }
  90. }
  91. else deltaTime = updateDrawTime; // Framerate could be variable
  92. previousTime = currentTime;
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. CloseWindow(); // Close window and OpenGL context
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }