Class: SFML::Time
- Inherits:
-
Object
- Object
- SFML::Time
- Includes:
- Comparable
- Defined in:
- lib/sfml/system/time.rb
Overview
Represents a time value. Stored internally as microseconds (int64),
same as sfTime in CSFML. Immutable and Comparable, so two Times
can be == / < / > / sorted.
SFML::Time.seconds(1.5) #=> 1_500_000 µs
SFML::Time.milliseconds(500)
SFML::Time.microseconds(42)
SFML::Time.zero
t1 + t2 # sum of two Times
t.as_seconds # Float seconds
Instance Attribute Summary collapse
-
#microseconds ⇒ Object
readonly
The microseconds.
Class Method Summary collapse
-
.from_native(struct) ⇒ Object
Returns the self.
-
.microseconds(value) ⇒ Object
Build a Time directly from microseconds.
-
.milliseconds(value) ⇒ Object
Build a Time from an Integer of milliseconds.
-
.seconds(value) ⇒ Object
Build a Time from a Float of seconds.
-
.zero ⇒ Object
The zero Time.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Sum of two Times — returns a fresh Time.
-
#-(other) ⇒ Object
Difference between two Times.
-
#-@ ⇒ Object
Negate (useful when computing a "ago" offset).
-
#<=>(other) ⇒ Object
Compare two Times — implements
Comparable, so<,<=,>=,>,clamp, andArray#sortall work. -
#as_microseconds ⇒ Object
As Integer microseconds — the raw stored value.
-
#as_milliseconds ⇒ Object
As Integer milliseconds (truncated).
-
#as_seconds ⇒ Object
As Float seconds.
-
#eql?(other) ⇒ Boolean
(also: #==)
Value equality.
-
#hash ⇒ Object
Hash key support — Times can be Hash keys / Set members.
-
#initialize(microseconds) ⇒ Time
constructor
A new instance of Time.
-
#to_native ⇒ Object
Returns the to native.
-
#to_s ⇒ Object
(also: #inspect)
String representation for debugging.
Constructor Details
#initialize(microseconds) ⇒ Time
Returns a new instance of Time.
28 29 30 31 |
# File 'lib/sfml/system/time.rb', line 28 def initialize(microseconds) @microseconds = Integer(microseconds) freeze end |
Instance Attribute Details
#microseconds ⇒ Object (readonly)
The microseconds.
17 18 19 |
# File 'lib/sfml/system/time.rb', line 17 def microseconds @microseconds end |
Class Method Details
.from_native(struct) ⇒ Object
Returns the self.
62 63 64 |
# File 'lib/sfml/system/time.rb', line 62 def self.from_native(struct) # :nodoc: new(struct[:microseconds]) end |
.microseconds(value) ⇒ Object
Build a Time directly from microseconds.
24 25 |
# File 'lib/sfml/system/time.rb', line 24 def self.microseconds(value) = new(Integer(value)) # The zero Time. |
.milliseconds(value) ⇒ Object
Build a Time from an Integer of milliseconds.
22 23 |
# File 'lib/sfml/system/time.rb', line 22 def self.milliseconds(value) = new(Integer(value) * 1_000) # Build a Time directly from microseconds. |
.seconds(value) ⇒ Object
Build a Time from a Float of seconds.
20 21 |
# File 'lib/sfml/system/time.rb', line 20 def self.seconds(value) = new((value * 1_000_000).to_i) # Build a Time from an Integer of milliseconds. |
.zero ⇒ Object
The zero Time.
26 |
# File 'lib/sfml/system/time.rb', line 26 def self.zero = new(0) |
Instance Method Details
#+(other) ⇒ Object
Sum of two Times — returns a fresh Time.
41 42 |
# File 'lib/sfml/system/time.rb', line 41 def +(other) = Time.new(@microseconds + other.microseconds) # Difference between two Times. |
#-(other) ⇒ Object
Difference between two Times.
43 44 |
# File 'lib/sfml/system/time.rb', line 43 def -(other) = Time.new(@microseconds - other.microseconds) # Negate (useful when computing a "ago" offset). |
#-@ ⇒ Object
Negate (useful when computing a "ago" offset).
45 |
# File 'lib/sfml/system/time.rb', line 45 def -@ = Time.new(-@microseconds) |
#<=>(other) ⇒ Object
Compare two Times — implements Comparable, so <, <=, >=,
>, clamp, and Array#sort all work.
49 |
# File 'lib/sfml/system/time.rb', line 49 def <=>(other) = @microseconds <=> other.microseconds |
#as_microseconds ⇒ Object
As Integer microseconds — the raw stored value.
38 |
# File 'lib/sfml/system/time.rb', line 38 def as_microseconds = @microseconds |
#as_milliseconds ⇒ Object
As Integer milliseconds (truncated).
36 37 |
# File 'lib/sfml/system/time.rb', line 36 def as_milliseconds = @microseconds / 1_000 # As Integer microseconds — the raw stored value. |
#as_seconds ⇒ Object
As Float seconds.
34 35 |
# File 'lib/sfml/system/time.rb', line 34 def as_seconds = @microseconds / 1_000_000.0 # As Integer milliseconds (truncated). |
#eql?(other) ⇒ Boolean Also known as: ==
Value equality.
54 |
# File 'lib/sfml/system/time.rb', line 54 def eql?(other) = other.is_a?(Time) && @microseconds == other.microseconds |
#hash ⇒ Object
Hash key support — Times can be Hash keys / Set members.
52 53 |
# File 'lib/sfml/system/time.rb', line 52 def hash = @microseconds.hash # Value equality. |
#to_native ⇒ Object
Returns the to native.
67 68 69 |
# File 'lib/sfml/system/time.rb', line 67 def to_native # :nodoc: C::System::Time.new.tap { |t| t[:microseconds] = @microseconds } end |
#to_s ⇒ Object Also known as: inspect
String representation for debugging.
58 |
# File 'lib/sfml/system/time.rb', line 58 def to_s = "#<SFML::Time #{as_seconds}s>" |