sfml3.rb

Class: SFML::Sprite

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

Overview

A drawable representation of a Texture (or a sub-rectangle of one), positioned, rotated, scaled and tinted like any other Transformable object.

Includes Drawable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(texture) ⇒ Sprite

CSFML requires a texture at creation time -- its C API unconditionally dereferences a NULL texture argument rather than tolerating one, so unlike most other constructors here, the texture is not optional.

Raises:

  • (ArgumentError) —

    if texture is not a Texture



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/graphics/sprite.c', line 93

static VALUE Sprite_initialize(VALUE self, VALUE rb_texture) {
    Sprite* ptr;

    if (!rb_obj_is_kind_of(rb_texture, Get_Klass_Texture())) {
        raise_invalid_argument_class(Get_Klass_Texture());
    }

    TypedData_Get_Struct(self, Sprite, &Sprite_data_type, ptr);

    ptr->sprite = sfSprite_create(Get_Texture_Struct(rb_texture));
    ptr->rb_texture = rb_texture;

    if (ptr->sprite == NULL) {
        rb_raise(rb_eRuntimeError, "failed to create sprite");
    }

    return self;
}

Instance Attribute Details

#color ⇒ Color

Returns the object's color.

Returns:

Returns:

  • (Color) —

    the tint color multiplied with the texture's pixels



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_color(VALUE self) {
    return color_to_rb(sfSprite_getColor(Get_Sprite_Struct(self)));
}

#origin ⇒ Vector2

Returns the object's origin.

Returns:

Returns:



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_origin(VALUE self) {
    return vec2f_to_rb(sfSprite_getOrigin(Get_Sprite_Struct(self)));
}

#position ⇒ Vector2

Returns the object's position.

Returns:

Returns:



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_position(VALUE self) {
    return vec2f_to_rb(sfSprite_getPosition(Get_Sprite_Struct(self)));
}

#rotation ⇒ Float

Returns the object's rotation, in degrees.

Returns:

  • (Float)

Returns:

  • (Float) —

    degrees



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_rotation(VALUE self) {
    return DBL2NUM(sfSprite_getRotation(Get_Sprite_Struct(self)));
}

#scale ⇒ Vector2

Returns the object's scale factors.

Returns:

Returns:



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_scale(VALUE self) {
    return vec2f_to_rb(sfSprite_getScale(Get_Sprite_Struct(self)));
}

#texture ⇒ Texture?

Returns the object's texture, or nil if it has none.

Returns:

Returns:



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_texture(VALUE self) {
    return Get_Sprite(self)->rb_texture;
}

#texture_rect ⇒ Rect

Returns the sub-rectangle of the texture displayed on the object.

Returns:

Returns:

  • (Rect) —

    the sub-rectangle of the texture that gets displayed



394
395
396
# File 'ext/graphics/sprite.c', line 394

static VALUE Sprite_get_texture_rect(VALUE self) {
    return int_rect_to_rb(sfSprite_getTextureRect(Get_Sprite_Struct(self)));
}

Instance Method Details

#copy ⇒ Sprite

Returns a deep copy of the object.

Returns:

Returns:

  • (Sprite) —

    an independent copy



118
119
120
121
122
# File 'ext/graphics/sprite.c', line 118

static VALUE Sprite_copy(VALUE self) {
    Sprite* ptr = Get_Sprite(self);

    return Sprite_wrap(Get_Klass_Sprite(), sfSprite_copy(ptr->sprite));
}

#draw(target, state) ⇒ nil

Part of the Drawable interface; call Target#draw instead of this directly.

Returns:

  • (nil)

Returns:

  • (nil)

Raises:

  • (TypeError) —

    if target is not a Target or state is not a RenderState



381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/graphics/sprite.c', line 381

static VALUE Sprite_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
    TargetView view = Get_RenderTarget_View(rb_target);

    if (!rb_obj_is_kind_of(rb_state, Get_Klass_RenderState())) {
        raise_invalid_argument_class(Get_Klass_RenderState());
    }

    TARGET_DRAW(view, sfRenderWindow_drawSprite, sfRenderTexture_drawSprite,
                Get_Sprite_Struct(self), Get_RenderState_Struct(rb_state));

    return Qnil;
}

#global_bounds ⇒ Rect

Returns the bounding box after the transform is applied.

Returns:

Returns:

  • (Rect) —

    the bounding box in the parent's coordinate system, after the current transform is applied



367
368
369
# File 'ext/graphics/sprite.c', line 367

static VALUE Sprite_get_global_bounds(VALUE self) {
    return rect_to_rb(sfSprite_getGlobalBounds(Get_Sprite_Struct(self)));
}

#inverse_transform ⇒ Array<Float>

Returns the 3x3 row-major inverse of the object's transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the inverse of #transform



345
346
347
# File 'ext/graphics/sprite.c', line 345

static VALUE Sprite_get_inverse_transform(VALUE self) {
    return Transform_MatrixToArray(sfSprite_getInverseTransform(Get_Sprite_Struct(self)).matrix);
}

#local_bounds ⇒ Rect

Returns the bounding box in local (untransformed) coordinates.

Returns:

Returns:

  • (Rect) —

    the bounding box in local coordinates, before any transform is applied



356
357
358
# File 'ext/graphics/sprite.c', line 356

static VALUE Sprite_get_local_bounds(VALUE self) {
    return rect_to_rb(sfSprite_getLocalBounds(Get_Sprite_Struct(self)));
}

#transform ⇒ Array<Float>

Returns the object's 3x3 row-major transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element matrix (also available as #matrix)



335
336
337
# File 'ext/graphics/sprite.c', line 335

static VALUE Sprite_get_transform(VALUE self) {
    return Transform_MatrixToArray(sfSprite_getTransform(Get_Sprite_Struct(self)).matrix);
}

#move(offset) ⇒ self

Moves the object by offset.

Returns:

  • (self)

Returns:

  • (self)


300
301
302
303
# File 'ext/graphics/sprite.c', line 300

static VALUE Sprite_move(VALUE self, VALUE rb_offset) {
    sfSprite_move(Get_Sprite_Struct(self), vec2f_from_rb(rb_offset));
    return self;
}

#rotate(angle) ⇒ self

Rotates the object by angle degrees.

Returns:

  • (self)

Returns:

  • (self)


312
313
314
315
# File 'ext/graphics/sprite.c', line 312

static VALUE Sprite_rotate(VALUE self, VALUE rb_angle) {
    sfSprite_rotate(Get_Sprite_Struct(self), NUM2DBL(rb_angle));
    return self;
}

#scale!(factors) ⇒ self

Scales the object by factors relative to its current scale.

Returns:

  • (self)

Returns:

  • (self)


324
325
326
327
# File 'ext/graphics/sprite.c', line 324

static VALUE Sprite_scale(VALUE self, VALUE rb_factors) {
    sfSprite_scale(Get_Sprite_Struct(self), vec2f_from_rb(rb_factors));
    return self;
}

#transform ⇒ Array<Float>

Returns the object's 3x3 row-major transform matrix.

Returns:

  • (Array<Float>)

Returns:

  • (Array<Float>) —

    the 9-element matrix (also available as #matrix)



335
336
337
# File 'ext/graphics/sprite.c', line 335

static VALUE Sprite_get_transform(VALUE self) {
    return Transform_MatrixToArray(sfSprite_getTransform(Get_Sprite_Struct(self)).matrix);
}