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