Debian Codegraph @

doc/doc-src/tween/tween.dox

raw
1 /*! \page tweeners C++ Tweeners
2
3 Tween is a C++ library providing an implementation for tweening. In
4 details, this library allows to interpolate the intermediate values
5 between two given values during a time interval. This practice is
6 well used in animation and is widely popular in the context of
7 ActionScript/Flash games and websites. The goal of this library is to
8 provide similar functionalities to C++ programs.
9
10 <!--
11 Table of content generated with:
12 grep '\\\(sub\)*section' tween.dox \
13 | cut -d' ' -f1-2 \
14 | sed 's:\\section: - \\ref:;s:\\subsection: - \\ref:'
15 -->
16 - \ref tweener-why
17 - \ref tweener-examples
18 - \ref tweener-example-var
19 - \ref tweener-example-group
20 - \ref tweener-example-callback
21 - \ref tweener-classes
22 - \ref tweener-tweener-contract
23 - \ref tweener-single_tweener
24 - \ref tweener-tweener_group
25 - \ref tweener-tweener_sequence
26 - \ref tweener-easing-functions
27 - \ref tweener-easing-contract
28 - \ref tweener-easing-symmetric
29 - \ref tweener-easing-predefined
30
31 \section tweener-why Why this library
32
33 Making a variable to vary from a original value to a final value
34 following a predefined function is something recurrent in programming
35 and especially during the development of games. As far as we know, the
36 only free C++ library providing this functionality is <a
37 href="http://code.google.com/p/cpptweener/">cpptweener</a>, a port of
38 the <a href="http://code.google.com/p/tweener/">tweener</a>
39 library. Unfortunately, even if cpptweener is an accurate port of the
40 ActionScript tweener library, she lacks several functionalities.
41
42 Our implementation is inspired from the aforementioned cpptweener and
43 tweener libraries. Indeed, the same terminology will be used and the
44 <a href="http://robertpenner.com/easing/">equations of Penners</a> are
45 also implemented. claw::tween provides the following functionalities:
46
47 - Interpolation is done on the value of a given variable or using an
48 user-provided callback function,
49 - The interpolated values are computed using a predefined easing
50 function or any user-provided function respecting the contract,
51 - Several tweeners can easily be executed simultaneously or in a row,
52 - The library has a modern C++ design.
53
54 \section tweener-examples Examples
55
56 This section presents some simple programs to show the usage of the
57 claw::tween library.
58
59 \subsection tweener-example-var Interpolating the value of a variable
60
61 The following C++ program changes the value of a given variable from
62 its initial value to a target value using a linear interpolation of
63 the values in-between.
64
65 \code
66 #include <iostream>
67 #include <claw/tween/single_tweener.hpp>
68 #include <claw/tween/easing/easing_linear.hpp>
69
70 int main()
71 {
72 double x(10);
73 claw::tween::single_tweener t(x, 15, 1, claw::tween::easing_linear::ease_in);
74
75 while ( !t.is_finished() )
76 {
77 t.update(0.1);
78 std::cout << x << std::endl;
79 }
80
81 return 0;
82 }
83 \endcode
84
85 In the above function a variable \c x is created with an initial
86 value. The tweener is then created:
87 \code
88 claw::tween::single_tweener t(x, 10, 1, claw::tween::easing_linear::ease_in);
89 \endcode
90
91 Since there is only a single value to interpolate, a \c single_tweener is
92 used. Let's observe the detail of the constructor's parameters:
93
94 \code
95 single_tweener( double& val, double end, double duration, easing_function e );
96 \endcode
97
98 The first parameter is the variable whose value is interpolated. The
99 second parameter is the value that must be assigned to \a val at the
100 end of the interpolation. Then the third parameter is the total
101 duration of the interpolation. The unit can be whatever the user wants
102 (seconds, milliseconds, …) as long as this is the same unit used for
103 the updates. Finally, the fourth parameter is the function used to
104 compute the intermediate values. See \ref tweener-easing-functions for
105 details about those functions.
106
107 The next part of the example updates the tweeners until the end value
108 is reached.
109
110 \code
111 while ( !t.is_finished() )
112 {
113 t.update(0.1);
114 std::cout << x << std::endl;
115 }
116 \endcode
117
118 The parameter of the \c update function is the elapsed time since the
119 last update. Here the loop does ten iterations and produces the
120 following output:
121
122 \verbatim
123 10.5
124 11
125 11.5
126 12
127 12.5
128 13
129 13.5
130 14
131 14.5
132 15
133 \endverbatim
134
135 \subsection tweener-example-group Interpolating the value of several variables
136
137 The following C++ program changes the value of several variables from
138 their initial values to a target value using a linear interpolation
139 of the values in-between. Since we do not want to manage each tweener
140 individually, we will use a \c tweener_group to manage them.
141
142 \code
143 #include <iostream>
144 #include <claw/tween/single_tweener.hpp>
145 #include <claw/tween/tweener_group.hpp>
146 #include <claw/tween/easing/easing_quad.hpp>
147
148 struct coord
149 {
150 double x;
151 double y;
152 };
153
154 int main()
155 {
156 coord c = { 0, 0 };
157 claw::tween::tweener_group t;
158 t.insert
159 ( claw::tween::single_tweener
160 ( c.x, 10, 1, claw::tween::easing_linear::ease_in ) );
161 t.insert
162 ( claw::tween::single_tweener
163 ( c.y, 10, 1, claw::tween::easing_linear::ease_in ) );
164
165 do
166 {
167 t.update(0.1);
168 std::cout << c.x << ' ' << c.y << std::endl;
169 }
170 while ( !t.is_finished() );
171
172 return 0;
173 }
174 \endcode
175
176 Here there are several values to interpolate simultaneously. Instead
177 of updating each tweener individually, they are inserted into a \c
178 tweener_group that will manage them. One can notice that
179 claw::tween::tweener_group is also a tweener, thus we insert groups
180 inside groups.
181
182 \subsection tweener-example-callback Interpolating values using callbacks
183
184 In the previous example all the member variables of \c coord are
185 public, thus they can be passed by reference to the constructor of \c
186 single_tweener. Unfortunately in most real-life situations the member
187 variables are not public and those values are modified using
188 setters. In this situation, one can use the second constructor of \c
189 single_tweener:
190
191 \code
192 single_tweener
193 ( double init, double end, double duration, update_function callback,
194 easing_function e );
195 \endcode
196
197 This constructor accept a callback function as the fourth
198 parameter. This function, whose signature must be <tt>void
199 (double)</tt>, is called at each update with the interpolated value as
200 its only parameter.
201
202 Here is a modification of the previous example where setters are used
203 to update the values of the \c coord. The callback is created using
204 <a
205 href="http://www.boost.org/doc/libs/release/libs/bind">boost::bind</a>
206 to create a function with the adequate signature.
207
208 \code
209 #include <iostream>
210 #include <claw/tween/single_tweener.hpp>
211 #include <claw/tween/tweener_group.hpp>
212 #include <claw/tween/easing/easing_linear.hpp>
213 #include <boost/bind.hpp>
214
215 class coord
216 {
217 public:
218 double get_x() const { return m_x; }
219 void set_x( double x ) { m_x = x; }
220
221 double get_y() const { return m_y; }
222 void set_y( double y ) { m_y = y; }
223
224 private:
225 double m_x;
226 double m_y;
227 };
228
229 int main()
230 {
231 coord c;
232 claw::tween::tweener_group t;
233 t.insert
234 ( claw::tween::single_tweener
235 ( 0, 10, 1, boost::bind( &coord::set_x, &c, _1 ),
236 claw::tween::easing_linear::ease_in ) );
237 t.insert
238 ( claw::tween::single_tweener
239 ( 0, 10, 1, boost::bind( &coord::set_y, &c, _1 ),
240 claw::tween::easing_linear::ease_in ) );
241
242 do
243 {
244 t.update(0.1);
245 std::cout << c.get_x() << ' ' << c.get_y() << std::endl;
246 }
247 while ( !t.is_finished() );
248
249 return 0;
250 }
251 \endcode
252
253 \section tweener-classes Tweener classes
254
255 The library provides three tweener classes to manage tweeners, named
256 \c single_tweener, \c tweener_group and \c tweener_sequence. Each one
257 fills a particular need described in the following sections.
258
259 \subsection tweener-tweener-contract The contract of the tweeners
260
261 The tweener interface, as described below, is quite simple:
262
263 \code
264 class tweener
265 {
266 public:
267 typedef boost::function<void()> finish_callback();
268
269 public:
270 tweener();
271 tweener( const tweener& that );
272
273 tweener& operator=( const tweener& that );
274
275 void swap( tweener& that ) throw();
276
277 bool is_finished() const;
278 double update( double dt );
279
280 void on_finished( finish_callback f );
281 };
282 \endcode
283
284 The tweener library use two functions to manage the tweeners. First,
285 the <tt>bool is_finished() const</tt> must return true if and only if
286 the final value has been reached. Then,
287 <tt>double update(double)</tt> is used to update the intermediate
288 values, where the single argument is the amount of time since the last
289 update or since the construction of the tweener. This function returns
290 the amount of \a dt not used in the update. For example, if a tweener
291 is at 1 time unit from the end of the interpolation and
292 <tt>update(1.5)</tt> is called on this tweener, then 0.5 is
293 returned. Indeed, only 1 of the 1.5 available units have been used to
294 reach the end of the interpolation.
295
296 When a tweener is finished, all the functions passed to
297 <tt>void on_finished( finish_callback )</tt> are called in sequence.
298
299 \subsection tweener-single_tweener claw::tween::single_tweener
300
301 The claw::tween::single_tweener object is the class to use to make one
302 value to vary from a source value to a final value in a given amount
303 of time and according to a given easing function. The interface is
304 described below:
305
306 \code
307 class single_tweener
308 {
309 public:
310 typedef boost::function<void (double)> update_function;
311 typedef boost::function<double (double)> easing_function;
312
313 public:
314 single_tweener();
315 single_tweener
316 ( double init, double end, double duration, update_function callback,
317 easing_function e );
318 single_tweener
319 ( double& val, double end, double duration, easing_function e );
320
321 void set_init( double v );
322 void set_end( double v );
323 void set_duration( double v );
324 void set_callback( update_function f );
325 void set_easing( easing_function f );
326
327 bool is_finished() const;
328 double update( double dt );
329
330 void on_finished( finish_callback f );
331 };
332 \endcode
333
334 The default behavior of the single_tweener is to use a callback
335 function to send the new value to the caller at each update. Let's
336 have a look at the following constructor.
337
338 \code
339 single_tweener
340 ( double init, double end, double duration, update_function callback,
341 easing_function e );
342 \endcode
343
344 The two first arguments defined the value returned by the tweener
345 respectively at the beginning and the end of the interpolation. The
346 third parameter is the total duration of the interpolation. The unit
347 can be whatever the user wants (seconds, milliseconds, …) as long as
348 this is the same unit used for the updates. The fourth argument is the
349 callback function called at each update. His signature must be
350 <tt>void (double)</tt>, where the argument receive the current
351 intermediate value. Finally, the fifth parameter is the function used
352 to compute the intermediate values. See \ref tweener-easing-functions
353 for details about this topic.
354
355 In the situation where the interpolation is done on a known variable,
356 one would have to provide a callback function to assign the
357 intermediate values to the variable. In order to ease the use of
358 tweeners for this kind of situation, an other constructor is defined:
359
360 \code
361 single_tweener( double& val, double end, double duration, easing_function e );
362 \endcode
363
364 The difference with the aforementioned constructor is that there is no
365 callback function to provide. Here, the first parameter is the
366 variable whose value is interpolated. Its initial value is taken for
367 the initial value of the interpolation. The created tweener will
368 manage the assignment of the intermediate value to \a val.
369
370 In other words, the following program:
371
372 \code
373 double v;
374
375 void assign_v( double i )
376 {
377 v = i;
378 }
379
380 single_tweener create_tweener()
381 {
382 return single_tweener(0, 10, 1, &assign_v, easing_linear::ease_in);
383 }
384 \endcode
385
386 is completely equivalent to this more concise program:
387
388 \code
389 double v(0);
390
391 single_tweener create_tweener()
392 {
393 return single_tweener(v, 10, 1, easing_linear::ease_in);
394 }
395 \endcode
396
397 The <tt>void on_finished()</tt> function allows to insert a callback
398 that will be called when the tweener finishes, during a call to
399 <tt>double update(double)</tt>.
400
401 \subsection tweener-tweener_group claw::tween::tweener_group
402
403 Once that single_tweener is understood, the other tweener classes are
404 quite simple. The claw::tween::tweener_group class is a tweener class
405 that executes several tweeners simultaneously. Its interface is
406 defined as follows:
407
408 \code
409 class tweener_group:
410 {
411 public:
412 void insert( const tweener& t );
413
414 void clear();
415
416 bool is_finished() const;
417 double update( double dt );
418
419 void on_finished( finish_callback f );
420 };
421 \endcode
422
423 The \c tweener_group class does not compute values by itself but
424 forwards the call to <tt>update(double)</tt> to other tweeners, thus
425 allowing to play several tweeners in parallel.
426
427 The <tt>bool is_finished()</tt> function returns true when all the
428 tweeners of the group are finished. Also, tweeners can be inserted in
429 a \c tweener_group at any time.
430
431 The <tt>void on_finished()</tt> function allows to insert a callback
432 that will be called when the last tweener in the group finishes,
433 during a call to <tt>double update(double)</tt>.
434
435 \subsection tweener-tweener_sequence claw::tween::tweener_sequence
436
437 The claw::tween::tweener_sequence class is a tweener class that
438 executes several tweeners in a row. Its interface is defined as
439 follows:
440
441 \code
442 class tweener_sequence:
443 {
444 public:
445 void insert( const tweener& t );
446
447 void clear();
448
449 bool is_finished() const;
450 double update( double dt );
451
452 void on_finished( finish_callback f );
453 };
454 \endcode
455
456 The \c tweener_sequence class does not compute values by itself but
457 forwards the call to <tt>update(double)</tt> to the current active
458 tweener, splitting the argument value among several tweeners if
459 needed.
460
461 The <tt>bool is_finished()</tt> function returns true when all the
462 tweeners of the sequence are finished. Also, tweeners can be inserted in
463 a \c tweener_sequence at any time.
464
465 The <tt>void on_finished()</tt> function allows to insert a callback
466 that will be called when the last tweener in the sequence finishes,
467 during a call to <tt>double update(double)</tt>.
468
469 \section tweener-easing-functions Easing functions
470
471 The easing functions define how the intermediate values are computed
472 during the interpolation. For example, when tweening a value from 0 to
473 1 during 1 second using a linear interpolation, the value at 0.5
474 second must be 0.5. But when using a quadratic interpolation the value
475 at this same date must be 0.25. The goal of an easing function is to
476 compute those intermediate values according to a predefined curve.
477
478 \subsection tweener-easing-contract The contract of the easing functions
479
480 An easing function is a function whose signature is <tt>double
481 (double)</tt> verifing the following contract:
482
483 - The argument is a number in (0,1) representing the ratio of the
484 total time for which we want the value.
485 - The return value is the amount of change at the time given by the
486 argument, relatively to the interval of the interpolated values,
487 without restriction on its range.
488
489 The second criteria may need more explainations. For example, if the
490 tweener's start value is \a v1 and its last value is \a v2. Then a
491 return value of 0 means that the intermediate value set by the tweener
492 must be \a v1, while a return value equal to 1 corresponds to an
493 intermediate value equal to \a v2. In other words, a return value of
494 \a x implies an intermediate value set by the tweener to
495 <i>v1 + x × (v2 - v1)</i>.
496
497 \subsection tweener-easing-symmetric claw::tween::symmetric_easing
498
499 There are typically three ways to apply an easing effect: when
500 reaching the final value (<i>ease-in</i>), when leaving the initial
501 value (<i>ease-out</i>), or in both situations (<i>ease-in-out</i>).
502
503 One can observe that the ease-out can be computed from the ease-in
504 value, and ease-in-out can be computed from ease-in and ease-out. The
505 goal of the claw::tween::symmetric_easing class is to define the
506 ease-out and ease-in-out functions given the ease-out-function.
507
508 This class is defined as:
509
510 \code
511 template<double (*Function)(double)>
512 class symmetric_easing
513 {
514 public:
515 static double ease_in( double t );
516 static double ease_out( double t );
517 static double ease_in_out( double t );
518
519 };
520 \endcode
521
522 Given a function <tt>double f(double)</tt> verifying the easing
523 contract, then <tt>symmetric_easing<f></tt> provides the following
524 implementations:
525
526 - <tt>symmetric_easing<f>::ease_in(t)</tt> returns <tt>f(t)</tt>,
527 - <tt>symmetric_easing<f>::ease_out(t)</tt>
528 returns <tt>1 - ease_in(1 - t)</tt>,
529 - <tt>symmetric_easing<f>::ease_in_out(t)</tt>
530 returns <tt>ease_in(2 * t) ÷ 2</tt> if \a t is less than 0.5,
531 <tt>0.5 + ease_out(2 * t - 1) ÷ 2</tt> otherwise.
532
533 \subsection tweener-easing-predefined Predefined easing functions
534
535 The following easing classes are implementations of the
536 \c symmetric_easing class in the library.
537
538 <!--
539 for f in *.hpp
540 do
541 CLASS=$(basename $f .hpp)
542 NAME=$(echo $CLASS | cut -d_ -f2)
543
544 echo " <tr>"
545 echo ' <td>'
546 echo " claw::tween::$CLASS"
547 echo ""
548 echo ' <i>f(t) = </i>'
549 echo ' </td>'
550 echo " <td>\image html tween/$NAME-in.png</td>"
551 echo " <td>\image html tween/$NAME-out.png</td>"
552 echo " <td>\image html tween/$NAME-inout.png</td>"
553 echo " </tr>"
554 done
555 -->
556
557 <table>
558 <tr>
559 <th>Easing Class and Ease-In Equation</th>
560 <th>Ease-In</th>
561 <th>Ease-Out</th>
562 <th>Ease-In-Out</th>
563 </tr>
564 <tr>
565 <td>
566 claw::tween::easing_back
567
568 <i>f(t) = t² × (2.70158 × t − 1.70158)</i>
569 </td>
570 <td>\image html tween/back-in.png</td>
571 <td>\image html tween/back-out.png</td>
572 <td>\image html tween/back-inout.png</td>
573 </tr>
574 <tr>
575 <td>
576 claw::tween::easing_bounce
577
578 <table>
579 <tr>
580 <td style="border: none;"><i>f(t) =</i></td>
581 <td style="border-right:none; border-top:none; border-bottom:none;">
582 <i>1 − 7.5625 × (1-t)²</i> if <i>1 − t < 1 ÷ 2.75</i>
583
584 or <i>1 − (7.5625 × (1 − t − 1.5 ÷ 2.75)² + 0.75)</i>
585 if <i>1 − t < 2 ÷ 2.75</i>
586
587 or <i>1 − (7.5625 × (1 − t − 2.25 ÷ 2.75)² + 0.9375)</i>
588 if <i>1 − t < 2.5 ÷ 2.75</i>
589
590 <i>1 − (7.5625 × (1 − t − 2.625 / 2.75)² + 0.984375)</i>
591 otherwise
592 </td>
593 </tr>
594 </table>
595 </td>
596 <td>\image html tween/bounce-in.png</td>
597 <td>\image html tween/bounce-out.png</td>
598 <td>\image html tween/bounce-inout.png</td>
599 </tr>
600 <tr>
601 <td>
602 claw::tween::easing_circ
603
604 <i>f(t) = 1 − √(1 − t²) </i>
605 </td>
606 <td>\image html tween/circ-in.png</td>
607 <td>\image html tween/circ-out.png</td>
608 <td>\image html tween/circ-inout.png</td>
609 </tr>
610 <tr>
611 <td>
612 claw::tween::easing_cubic
613
614 <i>f(t) = t³</i>
615 </td>
616 <td>\image html tween/cubic-in.png</td>
617 <td>\image html tween/cubic-out.png</td>
618 <td>\image html tween/cubic-inout.png</td>
619 </tr>
620 <tr>
621 <td>
622 claw::tween::easing_elastic
623
624 <i>f(t) = −2<sup>10 × (t − 1)</sup> × sin(((t − 1) − 0.3 ÷ 4) × 2 × π ÷ 4)</i>
625 </td>
626 <td>\image html tween/elastic-in.png</td>
627 <td>\image html tween/elastic-out.png</td>
628 <td>\image html tween/elastic-inout.png</td>
629 </tr>
630 <tr>
631 <td>
632 claw::tween::easing_expo
633
634 <table>
635 <tr>
636 <td style="border: none;"><i>f(t) =</i></td>
637 <td style="border-right:none; border-top:none; border-bottom:none;">
638 <i>0</i> if <i>t = 0</i>
639
640 <i>2<sup>10 × (t − 1)</sup></i>
641 </td>
642 </tr>
643 </table>
644 <td>\image html tween/expo-in.png</td>
645 <td>\image html tween/expo-out.png</td>
646 <td>\image html tween/expo-inout.png</td>
647 </tr>
648 <tr>
649 <td>
650 claw::tween::easing_linear
651
652 <i>f(t) = t</i>
653 </td>
654 <td>\image html tween/linear-in.png</td>
655 <td>\image html tween/linear-out.png</td>
656 <td>\image html tween/linear-inout.png</td>
657 </tr>
658 <tr>
659 <td>
660 claw::tween::easing_none
661
662 <i>f(t) = 0</i>
663 </td>
664 <td>\image html tween/none-in.png</td>
665 <td>\image html tween/none-out.png</td>
666 <td>\image html tween/none-inout.png</td>
667 </tr>
668 <tr>
669 <td>
670 claw::tween::easing_quad
671
672 <i>f(t) = t²</i>
673 </td>
674 <td>\image html tween/quad-in.png</td>
675 <td>\image html tween/quad-out.png</td>
676 <td>\image html tween/quad-inout.png</td>
677 </tr>
678 <tr>
679 <td>
680 claw::tween::easing_quart
681
682 <i>f(t) = t⁴</i>
683 </td>
684 <td>\image html tween/quart-in.png</td>
685 <td>\image html tween/quart-out.png</td>
686 <td>\image html tween/quart-inout.png</td>
687 </tr>
688 <tr>
689 <td>
690 claw::tween::easing_quint
691
692 <i>f(t) = t⁵</i>
693 </td>
694 <td>\image html tween/quint-in.png</td>
695 <td>\image html tween/quint-out.png</td>
696 <td>\image html tween/quint-inout.png</td>
697 </tr>
698 <tr>
699 <td>
700 claw::tween::easing_sine
701
702 <i>f(t) = 1 − cos(t × π ÷ 2)</i>
703 </td>
704 <td>\image html tween/sine-in.png</td>
705 <td>\image html tween/sine-out.png</td>
706 <td>\image html tween/sine-inout.png</td>
707 </tr>
708 </table>
709 */