Skip to content

class SF::Texture
inherits Reference #

Image living on the graphics card that can be used for drawing

SF::Texture stores pixels that can be drawn, with a sprite for example. A texture lives in the graphics card memory, therefore it is very fast to draw a texture to a render target, or copy a render target to a texture (the graphics card can access both directly).

Being stored in the graphics card memory has some drawbacks. A texture cannot be manipulated as freely as a SF::Image, you need to prepare the pixels first and then upload them to the texture in a single operation (see Texture.update).

SF::Texture makes it easy to convert from/to SF::Image, but keep in mind that these calls require transfers between the graphics card and the central memory, therefore they are slow operations.

A texture can be loaded from an image, but also directly from a file/memory/stream. The necessary shortcuts are defined so that you don't need an image first for the most common cases. However, if you want to perform some modifications on the pixels before creating the final texture, you can load your file to a SF::Image, do whatever you need with the pixels, and then call Texture.load_from_image.

Since they live in the graphics card memory, the pixels of a texture cannot be accessed without a slow copy first. And they cannot be accessed individually. Therefore, if you need to read the texture's pixels (like for pixel-perfect collisions), it is recommended to store the collision information separately, for example in an array of booleans.

Like SF::Image, SF::Texture can handle a unique internal representation of pixels, which is RGBA 32 bits. This means that a pixel must be composed of 8 bits red, green, blue and alpha channels -- just like a SF::Color.

Usage example:

# This example shows the most common use of SF::Texture:
# drawing a sprite

# Load a texture from a file
texture = SF::Texture.from_file("texture.png")

# Assign it to a sprite
sprite = SF::Sprite.new(texture)

# Draw the textured sprite
window.draw sprite

# This example shows another common use of SF::Texture:
# streaming real-time data, like video frames

# Create an empty texture
texture = SF::Texture.new(640, 480)

# Create a sprite that will display the texture
sprite = SF::Sprite.new(texture)

loop do # the main loop
  ...

  # update the texture
  pixels = (...).to_unsafe # get a fresh chunk of pixels (the next frame of a movie, for example)
  texture.update(pixels)

  # draw it
  window.draw sprite

  ...
end

Like SF::Shader that can be used as a raw OpenGL shader, SF::Texture can also be used directly as a raw texture for custom OpenGL geometry.

SF::Texture.bind(texture)
... render OpenGL geometry ...
SF::Texture.bind(nil)

See also: SF::Sprite, SF::Image, SF::RenderTexture

Included modules

SF::GlResource

Class methods#

.bind(texture : Texture?, coordinate_type : Texture::CoordinateType = Normalized) #

Bind a texture for rendering

This function is not part of the graphics API, it mustn't be used when drawing SFML entities. It must be used only if you mix SF::Texture with OpenGL code.

t1 = SF::Texture.new
t2 = SF::Texture.new
...
SF::Texture.bind t1
# draw OpenGL stuff that use t1...
SF::Texture.bind t2
# draw OpenGL stuff that use t2...
SF::Texture.bind nil
# draw OpenGL stuff that use no texture...

The coordinate_type argument controls how texture coordinates will be interpreted. If Normalized (the default), they must be in range 0.0 .. 1.0, which is the default way of handling texture coordinates with OpenGL. If Pixels, they must be given in pixels (range 0.0 .. size). This mode is used internally by the graphics classes of SFML, it makes the definition of texture coordinates more intuitive for the high-level API, users don't need to compute normalized values.

  • texture - Pointer to the texture to bind, can be null to use no texture
  • coordinate_type - Type of texture coordinates to use
View source

.maximum_size : Int32 #

Get the maximum texture size allowed

This maximum size is defined by the graphics driver. You can expect a value of 512 pixels for low-end graphics card, and up to 8192 pixels or more for newer hardware.

Returns: Maximum size allowed for textures, in pixels

View source

.from_file(*args, **kwargs) : self #

Shorthand for texture = Texture.new; texture.load_from_file(...); texture

Raises InitError on failure

View source

.from_image(*args, **kwargs) : self #

Shorthand for texture = Texture.new; texture.load_from_image(...); texture

Raises InitError on failure

View source

.from_memory(*args, **kwargs) : self #

Shorthand for texture = Texture.new; texture.load_from_memory(...); texture

Raises InitError on failure

View source

.from_stream(*args, **kwargs) : self #

Shorthand for texture = Texture.new; texture.load_from_stream(...); texture

Raises InitError on failure

View source

.new #

Default constructor

Creates an empty texture.

View source

.new(*args, **kwargs) : self #

Shorthand for texture = Texture.new; texture.create(...); texture

Raises InitError on failure

View source

Methods#

#copy_to_image : Image #

Copy the texture pixels to an image

This function performs a slow operation that downloads the texture's pixels from the graphics card and copies them to a new image, potentially applying transformations to pixels if necessary (texture may be padded or flipped).

