Skip to content

Commit 8932ba0

Browse files
committed
Update core_delta_time.c
1 parent 6966ad5 commit 8932ba0

File tree

1 file changed

+34
-53
lines changed

1 file changed

+34
-53
lines changed

examples/core/core_delta_time.c

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ int main(void)
3232
int currentFps = 60;
3333

3434
// 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)};
35+
Vector2 deltaCircle = { 0, (float)screenHeight/3.0f };
36+
Vector2 frameCircle = { 0, (float)screenHeight*(2.0f/3.0f) };
3737

3838
// The speed applied to both circles
39-
const float speed = 10.0;
40-
const float circleRadius = 32;
39+
const float speed = 10.0f;
40+
const float circleRadius = 32.0f;
4141

4242
SetTargetFPS(currentFps);
4343
//--------------------------------------------------------------------------------------
@@ -46,77 +46,58 @@ int main(void)
4646
while (!WindowShouldClose()) // Detect window close button or ESC key
4747
{
4848
// 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-
49+
//----------------------------------------------------------------------------------
5650
// Adjust the FPS target based on the mouse wheel
5751
float mouseWheel = GetMouseWheelMove();
5852
if (mouseWheel != 0)
5953
{
6054
currentFps += (int)mouseWheel;
55+
if (currentFps < 0) currentFps = 0;
6156
SetTargetFPS(currentFps);
6257
}
6358

6459
// GetFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time)
6560
// Uses the delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS
6661

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;
62+
// Multiply by 6.0 (an arbitrary value) in order to make the speed
63+
// visually closer to the other circle (at 60 fps), for comparison
64+
deltaCircle.x += GetFrameTime()*6.0f*speed;
6965
// This circle can move faster or slower visually depending on the FPS
70-
frameCircle.x += .1f * speed;
66+
frameCircle.x += 0.1f*speed;
7167

7268
// If either circle is off the screen, reset it back to the start
73-
if (deltaCircle.x > screenWidth)
69+
if (deltaCircle.x > screenWidth) deltaCircle.x = 0;
70+
if (frameCircle.x > screenWidth) frameCircle.x = 0;
71+
72+
// Reset both circles positions
73+
if (IsKeyPressed(KEY_R))
7474
{
7575
deltaCircle.x = 0;
76-
}
77-
78-
if (frameCircle.x > screenWidth)
79-
{
8076
frameCircle.x = 0;
8177
}
78+
//----------------------------------------------------------------------------------
8279

8380
// Draw
8481
//----------------------------------------------------------------------------------
8582
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);
83+
ClearBackground(RAYWHITE);
84+
85+
// Draw both circles to the screen
86+
DrawCircleV(deltaCircle, circleRadius, RED);
87+
DrawCircleV(frameCircle, circleRadius, BLUE);
88+
89+
// Draw the help text
90+
// Determine what help text to show depending on the current FPS target
91+
const char *fpsText = 0;
92+
if (currentFps <= 0) fpsText = TextFormat("FPS: unlimited (%i)", GetFPS());
93+
else fpsText = TextFormat("FPS: %i (target: %i)", GetFPS(), currentFps);
94+
DrawText(fpsText, 10, 10, 20, DARKGRAY);
95+
DrawText(TextFormat("Frame time: %02.02f ms", GetFrameTime()), 10, 30, 20, DARKGRAY);
96+
DrawText("Use the scroll wheel to change the fps limit, r to reset", 10, 50, 20, DARKGRAY);
97+
98+
// Draw the text above the circles
99+
DrawText("FUNC: x += GetFrameTime()*speed", 10, 90, 20, RED);
100+
DrawText("FUNC: x += speed", 10, 240, 20, BLUE);
120101

121102
EndDrawing();
122103
//----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)