sfml3.rb

Class: SFML::Transform

Inherits:
Object
  • Object
show all
Defined in:
ext/graphics/transform.c,
ext/graphics/transform.c

Overview

A 3x3 matrix describing a 2D transformation (translation, rotation, scaling, or any combination). Used to build up and query the transformations applied by Sprite, Text and Transformable.

Constant Summary collapse

IDENTITY =

The identity transform, frozen.

rb_obj_freeze(Transform_wrap(sfTransform_Identity))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ identity Transform #new(array_of_9) ⇒ Transform #new(a00, a01, ...a22) ⇒ Transform

A Transform wraps a 3x3 matrix (9 floats, row-major). With no arguments it is the identity transform; with a single 9-element Array or nine individual numbers, it is built from those matrix elements directly.

Overloads:

Raises:

  • (ArgumentError) —

    if given an argument count other than 0, 1 or 9



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/graphics/transform.c', line 77

static VALUE Transform_init(int argc, VALUE* argv, VALUE self) {
    sfTransform* transform = Get_Transform_Struct(self);
    float matrix[MATRIX_LENGTH];

    if (argc == 0) {
        *transform = sfTransform_Identity;
        return self;
    }

    if (argc == 1) {
        Transform_ArrayToMatrix(argv[0], matrix);
    } else if (argc == MATRIX_LENGTH) {
        size_t i;

        for (i = 0; i < MATRIX_LENGTH; i++) {
            matrix[i] = (float)NUM2DBL(argv[i]);
        }
    } else {
        raise_invalid_arguments_excepted(MATRIX_LENGTH, (size_t)argc);
        return self;
    }

    *transform = sfTransform_fromMatrix(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4],
                                        matrix[5], matrix[6], matrix[7], matrix[8]);

    return self;
}

Instance Attribute Details

#to_a ⇒ Array<Float> (readonly)

Returns the object as an Array.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9 matrix elements, row-major



437
438
439
# File 'ext/graphics/transform.c', line 437

static VALUE Transform_to_a(VALUE self) {
    return Transform_MatrixToArray(Get_Transform_Struct(self)->matrix);
}

Class Method Details

.combine(array_a, array_b) ⇒ Array<Float>

Deprecated.

Use #combine on a Transform instead.

Returns the transform that combines array_a and array_b.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element matrix of array_a combined with array_b



410
411
412
413
414
415
416
417
418
419
# File 'ext/graphics/transform.c', line 410

static VALUE Transform_s_combine(VALUE klass, VALUE rb_arr_a, VALUE rb_arr_b) {
    sfTransform transform_a = Transform_ArrayToTransform(rb_arr_a);
    sfTransform transform_b = Transform_ArrayToTransform(rb_arr_b);

    (void)klass;

    sfTransform_combine(&transform_a, &transform_b);

    return Transform_MatrixToArray(transform_a.matrix);
}

.from_a(array_of_9) ⇒ Transform

Creates a transform from a nine-element array.

Returns:

Returns:

Raises:

  • (ArgumentError) —

    if the Array has fewer than 9 elements



125
126
127
128
# File 'ext/graphics/transform.c', line 125

static VALUE Transform_s_from_a(VALUE klass, VALUE rb_matrix) {
    (void)klass;
    return Transform_wrap(Transform_ArrayToTransform(rb_matrix));
}

.identity ⇒ Transform

Returns the identity transform.

Returns:

Returns:



112
113
114
115
# File 'ext/graphics/transform.c', line 112

static VALUE Transform_s_identity(VALUE klass) {
    (void)klass;
    return Transform_wrap(sfTransform_Identity);
}

.inverse(array) ⇒ Array<Float>

Deprecated.

Use #inverse on a Transform instead.

Returns the inverse of the given transform.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element inverse matrix of array



429
430
431
432
433
434
435
# File 'ext/graphics/transform.c', line 429

static VALUE Transform_s_inverse(VALUE klass, VALUE rb_matrix) {
    sfTransform transform = Transform_ArrayToTransform(rb_matrix);

    (void)klass;

    return Transform_MatrixToArray(sfTransform_getInverse(&transform).matrix);
}

Instance Method Details

#*(other) ⇒ Transform #combine(other) ⇒ Transform

Non-mutating combination: returns a new Transform equal to self combined with other (a Transform or 9-element Array), leaving both operands untouched.

Overloads:

Returns:



285
286
287
288
289
290
291
292
# File 'ext/graphics/transform.c', line 285

static VALUE Transform_mul(VALUE self, VALUE rb_other) {
    sfTransform combined = *Get_Transform_Struct(self);
    sfTransform other = Transform_ArrayToTransform(rb_other);

    sfTransform_combine(&combined, &other);

    return Transform_wrap(combined);
}

