1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - easings box 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 box anim" ); |
24 | |
25 | // Box variables to be animated with easings |
26 | Rectangle rec = { GetScreenWidth()/2, -100, 100, 100 }; |
27 | float rotation = 0.0f; |
28 | float alpha = 1.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 | switch (state) |
42 | { |
43 | case 0: // Move box down to center of screen |
44 | { |
45 | framesCounter++; |
46 | |
47 | // NOTE: Remember that 3rd parameter of easing function refers to |
48 | // desired value variation, do not confuse it with expected final value! |
49 | rec.y = EaseElasticOut(framesCounter, -100, GetScreenHeight()/2 + 100, 120); |
50 | |
51 | if (framesCounter >= 120) |
52 | { |
53 | framesCounter = 0; |
54 | state = 1; |
55 | } |
56 | } break; |
57 | case 1: // Scale box to an horizontal bar |
58 | { |
59 | framesCounter++; |
60 | rec.height = EaseBounceOut(framesCounter, 100, -90, 120); |
61 | rec.width = EaseBounceOut(framesCounter, 100, GetScreenWidth(), 120); |
62 | |
63 | if (framesCounter >= 120) |
64 | { |
65 | framesCounter = 0; |
66 | state = 2; |
67 | } |
68 | } break; |
69 | case 2: // Rotate horizontal bar rectangle |
70 | { |
71 | framesCounter++; |
72 | rotation = EaseQuadOut(framesCounter, 0.0f, 270.0f, 240); |
73 | |
74 | if (framesCounter >= 240) |
75 | { |
76 | framesCounter = 0; |
77 | state = 3; |
78 | } |
79 | } break; |
80 | case 3: // Increase bar size to fill all screen |
81 | { |
82 | framesCounter++; |
83 | rec.height = EaseCircOut(framesCounter, 10, GetScreenWidth(), 120); |
84 | |
85 | if (framesCounter >= 120) |
86 | { |
87 | framesCounter = 0; |
88 | state = 4; |
89 | } |
90 | } break; |
91 | case 4: // Fade out animation |
92 | { |
93 | framesCounter++; |
94 | alpha = EaseSineOut(framesCounter, 1.0f, -1.0f, 160); |
95 | |
96 | if (framesCounter >= 160) |
97 | { |
98 | framesCounter = 0; |
99 | state = 5; |
100 | } |
101 | } break; |
102 | default: break; |
103 | } |
104 | |
105 | // Reset animation at any moment |
106 | if (IsKeyPressed(KEY_SPACE)) |
107 | { |
108 | rec = (Rectangle){ GetScreenWidth()/2, -100, 100, 100 }; |
109 | rotation = 0.0f; |
110 | alpha = 1.0f; |
111 | state = 0; |
112 | framesCounter = 0; |
113 | } |
114 | //---------------------------------------------------------------------------------- |
115 | |
116 | // Draw |
117 | //---------------------------------------------------------------------------------- |
118 | BeginDrawing(); |
119 | |
120 | ClearBackground(RAYWHITE); |
121 | |
122 | DrawRectanglePro(rec, (Vector2){ rec.width/2, rec.height/2 }, rotation, Fade(BLACK, alpha)); |
123 | |
124 | DrawText("PRESS [SPACE] TO RESET BOX ANIMATION!" , 10, GetScreenHeight() - 25, 20, LIGHTGRAY); |
125 | |
126 | EndDrawing(); |
127 | //---------------------------------------------------------------------------------- |
128 | } |
129 | |
130 | // De-Initialization |
131 | //-------------------------------------------------------------------------------------- |
132 | CloseWindow(); // Close window and OpenGL context |
133 | //-------------------------------------------------------------------------------------- |
134 | |
135 | return 0; |
136 | } |