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/game/StarGameTimers.hpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/game/StarGameTimers.hpp')
-rw-r--r-- | source/game/StarGameTimers.hpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/source/game/StarGameTimers.hpp b/source/game/StarGameTimers.hpp new file mode 100644 index 0000000..9dff1ae --- /dev/null +++ b/source/game/StarGameTimers.hpp @@ -0,0 +1,77 @@ +#ifndef STAR_GAME_TIMERS_HPP +#define STAR_GAME_TIMERS_HPP + +#include "StarGameTypes.hpp" +#include "StarJson.hpp" + +namespace Star { + +struct GameTimer { + GameTimer(); + explicit GameTimer(float time); + + float time; + float timer; + + bool tick(float dt = WorldTimestep); // returns true if time is up + bool wrapTick(float dt = WorldTimestep); // auto resets + void reset(); + void setDone(); + void invert(); + + bool ready() const; + float percent() const; +}; + +DataStream& operator>>(DataStream& ds, GameTimer& gt); +DataStream& operator<<(DataStream& ds, GameTimer const& gt); + +struct SlidingWindow { + SlidingWindow(); + SlidingWindow(float windowSize, size_t resolution, float initialValue); + + GameTimer sampleTimer; + float windowSize; + size_t resolution; + + float currentMin; + float currentMax; + float currentAverage; + + size_t currentIndex; + std::vector<float> window; + + void reset(float initialValue); + void update(function<float()> sampleFunction); + void update(float newValue); + void processUpdate(float newValue); + + float min(); + float max(); + float average(); +}; + +// Keeps long term track of elapsed time based on epochTime. +class EpochTimer { +public: + EpochTimer(); + explicit EpochTimer(Json json); + + Json toJson() const; + + void update(double newEpochTime); + + double elapsedTime() const; + void setElapsedTime(double elapsedTime); + + friend DataStream& operator>>(DataStream& ds, EpochTimer& et); + friend DataStream& operator<<(DataStream& ds, EpochTimer const& et); + +private: + Maybe<double> m_lastSeenEpochTime; + double m_elapsedTime; +}; + +} + +#endif |