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.instantfor accurate, non-jumping timing elapsed- Total time since timer starteddelta- 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 tick interval (~60 FPS).
Constructors
Creates a new timer with the specified interval.
interval- Time between tick events (default: 16ms for ~60 FPS)
Instance methods
Sets the interval between ticks.
The new interval takes effect on the next tick. Can be changed while the timer is running.
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.
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.