Returns: Image containing the texture's pixels

See also: load_from_image

View source

#create(width : Int, height : Int) : Bool #

Create the texture

If this function fails, the texture is left unchanged.

  • width - Width of the texture
  • height - Height of the texture

Returns: True if creation was successful

View source

#dup : Texture #

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

View source

#finalize #

Destructor

View source

#generate_mipmap : Bool #

Generate a mipmap using the current texture data

Mipmaps are pre-computed chains of optimized textures. Each level of texture in a mipmap is generated by halving each of the previous level's dimensions. This is done until the final level has the size of 1x1. The textures generated in this process may make use of more advanced filters which might improve the visual quality of textures when they are applied to objects much smaller than they are. This is known as minification. Because fewer texels (texture elements) have to be sampled from when heavily minified, usage of mipmaps can also improve rendering performance in certain scenarios.

Mipmap generation relies on the necessary OpenGL extension being available. If it is unavailable or generation fails due to another reason, this function will return false. Mipmap data is only valid from the time it is generated until the next time the base level image is modified, at which point this function will have to be called again to regenerate it.

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

View source

#load_from_file(filename : String, area : IntRect = IntRect.new) : Bool #

Load the texture from a file on disk

This function is a shortcut for the following code:

image = SF::Image.new
image.load_from_file(filename)
texture.load_from_image(image, area)

The area argument can be used to load only a sub-rectangle of the whole image. If you want the entire image then leave the default value (which is an empty IntRect). If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size.

The maximum size for a texture depends on the graphics driver and can be retrieved with the maximum_size function.

If this function fails, the texture is left unchanged.

  • filename - Path of the image file to load
  • area - Area of the image to load

Returns: True if loading was successful

See also: load_from_memory, load_from_stream, load_from_image

View source

#load_from_image(image : Image, area : IntRect = IntRect.new) : Bool #

Load the texture from an image

The area argument can be used to load only a sub-rectangle of the whole image. If you want the entire image then leave the default value (which is an empty IntRect). If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size.

The maximum size for a texture depends on the graphics driver and can be retrieved with the maximum_size function.

If this function fails, the texture is left unchanged.

  • image - Image to load into the texture
  • area - Area of the image to load

Returns: True if loading was successful

See also: load_from_file, load_from_memory

View source

#load_from_memory(data : Slice, area : IntRect = IntRect.new) : Bool #

Load the texture from a file in memory

This function is a shortcut for the following code:

image = SF::Image.new
image.load_from_memory(data, size)
texture.load_from_image(image, area)

The area argument can be used to load only a sub-rectangle of the whole image. If you want the entire image then leave the default value (which is an empty IntRect). If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size.

The maximum size for a texture depends on the graphics driver and can be retrieved with the maximum_size function.

If this function fails, the texture is left unchanged.

  • data - Slice containing the file data in memory
  • area - Area of the image to load

Returns: True if loading was successful

See also: load_from_file, load_from_stream, load_from_image

View source

#load_from_stream(stream : InputStream, area : IntRect = IntRect.new) : Bool #

Load the texture from a custom stream

This function is a shortcut for the following code:

image = SF::Image.new
image.load_from_stream(stream)
texture.load_from_image(image, area)

The area argument can be used to load only a sub-rectangle of the whole image. If you want the entire image then leave the default value (which is an empty IntRect). If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size.

The maximum size for a texture depends on the graphics driver and can be retrieved with the maximum_size function.

If this function fails, the texture is left unchanged.

  • stream - Source stream to read from
  • area - Area of the image to load

Returns: True if loading was successful

See also: load_from_file, load_from_memory, load_from_image

View source

#native_handle : Int32 #

Get the underlying OpenGL handle of the texture.

You shouldn't need to use this function, unless you have very specific stuff to implement that SFML doesn't support, or implement a temporary workaround until a bug is fixed.

Returns: OpenGL handle of the texture or 0 if not yet created

View source

#repeated=(repeated : Bool) #

Enable or disable repeating

Repeating is involved when using texture coordinates outside the texture rectangle [0, 0, width, height]. In this case, if repeat mode is enabled, the whole texture will be repeated as many times as needed to reach the coordinate (for example, if the X texture coordinate is 3 * width, the texture will be repeated 3 times). If repeat mode is disabled, the "extra space" will instead be filled with border pixels. Warning: on very old graphics cards, white pixels may appear when the texture is repeated. With such cards, repeat mode can be used reliably only if the texture has power-of-two dimensions (such as 256x128). Repeating is disabled by default.

  • repeated - True to repeat the texture, false to disable repeating

See also: repeated?

View source

#repeated? : Bool #

Tell whether the texture is repeated or not

Returns: True if repeat mode is enabled, false if it is disabled

See also: repeated=

View source

#size : Vector2u #

Return the size of the texture

Returns: Size in pixels

View source

#smooth=(smooth : Bool) #

