API Clock - shmellyorc/Box GitHub Wiki

Clock

Namespace: Box.Systems

Measures elapsed time between frames and tracks total real time. Provides clamped and raw delta values for use in update loops.


Constructors

internal Clock()

Creates the singleton Instance, starts the internal SFML clock, and initializes RealTime to 0.


Properties

Name Type Description
static Clock Instance Clock Global clock instance, initialized on first construction.
int DeltaTimeAsMilliseconds int Time elapsed since last Update() in milliseconds.
long DeltaTimeAsMicroseconds long Time elapsed since last Update() in microseconds.
float DeltaTime float Clamped delta time in seconds (max 1/30f).
float DeltaTimeRaw float Unclamped delta time in seconds.
double RealTime double Total unscaled real-world time in seconds since creation.

Methods

Signature Description
internal void Update() Restart the internal clock to compute DeltaTimeRaw, clamp to DeltaTime, and accumulate into RealTime.
static float ToFps(float seconds) Convert a time delta in seconds to frames per second (1f / seconds).

Usage Example

// At initialization
env.clock = new Clock();  // sets Clock.Instance

// In your game loop:
while (running)
{
    // At start of frame
    Clock.Instance.Update();

    // Use delta for updates
delta = Clock.Instance.DeltaTime;
positions += velocity * delta;

    // Optionally display FPS
float fps = Clock.ToFps(Clock.Instance.DeltaTimeRaw);
    Console.WriteLine($"FPS: {fps:F1}");
}