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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*******************************************************************************************
*
* raylib [easings] example
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2019 Juan Miguel López
*
********************************************************************************************/
#include <raylib.h>
#include "easings.h"
// Application constants
#define SCR_WIDTH 800
#define SCR_HEIGHT 450
#define BALL_RADIUS 16.0f
#define BALL_COLOR MAROON
#define PAD 80.0f
#define START_X (0.0f + (BALL_RADIUS) + (PAD))
#define END_X ((SCR_WIDTH) - ((BALL_RADIUS) + (PAD)))
#define START_Y (0.0f + (BALL_RADIUS) + (PAD))
#define END_Y ((SCR_HEIGHT) - ((BALL_RADIUS) + (PAD)))
#define T_ADVANCE 1.0f
#define D_DFT 300.0f
#define TARGET_FPS 60
#define BG_COLOR RAYWHITE
#define TEXT_COLOR LIGHTGRAY
#define FONT_SIZE 20
#define D_STEP 20.0f
#define D_STEP_FINE 2.0f
#define D_MIN 1.0f
#define D_MAX 10000.0f
// Application control keys
#define KEY_NEXT_EASE_X KEY_RIGHT
#define KEY_PREV_EASE_X KEY_LEFT
#define KEY_NEXT_EASE_Y KEY_DOWN
#define KEY_PREV_EASE_Y KEY_UP
#define KEY_INCR_D_STEP KEY_W
#define KEY_DECR_D_STEP KEY_Q
#define KEY_INCR_D_FINE KEY_S
#define KEY_DECR_D_FINE KEY_A
#define KEY_PLAY_PAUSE KEY_ENTER
#define KEY_RESTART KEY_SPACE
#define KEY_TOGGLE_UNBOUNDED_T KEY_T
// Easing types
enum EasingTypes {
EASE_LINEAR_NONE,
EASE_LINEAR_IN,
EASE_LINEAR_OUT,
EASE_LINEAR_IN_OUT,
EASE_SINE_IN,
EASE_SINE_OUT,
EASE_SINE_IN_OUT,
EASE_CIRC_IN,
EASE_CIRC_OUT,
EASE_CIRC_IN_OUT,
EASE_CUBIC_IN,
EASE_CUBIC_OUT,
EASE_CUBIC_IN_OUT,
EASE_QUAD_IN,
EASE_QUAD_OUT,
EASE_QUAD_IN_OUT,
EASE_EXPO_IN,
EASE_EXPO_OUT,
EASE_EXPO_IN_OUT,
EASE_BACK_IN,
EASE_BACK_OUT,
EASE_BACK_IN_OUT,
EASE_BOUNCE_OUT,
EASE_BOUNCE_IN,
EASE_BOUNCE_IN_OUT,
EASE_ELASTIC_IN,
EASE_ELASTIC_OUT,
EASE_ELASTIC_IN_OUT,
NUM_EASING_TYPES,
EASING_NONE = NUM_EASING_TYPES
};
static float NoEase(float t, float b, float c, float d); // NoEase function declaration, function used when "no easing" is selected for any axis
// Easing functions reference data
static const struct {
const char *name;
float (*func)(float, float, float, float);
} Easings[] = {
[EASE_LINEAR_NONE] = {
.name = "EaseLinearNone",
.func = EaseLinearNone,
},
[EASE_LINEAR_IN] = {
.name = "EaseLinearIn",
.func = EaseLinearIn,
},
[EASE_LINEAR_OUT] = {
.name = "EaseLinearOut",
.func = EaseLinearOut,
},
[EASE_LINEAR_IN_OUT] = {
.name = "EaseLinearInOut",
.func = EaseLinearInOut,
},
[EASE_SINE_IN] = {
.name = "EaseSineIn",
.func = EaseSineIn,
},
[EASE_SINE_OUT] = {
.name = "EaseSineOut",
.func = EaseSineOut,
},
[EASE_SINE_IN_OUT] = {
.name = "EaseSineInOut",
.func = EaseSineInOut,
},
[EASE_CIRC_IN] = {
.name = "EaseCircIn",
.func = EaseCircIn,
},
[EASE_CIRC_OUT] = {
.name = "EaseCircOut",
.func = EaseCircOut,
},
[EASE_CIRC_IN_OUT] = {
.name = "EaseCircInOut",
.func = EaseCircInOut,
},
[EASE_CUBIC_IN] = {
.name = "EaseCubicIn",
.func = EaseCubicIn,
},
[EASE_CUBIC_OUT] = {
.name = "EaseCubicOut",
.func = EaseCubicOut,
},
[EASE_CUBIC_IN_OUT] = {
.name = "EaseCubicInOut",
.func = EaseCubicInOut,
},
[EASE_QUAD_IN] = {
.name = "EaseQuadIn",
.func = EaseQuadIn,
},
[EASE_QUAD_OUT] = {
.name = "EaseQuadOut",
.func = EaseQuadOut,
},
[EASE_QUAD_IN_OUT] = {
.name = "EaseQuadInOut",
.func = EaseQuadInOut,
},
[EASE_EXPO_IN] = {
.name = "EaseExpoIn",
.func = EaseExpoIn,
},
[EASE_EXPO_OUT] = {
.name = "EaseExpoOut",
.func = EaseExpoOut,
},
[EASE_EXPO_IN_OUT] = {
.name = "EaseExpoInOut",
.func = EaseExpoInOut,
},
[EASE_BACK_IN] = {
.name = "EaseBackIn",
.func = EaseBackIn,
},
[EASE_BACK_OUT] = {
.name = "EaseBackOut",
.func = EaseBackOut,
},
[EASE_BACK_IN_OUT] = {
.name = "EaseBackInOut",
.func = EaseBackInOut,
},
[EASE_BOUNCE_OUT] = {
.name = "EaseBounceOut",
.func = EaseBounceOut,
},
[EASE_BOUNCE_IN] = {
.name = "EaseBounceIn",
.func = EaseBounceIn,
},
[EASE_BOUNCE_IN_OUT] = {
.name = "EaseBounceInOut",
.func = EaseBounceInOut,
},
[EASE_ELASTIC_IN] = {
.name = "EaseElasticIn",
.func = EaseElasticIn,
},
[EASE_ELASTIC_OUT] = {
.name = "EaseElasticOut",
.func = EaseElasticOut,
},
[EASE_ELASTIC_IN_OUT] = {
.name = "EaseElasticInOut",
.func = EaseElasticInOut,
},
[EASING_NONE] = {
.name = "None",
.func = NoEase,
},
};
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
Vector2 ballPos = { .x = START_X, .y = START_Y };
float t = 0.0f; // Current time (in any unit measure, but same unit as duration)
float d = D_DFT; // Total time it should take to complete (duration)
bool paused = true;
bool boundedT = true; // If true, t will stop when d >= td, otherwise t will keep adding td to its value every loop
enum EasingTypes easingX = EASING_NONE; // Easing selected for x axis
enum EasingTypes easingY = EASING_NONE; // Easing selected for y axis
InitWindow(SCR_WIDTH, SCR_HEIGHT, "raylib [easings] example");
SetTargetFPS(TARGET_FPS);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_TOGGLE_UNBOUNDED_T))
boundedT = 1 - boundedT;
// Choose easing for the X axis
if (IsKeyPressed(KEY_NEXT_EASE_X))
{
++easingX;
if (easingX > EASING_NONE)
easingX = 0;
}
else if (IsKeyPressed(KEY_PREV_EASE_X))
{
if (easingX == 0)
easingX = EASING_NONE;
else
--easingX;
}
// Choose easing for the Y axis
if (IsKeyPressed(KEY_NEXT_EASE_Y))
{
++easingY;
if (easingY > EASING_NONE)
easingY = 0;
}
else if (IsKeyPressed(KEY_PREV_EASE_Y))
{
if (easingY == 0)
easingY = EASING_NONE;
else
--easingY;
}
// Change d (duration) value
if (IsKeyPressed(KEY_INCR_D_STEP) && d < D_MAX - D_STEP)
d += D_STEP;
else if (IsKeyPressed(KEY_DECR_D_STEP) && d > D_MIN + D_STEP)
d -= D_STEP;
if (IsKeyDown(KEY_INCR_D_FINE) && d < D_MAX - D_STEP_FINE)
d += D_STEP_FINE;
else if (IsKeyDown(KEY_DECR_D_FINE) && d > D_MIN + D_STEP_FINE)
d -= D_STEP_FINE;
// Play, pause and restart controls
if (IsKeyPressed(KEY_RESTART) ||
IsKeyPressed(KEY_NEXT_EASE_X) || IsKeyPressed(KEY_PREV_EASE_X) ||
IsKeyPressed(KEY_NEXT_EASE_Y) || IsKeyPressed(KEY_PREV_EASE_Y) ||
IsKeyPressed(KEY_INCR_D_STEP) || IsKeyPressed(KEY_DECR_D_STEP) ||
IsKeyPressed(KEY_TOGGLE_UNBOUNDED_T) ||
IsKeyDown(KEY_INCR_D_FINE) || IsKeyDown(KEY_DECR_D_FINE) ||
(IsKeyPressed(KEY_PLAY_PAUSE) && boundedT == true && t >= d))
{
t = 0.0f;
ballPos.x = START_X;
ballPos.y = START_Y;
paused = true;
}
if (IsKeyPressed(KEY_PLAY_PAUSE))
paused = 1 - paused;
// Movement computation
if ((paused == false) &&
((boundedT == true && t < d) || boundedT == false))
{
ballPos.x = Easings[easingX].func(t, START_X, END_X - START_X, d);
ballPos.y = Easings[easingY].func(t, START_Y, END_Y - START_Y, d);
t += T_ADVANCE;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BG_COLOR);
// Draw information text
int line = 0;
DrawText(TextFormat("Easing x: %s", Easings[easingX].name), 0, FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
DrawText(TextFormat("Easing y: %s", Easings[easingY].name), 0, FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
DrawText(TextFormat("t (%c) = %.2f d = %.2f", (boundedT == true)? 'b' : 'u', t, d),
0, FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
// Draw instructions text
line = 1;
DrawText("Use ENTER to play or pause movement, use SPACE to restart", 0, SCR_HEIGHT - FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
DrawText("Use D and W or A and S keys to change duration", 0, SCR_HEIGHT - FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
DrawText("Use LEFT or RIGHT keys to choose easing for the x axis", 0, SCR_HEIGHT - FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
DrawText("Use UP or DOWN keys to choose easing for the y axis", 0, SCR_HEIGHT - FONT_SIZE * (line++), FONT_SIZE, TEXT_COLOR);
// Draw ball
DrawCircleV(ballPos, BALL_RADIUS, BALL_COLOR);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// NoEase function, used when "no easing" is selected for any axis. It just ignores all parameters besides b.
static float NoEase(float t, float b, float c, float d)
{
float burn = t + b + c + d; // Hack to avoid compiler warning (about unused variables)
d += burn;
return b;
}
|