Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::Texture Class Reference

A texture for colored images. More...

#include <gf/Texture.h>

Inheritance diagram for gf::Texture:
Inheritance graph
[legend]

Public Member Functions

 Texture ()
 Constructor. More...
 
bool create (Vector2u size)
 Create the texture. More...
 
bool loadFromImage (const Image &image)
 Load the texture from an image. More...
 
bool loadFromFile (const Path &filename)
 Load the texture from a file on disk. More...
 
bool loadFromStream (InputStream &stream)
 Load the texture from a custom stream. More...
 
bool loadFromMemory (const uint8_t *data, std::size_t length)
 Load the texture from a file in memory. More...
 
void update (const Image &image)
 Update the texture from an image. More...
 
Image copyToImage () const
 Copy the texture pixels to an image. More...
 
- Public Member Functions inherited from gf::BareTexture
 BareTexture (Format format)
 Constructor. More...
 
 ~BareTexture ()
 Destructor. More...
 
 BareTexture (const BareTexture &)=delete
 Deleted copy constructor. More...
 
BareTextureoperator= (const BareTexture &)=delete
 Deleted copy assignment. More...
 
 BareTexture (BareTexture &&other)
 Move constructor. More...
 
BareTextureoperator= (BareTexture &&other)
 Move assignment. More...
 
Format getFormat () const
 Get the format of the texture. More...
 
unsigned getName () const
 Get the internal representation of the texture. More...
 
Vector2u getSize () const
 Return the size of the texture. More...
 
void setSmooth (bool smooth=true)
 Enable or disable the smooth filter. More...
 
bool isSmooth () const noexcept
 Check if the smooth filter is enabled or not. More...
 
void setRepeated (bool repeated=true)
 Enable or disable repeating. More...
 
bool isRepeated () const noexcept
 Check if the texture is repeated or not. More...
 
void update (const uint8_t *data)
 Update the whole texture from an array of pixels. More...
 
void update (const uint8_t *data, const RectU &rect)
 Update a part of the texture from an array of pixels. More...
 
RectF computeTextureCoords (const RectU &rect) const
 Compute normalized texture coordinates. More...
 
bool generateMipmap ()
 Generate a mipmap using the current texture data. More...
 

Additional Inherited Members

- Public Types inherited from gf::BareTexture
enum  Format {
  Format::Color,
  Format::Alpha
}
 Format of the texture. More...
 
- Static Public Member Functions inherited from gf::BareTexture
static void bind (const BareTexture *texture)
 Bind a texture for rendering. More...
 
- Protected Member Functions inherited from gf::BareTexture
bool create (Vector2u size, const uint8_t *data)
 Create the texture. More...
 

Detailed Description

A texture for colored images.

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 gf::Image, do whatever you need with the pixels, and then call Texture::loadFromImage.

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

Usage example:

// This example shows the most common use of gf::Texture:
// drawing a sprite
// Load a texture from a file
gf::Texture texture;
if (!texture.loadFromFile("texture.png")) {
return -1;
}
// Assign it to a sprite
gf::Sprite sprite;
sprite.setTexture(texture);
// Draw the textured sprite
window.draw(sprite);
See also
gf::Sprite, gf::Image, gf::RenderTexture

Constructor & Destructor Documentation

gf::Texture::Texture ( )

Constructor.

No texture is created.

Member Function Documentation

Image gf::Texture::copyToImage ( ) const

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
An image containing the texture's pixels
See also
loadFromImage()
bool gf::Texture::create ( Vector2u  size)

Create the texture.

If this function fails, the texture is left unchanged.

Parameters
sizeSize of the texture
Returns
True if creation was successful
bool gf::Texture::loadFromFile ( const Path filename)

Load the texture from a file on disk.

This function is a shortcut for the following code:

gf::Image image;
image.loadFromFile(filename);
texture.loadFromImage(image);

If this function fails, the texture is left unchanged.

Parameters
filenamePath of the image file to load
Returns
True if loading was successful
See also
loadFromMemory(), loadFromStream(), loadFromImage()
bool gf::Texture::loadFromImage ( const Image image)

Load the texture from an image.

If this function fails, the texture is left unchanged.

Parameters
imageImage to load into the texture
Returns
True if loading was successful
See also
loadFromFile(), loadFromMemory(), loadFromStream()
bool gf::Texture::loadFromMemory ( const uint8_t *  data,
std::size_t  length 
)

Load the texture from a file in memory.

This function is a shortcut for the following code:

gf::Image image;
image.loadFromMemory(data, length);
texture.loadFromImage(image);

If this function fails, the texture is left unchanged.

Parameters
dataPointer to the file data in memory
lengthLength of the data to load, in bytes
Returns
True if loading was successful
See also
loadFromFile(), loadFromStream(), loadFromImage()
bool gf::Texture::loadFromStream ( InputStream stream)

Load the texture from a custom stream.

This function is a shortcut for the following code:

gf::Image image;
image.loadFromStream(stream);
texture.loadFromImage(image);

If this function fails, the texture is left unchanged.

Parameters
streamSource stream to read from
Returns
True if loading was successful
See also
loadFromFile(), loadFromMemory(), loadFromImage()
void gf::Texture::update ( const 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.

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.

Parameters
imageImage to copy to the texture