ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
easing.h
Go to the documentation of this file.
1
8
9#ifndef ASW_EASING_H
10#define ASW_EASING_H
11
12#include <algorithm>
13#include <concepts>
14#include <functional>
15#include <limits>
16#include <type_traits>
17
18#include "./types.h"
19
20namespace asw::easing {
21
22namespace detail {
23
33 template <typename T> T mix(const T& a, const T& b, float f)
34 {
35 if constexpr (std::is_integral_v<T>) {
36 const auto lo = static_cast<double>(std::numeric_limits<T>::lowest());
37 const auto hi = static_cast<double>(std::numeric_limits<T>::max());
38 const double v = static_cast<double>(a)
39 + ((static_cast<double>(b) - static_cast<double>(a)) * static_cast<double>(f));
40 if (v <= lo) {
41 return std::numeric_limits<T>::lowest();
42 }
43 if (v >= hi) {
44 return std::numeric_limits<T>::max();
45 }
46 return static_cast<T>(v);
47 } else {
48 return a + (b - a) * f;
49 }
50 }
51
52} // namespace detail
53
54// --- Linear ---
55float linear(float t);
56
57// --- Smoothstep ---
58
60float smoothstep(float t);
61
62// --- Quadratic ---
63float ease_in_quad(float t);
64float ease_out_quad(float t);
65float ease_in_out_quad(float t);
66
67// --- Cubic ---
68float ease_in_cubic(float t);
69float ease_out_cubic(float t);
70float ease_in_out_cubic(float t);
71
72// --- Sine ---
73float ease_in_sine(float t);
74float ease_out_sine(float t);
75float ease_in_out_sine(float t);
76
77// --- Exponential ---
78float ease_in_expo(float t);
79float ease_out_expo(float t);
80float ease_in_out_expo(float t);
81
82// --- Elastic ---
83float ease_in_elastic(float t);
84float ease_out_elastic(float t);
85
86// --- Bounce ---
87float ease_in_bounce(float t);
88float ease_out_bounce(float t);
89
90// --- Back (overshoot) ---
91float ease_in_back(float t);
92float ease_out_back(float t);
93
94// --- Convenience ---
95
100float falloff(float t, asw::Falloff mode);
101
107template <typename T, typename Func> T ease(const T& a, const T& b, float t, Func func)
108{
109 static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type");
110 static_assert(std::is_invocable_r_v<float, Func, float>,
111 "Func must be a callable that takes a float and returns a float");
112
113 return detail::mix(a, b, func(std::clamp(t, 0.0F, 1.0F)));
114}
115
116} // namespace asw::easing
117
118#endif // ASW_EASING_H
T mix(const T &a, const T &b, float f)
a + (b - a) * f. Integer types are worked in floating point, so a falling unsigned range does not wra...
Definition easing.h:33
float ease_out_cubic(float t)
Definition easing.cpp:40
float smoothstep(float t)
Soft at both ends, 3t^2 - 2t^3.
Definition easing.cpp:13
float ease_out_quad(float t)
Definition easing.cpp:24
float ease_in_out_expo(float t)
Definition easing.cpp:79
float linear(float t)
Definition easing.cpp:7
float ease_in_elastic(float t)
Definition easing.cpp:97
T ease(const T &a, const T &b, float t, Func func)
Definition easing.h:107
float ease_out_bounce(float t)
Definition easing.cpp:119
float ease_in_out_quad(float t)
Definition easing.cpp:29
float ease_out_back(float t)
Definition easing.cpp:152
float ease_in_out_cubic(float t)
Definition easing.cpp:46
float ease_in_back(float t)
Definition easing.cpp:146
float ease_out_sine(float t)
Definition easing.cpp:58
float ease_in_cubic(float t)
Definition easing.cpp:35
float ease_in_quad(float t)
Definition easing.cpp:19
float falloff(float t, asw::Falloff mode)
Definition easing.cpp:160
float ease_in_sine(float t)
Definition easing.cpp:53
float ease_in_out_sine(float t)
Definition easing.cpp:63
float ease_out_elastic(float t)
Definition easing.cpp:107
float ease_out_expo(float t)
Definition easing.cpp:74
float ease_in_expo(float t)
Definition easing.cpp:69
float ease_in_bounce(float t)
Definition easing.cpp:140
Falloff
How a gradient or light fades from its centre to its edge.
Definition types.h:46
Types used throughout the ASW library.