Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
TimerManager.h
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <vector>
6
7#include <entt/entt.hpp>
8
9namespace Engine
10{
11 class EngineKern;
12}
13
14namespace Engine::Entities
15{
16 class Actor;
17}
18
19namespace Engine::Core
20{
33 {
34 public:
42 {
43 uint64_t id = 0;
44
52 [[nodiscard]] explicit operator bool() const
53 {
54 return id != 0;
55 }
56 };
57
61 using Callback = std::function<void()>;
62
69 TimerManager() = default;
70
79 explicit TimerManager( EngineKern * engine );
80
91 TimerHandle setTimer( double delaySeconds, Callback callback );
92
109 Entities::Actor * owner,
110 double delaySeconds,
111 std::function<void( Entities::Actor & )> callback
112 );
113
121 void cancelTimer( TimerHandle handle );
122
130 void cancelTimersFor( const Entities::Actor * owner );
131
137 void clear();
138
150 void tick( double nowSeconds );
151
152 private:
154 {
156 double fireTimeSeconds = 0.0;
158 entt::entity ownerEntity = entt::null;
160 };
161
162 [[nodiscard]] double getCurrentTimeSeconds() const;
163 [[nodiscard]] bool isEventStillValid( const TimerEvent & event ) const;
164 void insertTimer( TimerEvent event );
165
166 EngineKern * engine_ = nullptr;
167 std::vector<TimerEvent> timers_;
168 uint64_t nextTimerId_ = 1;
170 };
171} // namespace Engine::Core
void cancelTimersFor(const Entities::Actor *owner)
Cancels every actor-owned timer registered for owner.
std::function< void()> Callback
Callback type used by unowned timers.
bool isEventStillValid(const TimerEvent &event) const
double getCurrentTimeSeconds() const
std::vector< TimerEvent > timers_
TimerHandle setTimer(double delaySeconds, Callback callback)
Schedules a one-shot callback after delaySeconds.
void clear()
Removes all pending timers without running callbacks.
TimerManager(EngineKern *engine)
Creates an engine-owned timer manager.
void insertTimer(TimerEvent event)
TimerHandle setActorTimer(Entities::Actor *owner, double delaySeconds, std::function< void(Entities::Actor &)> callback)
Schedules a one-shot callback tied to an actor's lifetime.
TimerManager()=default
Creates a standalone timer manager.
void tick(double nowSeconds)
Executes all timers due at or before nowSeconds.
void cancelTimer(TimerHandle handle)
Cancels a scheduled timer.
Owns the engine runtime and provides access to engine subsystems.
Definition Engine.h:67
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Definition Actor.h:71
Opaque identifier for a scheduled timer.