Enable or disable the smooth filter

When the filter is activated, the texture appears smoother so that pixels are less noticeable. However if you want the texture to look exactly the same as its source file, you should leave it disabled. The smooth filter is disabled by default.

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

See also: smooth?

View source

#smooth? : Bool #

Tell whether the smooth filter is enabled or not

Returns: True if smoothing is enabled, false if it is disabled

See also: smooth=

View source

#srgb=(s_rgb : Bool) #

Enable or disable conversion from sRGB

When providing texture data from an image file or memory, it can either be stored in a linear color space or an sRGB color space. Most digital images account for gamma correction already, so they would need to be "uncorrected" back to linear color space before being processed by the hardware. The hardware can automatically convert it from the sRGB color space to a linear color space when it gets sampled. When the rendered image gets output to the final framebuffer, it gets converted back to sRGB.

After enabling or disabling sRGB conversion, make sure to reload the texture data in order for the setting to take effect.

This option is only useful in conjunction with an sRGB capable framebuffer. This can be requested during window creation.

  • s_rgb - True to enable sRGB conversion, false to disable it

See also: srgb?

View source

#srgb? : Bool #

Tell whether the texture source is converted from sRGB or not

Returns: True if the texture source is converted from sRGB, false if not

See also: srgb=

View source

#swap(right : Texture) #

Swap the contents of this texture with those of another

  • right - Instance to swap with
View source

#update(window : Window) #

Update the texture from the contents of a window

Although the source window can be smaller than the texture, this function is usually used for updating the whole texture. The other overload, which has (x, y) additional arguments, is more convenient for updating a sub-area of the texture.

No additional check is performed on the size of the window, passing a window bigger than the texture will lead to an undefined behavior.

This function does nothing if either the texture or the window was not previously created.

  • window - Window to copy to the texture
View source

#update(image : Image) #

Update the texture from an image

Although the source image can be smaller than the texture, this function is usually used for updating the whole texture. The other overload, which has (x, y) additional arguments, is more convenient for updating a sub-area of the texture.

No additional check is performed on the size of the image, passing an image bigger than the texture will lead to an undefined behavior.

This function does nothing if the texture was not previously created.

  • image - Image to copy to the texture
View source

#update(pixels : Pointer(UInt8)) #

Update the whole texture from an array of pixels

The pixel array is assumed to have the same size as the area rectangle, and to contain 32-bits RGBA pixels.

No additional check is performed on the size of the pixel array, passing invalid arguments will lead to an undefined behavior.

This function does nothing if pixels is null or if the texture was not previously created.

  • pixels - Array of pixels to copy to the texture
View source

#update(window : Window, x : Int, y : Int) #

Update a part of the texture from the contents of a window

No additional check is performed on the size of the window, passing an invalid combination of window size and offset will lead to an undefined behavior.

This function does nothing if either the texture or the window was not previously created.

  • window - Window to copy to the texture
  • x - X offset in the texture where to copy the source window
  • y - Y offset in the texture where to copy the source window
View source

#update(image : Image, x : Int, y : Int) #

Update a part of the texture from an image

No additional check is performed on the size of the image, passing an invalid combination of image size and offset will lead to an undefined behavior.

This function does nothing if the texture was not previously created.

  • image - Image to copy to the texture
  • x - X offset in the texture where to copy the source image
  • y - Y offset in the texture where to copy the source image
View source

#update(texture : Texture, x : Int, y : Int) #

Update a part of this texture from another texture

No additional check is performed on the size of the texture, passing an invalid combination of texture size and offset will lead to an undefined behavior.

This function does nothing if either texture was not previously created.

  • texture - Source texture to copy to this texture
  • x - X offset in this texture where to copy the source texture
  • y - Y offset in this texture where to copy the source texture
View source

#update(pixels : Pointer(UInt8), width : Int, height : Int, x : Int, y : Int) #

Update a part of the texture from an array of pixels

The size of the pixel array must match the width and height arguments, and it must contain 32-bits RGBA pixels.

No additional check is performed on the size of the pixel array or the bounds of the area to update, passing invalid arguments will lead to an undefined behavior.

This function does nothing if pixels is null or if the texture was not previously created.

  • pixels - Array of pixels to copy to the texture
  • width - Width of the pixel region contained in pixels
  • height - Height of the pixel region contained in pixels
  • x - X offset in the texture where to copy the source pixels
  • y - Y offset in the texture where to copy the source pixels
View source

#update(texture : Texture) #

Update a part of this texture from another texture

Although the source texture can be smaller than this texture, this function is usually used for updating the whole texture. The other overload, which has (x, y) additional arguments, is more convenient for updating a sub-area of this texture.

No additional check is performed on the size of the passed texture, passing a texture bigger than this texture will lead to an undefined behavior.

This function does nothing if either texture was not previously created.

  • texture - Source texture to copy to this texture
View source