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

Class Method 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 GraphicsError, "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

#handle ⇒ Object (readonly)

:nodoc:



118
119
120
# File 'lib/sfml/graphics/render_texture.rb', line 118

def handle
  @handle
end

Class Method Details

.maximum_anti_aliasing_level ⇒ Object

Maximum MSAA level the driver can apply to a RenderTexture at this moment. Class-level — independent of the instance.



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

def self.maximum_anti_aliasing_level
  C::Graphics.sfRenderTexture_getMaximumAntiAliasingLevel
end

Instance Method Details

#active=(value) ⇒ Object

---- GL interop (mirror of RenderWindow's) ----



79
80
81
# File 'lib/sfml/graphics/render_texture.rb', line 79

def active=(value)
  C::Graphics.sfRenderTexture_setActive(@handle, value ? true : false)
end

#generate_mipmap ⇒ Object

Generate mipmaps for the underlying texture. Useful when the texture will be sampled at a downscaled size — without mipmaps you get aliasing on minification. Returns true if the GPU honoured the request (NPOT or unsupported drivers return false).



67
68
69
# File 'lib/sfml/graphics/render_texture.rb', line 67

def generate_mipmap
  C::Graphics.sfRenderTexture_generateMipmap(@handle)
end

#pop_gl_states ⇒ Object

Returns the pop gl states.



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

def pop_gl_states   = C::Graphics.sfRenderTexture_popGLStates(@handle)
# Returns the reset gl states.

#push_gl_states ⇒ Object

Returns the push gl states.



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

def push_gl_states  = C::Graphics.sfRenderTexture_pushGLStates(@handle)
# Returns the pop gl states.

#repeated=(value) ⇒ Object

Set the repeated.



55
56
57
# File 'lib/sfml/graphics/render_texture.rb', line 55

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

#repeated? ⇒ Boolean

true if repeated.

Returns:

  • (Boolean)


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

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

#reset_gl_states ⇒ Object

Returns the reset gl states.



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

def reset_gl_states = C::Graphics.sfRenderTexture_resetGLStates(@handle)

#scissor(view = self.view) ⇒ Object

Returns the scissor.

Raises:

  • (ArgumentError)


97
98
99
100
# File 'lib/sfml/graphics/render_texture.rb', line 97

def scissor(view = self.view)
  raise ArgumentError, "expected a SFML::View" unless view.is_a?(View)
  Rect.from_native(C::Graphics.sfRenderTexture_getScissor(@handle, view.handle))
end

#size ⇒ Object

Returns the size.



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

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

#smooth=(value) ⇒ Object

Set the smooth.



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

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

#smooth? ⇒ Boolean

true if smooth.

Returns:

  • (Boolean)


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

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

#srgb? ⇒ Boolean

true if the framebuffer is sRGB-capable.

Returns:

  • (Boolean)


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

def srgb? = C::Graphics.sfRenderTexture_isSrgb(@handle)

#texture ⇒ Object

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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sfml/graphics/render_texture.rb', line 105

def texture
  @texture ||= begin
    ptr = C::Graphics.sfRenderTexture_getTexture(@handle)
    raise GraphicsError, "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

#viewport(view = self.view) ⇒ Object

Pixel-space viewport / scissor for the given view (defaults to the active view). Same shape as RenderWindow's.

Raises:

  • (ArgumentError)


91
92
93
94
# File 'lib/sfml/graphics/render_texture.rb', line 91

def viewport(view = self.view)
  raise ArgumentError, "expected a SFML::View" unless view.is_a?(View)
  Rect.from_native(C::Graphics.sfRenderTexture_getViewport(@handle, view.handle))
end