Class: SFML::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/system/clock.rb

Overview

Monotonic high-resolution timer. Wraps sfClock from CSFML.

clock = SFML::Clock.new
# ... do work ...
puts clock.elapsed_time.as_seconds
clock.restart  # returns the elapsed time and resets to zero

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Clock

Create a fresh clock — starts running at zero.

Raises:



10
11
12
13
14
# File 'lib/sfml/system/clock.rb', line 10

def initialize
  ptr = C::System.sfClock_create
  raise Error, "sfClock_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::System.method(:sfClock_destroy))
end

Instance Attribute Details

#handle ⇒ Object (readonly)

:nodoc:



52
53
54
# File 'lib/sfml/system/clock.rb', line 52

def handle
  @handle
end

Instance Method Details

#elapsed_time ⇒ Object Also known as: elapsed

Time elapsed since the clock was last started / restarted, as a SFML::Time. Aliased as #elapsed.



18
19
20
# File 'lib/sfml/system/clock.rb', line 18

def elapsed_time
  Time.from_native(C::System.sfClock_getElapsedTime(@handle))
end

#reset ⇒ Object

Reset to zero without affecting the running state. Returns the elapsed time before the reset.



48
49
50
# File 'lib/sfml/system/clock.rb', line 48

def reset
  Time.from_native(C::System.sfClock_reset(@handle))
end

#restart ⇒ Object

Atomic "read elapsed + reset to zero". Standard pattern for per-frame dt: dt = clock.restart.



42
43
44
# File 'lib/sfml/system/clock.rb', line 42

def restart
  Time.from_native(C::System.sfClock_restart(@handle))
end

#running? ⇒ Boolean

true if the clock is currently ticking (not paused via #stop).

Returns:

  • (Boolean)


24
25
26
# File 'lib/sfml/system/clock.rb', line 24

def running?
  C::System.sfClock_isRunning(@handle)
end

#start ⇒ Object

Resume after #stop. No-op if already running.



29
30
31
32
# File 'lib/sfml/system/clock.rb', line 29

def start
  C::System.sfClock_start(@handle)
  self
end

#stop ⇒ Object

Pause the clock — #elapsed_time freezes until #start.



35
36
37
38
# File 'lib/sfml/system/clock.rb', line 35

def stop
  C::System.sfClock_stop(@handle)
  self
end