#==(other) ⇒ Boolean

other may be a Transform or a 9-element Array.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
# File 'ext/graphics/transform.c', line 177

static VALUE Transform_eql(VALUE self, VALUE rb_other) {
    sfTransform other;

    if (!rb_obj_is_kind_of(rb_other, rb_cTransform) && !RB_TYPE_P(rb_other, T_ARRAY)) {
        return Qfalse;
    }

    other = Transform_ArrayToTransform(rb_other);

    return BOOL2RB(sfTransform_equal(Get_Transform_Struct(self), &other));
}

#*(other) ⇒ Transform #combine(other) ⇒ Transform

Non-mutating combination: returns a new Transform equal to self combined with other (a Transform or 9-element Array), leaving both operands untouched.

Overloads:

Returns:



285
286
287
288
289
290
291
292
# File 'ext/graphics/transform.c', line 285

static VALUE Transform_mul(VALUE self, VALUE rb_other) {
    sfTransform combined = *Get_Transform_Struct(self);
    sfTransform other = Transform_ArrayToTransform(rb_other);

    sfTransform_combine(&combined, &other);

    return Transform_wrap(combined);
}

#combine!(other) ⇒ self

Combines this transform with other (a Transform or 9-element Array) in place, equivalent to matrix multiplication (+self+ * other).

Returns:

  • (self)

Returns:

  • (self)


267
268
269
270
271
272
273
# File 'ext/graphics/transform.c', line 267

static VALUE Transform_combine_bang(VALUE self, VALUE rb_other) {
    sfTransform other = Transform_ArrayToTransform(rb_other);

    sfTransform_combine(Transform_writable(self), &other);

    return self;
}

#copy ⇒ Transform

Returns a deep copy of the object.

Returns:

Returns:



251
252
253
# File 'ext/graphics/transform.c', line 251

static VALUE Transform_copy(VALUE self) {
    return Transform_wrap(*Get_Transform_Struct(self));
}

#gl_matrix ⇒ Array<Float>

Returns the transform as a 4x4 OpenGL matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the equivalent 16-element 4x4 matrix, ready for glLoadMatrixf



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/graphics/transform.c', line 154

static VALUE Transform_gl_matrix(VALUE self) {
    float matrix[GL_MATRIX_LENGTH];
    VALUE rb_arr;
    size_t i;

    sfTransform_getMatrix(Get_Transform_Struct(self), matrix);

    rb_arr = rb_ary_new_capa(GL_MATRIX_LENGTH);

    for (i = 0; i < GL_MATRIX_LENGTH; i++) {
        rb_ary_push(rb_arr, DBL2NUM(matrix[i]));
    }

    return rb_arr;
}

#to_s ⇒ String

Returns a human-readable representation of the object.

Returns:

  • (String)

Returns:

  • (String)


195
196
197
198
199
200
# File 'ext/graphics/transform.c', line 195

static VALUE Transform_to_s(VALUE self) {
    const float* m = Get_Transform_Struct(self)->matrix;

    return rb_sprintf("#<SFML::Transform [%g, %g, %g] [%g, %g, %g] [%g, %g, %g]>", m[0], m[1], m[2],
                      m[3], m[4], m[5], m[6], m[7], m[8]);
}

#inverse ⇒ Transform

Returns the inverse of the transform.

Returns:

Returns:

  • (Transform) —

    the inverse of this transform, or the identity if it is not invertible



241
242
243
# File 'ext/graphics/transform.c', line 241

static VALUE Transform_get_inverse(VALUE self) {
    return Transform_wrap(sfTransform_getInverse(Get_Transform_Struct(self)));
}

#rotate(angle) ⇒ Transform #rotate(angle, center) ⇒ Transform

Returns a copy rotated by angle degrees, around center if given.

Overloads:

Returns:

  • (Transform) —

    a new transform, rotated by angle degrees around center if given, otherwise around the origin



377
378
379
# File 'ext/graphics/transform.c', line 377

static VALUE Transform_rotate(int argc, VALUE* argv, VALUE self) {
    return Transform_rotate_bang(argc, argv, Transform_copy(self));
}

#rotate!(angle) ⇒ self #rotate!(angle, center) ⇒ self

Rotates by angle degrees in place, around center if given, otherwise around the origin.

Overloads:

  • #rotate!(angle) ⇒ self

    Returns:

    • (self)
  • #rotate!(angle, center) ⇒ self

    Returns:

    • (self)

Returns:

  • (self)


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/graphics/transform.c', line 315

