Texture

sfml.graphics.Texture
See theTexture companion object
trait Texture

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

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 Image, you need to prepare the pixels first and then upload them to the texture in a single operation (see update).

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 Image, 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 Color.

val window: RenderWindow = ???
val sprite: Sprite = ???

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

// Load a texture from a file
val texture = Texture()
if !(texture.loadFromFile("texture.png")) then
   // error...
   ???

// Assign it to a sprite sf::Sprite sprite;
sprite.texture = texture

// Draw the textured sprite
window.draw(sprite)

Attributes

See also
Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

Copy the texture pixels to an 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).

Attributes

Returns

Image containing the texture's pixels

def create(width: Int, height: Int): Boolean

Create the texture.

Create the texture.

If this function fails, the texture is left unchanged.

Value parameters

height

Height of the texture

width

Width of the texture

Attributes

Returns

True if creation was successful

def loadFromFile(filename: String, area: Rect[Int]): Boolean

Load the texture from a file on disk.

Load the texture from a file on disk.

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 Rect). If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size.

If this function fails, the texture is left unchanged.

Value parameters

area

Area of the image to load

filename

Path of the image file to load

Attributes

Returns

True if loading was successful

def repeated: Boolean

Tell whether the texture is repeated or not.

Tell whether the texture is repeated or not.

Attributes

Returns

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

See also
def repeated_=(repeated: Boolean): Unit

Enable or disable repeating.

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.

Value parameters

repeated

True to repeat the texture, false to disable repeating

Attributes

See also
def smooth: Boolean

Tell whether the smooth filter is enabled or not.

Tell whether the smooth filter is enabled or not.

Attributes

Returns

True if smoothing is enabled, false if it is disabled

See also
def smooth_=(smooth: Boolean): Unit

Enable or disable the smooth filter.

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.

Value parameters

smooth

True to enable smoothing, false to disable it

Attributes

See also
def update(window: Immutable[Window]): Unit

Update the texture from the contents of a 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.

Value parameters

window

Window to copy to the texture

Attributes

def update(window: Immutable[Window], x: Int, y: Int): Unit

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

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.

Value parameters

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

Attributes