Class: SFML::Sprite
- Inherits:
-
Object
- Object
- SFML::Sprite
- Includes:
- Graphics::Transformable
- Defined in:
- lib/sfml/graphics/sprite.rb
Overview
A textured rectangle. SFML 3 requires a Texture at construction time (no default-constructible Sprite anymore), so we keep the texture reference alive inside the Ruby Sprite to prevent the GC from freeing the GPU resource while it's still being drawn.
tex = SFML::Texture.load("hero.png")
sprite = SFML::Sprite.new(tex, position: [100, 100])
window.draw(sprite)
Constant Summary collapse
- CSFML_PREFIX =
:sfSprite
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
-
#texture ⇒ Object
The Texture this sprite was last bound to.
Instance Method Summary collapse
-
#color ⇒ Object
Returns the color.
-
#color=(c) ⇒ Object
Set the color.
-
#draw_on(target, states_ptr = nil) ⇒ Object
Returns the draw on.
-
#dup ⇒ Object
(also: #clone)
Deep copy — same texture binding (textures are shared GPU objects), independent transform / colour state.
-
#global_bounds ⇒ Object
Bounding box after applying the sprite's transform.
-
#initialize(texture, **opts) ⇒ Sprite
constructor
Build a Sprite.
-
#inverse_transform ⇒ Object
Inverse of
#transform— maps world-space coords back to the sprite's local space. -
#local_bounds ⇒ Object
Bounding box of the sprite in its own (untransformed) coords.
-
#texture_rect ⇒ Object
Sub-region of the texture this sprite shows.
-
#texture_rect=(rect) ⇒ Object
Set the texture rect.
-
#transform ⇒ Object
Combined transform (translation + rotation + scale + origin) the renderer applies when drawing this sprite.
Methods included from Graphics::Transformable
#move, #origin, #origin=, #position, #position=, #rotate, #rotation, #rotation=, #scale, #scale=, #scale_by
Constructor Details
#initialize(texture, **opts) ⇒ Sprite
Build a Sprite. texture is required (SFML 3 dropped the
default constructor). Kwargs cover color / transform.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sfml/graphics/sprite.rb', line 16 def initialize(texture, **opts) raise ArgumentError, "Sprite requires a SFML::Texture" unless texture.is_a?(Texture) ptr = C::Graphics.sfSprite_create(texture.handle) raise GraphicsError, "sfSprite_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfSprite_destroy)) @texture = texture # keep alive for GC self.color = opts[:color] if opts.key?(:color) self.position = opts[:position] if opts.key?(:position) self.origin = opts[:origin] if opts.key?(:origin) self.rotation = opts[:rotation] if opts.key?(:rotation) self.scale = opts[:scale] if opts.key?(:scale) end |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
117 118 119 |
# File 'lib/sfml/graphics/sprite.rb', line 117 def handle @handle end |
#texture ⇒ Object
The Texture this sprite was last bound to. Borrowed — caller is responsible for keeping the source texture alive.
32 33 34 |
# File 'lib/sfml/graphics/sprite.rb', line 32 def texture @texture end |
Instance Method Details
#color ⇒ Object
Returns the color.
42 |
# File 'lib/sfml/graphics/sprite.rb', line 42 def color = Color.from_native(C::Graphics.sfSprite_getColor(@handle)) |
#color=(c) ⇒ Object
Set the color.
45 46 47 |
# File 'lib/sfml/graphics/sprite.rb', line 45 def color=(c) C::Graphics.sfSprite_setColor(@handle, c.to_native) end |
#draw_on(target, states_ptr = nil) ⇒ Object
Returns the draw on.
79 80 81 |
# File 'lib/sfml/graphics/sprite.rb', line 79 def draw_on(target, states_ptr = nil) # :nodoc: target._draw_native(:Sprite, @handle, states_ptr) end |
#dup ⇒ Object Also known as: clone
Deep copy — same texture binding (textures are shared GPU objects), independent transform / colour state.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sfml/graphics/sprite.rb', line 105 def dup ptr = C::Graphics.sfSprite_copy(@handle) raise GraphicsError, "sfSprite_copy returned NULL" if ptr.null? copy = self.class.allocate copy.instance_variable_set(:@handle, FFI::AutoPointer.new(ptr, C::Graphics.method(:sfSprite_destroy))) copy.instance_variable_set(:@texture, @texture) # keep source alive copy end |
#global_bounds ⇒ Object
Bounding box after applying the sprite's transform.
74 75 76 |
# File 'lib/sfml/graphics/sprite.rb', line 74 def global_bounds Rect.from_native(C::Graphics.sfSprite_getGlobalBounds(@handle)) end |
#inverse_transform ⇒ Object
Inverse of #transform — maps world-space coords back to
the sprite's local space.
99 100 101 |
# File 'lib/sfml/graphics/sprite.rb', line 99 def inverse_transform C::Graphics.sfSprite_getInverseTransform(@handle) end |
#local_bounds ⇒ Object
Bounding box of the sprite in its own (untransformed) coords.
69 70 71 |
# File 'lib/sfml/graphics/sprite.rb', line 69 def local_bounds Rect.from_native(C::Graphics.sfSprite_getLocalBounds(@handle)) end |
#texture_rect ⇒ Object
53 54 55 |
# File 'lib/sfml/graphics/sprite.rb', line 53 def texture_rect Rect.from_native(C::Graphics.sfSprite_getTextureRect(@handle)) end |
#texture_rect=(rect) ⇒ Object
Set the texture rect.
58 59 60 61 62 63 64 65 66 |
# File 'lib/sfml/graphics/sprite.rb', line 58 def texture_rect=(rect) raise ArgumentError, "Sprite#texture_rect= requires a SFML::Rect" unless rect.is_a?(Rect) native = C::Graphics::IntRect.new native[:position][:x] = Integer(rect.x) native[:position][:y] = Integer(rect.y) native[:size][:x] = Integer(rect.width) native[:size][:y] = Integer(rect.height) C::Graphics.sfSprite_setTextureRect(@handle, native) end |