1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - easings ball anim |
4 | * |
5 | * This example has been created using raylib 2.5 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #include "easings.h" // Required for easing functions |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball anim" ); |
24 | |
25 | // Ball variable value to be animated with easings |
26 | int ballPositionX = -100; |
27 | int ballRadius = 20; |
28 | float ballAlpha = 0.0f; |
29 | |
30 | int state = 0; |
31 | int framesCounter = 0; |
32 | |
33 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
34 | //-------------------------------------------------------------------------------------- |
35 | |
36 | // Main game loop |
37 | while (!WindowShouldClose()) // Detect window close button or ESC key |
38 | { |
39 | // Update |
40 | //---------------------------------------------------------------------------------- |
41 | if (state == 0) // Move ball position X with easing |
42 | { |
43 | framesCounter++; |
44 | ballPositionX = EaseElasticOut(framesCounter, -100, screenWidth/2 + 100, 120); |
45 | |
46 | if (framesCounter >= 120) |
47 | { |
48 | framesCounter = 0; |
49 | state = 1; |
50 | } |
51 | } |
52 | else if (state == 1) // Increase ball radius with easing |
53 | { |
54 | framesCounter++; |
55 | ballRadius = EaseElasticIn(framesCounter, 20, 500, 200); |
56 | |
57 | if (framesCounter >= 200) |
58 | { |
59 | framesCounter = 0; |
60 | state = 2; |
61 | } |
62 | } |
63 | else if (state == 2) // Change ball alpha with easing (background color blending) |
64 | { |
65 | framesCounter++; |
66 | ballAlpha = EaseCubicOut(framesCounter, 0.0f, 1.0f, 200); |
67 | |
68 | if (framesCounter >= 200) |
69 | { |
70 | framesCounter = 0; |
71 | state = 3; |
72 | } |
73 | } |
74 | else if (state == 3) // Reset state to play again |
75 | { |
76 | if (IsKeyPressed(KEY_ENTER)) |
77 | { |
78 | // Reset required variables to play again |
79 | ballPositionX = -100; |
80 | ballRadius = 20; |
81 | ballAlpha = 0.0f; |
82 | state = 0; |
83 | } |
84 | } |
85 | |
86 | if (IsKeyPressed(KEY_R)) framesCounter = 0; |
87 | //---------------------------------------------------------------------------------- |
88 | |
89 | // Draw |
90 | //---------------------------------------------------------------------------------- |
91 | BeginDrawing(); |
92 | |
93 | ClearBackground(RAYWHITE); |
94 | |
95 | if (state >= 2) DrawRectangle(0, 0, screenWidth, screenHeight, GREEN); |
96 | DrawCircle(ballPositionX, 200, ballRadius, Fade(RED, 1.0f - ballAlpha)); |
97 | |
98 | if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!" , 240, 200, 20, BLACK); |
99 | |
100 | EndDrawing(); |
101 | //---------------------------------------------------------------------------------- |
102 | } |
103 | |
104 | // De-Initialization |
105 | //-------------------------------------------------------------------------------------- |
106 | CloseWindow(); // Close window and OpenGL context |
107 | //-------------------------------------------------------------------------------------- |
108 | |
109 | return 0; |
110 | } |