Gamedev Framework (gf)  0.17.0
A C++14 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...
 
 Texture (Vector2i size)
 Create the texture. More...
 
 Texture (const Image &image)
 Load the texture from an image. More...
 
 Texture (const Path &filename)
 Load the texture from a file on disk. More...
 
 Texture (InputStream &stream)
 Load the texture from a custom stream. More...
 
 Texture (ArrayRef< uint8_t > content)
 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 (Format format, Vector2i size, const uint8_t *data)
 Create the texture. More...
 
Format getFormat () const
 Get the format of the texture. More...
 
unsigned getName () const
 Get the internal representation of the texture. More...
 
Vector2i 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 RectI &rect)
 Update a part of the texture from an array of pixels. More...
 
void resize (Vector2i size, const uint8_t *data=nullptr)
 Resize a texture. More...
 
RectF computeTextureCoords (const RectI &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...
 

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 the appropriate constructor.

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("texture.png");
// Assign it to a sprite
gf::Sprite sprite;
sprite.setTexture(texture);
// Draw the textured sprite
renderer.draw(sprite);
See also
gf::Sprite, gf::Image, gf::RenderTexture

Constructor & Destructor Documentation

◆ Texture() [1/6]

gf::Texture::Texture ( )

Constructor.

No texture is created.

◆ Texture() [2/6]

gf::Texture::Texture ( Vector2i  size)

Create the texture.

Parameters
sizeSize of the texture

◆ Texture() [3/6]

gf::Texture::Texture ( const Image image)

Load the texture from an image.

Parameters
imageImage to load into the texture

◆ Texture() [4/6]

gf::Texture::Texture ( const Path filename)

Load the texture from a file on disk.

Parameters
filenamePath of the image file to load

◆ Texture() [5/6]

gf::Texture::Texture ( InputStream stream)

Load the texture from a custom stream.

Parameters
streamSource stream to read from

◆ Texture() [6/6]

gf::Texture::Texture ( ArrayRef< uint8_t >  content)

Load the texture from a file in memory.

Parameters
contentContent of the file data in memory

Member Function Documentation

◆ copyToImage()

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

◆ update()

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