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
Returns the value of attribute texture.
Instance Method Summary collapse
- #color ⇒ Object
- #color=(c) ⇒ Object
-
#draw_on(target, states_ptr = nil) ⇒ Object
:nodoc:.
- #global_bounds ⇒ Object
-
#initialize(texture, **opts) ⇒ Sprite
constructor
A new instance of Sprite.
- #local_bounds ⇒ Object
-
#texture_rect ⇒ Object
Sub-region of the texture this sprite shows.
- #texture_rect=(rect) ⇒ Object
Methods included from Graphics::Transformable
#move, #origin, #origin=, #position, #position=, #rotate, #rotation, #rotation=, #scale, #scale=, #scale_by
Constructor Details
#initialize(texture, **opts) ⇒ Sprite
Returns a new instance of Sprite.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sfml/graphics/sprite.rb', line 14 def initialize(texture, **opts) raise ArgumentError, "Sprite requires a SFML::Texture" unless texture.is_a?(Texture) ptr = C::Graphics.sfSprite_create(texture.handle) raise Error, "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:
73 74 75 |
# File 'lib/sfml/graphics/sprite.rb', line 73 def handle @handle end |
#texture ⇒ Object
Returns the value of attribute texture.
29 30 31 |
# File 'lib/sfml/graphics/sprite.rb', line 29 def texture @texture end |
Instance Method Details
#color ⇒ Object
37 |
# File 'lib/sfml/graphics/sprite.rb', line 37 def color = Color.from_native(C::Graphics.sfSprite_getColor(@handle)) |
#color=(c) ⇒ Object
39 40 41 |
# File 'lib/sfml/graphics/sprite.rb', line 39 def color=(c) C::Graphics.sfSprite_setColor(@handle, c.to_native) end |
#draw_on(target, states_ptr = nil) ⇒ Object
:nodoc:
69 70 71 |
# File 'lib/sfml/graphics/sprite.rb', line 69 def draw_on(target, states_ptr = nil) # :nodoc: target._draw_native(:Sprite, @handle, states_ptr) end |
#global_bounds ⇒ Object
65 66 67 |
# File 'lib/sfml/graphics/sprite.rb', line 65 def global_bounds Rect.from_native(C::Graphics.sfSprite_getGlobalBounds(@handle)) end |
#local_bounds ⇒ Object
61 62 63 |
# File 'lib/sfml/graphics/sprite.rb', line 61 def local_bounds Rect.from_native(C::Graphics.sfSprite_getLocalBounds(@handle)) end |
#texture_rect ⇒ Object
47 48 49 |
# File 'lib/sfml/graphics/sprite.rb', line 47 def texture_rect Rect.from_native(C::Graphics.sfSprite_getTextureRect(@handle)) end |
#texture_rect=(rect) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/sfml/graphics/sprite.rb', line 51 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 |