Merge 'remotes/trunk'
[0ad.git] / source / graphics / SmoothedValue.h
blob50fff0eb7270bd7292714e0d186f193ba59fd5ae
1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_SMOOTHEDVALUE
19 #define INCLUDED_SMOOTHEDVALUE
21 #include "maths/MathUtil.h"
23 /**
24 * A value with exponential decay towards the target value.
26 class CSmoothedValue
28 public:
29 CSmoothedValue(float value, float smoothness, float minDelta);
31 float GetSmoothedValue() const
33 return m_Current;
36 void SetValueSmoothly(float value)
38 m_Target = value;
41 void AddSmoothly(float value)
43 m_Target += value;
46 void Add(float value)
48 m_Target += value;
49 m_Current += value;
52 float GetValue() const
54 return m_Target;
57 void SetValue(float value)
59 m_Target = value;
60 m_Current = value;
63 float GetSmoothness() const
65 return m_Smoothness;
68 void SetSmoothness(float smoothness)
70 m_Smoothness = smoothness;
73 void ClampSmoothly(float min, float max)
75 m_Target = Clamp<double>(m_Target, min, max);
78 float Update(float time);
80 // Wrap so 'target' is in the range [min, max]
81 void Wrap(float min, float max);
83 private:
84 float m_Smoothness;
86 double m_Target; // the value which m_Current is tending towards
87 double m_Current;
88 // (We use double because the extra precision is worthwhile here)
90 float m_MinDelta; // cutoff where we stop moving (to avoid ugly shimmering effects)
93 #endif // INCLUDED_SMOOTHEDVALUE