sfml3.rb

Class: SFML::Time

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/system/time.c,
ext/system/time.c

Overview

A time duration, with microsecond precision internally. Used throughout the library wherever a duration or timestamp is needed (Clock#elapsed_time, SoundSource#playing_offset, sleep, animation timers, ...).

Includes Comparable.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ Time(0) #new(seconds) ⇒ Time #new(microseconds_integer) ⇒ Time #new(other_time) ⇒ Time

A Float argument is interpreted as seconds; an Integer argument is interpreted as microseconds. Prefer .seconds/.milliseconds/.microseconds to be explicit.

Overloads:

  • #new ⇒ Time(0)
  • #new(seconds) ⇒ Time
  • #new(microseconds_integer) ⇒ Time

Raises:

  • (ArgumentError) —

    if given more than one argument



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'ext/system/time.c', line 61

static VALUE Time_initialize(int argc, VALUE* argv, VALUE self) {
    sfTime time = sfTime_Zero;

    if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cSFTime)) {
        time = ((Time*)Get_Time_Struct(argv[0]))->time;
    } else if (argc == 1 && RB_INTEGER_TYPE_P(argv[0])) {
        time = sfMicroseconds((int64_t)NUM2LL(argv[0]));
    } else if (argc == 1) {
        time = sfSeconds((float)NUM2DBL(argv[0]));
    } else if (argc != 0) {
        raise_invalid_arguments_excepted(1, argc);
    }

    ((Time*)Get_Time_Struct(self))->time = time;

    return self;
}

Class Method Details

.microseconds(value) ⇒ Time

Creates a Time of value microseconds.

Returns:

Returns:



109
110
111
# File 'ext/system/time.c', line 109

static VALUE Time_microseconds(VALUE klass, VALUE rb_microseconds) {
    return Time_wrap(sfMicroseconds((int64_t)NUM2LL(rb_microseconds)));
}

.milliseconds(value) ⇒ Time

Creates a Time of value milliseconds.

Returns:

Returns:



98
99
100
# File 'ext/system/time.c', line 98

static VALUE Time_milliseconds(VALUE klass, VALUE rb_milliseconds) {
    return Time_wrap(sfMilliseconds((int32_t)NUM2INT(rb_milliseconds)));
}

.seconds(value) ⇒ Time

Creates a Time of value seconds, where value is any Float-convertible number.

Returns:

Returns:



87
88
89
# File 'ext/system/time.c', line 87

static VALUE Time_seconds(VALUE klass, VALUE rb_seconds) {
    return Time_wrap(sfSeconds((float)NUM2DBL(rb_seconds)));
}

.zero ⇒ Time

Returns the zero-duration Time.

Returns:

Returns:

  • (Time) —

    a zero-duration Time



120
121
122
# File 'ext/system/time.c', line 120

static VALUE Time_zero(VALUE klass) {
    return Time_wrap(sfTime_Zero);
}

Instance Method Details

#*(scalar) ⇒ Time

Scales the duration by scalar, returning a new Time.

Returns:

Returns:



209
210
211
212
213
# File 'ext/system/time.c', line 209

static VALUE Time_mul(VALUE self, VALUE rb_scalar) {
    sfTime a = ((Time*)Get_Time_Struct(self))->time;

    return Time_wrap(sfMicroseconds((int64_t)(sfTime_asMicroseconds(a) * NUM2DBL(rb_scalar))));
}

#+(other) ⇒ Time

Adds other to the duration, returning a new Time.

Returns:

Returns:



181
182
183
184
185
186
# File 'ext/system/time.c', line 181

static VALUE Time_add(VALUE self, VALUE rb_other) {
    sfTime a = ((Time*)Get_Time_Struct(self))->time;
    sfTime b = time_from_rb(rb_other);

    return Time_wrap(sfMicroseconds(sfTime_asMicroseconds(a) + sfTime_asMicroseconds(b)));
}

#-(other) ⇒ Time

Subtracts other from the duration, returning a new Time.

Returns:

Returns:



195
196
197
198
199
200
# File 'ext/system/time.c', line 195

static VALUE Time_sub(VALUE self, VALUE rb_other) {
    sfTime a = ((Time*)Get_Time_Struct(self))->time;
    sfTime b = time_from_rb(rb_other);

    return Time_wrap(sfMicroseconds(sfTime_asMicroseconds(a) - sfTime_asMicroseconds(b)));
}

#/(other) ⇒ Time, Float

