LkEngine 0.1.2
 
Loading...
Searching...
No Matches
TimerManager.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8
11#include "LkEngine/Core/Assert.h"
12#include "LkEngine/Core/Log/Log.h"
13#include "LkEngine/Core/Delegate/Delegate.h"
14
15#include "LkEngine/Core/Time/Time.h"
18
19namespace LkEngine {
20
32 LK_DECLARE_DELEGATE(FTimerDelegate);
33
40 {
41 public:
46 FTimerHandle SetTimer(const FTimerDelegate& TimerDelegate,
47 const float DelayInSeconds,
48 const bool Loop = false)
49 {
50 using namespace std::chrono;
51 FTimerHandle Handle = FTimerHandle::CreateHandle();
52 Internal_FTimer Timer;
53 Timer.Delegate = TimerDelegate; /* Copy the delegate. */
54 Timer.EndTime = high_resolution_clock::now() + milliseconds(static_cast<int>(DelayInSeconds * 1000));
55 Timer.bShouldLoop = Loop;
56 Timer.Delay = DelayInSeconds;
57
58 Timers[Handle] = std::move(Timer);
59
60 return Handle;
61 }
62
68 FORCEINLINE void ClearTimer(const FTimerHandle Handle)
69 {
70 if (Timers.contains(Handle))
71 {
72 Timers.erase(Handle);
73 }
74 else
75 {
76 LK_CORE_ERROR_TAG("TimerManager", "Timer with handle {} is not in timer collection", Handle.GetID());
77 }
78 }
79
84 FORCEINLINE bool IsTimerActive(const FTimerHandle Handle) const
85 {
86 return Timers.find(Handle) != Timers.end();
87 }
88
92 FORCEINLINE void Update()
93 {
94 using namespace std::chrono;
95 const high_resolution_clock::time_point Now = high_resolution_clock::now();
96
97 uint8_t ExpiredTimers = 0;
98 std::array<FTimerHandle, MaxExpiredTimerStackSize> ExpiredTimersArray;
99
100 for (auto& [Handle, Timer] : Timers)
101 {
102 if (Now >= Timer.EndTime)
103 {
104 Timer.Delegate.ExecuteIfBound();
105
106 if (Timer.bShouldLoop)
107 {
108 /* Reset the end time for the next interval. */
109 Timer.EndTime = Now + milliseconds(static_cast<int>(Timer.Delay * 1000));
110 }
111 else
112 {
113 /* Mark for removal if it's a one-time timer. */
114 LK_CORE_ASSERT((ExpiredTimers + 1) < MaxExpiredTimerStackSize, "Expired timer index exceed max stack count");
115 ExpiredTimersArray[ExpiredTimers++] = Handle;
116 }
117 }
118 }
119
120 /* Remove non-looping expired timers. */
121 for (uint8_t TimerIndex = 0; TimerIndex < ExpiredTimers; TimerIndex++)
122 {
123 Timers.erase(ExpiredTimersArray[TimerIndex]);
124 }
125 }
126
127 private:
133 struct Internal_FTimer
134 {
135 FTimerDelegate Delegate;
136 std::chrono::high_resolution_clock::time_point EndTime{};
137 float Delay = 0.0f;
138 bool bShouldLoop = false;
139 };
140
141 private:
146 std::unordered_map<FTimerHandle, Internal_FTimer> Timers{};
147
152 static constexpr uint8_t MaxExpiredTimerStackSize = 40;
153 };
154
157}
Core macros used by the entire engine.
Core types.
Timer handle.
Timer.
Definition TimerHandle.h:18
Definition TimerManager.h:40
FTimerHandle SetTimer(const FTimerDelegate &TimerDelegate, const float DelayInSeconds, const bool Loop=false)
Set a timer with a delegate, delay and optional looping.
Definition TimerManager.h:46
FORCEINLINE void Update()
Update timers, execute callbacks as they expire.
Definition TimerManager.h:92
FORCEINLINE bool IsTimerActive(const FTimerHandle Handle) const
Check if a timer is active.
Definition TimerManager.h:84
FORCEINLINE void ClearTimer(const FTimerHandle Handle)
Clear a timer by its handle.
Definition TimerManager.h:68
Definition Asset.h:11