From a7271fb9faa52289b44b48ee481c16fd7e878e44 Mon Sep 17 00:00:00 2001 From: RobinsAviary Date: Mon, 29 Sep 2025 12:17:58 -0400 Subject: [PATCH] delta example --- examples/core/core_delta_time.c | 155 ++++++++++++++++++++++++++++++ examples/core/core_delta_time.png | Bin 0 -> 16482 bytes 2 files changed, 155 insertions(+) create mode 100644 examples/core/core_delta_time.c create mode 100644 examples/core/core_delta_time.png diff --git a/examples/core/core_delta_time.c b/examples/core/core_delta_time.c new file mode 100644 index 000000000..5e327d2fa --- /dev/null +++ b/examples/core/core_delta_time.c @@ -0,0 +1,155 @@ +/* +4. Code should follow raylib conventions: https://github.com/raysan5/raylib/wiki/raylib-coding-conventions +Try to be very organized, using line-breaks appropiately + +/******************************************************************************************* +* +* raylib [core] example - delta time +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.5 +* +* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025-2025 Robin (@RobinsAviary) +* +********************************************************************************************/ + +#include "raylib.h" + +int currentFps = 60; + +// Used only in this example in order to allow finer control of the FPS limit. +void UpdateFPS(int fps) +{ + currentFps = fps; + SetTargetFPS(currentFps); +} + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time"); + + // The x positions for each circle. + float deltaX = 0; + float frameX = 0; + + // The speed applied to both circles. + const float speed = 10.0; + const int circleRadius = 32; + + // Calculate the visual Y position for both circles. + const float deltaY = screenHeight / 3.0; + const float frameY = screenHeight * (2.0/3.0); + + SetTargetFPS(currentFps); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + + // Update the target FPS based on number key. + if (IsKeyPressed(KEY_ONE)) UpdateFPS(10); + if (IsKeyPressed(KEY_TWO)) UpdateFPS(20); + if (IsKeyPressed(KEY_THREE)) UpdateFPS(30); + if (IsKeyPressed(KEY_FOUR)) UpdateFPS(40); + if (IsKeyPressed(KEY_FIVE)) UpdateFPS(50); + if (IsKeyPressed(KEY_SIX)) UpdateFPS(60); + if (IsKeyPressed(KEY_SEVEN)) UpdateFPS(70); + if (IsKeyPressed(KEY_EIGHT)) UpdateFPS(80); + if (IsKeyPressed(KEY_NINE)) UpdateFPS(90); + if (IsKeyPressed(KEY_ZERO)) UpdateFPS(0); // Unlimited framerate + + if (IsKeyPressed(KEY_R)) // Reset both circles' positions when you press R. + { + deltaX = 0; + frameX = 0; + } + + // Adjust the FPS target based on the mouse wheel. + int mouseWheel = GetMouseWheelMove(); + if (mouseWheel != 0) + { + currentFps += mouseWheel; + SetTargetFPS(currentFps); + } + + // Use of delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS. + // Multiply by 6.0 (an arbitrary value) in order to make the speed visually closer to the other circle (at 60 fps), for comparison. + deltaX += GetFrameTime() * 6.0 * speed; + // This circle can move faster or slower visually depending on the FPS. + frameX += .1 * speed; + + // If either circle is off the screen, reset it back to the start. + if (deltaX > screenWidth) + { + deltaX = 0; + } + + if (frameX > screenWidth) + { + frameX = 0; + } + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Draw both circles to the screen. + DrawCircle(deltaX, deltaY, circleRadius, RED); + DrawCircle(frameX, frameY, circleRadius, BLUE); + + // Determine what help text to show depending on the current FPS target. + char* fpsText; + + if (currentFps <= 0) + { + if (currentFps < 0) + { + // Clamp values below 0. + currentFps = 0; + } + + // Special text for when the FPS target is set to 0 or less, which makes it unlimited. + fpsText = TextFormat("fps: unlimited (%i)", GetFPS()); + } + else { + fpsText = TextFormat("fps: %i", GetFPS()); + } + + // Draw the help text + DrawText(fpsText, 10, 10, 20, DARKGRAY); + DrawText(TextFormat("frame time: %02.02f ms", GetFrameTime()), 10, 30, 20, DARKGRAY); + DrawText("use the scroll wheel/number keys to change the fps limit, r to reset", 10, 50, 20, DARKGRAY); + + // Draw the text above the circles. + DrawText("x += GetFrameTime() * speed", 10, 90, 20, RED); + DrawText("x += speed", 10, 240, 20, BLUE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/core/core_delta_time.png b/examples/core/core_delta_time.png new file mode 100644 index 0000000000000000000000000000000000000000..e074784875595f17c4dcc7d655404f6baffa5254 GIT binary patch literal 16482 zcmeHPdt6gjwoV{IVBq)!Rj@4FeB;ipIh=eBi0xDW9MH@jOAlE7aDq0K@5NnV} zRca~<9_68;SfPVdTM%04Xp{peNJRyeA_$7S1XS)mXGfDjlF_#J-nldUlV3<-s%5Gu0yy2!`|A-4+uF+Li@|y0^QZD+=RGd0a>i#g&~e$bB#+>e+hn& zryq^dl2A7%b!M>tPNB97#Z$jmYeNyV;fz`vFcYfP*au8_3{6<_e`*48f1`O0ilS~? zHaw_0#t%Pw+(J50?Z%YtEN+=_60Nb;;5Exg9Z}_FLWs_)ykE@DbXXnTQ%6fdS##9= zL`c&@n3hP6AB)80@KwAB*U{Ne%qhWAO41L%YuRegt_$?#RzFs^wS)k&_=-@R`IUx^ zBS<|;F4}OS*Nz`k&?~s3_u=BcJaI=4RT`^$>FnneY8sKYXsPP2K^TYW6H@}&m4Q0h z1tu|{WQ8RE5ExrSJBYFz)vcRtPvQo$x!%93T}Xqgur-UH+#0$#a2nUM#8hgg=J8=1 zewfZ`R?i+4iGU|ZI(h`6EQ`B=8(_3nMgsp^~2)8j%?KNN<_oI1JXOiMC?D{hAcAdw=FW)Wn7-O~6 zYeVpo~C zKXRl&)~@)T9!u1)r|!43U2~H|zWFf!)&lm43%VliQ?yT5dFim#nyJBIr!;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