Dividing by a Time returns the (unitless) ratio of the two durations as a Float; dividing by a Number scales the duration.

Returns:

Returns:

Raises:

  • (ZeroDivError) —

    if dividing by a zero Time



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/system/time.c', line 224

static VALUE Time_div(VALUE self, VALUE rb_other) {
    sfTime a = ((Time*)Get_Time_Struct(self))->time;

    if (rb_obj_is_kind_of(rb_other, rb_cSFTime)) {
        int64_t b = sfTime_asMicroseconds(((Time*)Get_Time_Struct(rb_other))->time);

        if (b == 0) {
            rb_raise(rb_eZeroDivError, "divided by 0");
        }

        return DBL2NUM((double)sfTime_asMicroseconds(a) / (double)b);
    }

    return Time_wrap(sfMicroseconds((int64_t)(sfTime_asMicroseconds(a) / NUM2DBL(rb_other))));
}

#<=>(other) ⇒ -1, ...

Compares the duration with other, which may be a Time or a number.

Returns:

  • (-1, 0, 1)

Returns:

  • (Integer)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'ext/system/time.c', line 247

static VALUE Time_cmp(VALUE self, VALUE rb_other) {
    sfTime a = ((Time*)Get_Time_Struct(self))->time;
    sfTime b = time_from_rb(rb_other);
    int64_t micro_a = sfTime_asMicroseconds(a);
    int64_t micro_b = sfTime_asMicroseconds(b);

    if (micro_a < micro_b) {
        return INT2NUM(-1);
    }

    if (micro_a > micro_b) {
        return INT2NUM(1);
    }

    return INT2NUM(0);
}

#==(other) ⇒ Boolean

Returns true if other is a Time holding the same duration.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


271
272
273
274
275
276
277
278
# File 'ext/system/time.c', line 271

static VALUE Time_eql(VALUE self, VALUE rb_other) {
    if (!rb_obj_is_kind_of(rb_other, rb_cSFTime)) {
        return Qfalse;
    }

    return BOOL2RB(sfTime_asMicroseconds(((Time*)Get_Time_Struct(self))->time) ==
                   sfTime_asMicroseconds(((Time*)Get_Time_Struct(rb_other))->time));
}

#as_microseconds ⇒ Integer

Returns the duration expressed in whole microseconds.

Returns:

  • (Integer)

Returns:

  • (Integer)


150
151
152
# File 'ext/system/time.c', line 150

static VALUE Time_as_microseconds(VALUE self) {
    return LL2NUM(sfTime_asMicroseconds(((Time*)Get_Time_Struct(self))->time));
}

#as_milliseconds ⇒ Integer

Returns the duration expressed in whole milliseconds.

Returns:

  • (Integer)

Returns:

  • (Integer)


140
141
142
# File 'ext/system/time.c', line 140

static VALUE Time_as_milliseconds(VALUE self) {
    return INT2NUM(sfTime_asMilliseconds(((Time*)Get_Time_Struct(self))->time));
}

#as_seconds ⇒ Float

Returns the duration expressed in seconds.

Returns:

  • (Float)

Returns:

  • (Float)


130
131
132
# File 'ext/system/time.c', line 130

static VALUE Time_as_seconds(VALUE self) {
    return DBL2NUM(sfTime_asSeconds(((Time*)Get_Time_Struct(self))->time));
}

#to_f ⇒ Float

Alias for #as_seconds.

Returns:

  • (Float)

Returns:

  • (Float)


160
161
162
# File 'ext/system/time.c', line 160

static VALUE Time_to_f(VALUE self) {
    return Time_as_seconds(self);
}

#to_i ⇒ Integer

Alias for #as_microseconds.

Returns:

  • (Integer)

Returns:

  • (Integer)


170
171
172
# File 'ext/system/time.c', line 170

static VALUE Time_to_i(VALUE self) {
    return Time_as_microseconds(self);
}

#to_s ⇒ String

Returns the duration formatted as a number of seconds.

Returns:

  • (String)

Returns:

  • (String) —

    "N s", e.g. "1.5 s"



286
287
288
289
290
291
292
293
# File 'ext/system/time.c', line 286

static VALUE Time_to_s(VALUE self) {
    char buffer[64];

    snprintf(buffer, sizeof(buffer), "%g s",
             sfTime_asSeconds(((Time*)Get_Time_Struct(self))->time));

    return rb_str_new2(buffer);
}