diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarTickRateMonitor.hpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/core/StarTickRateMonitor.hpp')
-rw-r--r-- | source/core/StarTickRateMonitor.hpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/source/core/StarTickRateMonitor.hpp b/source/core/StarTickRateMonitor.hpp new file mode 100644 index 0000000..a50ff5e --- /dev/null +++ b/source/core/StarTickRateMonitor.hpp @@ -0,0 +1,78 @@ +#ifndef STAR_TICK_RATE_MONITOR_HPP +#define STAR_TICK_RATE_MONITOR_HPP + +#include "StarList.hpp" + +namespace Star { + +// Monitors the rate at which 'tick()' is called in wall-clock seconds. +class TickRateMonitor { +public: + // 'window' controls the dropoff at which 'rate' will approach zero if tick + // is not called, measured in seconds. + TickRateMonitor(double window); + + double window() const; + + // Resets to a zero tick-rate state + void reset(); + + // Ticks the given number of times, returns the current rate. + double tick(unsigned count = 1); + + // Returns the rate as of the *current* time, not the time of the last tick. + double rate() const; + +private: + void dropOff(double currentTime); + + double m_window; + double m_lastTick; + double m_ticks; +}; + +// Helps tick at as close as possible to a given tick rate +class TickRateApproacher { +public: + TickRateApproacher(double targetTickRate, double window); + + // The TickRateMonitor window influences how long the TickRateApproacher will + // try and speed up or slow down the tick rate to match the target tick rate. + // It should be chosen so that it is not so short that the actual target rate + // drifts, but not too long so that the rate returns to normal quickly enough + // with outliers. + double window() const; + // Setting the window to a new value will reset the TickRateApproacher + void setWindow(double window); + + double targetTickRate() const; + void setTargetTickRate(double targetTickRate); + + // Resets such that the current tick rate is assumed to be perfectly at the + // target. + void reset(); + + double tick(unsigned count = 1); + double rate() const; + + // How many ticks we currently should perform, so that if each tick happened + // instantly, we would be as close to the target tick rate as possible. If + // we are ahead, may be negative. + double ticksBehind(); + + // The negative of ticksBehind, is positive for how many ticks ahead we + // currently are. + double ticksAhead(); + + // How much spare time we have until the tick rate will begin to be behind + // the target tick rate. + double spareTime(); + +private: + TickRateMonitor m_tickRateMonitor; + double m_targetTickRate; +}; + +}; + +#endif |