Class: SFML::RenderTexture

Inherits:
Object
  • Object
show all
Includes:
Graphics::RenderTarget
Defined in:
lib/sfml/graphics/render_texture.rb

Overview

Off-screen rendering target. Anything you can draw on a RenderWindow — sprites, shapes, text, vertex arrays — you can also draw on a RenderTexture and then use its #texture as a Sprite source. Typical uses: minimaps, post-processing, motion-blur trails, custom UIs that composite multiple layers.

rt = SFML::RenderTexture.new(400, 300)
rt.clear(SFML::Color.cornflower_blue)
rt.draw(sprite)
rt.draw(text)
rt.display

sprite = SFML::Sprite.new(rt.texture)
window.draw(sprite)

Note: rt.texture returns a borrowed reference owned by the RenderTexture. Keep the RenderTexture alive for as long as anything uses its texture.

Constant Summary collapse

CSFML_PREFIX =
:sfRenderTexture

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graphics::RenderTarget

#clear, #default_view, #display, #draw, #draw_primitives, #map_coords_to_pixel, #map_pixel_to_coords, #view, #view=

Constructor Details

#initialize(width, height, smooth: false, repeated: false) ⇒ RenderTexture

Returns a new instance of RenderTexture.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sfml/graphics/render_texture.rb', line 24

def initialize(width, height, smooth: false, repeated: false)
  size = C::System::Vector2u.new
  size[:x] = Integer(width)
  size[:y] = Integer(height)

  ptr = C::Graphics.sfRenderTexture_create(size, nil)
  raise Error, "sfRenderTexture_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfRenderTexture_destroy))

  self.smooth   = smooth
  self.repeated = repeated
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



70
71
72
# File 'lib/sfml/graphics/render_texture.rb', line 70

def handle
  @handle
end

Instance Method Details

#repeated=(value) ⇒ Object



50
51
52
# File 'lib/sfml/graphics/render_texture.rb', line 50

def repeated=(value)
  C::Graphics.sfRenderTexture_setRepeated(@handle, !!value)
end

#repeated?Boolean

Returns:

  • (Boolean)


48
# File 'lib/sfml/graphics/render_texture.rb', line 48

def repeated? = C::Graphics.sfRenderTexture_isRepeated(@handle)

#sizeObject



37
38
39
40
# File 'lib/sfml/graphics/render_texture.rb', line 37

def size
  v = C::Graphics.sfRenderTexture_getSize(@handle)
  Vector2.new(v[:x], v[:y])
end

#smooth=(value) ⇒ Object



44
45
46
# File 'lib/sfml/graphics/render_texture.rb', line 44

def smooth=(value)
  C::Graphics.sfRenderTexture_setSmooth(@handle, !!value)
end

#smooth?Boolean

Returns:

  • (Boolean)


42
# File 'lib/sfml/graphics/render_texture.rb', line 42

def smooth?  = C::Graphics.sfRenderTexture_isSmooth(@handle)

#textureObject

The Texture this RenderTexture is rendering into. Borrowed — its lifetime is bounded by ‘self`. Memoised so repeated calls return the same Ruby wrapper.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sfml/graphics/render_texture.rb', line 57

def texture
  @texture ||= begin
    ptr = C::Graphics.sfRenderTexture_getTexture(@handle)
    raise Error, "sfRenderTexture_getTexture returned NULL" if ptr.null?
    # Borrowed — RenderTexture owns the underlying sf::Texture, so
    # we wrap with a raw pointer (no AutoPointer / no destructor).
    # Sprite.new(@texture) will still get a valid handle through it.
    tex = Texture.allocate
    tex.instance_variable_set(:@handle, ptr)
    tex
  end
end