Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cValueLerp.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cValueLerp_H
8 #define _INC_cValueLerp_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "Calc.h"
15 
16 namespace GrayLib
17 {
19 
20  template <typename TYPE>
21  class cValueLerp
22  {
26  private:
27  TYPE m_nValCur;
28  TIMESYS_t m_TimeLast;
29  public:
30  cValueLerp(TYPE nValCur = 0) noexcept
31  : m_nValCur(nValCur)
32  , m_TimeLast(cTimeSys::k_CLEAR)
33  {
34  }
35  TYPE get_Val() const noexcept
36  {
37  return m_nValCur;
38  }
39  void put_Val(TYPE nValCur) noexcept
40  {
42  m_nValCur = nValCur;
43  m_TimeLast = cTimeSys::k_CLEAR;
44  }
45 
46  static bool TickX(TYPE& nValCur, TYPE nValEnd, TIMESECF_t fTimePassed, float fValPerSec)
47  {
53 
54  ASSERT(fValPerSec > 0); // allowed change rate per sec.
55  ASSERT(fTimePassed >= 0);
56 
57  TYPE nDiff = nValEnd - nValCur; // How far left to go ?
58  if (nDiff == 0)
59  {
60  return false; // done
61  }
62  float fChange = fValPerSec * fTimePassed;
63  TYPE nDiffA = Calc::Abs(nDiff); // How far left to go ABS ?
64  if (fChange >= nDiffA)
65  {
66  nValCur = nValEnd; // reached my goal. Stop.
67  return true;
68  }
69 
70  // always do the compare as a float to watch for overflow!
71  TYPE nValNext;
72  if (nDiff > 0)
73  {
74  nValNext = (TYPE)(nValCur + fChange);
75  }
76  else
77  {
78  nValNext = (TYPE)(nValCur - fChange);
79  }
80  if (nValCur == nValNext)
81  return false; // no change with this amount of time. (mostly for integer types)
82  nValCur = nValNext;
83  return true; // there is a value change. for integer types.
84  }
85 
86  bool TickCap(float fValPerSec, TYPE nValEnd, TIMESYS_t TimeNow = cTimeSys::k_CLEAR)
87  {
96 
97  ASSERT(fValPerSec > 0); // allowed change rate per second.
98  if (m_nValCur == nValEnd)
99  {
100  m_TimeLast = cTimeSys::k_CLEAR; // finished.
101  return false;
102  }
103 
104  if (TimeNow == cTimeSys::k_CLEAR)
105  {
106  TimeNow = cTimeSys::GetTimeNow();
107  }
108  if (m_TimeLast != cTimeSys::k_CLEAR) // starting motion.
109  {
110  TIMESYSD_t nTimeDiff = TimeNow - m_TimeLast;
111  if (nTimeDiff > 0) // SKIP time wrap!!
112  {
113  if (!TickX(m_nValCur, nValEnd, ((float)nTimeDiff) / cTimeSys::k_FREQ, fValPerSec))
114  {
115  return false; // not enough time to make a whole digit change (integer). dont advace time.
116  }
117  // Should we adjust TimeNow to undo partial (non integer) values ?
118  }
119  }
120  m_TimeLast = TimeNow;
121  return true;
122  }
123 
125  };
126 }
127 #endif
#define TYPE
Definition: StrT.cpp:38
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cValueLerp.h:22
void put_Val(TYPE nValCur) noexcept
Definition: cValueLerp.h:39
bool TickCap(float fValPerSec, TYPE nValEnd, TIMESYS_t TimeNow=cTimeSys::k_CLEAR)
Definition: cValueLerp.h:86
TYPE get_Val() const noexcept
Definition: cValueLerp.h:35
static bool TickX(TYPE &nValCur, TYPE nValEnd, TIMESECF_t fTimePassed, float fValPerSec)
Definition: cValueLerp.h:46
UNITTEST_FRIEND(cValueLerp)
cValueLerp(TYPE nValCur=0) noexcept
Definition: cValueLerp.h:30
Definition: cTimeSys.h:93
static const TIMESYS_t k_CLEAR
Definition: cTimeSys.h:99
static const TIMESYS_t k_FREQ
milliSec per Sec
Definition: cTimeSys.h:100
static TIMESYS_t GetTimeNow() noexcept
Definition: cTimeSys.h:121
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
float TIMESECF_t
delta float seconds.
Definition: cTimeSys.h:20
UINT32 TIMESYS_t
TIMESYS_t = The normal system tick timer. milli-seconds since start of system/app ?
Definition: cTimeSys.h:27
static TYPE Abs(TYPE a) noexcept
similar to ABS(n) macro. Does nothing for unsigned types.