1
0
forked from 0ad/0ad

Moves CSmoothedValue to separate file.

Reviewed By: elexis
Differential Revision: https://code.wildfiregames.com/D2346
This was SVN commit r23026.
This commit is contained in:
Vladislav Belov 2019-10-01 22:42:54 +00:00
parent 45a18c652d
commit 1cf6c6b63c
3 changed files with 149 additions and 94 deletions

View File

@ -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 @@ extern int g_xres, g_yres;
// 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<double>(m_Smoothness), 10.0 * static_cast<double>(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<float>(delta);
}
void ClampSmoothly(float min, float max)
{
m_Target = Clamp(m_Target, static_cast<double>(min), static_cast<double>(max));
}
// Wrap so 'target' is in the range [min, max]
void Wrap(float min, float max)
{
double t = fmod(m_Target - min, static_cast<double>(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
{

View File

@ -0,0 +1,54 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include <cmath>
#include "graphics/SmoothedValue.h"
CSmoothedValue::CSmoothedValue(float value, float smoothness, float minDelta)
: m_Target(value),
m_Current(value),
m_Smoothness(smoothness),
m_MinDelta(minDelta)
{
}
float CSmoothedValue::Update(float time)
{
if (fabs(m_Target - m_Current) < m_MinDelta)
return 0.0f;
double p = pow(static_cast<double>(m_Smoothness), 10.0 * static_cast<double>(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<float>(delta);
}
void CSmoothedValue::Wrap(float min, float max)
{
double t = fmod(m_Target - min, static_cast<double>(max - min));
if (t < 0)
t += max - min;
t += min;
m_Current += t - m_Target;
m_Target = t;
}

View File

@ -0,0 +1,93 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#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);
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<double>(min), static_cast<double>(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