Index: source/graphics/GameView.cpp =================================================================== --- source/graphics/GameView.cpp +++ source/graphics/GameView.cpp @@ -29,6 +29,7 @@ #include "graphics/ObjectManager.h" #include "graphics/Patch.h" #include "graphics/SkeletonAnimManager.h" +#include "graphics/SmoothedValue.h" #include "graphics/Terrain.h" #include "graphics/TerrainTextureManager.h" #include "graphics/TerritoryTexture.h" @@ -64,100 +65,7 @@ // Maximum distance outside the edge of the map that the camera's // focus point can be moved -static const float CAMERA_EDGE_MARGIN = 2.0f*TERRAIN_TILE_SIZE; - -/** - * A value with exponential decay towards the target value. - */ -class CSmoothedValue -{ -public: - CSmoothedValue(float value, float smoothness, float minDelta) - : m_Target(value), m_Current(value), m_Smoothness(smoothness), m_MinDelta(minDelta) - { - } - - float GetSmoothedValue() const - { - return m_Current; - } - - void SetValueSmoothly(float value) - { - m_Target = value; - } - - void AddSmoothly(float value) - { - m_Target += value; - } - - void Add(float value) - { - m_Target += value; - m_Current += value; - } - - float GetValue() const - { - return m_Target; - } - - void SetValue(float value) - { - m_Target = value; - m_Current = value; - } - - float Update(float time) - { - if (fabs(m_Target - m_Current) < m_MinDelta) - return 0.0f; - - double p = pow(static_cast(m_Smoothness), 10.0 * static_cast(time)); - // (add the factor of 10 so that smoothnesses don't have to be tiny numbers) - - double delta = (m_Target - m_Current) * (1.0 - p); - m_Current += delta; - return static_cast(delta); - } - - void ClampSmoothly(float min, float max) - { - m_Target = Clamp(m_Target, static_cast(min), static_cast(max)); - } - - // Wrap so 'target' is in the range [min, max] - void Wrap(float min, float max) - { - double t = fmod(m_Target - min, static_cast(max - min)); - if (t < 0) - t += max - min; - t += min; - - m_Current += t - m_Target; - m_Target = t; - } - - float GetSmoothness() const - { - return m_Smoothness; - } - - void SetSmoothness(float smoothness) - { - m_Smoothness = smoothness; - } - -private: - float m_Smoothness; - - double m_Target; // the value which m_Current is tending towards - double m_Current; - // (We use double because the extra precision is worthwhile here) - - float m_MinDelta; // cutoff where we stop moving (to avoid ugly shimmering effects) -}; +static const float CAMERA_EDGE_MARGIN = 2.0f * TERRAIN_TILE_SIZE; class CGameViewImpl { Index: source/graphics/SmoothedValue.h =================================================================== --- source/graphics/SmoothedValue.h +++ source/graphics/SmoothedValue.h @@ -0,0 +1,96 @@ +/* Copyright (C) 2019 Wildfire Games. +* This file is part of 0 A.D. +* +* 0 A.D. is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* 0 A.D. is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with 0 A.D. If not, see . +*/ + +#ifndef INCLUDED_SMOOTHEDVALUE +#define INCLUDED_SMOOTHEDVALUE + +#include "maths/MathUtil.h" + +/** +* A value with exponential decay towards the target value. +*/ +class CSmoothedValue +{ +public: + CSmoothedValue(float value, float smoothness, float minDelta) + : m_Target(value), m_Current(value), m_Smoothness(smoothness), m_MinDelta(minDelta) + { + } + + float GetSmoothedValue() const + { + return m_Current; + } + + void SetValueSmoothly(float value) + { + m_Target = value; + } + + void AddSmoothly(float value) + { + m_Target += value; + } + + void Add(float value) + { + m_Target += value; + m_Current += value; + } + + float GetValue() const + { + return m_Target; + } + + void SetValue(float value) + { + m_Target = value; + m_Current = value; + } + + float GetSmoothness() const + { + return m_Smoothness; + } + + void SetSmoothness(float smoothness) + { + m_Smoothness = smoothness; + } + + void ClampSmoothly(float min, float max) + { + m_Target = Clamp(m_Target, static_cast(min), static_cast(max)); + } + + float Update(float time); + + // Wrap so 'target' is in the range [min, max] + void Wrap(float min, float max); + +private: + float m_Smoothness; + + double m_Target; // the value which m_Current is tending towards + double m_Current; + // (We use double because the extra precision is worthwhile here) + + float m_MinDelta; // cutoff where we stop moving (to avoid ugly shimmering effects) +}; + +#endif // INCLUDED_SMOOTHEDVALUE Index: source/graphics/SmoothedValue.cpp =================================================================== --- source/graphics/SmoothedValue.cpp +++ source/graphics/SmoothedValue.cpp @@ -0,0 +1,47 @@ +/* Copyright (C) 2019 Wildfire Games. +* This file is part of 0 A.D. +* +* 0 A.D. is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* 0 A.D. is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with 0 A.D. If not, see . +*/ + +#include "precompiled.h" + +#include + +#include "graphics/SmoothedValue.h" + +float CSmoothedValue::Update(float time) +{ + if (fabs(m_Target - m_Current) < m_MinDelta) + return 0.0f; + + double p = pow(static_cast(m_Smoothness), 10.0 * static_cast(time)); + // (add the factor of 10 so that smoothnesses don't have to be tiny numbers) + + double delta = (m_Target - m_Current) * (1.0 - p); + m_Current += delta; + return static_cast(delta); +} + +// Wrap so 'target' is in the range [min, max] +void CSmoothedValue::Wrap(float min, float max) +{ + double t = fmod(m_Target - min, static_cast(max - min)); + if (t < 0) + t += max - min; + t += min; + + m_Current += t - m_Target; + m_Target = t; +}