Plasma Engine  2.0
Loading...
Searching...
No Matches
Clock.h
1#pragma once
2
4
5#include <Foundation/Basics.h>
6#include <Foundation/Communication/Event.h>
7#include <Foundation/IO/Stream.h>
8#include <Foundation/Reflection/Reflection.h>
9#include <Foundation/Time/Time.h>
10
12
14class PL_FOUNDATION_DLL plClock
15{
16public:
18 static plClock* GetGlobalClock() { return s_pGlobalClock; }
19
20public:
22 plClock(plStringView sName); // [tested]
23
28 void Reset(bool bEverything); // [tested]
29
35 void Update(); // [tested]
36
40 void SetTimeStepSmoothing(plTimeStepSmoothing* pSmoother);
41
43 plTimeStepSmoothing* GetTimeStepSmoothing() const; // [tested]
44
46 void SetPaused(bool bPaused); // [tested]
47
49 bool GetPaused() const; // [tested]
50
57 void SetFixedTimeStep(plTime diff = plTime()); // [tested]
58
60 plTime GetFixedTimeStep() const; // [tested]
61
68 void SetAccumulatedTime(plTime t); // [tested]
69
75 plTime GetAccumulatedTime() const; // [tested]
76
81 plTime GetTimeDiff() const; // [tested]
82
84 void SetSpeed(double fFactor); // [tested]
85
87 double GetSpeed() const; // [tested]
88
96 void SetMinimumTimeStep(plTime min); // [tested]
97
105 void SetMaximumTimeStep(plTime max); // [tested]
106
109 plTime GetMinimumTimeStep() const; // [tested]
110
113 plTime GetMaximumTimeStep() const; // [tested]
114
116 void Save(plStreamWriter& inout_stream) const;
117
119 void Load(plStreamReader& inout_stream);
120
122 void SetClockName(plStringView sName);
123
126 plStringView GetClockName() const;
127
128
129public:
132 {
133 plStringView m_sClockName;
134
135 plTime m_RawTimeStep;
136 plTime m_SmoothedTimeStep;
137 };
138
140
142 static void AddEventHandler(Event::Handler handler) { s_TimeEvents.AddEventHandler(handler); }
143
145 static void RemoveEventHandler(Event::Handler handler) { s_TimeEvents.RemoveEventHandler(handler); }
146
147
148private:
149 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(Foundation, Clock);
150
151 static Event s_TimeEvents;
152 static plClock* s_pGlobalClock;
153
154 plString m_sName;
155
156 plTime m_AccumulatedTime;
157 plTime m_LastTimeDiff;
158 plTime m_FixedTimeStep;
159 plTime m_LastTimeUpdate;
160 plTime m_MinTimeStep;
161 plTime m_MaxTimeStep;
162
163 double m_fSpeed;
164 bool m_bPaused;
165
166 plTimeStepSmoothing* m_pTimeStepSmoother;
167};
168
169
174class PL_FOUNDATION_DLL plTimeStepSmoothing
175{
176public:
177 virtual ~plTimeStepSmoothing() = default;
178
190 virtual plTime GetSmoothedTimeStep(plTime rawTimeStep, const plClock* pClock) = 0;
191
196 virtual void Reset(const plClock* pClock) = 0;
197};
198
199PL_DECLARE_REFLECTABLE_TYPE(PL_FOUNDATION_DLL, plClock);
200
201#include <Foundation/Time/Implementation/Clock_inl.h>
A clock that can be speed up, slowed down, paused, etc. Useful for updating game logic,...
Definition Clock.h:15
static void AddEventHandler(Event::Handler handler)
Allows to register a function as an event receiver. All receivers will be notified in the order that ...
Definition Clock.h:142
static void RemoveEventHandler(Event::Handler handler)
Unregisters a previously registered receiver. It is an error to unregister a receiver that was not re...
Definition Clock.h:145
static plClock * GetGlobalClock()
Returns the global clock.
Definition Clock.h:18
Definition Event.h:177
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Base class for all time step smoothing algorithms.
Definition Clock.h:175
virtual void Reset(const plClock *pClock)=0
Called when plClock::Reset(), plClock::Load() or plClock::SetPaused(true) was called.
virtual plTime GetSmoothedTimeStep(plTime rawTimeStep, const plClock *pClock)=0
The function to override to implement time step smoothing.
The data that is sent through the event interface.
Definition Clock.h:132
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12