Line data Source code
1 : /* Copyright (C) 2012 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef INCLUDED_EASE
19 : #define INCLUDED_EASE
20 :
21 : /*
22 : * Straightforward C++ port of Robert Penner's easing equations
23 : * http://www.robertpenner.com/easing/
24 : *
25 : * Copyright (c) 2001 Robert Penner
26 : * All rights reserved.
27 : *
28 : * Redistribution and use in source and binary forms, with or without modification, are permitted
29 : * provided that the following conditions are met:
30 : * - Redistributions of source code must retain the above copyright notice, this list of
31 : * conditions and the following disclaimer.
32 : * - Redistributions in binary form must reproduce the above copyright notice, this list of
33 : * conditions and the following disclaimer in the documentation and/or other materials provided
34 : * with the distribution.
35 : * - Neither the name of the author nor the names of contributors may be used to endorse or
36 : * promote products derived from this software without specific prior written permission.
37 : *
38 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
39 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40 : * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
41 : * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
44 : * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 : * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 : */
47 :
48 : #include <math.h>
49 :
50 : /**
51 : * Generic easing functions. In each function, the parameters are:
52 : *
53 : * @param t Current time in seconds, as a float between 0 and d (inclusive).
54 : * @param d Total duration of the ease, in seconds. Must be strictly positive.
55 : * @param b Baseline value (at t = 0).
56 : * @param c Delta from baseline value to reach the target value (at t = d). I.e., target = b + c.
57 : *
58 : * Each function outputs the eased value between 'b' and 'b+c' at time 't'.
59 : */
60 : class Ease
61 : {
62 : public:
63 : static float QuadIn(float t, const float b, const float c, const float d)
64 : {
65 : t /= d;
66 : return c*t*t + b;
67 : }
68 :
69 : static float QuadOut(float t, const float b, const float c, const float d)
70 : {
71 : t /= d;
72 : return -c * t*(t-2) + b;
73 : }
74 :
75 : static float QuadInOut(float t, const float b, const float c, const float d)
76 : {
77 : t /= d/2;
78 : if (t < 1)
79 : return c/2*t*t + b;
80 : --t;
81 : return -c/2 * (t*(t-2) - 1) + b;
82 : }
83 :
84 : static float CubicIn(float t, const float b, const float c, const float d)
85 : {
86 : t /= d;
87 : return c*t*t*t + b;
88 : }
89 :
90 : static float CubicOut(float t, const float b, const float c, const float d)
91 : {
92 : t = t/d - 1;
93 : return c*(t*t*t + 1) + b;
94 : }
95 :
96 : static float CubicInOut(float t, const float b, const float c, const float d)
97 : {
98 : t /= d/2;
99 : if (t < 1)
100 : return c/2*t*t*t + b;
101 : t -= 2;
102 : return c/2*(t*t*t + 2) + b;
103 : }
104 :
105 : static float QuartIn(float t, const float b, const float c, const float d)
106 : {
107 : t /= d;
108 : return c*t*t*t*t + b;
109 : }
110 :
111 0 : static float QuartOut(float t, const float b, const float c, const float d)
112 : {
113 0 : t = t/d - 1;
114 0 : return -c*(t*t*t*t - 1) + b;
115 : }
116 :
117 : static float QuartInOut(float t, const float b, const float c, const float d)
118 : {
119 : t /= d/2;
120 : if (t < 1)
121 : return c/2*t*t*t*t + b;
122 : t -= 2;
123 : return -c/2 * (t*t*t*t - 2) + b;
124 : }
125 :
126 : static float QuintIn(float t, const float b, const float c, const float d)
127 : {
128 : t /= d;
129 : return c*t*t*t*t*t + b;
130 : }
131 :
132 : static float QuintOut(float t, const float b, const float c, const float d)
133 : {
134 : t = t/d - 1;
135 : return c*(t*t*t*t*t + 1) + b;
136 : }
137 :
138 : static float QuintInOut(float t, const float b, const float c, const float d)
139 : {
140 : t /= d/2;
141 : if (t < 1)
142 : return c/2*t*t*t*t*t + b;
143 : t -= 2;
144 : return c/2*(t*t*t*t*t + 2) + b;
145 : }
146 : };
147 :
148 : #endif // INCLUDED_EASE
|