From 3f30533f1c7894536c3cf1f0b9e365f4aef17638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20=E2=9D=A4=EF=B8=8F?= Date: Tue, 30 Sep 2025 12:35:15 -0400 Subject: [PATCH] [examples] Add `core_delta_time` (#5216) * delta example * clarification * made it more clear what delta time is * more explanation * vector2 positions, removed all warnings * removed keys, moved global to local, conventions & comments, show target fps * new png * remove more periods * another period * remove note about conventions, remove period --- examples/core/core_delta_time.c | 131 ++++++++++++++++++++++++++++++ examples/core/core_delta_time.png | Bin 0 -> 16554 bytes 2 files changed, 131 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..aeedd7fff --- /dev/null +++ b/examples/core/core_delta_time.c @@ -0,0 +1,131 @@ +/******************************************************************************************* +* +* 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" + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time"); + + int currentFps = 60; + + // Store the position for the both of the circles + Vector2 deltaCircle = {0, screenHeight / 3.0f}; + Vector2 frameCircle = {0, screenHeight * (2.0f/3.0f)}; + + // The speed applied to both circles + const float speed = 10.0; + const float circleRadius = 32; + + SetTargetFPS(currentFps); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------- + if (IsKeyPressed(KEY_R)) // Reset both circles' positions when you press R + { + deltaCircle.x = 0; + frameCircle.x = 0; + } + + // Adjust the FPS target based on the mouse wheel + float mouseWheel = GetMouseWheelMove(); + if (mouseWheel != 0) + { + currentFps += (int)mouseWheel; + SetTargetFPS(currentFps); + } + + // GetFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time) + // Uses the 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 + deltaCircle.x += GetFrameTime() * 6.0f * speed; + // This circle can move faster or slower visually depending on the FPS + frameCircle.x += .1f * speed; + + // If either circle is off the screen, reset it back to the start + if (deltaCircle.x > screenWidth) + { + deltaCircle.x = 0; + } + + if (frameCircle.x > screenWidth) + { + frameCircle.x = 0; + } + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Draw both circles to the screen + DrawCircleV(deltaCircle, circleRadius, RED); + DrawCircleV(frameCircle, circleRadius, BLUE); + + // Determine what help text to show depending on the current FPS target + const 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 (target: %i)", GetFPS(), currentFps); + } + + // 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 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..cf8aa7c54e50ac477f8ea1011e0b61f1cedcb8ad GIT binary patch literal 16554 zcmeHPd03NI)=wbDsEGtYC1H&y7OOTwKm~zFY~sc;U|UVm22_Zkjxyi^$RcUbHY_R? zje=l6fvUwew1V1#K}1m`NCgqxwJ3thQUtZmeczjyg%JDAJk#&{=HZ__ZxXrpZ1;E0 z`JEeg`g(InBdH@X7z}B_{CR#DjG+w%gFQ#UgHOh}@HSwY&=k=HjBadbhl9@)= z&yE%}e;HPp*l64q=+HlEt@{mseU1*8~OzG z#-R~Cteyya&*;S;w6rn5eQuXNL4H4RX>F-20`El6 zURpues|BQ-^6!r(rg>b<44e>Ym%x3I_+fy3&*T#?EeoS}v8$yzw7|}X<`L-l4vlnd z>6e&`jy7^e>Ei>}ZExnLyfj)uwK#V4?stm~8)=!Hy=T#WVgzTf=v6cb-gc zbb3`$Xnta&g_p$tTHq^2AZn-Kma=8yQW~@U z+TnalkHmF8#Ix6K^HLAVUk{8cnHwtRwU_$DY<07dY;+hbW&pUQ3(H!GReY*0L!ii( z{W(F@fu+sJPpV$StLPY()6L$rFV^#}bDfWm`LwQq{>~MkD{`%Mm{z$W+0D?CUlG$C zL93r-KIQJ*uNP16O3=ijvSZFX_fw5Sl>Bv@^p6wN>r~(WCGOqJmgi zwaHDb(bU$5S9dv>eBAcjVp^iEmH8$xl)~N@N|rk(Z)Oi`c)aiwyVAa;E#AuZK@Fq! zQVVPKqr|3YR{q#5$!c8@gW2YZx;<~;nN~OsxP5*WmL}~@SdwwAO`4k%HRald*D! z=z4;(H|KlK>ikpxkUkxFaPL~7)?O1ca9gOxJAAKbbS}NXV|*vzJn zFSi&0u<+W`$ijT~QvPAU@XufGYRk&mJ(|-sD%9Oe&UmS6-x6k=&wTs3R(WljSYS82tp3UB4}{B~H@!ccr6fCz_}|pv3uCf3Z4-VQ%XO*hoi#(W9oQxT93|-# z)aPOGm3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