Plasma Engine  2.0
Loading...
Searching...
No Matches
IntervalScheduler.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Reflection/Reflection.h>
5
6struct PL_CORE_DLL plUpdateRate
7{
8 using StorageType = plUInt8;
9
10 enum Enum
11 {
12 EveryFrame,
13 Max30fps,
14 Max20fps,
15 Max10fps,
16 Max5fps,
17 Max2fps,
18 Max1fps,
19 Never,
20
21 Default = Max30fps
22 };
23
24 static plTime GetInterval(Enum updateRate);
25};
26
27PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plUpdateRate);
28
30
35class PL_CORE_DLL plIntervalSchedulerBase
36{
37protected:
38 plIntervalSchedulerBase(plTime minInterval, plTime maxInterval);
40
41 plUInt32 GetHistogramIndex(plTime value);
42 plTime GetHistogramSlotValue(plUInt32 uiIndex);
43
44 static float GetRandomZeroToOne(int pos, plUInt32& seed);
45 static plTime GetRandomTimeJitter(int pos, plUInt32& seed);
46
47 plTime m_MinInterval;
48 plTime m_MaxInterval;
49 double m_fInvIntervalRange;
50
51 plTime m_CurrentTime;
52
53 plUInt32 m_uiSeed = 0;
54
55 static constexpr plUInt32 HistogramSize = 32;
56 plUInt32 m_Histogram[HistogramSize] = {};
57 plTime m_HistogramSlotValues[HistogramSize] = {};
58};
59
61
63template <typename T>
65{
67
68public:
69 PL_ALWAYS_INLINE plIntervalScheduler(plTime minInterval = plTime::MakeFromMilliseconds(1), plTime maxInterval = plTime::MakeFromSeconds(1))
70 : SUPER(minInterval, maxInterval)
71 {
72 }
73
74 void AddOrUpdateWork(const T& work, plTime interval);
75 void RemoveWork(const T& work);
76
77 plTime GetInterval(const T& work) const;
78
79 // reference to the work that should be run and time passed since this work has been last run.
80 using RunWorkCallback = plDelegate<void(const T&, plTime)>;
81
84 void Update(plTime deltaTime, RunWorkCallback runWorkCallback);
85
86 void Clear();
87
88private:
89 struct Data
90 {
91 T m_Work;
92 plTime m_Interval;
93 plTime m_DueTime;
94 plTime m_LastScheduledTime;
95
96 bool IsValid() const;
97 void MarkAsInvalid();
98 };
99
101 DataMap m_Data;
103
104 typename DataMap::Iterator InsertData(Data& data);
106};
107
108#include <Core/Utils/Implementation/IntervalScheduler_inl.h>
Definition DynamicArray.h:81
Definition HashTable.h:333
Helper class to schedule work in intervals typically larger than the duration of one frame.
Definition IntervalScheduler.h:36
Definition IntervalScheduler.h:65
void Update(plTime deltaTime, RunWorkCallback runWorkCallback)
Advances the scheduler by deltaTime and triggers runWorkCallback for each work that should be run dur...
Definition IntervalScheduler_inl.h:109
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeFromMilliseconds(double fMilliseconds)
Creates an instance of plTime that was initialized from milliseconds.
Definition Time.h:26
PL_ALWAYS_INLINE static constexpr plTime MakeFromSeconds(double fSeconds)
Creates an instance of plTime that was initialized from seconds.
Definition Time.h:30
Definition IntervalScheduler.h:7