class SF::RenderTexture

Overview

Target for off-screen 2D rendering into a texture

SF::RenderTexture is the little brother of SF::RenderWindow. It implements the same 2D drawing and OpenGL-related functions (see their base class SF::RenderTarget for more details), the difference is that the result is stored in an off-screen texture rather than being show in a window.

Rendering to a texture can be useful in a variety of situations:

Usage example:

# Create a new render-window
window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "SFML window")

# Create a new render-texture
texture = SF::RenderTexture.new(500, 500)

# The main loop
while window.open?
  # Event processing
  # ...

  # Clear the whole texture with red color
  texture.clear(SF::Color::Red)

  # Draw stuff to the texture
  texture.draw(sprite) # sprite is a SF::Sprite
  texture.draw(shape)  # shape is a SF::Shape
  texture.draw(text)   # text is a SF::Text

  # We're done drawing to the texture
  texture.display

  # Now we start rendering to the window, clear it first
  window.clear

  # Draw the texture
  sprite = SF::Sprite.new(texture.texture)
  window.draw(sprite)

  # End the current frame and display its contents on screen
  window.display
end

Like SF::RenderWindow, SF::RenderTexture is still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands. If you need a depth buffer for 3D rendering, don't forget to request it when calling RenderTexture.create.

See also: SF::RenderTarget, SF::RenderWindow, SF::View, SF::Texture

Included Modules

Defined in:

graphics/graphics.cr
graphics/obj.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module SF::RenderTarget

active=(active : Bool = true) : Bool active=, clear(color : Color = Color.new(0, 0, 0, 255)) clear, default_view : View default_view, draw(vertices : Array(Vertex) | Slice(Vertex), type : PrimitiveType, states : RenderStates = RenderStates::Default)
draw(vertex_buffer : VertexBuffer, first_vertex : Int, vertex_count : Int, states : RenderStates = RenderStates::Default)
draw(vertex_buffer : VertexBuffer, states : RenderStates = RenderStates::Default)
draw(drawable : Drawable, states : RenderStates = RenderStates::Default)
draw
, get_viewport(view : View) : IntRect get_viewport, map_coords_to_pixel(point : Vector2 | Tuple, view : View) : Vector2i
map_coords_to_pixel(point : Vector2 | Tuple) : Vector2i
map_coords_to_pixel
, map_pixel_to_coords(point : Vector2 | Tuple, view : View) : Vector2f
map_pixel_to_coords(point : Vector2 | Tuple) : Vector2f
map_pixel_to_coords
, pop_gl_states pop_gl_states, push_gl_states push_gl_states, reset_gl_states reset_gl_states, size : Vector2u size, view : View view, view=(view : View) view=

Constructor Detail

def self.new #

Default constructor

Constructs an empty, invalid render-texture. You must call create to have a valid render-texture.

See also: #create


[View source]
def self.new(*args, **kwargs) : self #

Shorthand for render_texture = RenderTexture.new; render_texture.create(...); render_texture

Raises InitError on failure


[View source]

Class Method Detail

def self.maximum_antialiasing_level : Int32 #

Get the maximum anti-aliasing level supported by the system

Returns: The maximum anti-aliasing level supported by the system


[View source]

Instance Method Detail

def active=(active : Bool = true) : Bool #

Activate or deactivate the render-texture for rendering

This function makes the render-texture's context current for future OpenGL rendering operations (so you shouldn't care about it if you're not doing direct OpenGL stuff). Only one context can be current in a thread, so if you want to draw OpenGL geometry to another render target (like a RenderWindow) don't forget to activate it again.

  • active - True to activate, false to deactivate

Returns: True if operation was successful, false otherwise


[View source]
def create(width : Int, height : Int, depth_buffer : Bool) : Bool #

Create the render-texture

Before calling this function, the render-texture is in an invalid state, thus it is mandatory to call it before doing anything with the render-texture. The last parameter, depth_buffer, is useful if you want to use the render-texture for 3D OpenGL rendering that requires a depth buffer. Otherwise it is unnecessary, and you should leave this parameter to false (which is its default value).

  • width - Width of the render-texture
  • height - Height of the render-texture
  • depth_buffer - Do you want this render-texture to have a depth buffer?

Returns: True if creation has been successful

DEPRECATED Use #create(width, height, settings) instead.


[View source]
def create(width : Int, height : Int, settings : ContextSettings = ContextSettings.new()) : Bool #

Create the render-texture

Before calling this function, the render-texture is in an invalid state, thus it is mandatory to call it before doing anything with the render-texture. The last parameter, settings, is useful if you want to enable multi-sampling or use the render-texture for OpenGL rendering that requires a depth or stencil buffer. Otherwise it is unnecessary, and you should leave this parameter at its default value.

  • width - Width of the render-texture
  • height - Height of the render-texture
  • settings - Additional settings for the underlying OpenGL texture and context

Returns: True if creation has been successful


[View source]
def display #

Update the contents of the target texture

This function updates the target texture with what has been drawn so far. Like for windows, calling this function is mandatory at the end of rendering. Not calling it may leave the texture in an undefined state.


[View source]
def finalize #

Destructor


[View source]
def generate_mipmap : Bool #

Generate a mipmap using the current texture data

This function is similar to Texture.generate_mipmap and operates on the texture used as the target for drawing. Be aware that any draw operation may modify the base level image data. For this reason, calling this function only makes sense after all drawing is completed and display has been called. Not calling display after subsequent drawing will lead to undefined behavior if a mipmap had been previously generated.

Returns: True if mipmap generation was successful, false if unsuccessful


[View source]
def repeated=(repeated : Bool) #

Enable or disable texture repeating

This function is similar to Texture.repeated=. This parameter is disabled by default.

  • repeated - True to enable repeating, false to disable it

See also: #repeated?


[View source]
def repeated? : Bool #

Tell whether the texture is repeated or not

Returns: True if texture is repeated

See also: #repeated=


[View source]
def size : Vector2u #

Return the size of the rendering region of the texture

The returned value is the size that you passed to the create function.

Returns: Size in pixels


[View source]
def smooth=(smooth : Bool) #

Enable or disable texture smoothing

This function is similar to Texture.smooth=. This parameter is disabled by default.

  • smooth - True to enable smoothing, false to disable it

See also: #smooth?


[View source]
def smooth? : Bool #

Tell whether the smooth filtering is enabled or not

Returns: True if texture smoothing is enabled

See also: #smooth=


[View source]
def texture : Texture #

Get a read-only reference to the target texture

After drawing to the render-texture and calling Display, you can retrieve the updated texture using this function, and draw it using a sprite (for example). The internal SF::Texture of a render-texture is always the same instance, so that it is possible to call this function once and keep a reference to the texture even after it is modified.

Returns: Const reference to the texture


[View source]