Skip to content

Commit 3f30533

Browse files
authored
[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
1 parent a553fbd commit 3f30533

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

examples/core/core_delta_time.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [core] example - delta time
4+
*
5+
* Example complexity rating: [★☆☆☆] 1/4
6+
*
7+
* Example originally created with raylib 5.5
8+
*
9+
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2025-2025 Robin (@RobinsAviary)
15+
*
16+
********************************************************************************************/
17+
18+
#include "raylib.h"
19+
20+
//------------------------------------------------------------------------------------
21+
// Program main entry point
22+
//------------------------------------------------------------------------------------
23+
int main(void)
24+
{
25+
// Initialization
26+
//--------------------------------------------------------------------------------------
27+
const int screenWidth = 800;
28+
const int screenHeight = 450;
29+
30+
InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time");
31+
32+
int currentFps = 60;
33+
34+
// Store the position for the both of the circles
35+
Vector2 deltaCircle = {0, screenHeight / 3.0f};
36+
Vector2 frameCircle = {0, screenHeight * (2.0f/3.0f)};
37+
38+
// The speed applied to both circles
39+
const float speed = 10.0;
40+
const float circleRadius = 32;
41+
42+
SetTargetFPS(currentFps);
43+
//--------------------------------------------------------------------------------------
44+
45+
// Main game loop
46+
while (!WindowShouldClose()) // Detect window close button or ESC key
47+
{
48+
// Update
49+
//----------------------------------------------
50+
if (IsKeyPressed(KEY_R)) // Reset both circles' positions when you press R
51+
{
52+
deltaCircle.x = 0;
53+
frameCircle.x = 0;
54+
}
55+
56+
// Adjust the FPS target based on the mouse wheel
57+
float mouseWheel = GetMouseWheelMove();
58+
if (mouseWheel != 0)
59+
{
60+
currentFps += (int)mouseWheel;
61+
SetTargetFPS(currentFps);
62+
}
63+
64+
// GetFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time)
65+
// Uses the delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS
66+
67+
// Multiply by 6.0 (an arbitrary value) in order to make the speed visually closer to the other circle (at 60 fps), for comparison
68+
deltaCircle.x += GetFrameTime() * 6.0f * speed;
69+
// This circle can move faster or slower visually depending on the FPS
70+
frameCircle.x += .1f * speed;
71+
72+
// If either circle is off the screen, reset it back to the start
73+
if (deltaCircle.x > screenWidth)
74+
{
75+
deltaCircle.x = 0;
76+
}
77+
78+
if (frameCircle.x > screenWidth)
79+
{
80+
frameCircle.x = 0;
81+
}
82+
83+
// Draw
84+
//----------------------------------------------------------------------------------
85+
BeginDrawing();
86+
87+
ClearBackground(RAYWHITE);
88+
89+
// Draw both circles to the screen
90+
DrawCircleV(deltaCircle, circleRadius, RED);
91+
DrawCircleV(frameCircle, circleRadius, BLUE);
92+
93+
// Determine what help text to show depending on the current FPS target
94+
const char* fpsText;
95+
96+
if (currentFps <= 0)
97+
{
98+
if (currentFps < 0)
99+
{
100+
// Clamp values below 0
101+
currentFps = 0;
102+
}
103+
104+
// Special text for when the FPS target is set to 0 or less, which makes it unlimited
105+
fpsText = TextFormat("fps: unlimited (%i)", GetFPS());
106+
}
107+
else
108+
{
109+
fpsText = TextFormat("fps: %i (target: %i)", GetFPS(), currentFps);
110+
}
111+
112+
// Draw the help text
113+
DrawText(fpsText, 10, 10, 20, DARKGRAY);
114+
DrawText(TextFormat("frame time: %02.02f ms", GetFrameTime()), 10, 30, 20, DARKGRAY);
115+
DrawText("use the scroll wheel to change the fps limit, r to reset", 10, 50, 20, DARKGRAY);
116+
117+
// Draw the text above the circles
118+
DrawText("x += GetFrameTime() * speed", 10, 90, 20, RED);
119+
DrawText("x += speed", 10, 240, 20, BLUE);
120+
121+
EndDrawing();
122+
//----------------------------------------------------------------------------------
123+
}
124+
125+
// De-Initialization
126+
//--------------------------------------------------------------------------------------
127+
CloseWindow(); // Close window and OpenGL context
128+
//--------------------------------------------------------------------------------------
129+
130+
return 0;
131+
}

examples/core/core_delta_time.png

16.2 KB
Loading

0 commit comments

Comments
 (0)