module Eagle::Clock

Overview

Frame timing you can read from anywhere: how long the last frame took, how long the game has been running, the frame rate, and a global time scale.

The dt passed to App#update and Node#process is the same as Clock.delta. Multiply speeds by it so movement is the same at any frame rate.

.scale slows down or speeds up everything that uses .delta: set it to 0.3 for slow motion or 0 to freeze gameplay while menus keep working through .raw_delta.

class Hud < Node2D
  def draw(g : Graphics) : Nil
    g.print("fps #{Clock.fps.round}  t=#{Clock.elapsed.round(1)}s", 10, 10)
  end
end

Clock.scale = 0.25 # bullet time
pulse = Math.sin(Clock.elapsed * 4) # a value that oscillates over time

Defined in:

eagle/core/clock.cr

Class Method Summary

Class Method Detail

def self.delta : Float32 #

Seconds since the last frame, multiplied by .scale. Clamped so a long hitch (for example after a breakpoint) doesn't teleport everything.


def self.elapsed : Float64 #

Real seconds since the engine started. Handy for animations driven by Math.sin.


def self.fixed_alpha : Float32 #

How far the current frame sits between the last fixed step and the next, from 0 to 1. Use it to interpolate positions of physics objects for smooth rendering at high refresh rates.


def self.fixed_delta : Float32 #

Length of one fixed step in seconds, which is 1 / Config#fixed_fps.


def self.fixed_delta=(v : Number) #

Changes the fixed step length.


def self.fps : Float32 #

Measured frames per second, updated twice a second.


def self.frame : Int64 #

Number of frames since start.


def self.max_delta=(v : Number) #

Largest .delta a single frame can report, 0.25 s by default. This stops a long stall from causing a burst of physics steps.


def self.raw_delta : Float32 #

Seconds since the last frame, ignoring .scale. Use it for UI and menus that must keep moving while the game is paused or slowed down.


def self.scale : Float32 #

Time scale applied to .delta: 1 is normal, 0.5 is half speed and 0 pauses gameplay.


def self.scale=(v : Number) #

Sets the time scale. Tweens and fixed steps follow it too.