DIE Engine
Loading...
Searching...
No Matches
easings.h
Go to the documentation of this file.
1
12
13#ifndef EASINGS_H
14#define EASINGS_H
15
16#include <math.h>
17#include <stdint.h>
18
33
34/*****************************************************************************/
35static inline float easeInSine(float t)
36{
37 return sinf(1.5707963f * t);
38}
39
40static inline float easeOutSine(float t)
41{
42 return 1.0f + sinf(1.5707963f * (t - 1.0f));
43}
44
45static inline float easeInOutSine(float t)
46{
47 return 0.5f * (1.0f + sinf(3.1415926f * (t - 0.5f)));
48}
49
50static inline float easeInQuad(float t)
51{
52 return t * t;
53}
54
55static inline float easeOutQuad(float t)
56{
57 return t * (2.0f - t);
58}
59
60static inline float easeInOutQuad(float t)
61{
62 return (t < 0.5f) ?
63 2.0f * t * t :
64 t * (4.0f - 2.0f * t) - 1.0f;
65}
66
67static inline float easeInCubic(float t)
68{
69 return t * t * t;
70}
71
72static inline float easeOutCubic(float t)
73{
74 t -= 1.0f;
75 return 1.0f + t * t * t;
76}
77
78static inline float easeInOutCubic(float t)
79{
80 if (t < 0.5f) {
81 return 4.0f * t * t * t;
82 } else {
83 t -= 1.0f;
84 return 1.0f + 4.0f * t * t * t;
85 }
86}
87
88static inline float easeInQuart(float t)
89{
90 t *= t;
91 return t * t;
92}
93
94static inline float easeOutQuart(float t)
95{
96 t = (t - 1.0f) * (t - 1.0f);
97 return 1.0f - t * t;
98}
99
100static inline float easeInOutQuart(float t)
101{
102 if (t < 0.5f) {
103 t *= t;
104 return 8.0f * t * t;
105 } else {
106 t = (t - 1.0f) * (t - 1.0f);
107 return 1.0f - 8.0f * t * t;
108 }
109}
110
111static inline float easeInQuint(float t)
112{
113 float t2 = t * t;
114 return t * t2 * t2;
115}
116
117static inline float easeOutQuint(float t)
118{
119 t -= 1.0f;
120 float t2 = t * t;
121 return 1.0f + t * t2 * t2;
122}
123
124static inline float easeInOutQuint(float t)
125{
126 float t2;
127 if (t < 0.5f) {
128 t2 = t * t;
129 return 16.0f * t * t2 * t2;
130 } else {
131 t -= 1.0f;
132 t2 = t * t;
133 return 1.0f + 16.0f * t * t2 * t2;
134 }
135}
136
137static inline float easeInExpo(float t)
138{
139 return (powf(2.0f, 8.0f * t) - 1.0f) / 256.0f;
140}
141
142static inline float easeOutExpo(float t)
143{
144 return 1.0f - powf(2.0f, -8.0f * t);
145}
146
147static inline float easeInOutExpo(float t)
148{
149 if (t < 0.5f) {
150 return (powf(2.0f, 16.0f * t) - 1.0f) / 512.0f;
151 } else {
152 return 1.0f - 0.5f * powf(2.0f, -16.0f * (t - 0.5f));
153 }
154}
155
156static inline float easeInCirc(float t)
157{
158 return 1.0f - sqrtf(1.0f - t);
159}
160
161static inline float easeOutCirc(float t)
162{
163 return sqrtf(t);
164}
165
166static inline float easeInOutCirc(float t)
167{
168 if (t < 0.5f) {
169 return (1.0f - sqrtf(1.0f - 2.0f * t)) * 0.5f;
170 } else {
171 return (1.0f + sqrtf(2.0f * t - 1.0f)) * 0.5f;
172 }
173}
174
175static inline float easeInBack(float t)
176{
177 return t * t * (2.70158f * t - 1.70158f);
178}
179
180static inline float easeOutBack(float t)
181{
182 t -= 1.0f;
183 return 1.0f + t * t * (2.70158f * t + 1.70158f);
184}
185
186static inline float easeInOutBack(float t)
187{
188 if (t < 0.5f) {
189 return t * t * (7.0f * t - 2.5f) * 2.0f;
190 } else {
191 t -= 1.0f;
192 return 1.0f + t * t * 2.0f * (7.0f * t + 2.5f);
193 }
194}
195
196static inline float easeInElastic(float t)
197{
198 float t2 = t * t;
199 return t2 * t2 * sinf(t * 3.1415926f * 4.5f);
200}
201
202static inline float easeOutElastic(float t)
203{
204 float t2 = (t - 1.0f) * (t - 1.0f);
205 return 1.0f - t2 * t2 * cosf(t * 3.1415926f * 4.5f);
206}
207
208static inline float easeInOutElastic(float t)
209{
210 float t2;
211 if (t < 0.45f) {
212 t2 = t * t;
213 return 8.0f * t2 * t2 * sinf(t * 3.1415926f * 9.0f);
214 } else if (t < 0.55f) {
215 return 0.5f + 0.75f * sinf(t * 3.1415926f * 4.0f);
216 } else {
217 t2 = (t - 1.0f) * (t - 1.0f);
218 return 1.0f - 8.0f * t2 * t2 * sinf(t * 3.1415926f * 9.0f);
219 }
220}
221
222static inline float easeInBounce(float t)
223{
224 return powf(2.0f, 6.0f * (t - 1.0f)) * fabsf(sinf(t * 3.1415926f * 3.5f));
225}
226
227static inline float easeOutBounce(float t)
228{
229 return 1.0f - powf(2.0f, -6.0f * t) * fabsf(cosf(t * 3.1415926f * 3.5f));
230}
231
232static inline float easeInOutBounce(float t)
233{
234 if (t < 0.5f) {
235 return 8.0f * powf(2.0f, 8.0f * (t - 1.0f)) * fabsf(sinf(t * 3.1415926f * 7.0f));
236 } else {
237 return 1.0f - 8.0f * powf(2.0f, -8.0f * t) * fabsf(sinf(t * 3.1415926f * 7.0f));
238 }
239}
240
241/*****************************************************************************/
248static inline float applyEasing(uint16_t type, float t)
249{
250 switch (type) {
251 default:
252 case EASING_LINEAR: return t;
253 case EASING_IN_SINE: return easeInSine(t);
254 case EASING_OUT_SINE: return easeOutSine(t);
255 case EASING_IN_OUT_SINE: return easeInOutSine(t);
256 case EASING_IN_QUAD: return easeInQuad(t);
257 case EASING_OUT_QUAD: return easeOutQuad(t);
258 case EASING_IN_OUT_QUAD: return easeInOutQuad(t);
259 case EASING_IN_CUBIC: return easeInCubic(t);
260 case EASING_OUT_CUBIC: return easeOutCubic(t);
261 case EASING_IN_OUT_CUBIC: return easeInOutCubic(t);
262 case EASING_IN_QUART: return easeInQuart(t);
263 case EASING_OUT_QUART: return easeOutQuart(t);
264 case EASING_IN_OUT_QUART: return easeInOutQuart(t);
265 case EASING_IN_QUINT: return easeInQuint(t);
266 case EASING_OUT_QUINT: return easeOutQuint(t);
267 case EASING_IN_OUT_QUINT: return easeInOutQuint(t);
268 case EASING_IN_EXPO: return easeInExpo(t);
269 case EASING_OUT_EXPO: return easeOutExpo(t);
270 case EASING_IN_OUT_EXPO: return easeInOutExpo(t);
271 case EASING_IN_CIRC: return easeInCirc(t);
272 case EASING_OUT_CIRC: return easeOutCirc(t);
273 case EASING_IN_OUT_CIRC: return easeInOutCirc(t);
274 case EASING_IN_BACK: return easeInBack(t);
275 case EASING_OUT_BACK: return easeOutBack(t);
276 case EASING_IN_OUT_BACK: return easeInOutBack(t);
277 case EASING_IN_ELASTIC: return easeInElastic(t);
278 case EASING_OUT_ELASTIC: return easeOutElastic(t);
279 case EASING_IN_OUT_ELASTIC: return easeInOutElastic(t);
280 case EASING_IN_BOUNCE: return easeInBounce(t);
281 case EASING_OUT_BOUNCE: return easeOutBounce(t);
282 case EASING_IN_OUT_BOUNCE: return easeInOutBounce(t);
283 }
284}
285
286#endif // EASINGS_H
EASING_TYPES
Definition easings.h:19
@ EASING_OUT_QUART
Definition easings.h:24
@ EASING_IN_EXPO
Definition easings.h:26
@ EASING_IN_BOUNCE
Definition easings.h:30
@ EASING_OUT_QUINT
Definition easings.h:25
@ EASING_OUT_SINE
Definition easings.h:21
@ EASING_IN_OUT_BACK
Definition easings.h:28
@ EASING_IN_SINE
Definition easings.h:21
@ EASING_IN_OUT_SINE
Definition easings.h:21
@ EASING_OUT_CUBIC
Definition easings.h:23
@ EASING_COUNT
Definition easings.h:31
@ EASING_IN_CIRC
Definition easings.h:27
@ EASING_IN_QUINT
Definition easings.h:25
@ EASING_OUT_BOUNCE
Definition easings.h:30
@ EASING_IN_OUT_ELASTIC
Definition easings.h:29
@ EASING_IN_OUT_BOUNCE
Definition easings.h:30
@ EASING_OUT_EXPO
Definition easings.h:26
@ EASING_IN_OUT_EXPO
Definition easings.h:26
@ EASING_IN_ELASTIC
Definition easings.h:29
@ EASING_IN_OUT_QUART
Definition easings.h:24
@ EASING_OUT_BACK
Definition easings.h:28
@ EASING_IN_OUT_QUINT
Definition easings.h:25
@ EASING_OUT_ELASTIC
Definition easings.h:29
@ EASING_IN_QUAD
Definition easings.h:22
@ EASING_IN_CUBIC
Definition easings.h:23
@ EASING_IN_BACK
Definition easings.h:28
@ EASING_IN_OUT_CIRC
Definition easings.h:27
@ EASING_LINEAR
Definition easings.h:20
@ EASING_IN_OUT_QUAD
Definition easings.h:22
@ EASING_IN_OUT_CUBIC
Definition easings.h:23
@ EASING_IN_QUART
Definition easings.h:24
@ EASING_OUT_QUAD
Definition easings.h:22
@ EASING_OUT_CIRC
Definition easings.h:27