Class: SFML::Transform
- Inherits:
-
Object
- Object
- SFML::Transform
- 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
-
#to_a ⇒ Array<Float>
readonly
Returns the object as an Array.
Class Method Summary collapse
-
.combine(array_a, array_b) ⇒ Array<Float>
deprecated
Deprecated.
Use #combine on a Transform instead.
-
.from_a(array_of_9) ⇒ Transform
Creates a transform from a nine-element array.
-
.identity ⇒ Transform
Returns the identity transform.
-
.inverse(array) ⇒ Array<Float>
deprecated
Deprecated.
Use #inverse on a Transform instead.
Instance Method Summary collapse
-
#*(rb_other) ⇒ Transform
Non-mutating combination: returns a new Transform equal to
selfcombined withother(a Transform or 9-element Array), leaving both operands untouched. -
#==(other) ⇒ Boolean
othermay be a Transform or a 9-element Array. -
#combine(rb_other) ⇒ Transform
Non-mutating combination: returns a new Transform equal to
selfcombined withother(a Transform or 9-element Array), leaving both operands untouched. -
#combine!(other) ⇒ self
Combines this transform with
other(a Transform or 9-element Array) in place, equivalent to matrix multiplication (+self+ *other). -
#copy ⇒ Transform
Returns a deep copy of the object.
-
#gl_matrix ⇒ Array<Float>
Returns the transform as a 4x4 OpenGL matrix.
-
#initialize(*args) ⇒ Transform
constructor
A Transform wraps a 3x3 matrix (9 floats, row-major).
-
#to_s ⇒ String
Returns a human-readable representation of the object.
-
#inverse ⇒ Transform
Returns the inverse of the transform.
-
#rotate(*args) ⇒ Transform
Returns a copy rotated by
angledegrees, aroundcenterif given. -
#rotate!(*args) ⇒ self
Rotates by
angledegrees in place, aroundcenterif given, otherwise around the origin. -
#scale(*args) ⇒ Transform
Returns a copy scaled by
factors, aroundcenterif given. -
#scale!(*args) ⇒ self
Scales by
factors(a Vector2 or 2-element Array) in place, aroundcenterif given, otherwise around the origin. -
#to_a ⇒ Array<Float>
Returns the object as an Array.
-
#to_a ⇒ Array<Float>
Returns the object as an Array.
-
#to_s ⇒ String
Returns a human-readable representation of the object.
-
#transform_point(point) ⇒ Vector2
Applies the transform to a point and returns the result.
-
#transform_rect(rect) ⇒ Rect
Applies the transform to a rectangle and returns the result.
-
#translate(offset) ⇒ Transform
Returns a copy translated by the given offsets.
-
#translate!(offset) ⇒ self
Applies a translation to the transform in place.
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.
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.
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>
Use #combine on a Transform instead.
Returns the transform that combines array_a and 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.
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.
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>
Use #inverse on a Transform instead.
Returns the inverse of the given transform.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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;
}
|