View on GitHub

DSFML

dsfml.graphics.texture



class 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 Texture::update).

Texture makes it easy to convert from/to 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 Image, do whatever you need with the pixels, and then call Texture::loadFromImage.

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.

Authors:
Laurent Gomila, Jeremy DeHaan

See Also:


http:
//www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#details

bool loadFromFile(string filename, IntRect area = IntRect());
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 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 getMaximumSize function.

If this function fails, the texture is left unchanged.

Params:
string filename Path of the image file to load
IntRect area Area of the image to load

Returns:
True if loading was successful, false otherwise.

bool loadFromMemory(const(void)[] data, IntRect area = IntRect());
Load the texture from a file in memory.

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 getMaximumSize function.

If this function fails, the texture is left unchanged.

Params:
const(void)[] data Image in memory
size Size of the data to load, in bytes.
IntRect area Area of the image to load

Returns:
True if loading was successful, false otherwise.

bool loadFromStream(InputStream stream, IntRect area = IntRect());
Load the texture from a custom stream.

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 getMaximumSize function.

If this function fails, the texture is left unchanged.

Params:
InputStream stream Source stream to read from
IntRect area Area of the image to load

Returns:
True if loading was successful, false otherwise.

bool loadFromImage(Image image, IntRect area = IntRect());
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 getMaximumSize function.

If this function fails, the texture is left unchanged.

Params:
Image image Image to load into the texture
IntRect area Area of the image to load

Returns:
True if loading was successful, false otherwise.

static uint getMaximumSize();
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.

const Vector2u getSize();
Return the size of the texture.

Returns:
Size in pixels.

void setSmooth(bool smooth);
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.

Params:
bool smooth True to enable smoothing, false to disable it.

void setRepeated(bool repeated);
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.

Params:
bool repeated True to repeat the texture, false to disable repeating

static void bind(Texture texture);
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 Texture with OpenGL code.

Params:
Texture texture The texture to bind. Can be null to use no texture.

bool create(uint width, uint height);
Create the texture.

If this function fails, the texture is left unchanged.

Params:
uint width Width of the texture
uint height Height of the texture

Returns:
True if creation was successful, false otherwise.

const Image copyToImage();
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.

const @property Texture dup();
Creates a new texture from the same data (this means copying the entire set of pixels).

Returns:
New texture data.

const bool isRepeated();
Tell whether the texture is repeated or not.

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

const bool isSmooth();
Tell whether the smooth filter is enabled or not.

Returns:
True if something is enabled, false if it is disabled.

void updateFromImage(Image image, uint x, uint y);
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 behaviour.

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

Params:
Image image Image to copy to the texture.

void updateFromPixels(const(ubyte)[] pixels, uint width, uint height, uint x, uint y);
Update 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 behaviour.

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

Params:
const(ubyte)[] pixels Array of pixels to copy to the texture.
uint width Width of the pixel region contained in pixels
uint height Height of the pixel region contained in pixels
uint x X offset in the texture where to copy the source pixels
uint y Y offset in the texture where to copy the source pixels

void updateFromWindow(Window window, uint x, uint y);
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 behaviour.

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

Params:
Window window Window to copy to the texture
uint x X offset in the texture where to copy the source window
uint y Y offset in the texture where to copy the source window

void updateFromWindow(RenderWindow window, uint x, uint y);
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 behaviour.

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

Params:
RenderWindow window Window to copy to the texture
uint x X offset in the texture where to copy the source window
uint y Y offset in the texture where to copy the source window