-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd_tween.h
394 lines (338 loc) · 7.92 KB
/
d_tween.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#ifndef D_TWEEN_H
#define D_TWEEN_H
#include <stdlib.h>
#include <stdbool.h>
typedef float (*d_ease_func)(float);
typedef struct {
float from;
float to;
float duration;
d_ease_func easing_func;
float val;
float* val_src;
float elapsed;
bool paused;
bool done;
} d_tween;
d_tween d_tween_new(
float from,
float to,
float duration,
float* val_src,
d_ease_func func
);
bool d_tween_update(d_tween *tween, float dt, float* val);
typedef struct {
d_tween* tweens;
int num_tweens;
int max_tweens;
} d_tweener;
d_tweener d_tweener_new(int num);
void d_tweener_free(d_tweener* t);
void d_tweener_update(d_tweener* tweener, float dt);
d_tween* d_tweener_add_number(
d_tweener* tweener,
float from,
float to,
float duration,
float* val_src,
d_ease_func func
);
#ifdef D_MATH_H
void d_tweener_add_vec2(
d_tweener* tweener,
d_vec2 from,
d_vec2 to,
float duration,
d_vec2* val_src,
d_ease_func func
);
#endif
float d_ease_linear(float t);
float d_ease_in_sine(float t);
float d_ease_out_sine(float t);
float d_ease_in_out_sine(float t);
float d_ease_in_quad(float t);
float d_ease_out_quad(float t);
float d_ease_in_out_quad(float t);
float d_ease_in_cubic(float t);
float d_ease_out_cubic(float t);
float d_ease_in_out_cubic(float t);
float d_ease_in_quart(float t);
float d_ease_out_quart(float t);
float d_ease_in_out_quart(float t);
float d_ease_in_quint(float t);
float d_ease_out_quint(float t);
float d_ease_in_out_quint(float t);
float d_ease_in_etpo(float t);
float d_ease_out_etpo(float t);
float d_ease_in_out_etpo(float t);
float d_ease_in_circ(float t);
float d_ease_out_circ(float t);
float d_ease_in_out_circ(float t);
float d_ease_in_back(float t);
float d_ease_out_back(float t);
float d_ease_in_out_back(float t);
float d_ease_in_elastic(float t);
float d_ease_out_elastic(float t);
float d_ease_in_out_elastic(float t);
float d_ease_in_bounce(float t);
float d_ease_out_bounce(float t);
float d_ease_in_out_bounce(float t);
#endif
#ifdef D_IMPL
#define D_TWEEN_IMPL
#endif
#ifdef D_TWEEN_IMPL
#ifndef D_TWEEN_IMPL_ONCE
#define D_TWEEN_IMPL_ONCE
static float d_tween_lerp(float a, float b, float t) {
return a + (b - a) * t;
}
d_tween d_tween_new(
float from,
float to,
float duration,
float* val_src,
d_ease_func func
) {
return (d_tween) {
.from = from,
.to = to,
.duration = duration,
.easing_func = func == NULL ? d_ease_linear: func,
.val = from,
.val_src = val_src,
.elapsed = 0.0,
.paused = false,
.done = false,
};
}
bool d_tween_update(d_tween *tween, float dt, float* val) {
if (tween->paused || tween->done) return false;
tween->elapsed += dt;
float t = tween->elapsed / tween->duration;
if (t >= 1.0f) {
tween->val = tween->to;
tween->elapsed = tween->duration;
tween->done = true;
if (tween->val_src) *tween->val_src = tween->to;
if (val) *val = tween->to;
return true;
}
tween->val = d_tween_lerp(tween->from, tween->to, tween->easing_func(t));
if (tween->val_src) *tween->val_src = tween->val;
if (val) *val = tween->val;
return false;
}
d_tweener d_tweener_new(int num) {
d_tween* tweens = malloc(num * sizeof(d_tween));
return (d_tweener) {
.tweens = tweens,
.num_tweens = 0,
.max_tweens = num,
};
}
void d_tweener_free(d_tweener* t) {
free(t->tweens);
t->tweens = NULL;
t->num_tweens = 0;
}
d_tween* d_tweener_add(d_tweener* tweener, d_tween t) {
if (tweener->num_tweens >= tweener->max_tweens) return NULL;
int idx = tweener->num_tweens;
tweener->tweens[tweener->num_tweens++] = t;
// a bit pointer magic to cancel all previous tween on the value
// is this good?
for (int i = 0; i < idx; i++) {
d_tween* t2 = &tweener->tweens[i];
if (t2->val_src != NULL && t2->val_src == t.val_src) {
t2->done = true;
}
}
return &tweener->tweens[idx];
}
d_tween* d_tweener_add_number(
d_tweener* tweener,
float from,
float to,
float duration,
float* val_src,
d_ease_func func
) {
d_tween t = d_tween_new(from, to, duration, val_src, func);
return d_tweener_add(tweener, t);
}
#ifdef D_MATH_H
// TODO: return a handle
void d_tweener_add_vec2(
d_tweener* tweener,
d_vec2 from,
d_vec2 to,
float duration,
d_vec2* val_src,
d_ease_func func
) {
d_tweener_add_number(tweener, from.x, to.x, duration, &val_src->x, func);
d_tweener_add_number(tweener, from.y, to.y, duration, &val_src->y, func);
}
#endif
void d_tweener_update(d_tweener* tweener, float dt) {
for (int i = 0; i < tweener->num_tweens; i++) {
d_tween* t = &tweener->tweens[i];
if (t->done) {
memmove(
tweener->tweens + i,
tweener->tweens + i + 1,
(tweener->num_tweens - i - 1) * sizeof(d_tween)
);
tweener->num_tweens--;
i--;
continue;
}
d_tween_update(t, dt, NULL);
}
}
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
// https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts
#define C1 1.70158
#define C2 (C1 * 1.525)
#define C3 (C1 + 1)
#define C4 ((2 * M_PI) / 3)
#define C5 ((2 * M_PI) / 4.5)
float d_ease_linear(float t) {
return t;
}
float d_ease_in_sine(float t) {
return 1 - cos((t * M_PI) / 2);
}
float d_ease_out_sine(float t) {
return sin((t * M_PI) / 2);
}
float d_ease_in_out_sine(float t) {
return -(cos(M_PI * t) - 1) / 2;
}
float d_ease_in_quad(float t) {
return t * t;
}
float d_ease_out_quad(float t) {
return 1 - pow(1 - t, 2);
}
float d_ease_in_out_quad(float t) {
return t < 0.5 ? 2 * t * t : 1 - pow(-2 * t + 2, 2) / 2;
}
float d_ease_in_cubic(float t) {
return t * t * t;
}
float d_ease_out_cubic(float t) {
return 1 - pow(1 - t, 3);
}
float d_ease_in_out_cubic(float t) {
return t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2;
}
float d_ease_in_quart(float t) {
return t * t * t * t;
}
float d_ease_out_quart(float t) {
return 1 - pow(1 - t, 4);
}
float d_ease_in_out_quart(float t) {
return t < 0.5 ? 8 * t * t * t * t : 1 - pow(-2 * t + 2, 4) / 2;
}
float d_ease_in_quint(float t) {
return t * t * t * t * t;
}
float d_ease_out_quint(float t) {
return 1 - pow(1 - t, 5);
}
float d_ease_in_out_quint(float t) {
return t < 0.5 ? 16 * t * t * t * t * t : 1 - pow(-2 * t + 2, 5) / 2;
}
float d_ease_in_etpo(float t) {
return t == 0 ? 0 : pow(2, 10 * t - 10);
}
float d_ease_out_etpo(float t) {
return t == 1 ? 1 : 1 - pow(2, -10 * t);
}
float d_ease_in_out_etpo(float t) {
return t == 0
? 0
: t == 1
? 1
: t < 0.5
? pow(2, 20 * t - 10) / 2
: (2 - pow(2, -20 * t + 10)) / 2;
}
float d_ease_in_circ(float t) {
return 1 - sqrt(1 - pow(t, 2));
}
float d_ease_out_circ(float t) {
return sqrt(1 - pow(t - 1, 2));
}
float d_ease_in_out_circ(float t) {
return t < 0.5
? (1 - sqrt(1 - pow(2 * t, 2))) / 2
: (sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2;
}
float d_ease_in_back(float t) {
return C3 * t * t * t - C1 * t * t;
}
float d_ease_out_back(float t) {
return 1 + C3 * pow(t - 1, 3) + C1 * pow(t - 1, 2);
}
float d_ease_in_out_back(float t) {
return t < 0.5
? (pow(2 * t, 2) * ((C2 + 1) * 2 * t - C2)) / 2
: (pow(2 * t - 2, 2) * ((C2 + 1) * (t * 2 - 2) + C2) + 2) / 2;
}
float d_ease_in_elastic(float t) {
return t == 0
? 0
: t == 1
? 1
: -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * C4);
}
float d_ease_out_elastic(float t) {
return t == 0
? 0
: t == 1
? 1
: pow(2, -10 * t) * sin((t * 10 - 0.75) * C4) + 1;
}
float d_ease_in_out_elastic(float t) {
return t == 0
? 0
: t == 1
? 1
: t < 0.5
? -(pow(2, 20 * t - 10) * sin((20 * t - 11.125) * C5)) / 2
: (pow(2, -20 * t + 10) * sin((20 * t - 11.125) * C5)) / 2 + 1;
}
float d_ease_in_bounce(float t) {
return 1 - d_ease_out_bounce(1 - t);
}
float d_ease_out_bounce(float t) {
float n1 = 7.5625;
float d1 = 2.75;
if (t < 1 / d1) {
return n1 * t * t;
} else if (t < 2 / d1) {
t -= 1.5 / d1;
return n1 * t * t + 0.75;
} else if (t < 2.5 / d1) {
t -= 2.25 / d1;
return n1 * t * t + 0.9375;
} else {
t -= 2.625 / d1;
return n1 * t * t + 0.984375;
}
}
float d_ease_in_out_bounce(float t) {
return t < 0.5
? (1 - d_ease_out_bounce(1 - 2 * t)) / 2
: (1 + d_ease_out_bounce(2 * t - 1)) / 2;
}
#endif
#endif