Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
MovingAverage.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "FlingTypes.h"
4 
5 namespace Fling
6 {
10  template<typename T, size_t t_Size>
12  {
13  public:
14 
15  MovingAverage();
16  ~MovingAverage() = default;
17 
19  void Push(T t_Element);
20 
22  T GetAverage() const;
23 
24  private:
26  size_t m_CurrentIndex = {};
27 
29  size_t m_Count = {};
30 
32  size_t m_MaxSize = {};
33 
35  T m_Buffer [t_Size] = {};
36  };
37 
38  template<typename T, size_t t_Size>
40  : m_MaxSize(t_Size)
41  {
42  }
43 
44  template<typename T, size_t t_Size>
45  inline void MovingAverage<T, t_Size>::Push(T t_Element)
46  {
47  ++m_Count;
48  const UINT32 index = m_CurrentIndex++;
49  m_Buffer[index & (m_MaxSize - 1u)] = t_Element;
50  }
51 
52  template<typename T, size_t t_Size>
54  {
55  T Average = {};
56 
57  size_t Range = (m_Count < m_MaxSize ? m_Count : m_MaxSize);
58  for (size_t i = 0; i < Range; ++i)
59  {
60  Average += m_Buffer[i];
61  }
62 
63  return (Average / static_cast<T>(Range));
64  }
65 } // namespace Fling
void Push(T t_Element)
Push a value onto the buffer to be calculated.
Definition: MovingAverage.hpp:45
~MovingAverage()=default
size_t m_CurrentIndex
Index for keeping track of what part of the buffer we should be inserting into.
Definition: MovingAverage.hpp:26
size_t m_Count
Current count of things pushed to this average.
Definition: MovingAverage.hpp:29
size_t m_MaxSize
Max sample size in this moving average.
Definition: MovingAverage.hpp:32
T GetAverage() const
Get the average (Sum / num elements)
Definition: MovingAverage.hpp:53
uint32_t UINT32
Definition: FlingTypes.h:13
Definition: Engine.h:29
MovingAverage()
Definition: MovingAverage.hpp:39
A moving average can be used to calculate things like FPS.
Definition: MovingAverage.hpp:11
T m_Buffer[t_Size]
Buffer of samples used to calculate the moving average.
Definition: MovingAverage.hpp:35