Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Engine::Core::TimerManager Class Reference

Schedules one-shot callbacks against engine time. More...

#include <TimerManager.h>

Collaboration diagram for Engine::Core::TimerManager:

Classes

struct  TimerEvent
struct  TimerHandle
 Opaque identifier for a scheduled timer. More...

Public Types

using Callback = std::function<void()>
 Callback type used by unowned timers.

Public Member Functions

 TimerManager ()=default
 Creates a standalone timer manager.
 TimerManager (EngineKern *engine)
 Creates an engine-owned timer manager.
TimerHandle setTimer (double delaySeconds, Callback callback)
 Schedules a one-shot callback after delaySeconds.
TimerHandle setActorTimer (Entities::Actor *owner, double delaySeconds, std::function< void(Entities::Actor &)> callback)
 Schedules a one-shot callback tied to an actor's lifetime.
void cancelTimer (TimerHandle handle)
 Cancels a scheduled timer.
void cancelTimersFor (const Entities::Actor *owner)
 Cancels every actor-owned timer registered for owner.
void clear ()
 Removes all pending timers without running callbacks.
void tick (double nowSeconds)
 Executes all timers due at or before nowSeconds.

Private Member Functions

double getCurrentTimeSeconds () const
bool isEventStillValid (const TimerEvent &event) const
void insertTimer (TimerEvent event)

Private Attributes

EngineKern * engine_ = nullptr
std::vector< TimerEvent > timers_
uint64_t nextTimerId_ = 1
double currentTimeSeconds_ = 0.0

Detailed Description

Schedules one-shot callbacks against engine time.

TimerManager stores delayed callbacks and executes expired callbacks when tick() advances past their fire time. Engine-owned managers use EngineKern::getGlobalTimeSeconds() when scheduling new timers. Standalone managers use the last time value passed to tick().

Timers fire once. Schedule a new timer from the callback when repeated work is required.

See also
EngineKern::getTimerManager()

Definition at line 32 of file TimerManager.h.

Member Typedef Documentation

◆ Callback

using Engine::Core::TimerManager::Callback = std::function<void()>

Callback type used by unowned timers.

Definition at line 61 of file TimerManager.h.

Constructor & Destructor Documentation

◆ TimerManager() [1/2]

Engine::Core::TimerManager::TimerManager ( )
default

Creates a standalone timer manager.

Standalone instances use the time supplied to tick() as their current time base. This constructor is useful for tools and tests that drive time manually.

◆ TimerManager() [2/2]

Engine::Core::TimerManager::TimerManager ( EngineKern * engine)
explicit

Creates an engine-owned timer manager.

New timers use engine global time as their scheduling base. Passing nullptr creates a manager that behaves like a standalone manager.

Parameters
engineEngine instance used to read the current global time.

Member Function Documentation

◆ cancelTimer()

void Engine::Core::TimerManager::cancelTimer ( TimerHandle handle)

Cancels a scheduled timer.

Invalid handles, unknown handles, and handles for already-fired timers are ignored.

Parameters
handleTimer handle returned by setTimer() or setActorTimer().

◆ cancelTimersFor()

void Engine::Core::TimerManager::cancelTimersFor ( const Entities::Actor * owner)

Cancels every actor-owned timer registered for owner.

This does not cancel unowned timers created with setTimer(), even if their callback captures owner.

Parameters
ownerActor whose pending timers should be removed. nullptr is ignored.

◆ clear()

void Engine::Core::TimerManager::clear ( )

Removes all pending timers without running callbacks.

Existing handles become stale after this call.

◆ getCurrentTimeSeconds()

double Engine::Core::TimerManager::getCurrentTimeSeconds ( ) const
nodiscardprivate

◆ insertTimer()

void Engine::Core::TimerManager::insertTimer ( TimerEvent event)
private

◆ isEventStillValid()

bool Engine::Core::TimerManager::isEventStillValid ( const TimerEvent & event) const
nodiscardprivate

◆ setActorTimer()

TimerHandle Engine::Core::TimerManager::setActorTimer ( Entities::Actor * owner,
double delaySeconds,
std::function< void(Entities::Actor &)> callback )

Schedules a one-shot callback tied to an actor's lifetime.

The callback runs only if owner is still backed by a valid ECS entity when the timer expires. Passing nullptr returns an invalid handle and schedules no timer.

Actor::onEngineEndPlay() cancels actor timers for that actor before endPlay() runs.

Parameters
ownerActor that owns the timer.
delaySecondsDelay in seconds. Negative values are clamped to zero.
callbackFunction called with the owning actor when the timer expires.
Returns
Handle that can cancel the timer before it fires.
See also
cancelTimersFor()

◆ setTimer()

TimerHandle Engine::Core::TimerManager::setTimer ( double delaySeconds,
Callback callback )

Schedules a one-shot callback after delaySeconds.

Use this for callbacks whose captured data outlives the timer, or cancel the returned handle before captured data is destroyed. Prefer setActorTimer() for actor-owned gameplay logic.

Parameters
delaySecondsDelay in seconds. Negative values are clamped to zero.
callbackFunction called when the timer expires. Empty callbacks are allowed.
Returns
Handle that can cancel the timer before it fires.

◆ tick()

void Engine::Core::TimerManager::tick ( double nowSeconds)

Executes all timers due at or before nowSeconds.

Timers scheduled for the same fire time run in creation order. Timers scheduled by a callback for the current time can run during the same tick. Callbacks can cancel pending timers.

Game code normally does not call this on the engine-owned manager. EngineKern calls tick() after postTick() callbacks and before pending actor destruction is flushed.

Parameters
nowSecondsAbsolute time in seconds for this tick.

Member Data Documentation

◆ currentTimeSeconds_

double Engine::Core::TimerManager::currentTimeSeconds_ = 0.0
private

Definition at line 169 of file TimerManager.h.

◆ engine_

EngineKern* Engine::Core::TimerManager::engine_ = nullptr
private

Definition at line 166 of file TimerManager.h.

◆ nextTimerId_

uint64_t Engine::Core::TimerManager::nextTimerId_ = 1
private

Definition at line 168 of file TimerManager.h.

◆ timers_

std::vector<TimerEvent> Engine::Core::TimerManager::timers_
private

Definition at line 167 of file TimerManager.h.


The documentation for this class was generated from the following file:
  • /workspace/magerbeton/gl3-vulkan/code/Engine/include/Engine/Core/TimerManager.h