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 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYU_8XZ#=yWJp1k%11A~L6r;B4qMO^ZqUteF> zw*?wVF)dcaL6mSXF1r{Y$J=;OKmselSk-zUgFVG$ZExFw42~Wwgh5Z4gPZU{?2*GaR-HNW5+6QQL;o4Z9$2*oe~&ASYbL;l7@>10Ntx2pLc(Fiv|Y zxANk}i!POgFE1@kC|$Bj{;UYjh&j@J;Dykdi&u>BgqlJXTg!zr3^{Cx8;;3te5bsm z?}p^0izYU>v@}REEjCtIyfVpz#mzYZ3(=6L=%tRp3}^ig9x`=q%;sm^`Yq!Dmu1lR4L$F^N}O%Q?sbR10upl>C4ED& zOB+Zrm2fjoN_V)B&bDH!cyrkDoQb>BTdzpYxoBdI%ij(bENi>9 z6JHetrE9qPACx*Pi^KX7=Y$fr6qB!gSTX`Qb6n76$tdFz+qm3gV%05?mDPS8ruARG ztiE{19J}kbK$7MaQ|!`<6&R^?FprQdp&UB;HA9twifuVy8dqzg{Yp!EBN zhhw4On~T4V10LtBXkp))W^o}{r>aU(L~gP9h5l((m<}>>0>|mnemrsNagW()?ZliJ zmRs(5JFMQz-s1M-;`fFNr{^oDs%4e%pUTj*1bG{!lyx?SB)fNb0=pq?d4iy|kpH_L zu3tP{3nfLpzFR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