-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
78 lines (66 loc) · 2.72 KB
/
Copy pathMain.cpp
File metadata and controls
78 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <raylib.h>
#include <string>
#include <cmath>
// Easing function for smooth animation.
float EaseOutCubic(float x)
{ return 1.f - std::pow(1.f - x, 3); }
int main()
{
// Init and shit
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "Delta Time Example");
unsigned short FPS = 60;
SetTargetFPS(FPS);
// ------------------------------
// Variables for animation
float FrameTime = 0.0f, // Frame time is a counter of animation time.
EndTime = .8f; // End time is the time when the animation should end (animation duration).
bool Reached = false; // Flag to know if the animation reached the end.
// ------------------------------
bool UseEasing = false; // Flag to use easing function.
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
// Check if the animation reached the end time. If not, adding delta time to FrameTime.
if (!Reached)
FrameTime += GetFrameTime();
// If FrameTime reaches EndTime, set Reached flag to true and FrameTime to EndTime (in case it exceeds to not overflow the ratio).
if(FrameTime >= EndTime)
{
Reached = true;
FrameTime = EndTime;
}
float AnimRatio = FrameTime / EndTime; // Calculate animation ratio (0.0 to 1.0).
float EasedRatio = EaseOutCubic(AnimRatio); // Calculate eased ratio using easing function.
// Draw a rectangle moving from position 10 to 510 in EndTime seconds.
DrawRectangle(10 + (500 * (UseEasing ? EasedRatio : AnimRatio)), 100, 100, 100, LIGHTGRAY);
// Draw an indicator circle showing the end position of the rectangle.
DrawCircle(560, 150, 10, {255, 0, 0, 100});
// Draw FPS and FrameTime/EndTime ratio.
DrawFPS(10, 10);
DrawText(("Anim. ratio: " + std::to_string(AnimRatio)).c_str(), 10, 30, 20, Reached ? GREEN : RED);
DrawText(UseEasing ? ("Easing: ON, Eased Ratio: " + std::to_string(EasedRatio)).c_str() : "Easing: OFF", 10, 50, 20, UseEasing ? GREEN : RED);
EndDrawing();
if(IsKeyPressed(KEY_DOWN)) // Decrease target FPS.
{
FPS = std::max(10, FPS - 10);
SetTargetFPS(FPS);
}
else if (IsKeyPressed(KEY_UP)) // Increase target FPS.
{
FPS += 10;
SetTargetFPS(FPS);
}
else if(IsKeyPressed(KEY_SPACE)) // Restart the animation.
{
FrameTime = 0.0f;
Reached = false;
}
else if (IsKeyPressed(KEY_ENTER)) // Toggle easing function.
UseEasing = !UseEasing;
}
CloseWindow();
return 0;
}