1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See http://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 /** ****************************************************************************
12 *** \file    interpolator.h
13 *** \author  Raj Sharma, roos@allacrost.org
14 *** \author  Yohann Ferreira, yohann ferreira orange fr
15 *** \brief   Header file for Interpolator class
16 ***
17 *** The Interpolator class can interpolate between a single and final value
18 *** using various methods (linear, fast, slow, etc.)
19 *** ***************************************************************************/
20 
21 #ifndef __INTERPOLATOR_HEADER__
22 #define __INTERPOLATOR_HEADER__
23 
24 #include <cstdint>
25 
26 namespace vt_mode_manager
27 {
28 
29 //! \brief The various ways to create smoothed values between two numbers (e.g. linear interpolation)
30 enum InterpolationMethod {
31     INTERPOLATE_INVALID = -1,
32 
33     //! Rise from A to B and then down to A again
34     INTERPOLATE_EASE = 0,
35     //! Keeps a constant value of A
36     INTERPOLATE_SRCA = 1,
37     //! Keeps a constant value of B
38     INTERPOLATE_SRCB = 2,
39     //! Rises quickly at the beginning and levels out
40     INTERPOLATE_FAST = 3,
41     //! Rises slowly at the beginning then shoots up
42     INTERPOLATE_SLOW = 4,
43     //! Simple linear interpolation between A and B
44     INTERPOLATE_LINEAR = 5,
45 
46     INTERPOLATE_TOTAL = 6
47 };
48 
49 
50 /** ****************************************************************************
51 *** \brief Enables various methods of interpolation
52 ***
53 *** The basic way to use it is to set the interpolation method using
54 ***        SetMethod(), then call Start() with the values you want to
55 ***        interpolate between and the time to do it in.
56 *** ***************************************************************************/
57 class Interpolator
58 {
59 public:
60     Interpolator();
61 
62     /** \brief Sets the interpolation method to use
63     *** \param method interpolation method to use
64     *** \note This method should only be invoked when there is no interpolation
65     *** taking place. If the class is in the middle of an interpolation, the new
66     *** method will not be set.
67     **/
68     void SetMethod(InterpolationMethod method);
69 
70     /** \brief Begins a new interpolation
71     *** \param a The start value of interpolation
72     *** \param b The end value of interpolation
73     *** \param milliseconds The amount of time, in milliseconds, to interpolate over
74     **/
75     void Start(float a, float b, uint32_t milliseconds);
76 
77     /** \brief Updates the interpolation timer and value
78     *** \param frame_time The amount of milliseconds to update the time value by
79     **/
80     void Update(uint32_t frame_time);
81 
82     //! \brief Returns true if the interpolation is complete
IsFinished()83     bool IsFinished() const {
84         return _finished;
85     }
86 
87     //! \brief Returns the current value for the interpolation in progress
GetValue()88     float GetValue() const {
89         return _current_value;
90     }
91 
92 private:
93     //! \brief The interpolation method to be used
94     InterpolationMethod _method;
95 
96     //! \brief The two numbers to interpolate between
97     float _a, _b;
98 
99     //! \brief The current time in the interpolation
100     uint32_t _current_time;
101 
102     //! \brief The end of the interpolation
103     uint32_t _end_time;
104 
105     //! \brief The current interpolated value
106     float _current_value;
107 
108     //! \brief Set to true if the interpolation is finished
109     bool _finished;
110 
111     //! \brief Returns true if a valid interpolation method is set
_ValidMethod()112     bool _ValidMethod() {
113         return (_method > INTERPOLATE_INVALID && _method < INTERPOLATE_TOTAL);
114     }
115 }; // class Interpolator
116 
117 }  // namespace vt_video
118 
119 #endif // __INTERPOLATOR_HEADER__
120