static VALUE Transform_rotate_bang(int argc, VALUE* argv, VALUE self) {
    VALUE rb_angle, rb_center;
    sfTransform* transform;

    rb_scan_args(argc, argv, "11", &rb_angle, &rb_center);
    transform = Transform_writable(self);

    if (NIL_P(rb_center)) {
        sfTransform_rotate(transform, (float)NUM2DBL(rb_angle));
    } else {
        sfTransform_rotateWithCenter(transform, (float)NUM2DBL(rb_angle), vec2f_from_rb(rb_center));
    }

    return self;
}

#scale(factors) ⇒ Transform #scale(factors, center) ⇒ Transform

Returns a copy scaled by factors, around center if given.

Overloads:

Returns:

  • (Transform) —

    a new transform, scaled by factors around center if given, otherwise around the origin



390
391
392
# File 'ext/graphics/transform.c', line 390

static VALUE Transform_scale(int argc, VALUE* argv, VALUE self) {
    return Transform_scale_bang(argc, argv, Transform_copy(self));
}

#scale!(factors) ⇒ self #scale!(factors, center) ⇒ self

Scales by factors (a Vector2 or 2-element Array) in place, around center if given, otherwise around the origin.

Overloads:

  • #scale!(factors) ⇒ self

    Returns:

    • (self)
  • #scale!(factors, center) ⇒ self

    Returns:

    • (self)

Returns:

  • (self)


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/graphics/transform.c', line 340

static VALUE Transform_scale_bang(int argc, VALUE* argv, VALUE self) {
    VALUE rb_factors, rb_center;
    sfTransform* transform;

    rb_scan_args(argc, argv, "11", &rb_factors, &rb_center);
    transform = Transform_writable(self);

    if (NIL_P(rb_center)) {
        sfTransform_scale(transform, vec2f_from_rb(rb_factors));
    } else {
        sfTransform_scaleWithCenter(transform, vec2f_from_rb(rb_factors), vec2f_from_rb(rb_center));
    }

    return self;
}

#to_a ⇒ Array<Float>

Returns the object as an Array.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9 matrix elements, row-major



140
141
142
# File 'ext/graphics/transform.c', line 140

static VALUE Transform_to_a(VALUE self) {
    return Transform_MatrixToArray(Get_Transform_Struct(self)->matrix);
}

#to_a ⇒ Array<Float>

Returns the object as an Array.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9 matrix elements, row-major



140
141
142
# File 'ext/graphics/transform.c', line 140

static VALUE Transform_to_a(VALUE self) {
    return Transform_MatrixToArray(Get_Transform_Struct(self)->matrix);
}

#to_s ⇒ String

Returns a human-readable representation of the object.

Returns:

  • (String)

Returns:

  • (String)


195
196
197
198
199
200
# File 'ext/graphics/transform.c', line 195

static VALUE Transform_to_s(VALUE self) {
    const float* m = Get_Transform_Struct(self)->matrix;

    return rb_sprintf("#<SFML::Transform [%g, %g, %g] [%g, %g, %g] [%g, %g, %g]>", m[0], m[1], m[2],
                      m[3], m[4], m[5], m[6], m[7], m[8]);
}

#transform_point(point) ⇒ Vector2

Applies the transform to a point and returns the result.

Returns:

Returns:

  • (Vector2) —

    point transformed by this matrix



213
214
215
216
217
218
# File 'ext/graphics/transform.c', line 213

static VALUE Transform_transform_point(VALUE self, VALUE rb_point) {
    sfVector2f point =
        sfTransform_transformPoint(Get_Transform_Struct(self), vec2f_from_rb(rb_point));

    return vec2f_to_rb(point);
}

#transform_rect(rect) ⇒ Rect

Applies the transform to a rectangle and returns the result.

Returns:

Returns:

  • (Rect) —

    the axis-aligned bounding box of rect transformed by this matrix



228
229
230
231
232
# File 'ext/graphics/transform.c', line 228

static VALUE Transform_transform_rect(VALUE self, VALUE rb_rect) {
    sfFloatRect rect = sfTransform_transformRect(Get_Transform_Struct(self), rect_from_rb(rb_rect));

    return rect_to_rb(rect);
}

#translate(offset) ⇒ Transform

Returns a copy translated by the given offsets.

Returns:

Returns:

  • (Transform) —

    a new transform, translated by offset



364
365
366
# File 'ext/graphics/transform.c', line 364

static VALUE Transform_translate(VALUE self, VALUE rb_offset) {
    return Transform_translate_bang(Transform_copy(self), rb_offset);
}

#translate!(offset) ⇒ self

Applies a translation to the transform in place.

Returns:

  • (self)

Returns:

  • (self)


301
302
303
304
# File 'ext/graphics/transform.c', line 301

static VALUE Transform_translate_bang(VALUE self, VALUE rb_offset) {
    sfTransform_translate(Transform_writable(self), vec2f_from_rb(rb_offset));
    return self;
}