Class: SFML::Sprite

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

#handleObject (readonly)

:nodoc:



73
74
75
# File 'lib/sfml/graphics/sprite.rb', line 73

def handle
  @handle
end

#textureObject

Returns the value of attribute texture.



29
30
31
# File 'lib/sfml/graphics/sprite.rb', line 29

def texture
  @texture
end

Instance Method Details

#colorObject



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_boundsObject



65
66
67
# File 'lib/sfml/graphics/sprite.rb', line 65

def global_bounds
  Rect.from_native(C::Graphics.sfSprite_getGlobalBounds(@handle))
end

#local_boundsObject



61
62
63
# File 'lib/sfml/graphics/sprite.rb', line 61

def local_bounds
  Rect.from_native(C::Graphics.sfSprite_getLocalBounds(@handle))
end

#texture_rectObject

Sub-region of the texture this sprite shows. Returns a SFML::Rect of integer pixel coordinates. Use it for sprite-sheet animation:

sprite.texture_rect = SFML::Rect.new([frame * 32, 0], [32, 32])


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

Raises:

  • (ArgumentError)


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