class

Termisu::Event::Source::Timer

Inherits Termisu::Event::Source / Reference / Object

Usage

timer = Termisu::Event::Source::Timer.new(interval: 16.milliseconds)
loop = Termisu::Event::Loop.new
loop.add_source(timer)
loop.start

while event = loop.output.receive?
  case event
  when Termisu::Event::Tick
    position += velocity * event.delta.total_seconds
    puts "Frame #{event.frame}, elapsed: #{event.elapsed}"
  end
end

Timing Behavior

  • Uses Time.instant for accurate, non-jumping timing
  • elapsed - Total time since timer started
  • delta - Time since previous tick (for frame-rate independent updates)
  • frame - Counter starting at 0, increments each tick

Thread Safety

Uses Atomic(Bool) for the running state. Safe to call start/stop from different fibers. The interval= setter is thread-safe.

Constants

DEFAULT_INTERVAL = 16.milliseconds

Default tick interval (~60 FPS).

Log = Termisu::Logs::Event

Constructors

new(interval : Time::Span = DEFAULT_INTERVAL)

Creates a new timer with the specified interval.

  • interval - Time between tick events (default: 16ms for ~60 FPS)
Source

Instance methods

interval

Returns the current interval between ticks.

Source
interval=(value : Time::Span)

Sets the interval between ticks.

The new interval takes effect on the next tick. Can be changed while the timer is running.

Source
name

Returns the source name for identification.

Source
running?

Returns true if the timer is currently running.

Source
start(output : Channel(Event::Any)) : Nil

Starts generating tick events to the output channel.

Spawns a fiber that sleeps for the interval and sends Event::Tick events. The fiber continues until stop is called.

Prevents double-start with compare_and_set.

Source
stop

Stops generating tick events.

Sets the running flag to false, causing the fiber to exit on its next iteration. Uses compare_and_set for idempotent stop operations - calling stop twice is safe.

Source