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

An image that lives in the graphic memory that can be used for drawing. More...

#include <gf/Texture.h>

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

Public Types

enum  Format {
  Format::Color,
  Format::Alpha
}
 Format of the texture. More...
 

Public Member Functions

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

Static Public Member Functions

static void bind (const BareTexture *texture)
 Bind a texture for rendering. More...
 

Protected Member Functions

bool create (Vector2u size, const uint8_t *data)
 Create the texture. More...
 

Detailed Description

An image that lives in the graphic memory that can be used for drawing.

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

gf::BareTexture can handle two types of texture:

Generally, you do not manipulated a gf::BareTexture directly but you can use a gf::Texture.

See also
gf::Texture, gf::AlphaTexture

Constructor & Destructor Documentation

gf::BareTexture::BareTexture ( Format  format)

Constructor.

Parameters
formatThe format of the texture

Once set, the format can not be changed.

gf::BareTexture::~BareTexture ( )

Destructor.

gf::BareTexture::BareTexture ( const BareTexture )
delete

Deleted copy constructor.

gf::BareTexture::BareTexture ( BareTexture &&  other)

Move constructor.

Member Function Documentation

static void gf::BareTexture::bind ( const BareTexture texture)
static

Bind a texture for rendering.

This function is for internal use only.

Parameters
texturePointer to the texture to bind, can be nullptr to use no texture
RectF gf::BareTexture::computeTextureCoords ( const RectU rect) const

Compute normalized texture coordinates.

Parameters
rectThe rectangle in the texture, in pixels
Returns
Normalized texture coordinates
bool gf::BareTexture::create ( Vector2u  size,
const uint8_t *  data 
)
protected

Create the texture.

If this function fails, the texture is left unchanged.

Parameters
sizeSize of the texture
dataInitial pixels of the texture (can be nullptr)
Returns
True if creation was successful
bool gf::BareTexture::generateMipmap ( )

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 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
Format gf::BareTexture::getFormat ( ) const
inline

Get the format of the texture.

Returns
The format of the texture
unsigned gf::BareTexture::getName ( ) const
inline

Get the internal representation of the texture.

This function is for internal use only.

Returns
The OpenGL name of the texture
Vector2u gf::BareTexture::getSize ( ) const
inline

Return the size of the texture.

Returns
Size in pixels
bool gf::BareTexture::isRepeated ( ) const
inlinenoexcept

Check if the texture is repeated or not.

Returns
True if repeat mode is enabled, false if it is disabled
See also
setRepeated()
bool gf::BareTexture::isSmooth ( ) const
inlinenoexcept

Check if the smooth filter is enabled or not.

Returns
True if smoothing is enabled, false if it is disabled
See also
setSmooth()
BareTexture& gf::BareTexture::operator= ( const BareTexture )
delete

Deleted copy assignment.

BareTexture& gf::BareTexture::operator= ( BareTexture &&  other)

Move assignment.

void gf::BareTexture::setRepeated ( bool  repeated = true)

Enable or disable repeating.

Repeating is involved when using texture coordinates outside the texture rectangle \( [0, 1] @times [0, 1] \). 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 \( u \) texture coordinate is 3, the texture will be repeated 3 times).

If repeat mode is disabled, the "extra space" will instead be filled with border pixels.

Repeating is disabled by default.

Parameters
repeatedTrue to repeat the texture, false to disable repeating
See also
isRepeated()
void gf::BareTexture::setSmooth ( bool  smooth = true)

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.

Parameters
smoothTrue to enable smoothing, false to disable it
See also
isSmooth()
void gf::BareTexture::update ( const uint8_t *  data)

Update the whole texture from an array of pixels.

The data array is assumed to be in the right format (4 channels for colored texture and 1 channel for alpha texture) and have the right size.

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 data is nullptr or if the texture was not previously created.

Parameters
dataAn array of pixels to copy to the texture
void gf::BareTexture::update ( const uint8_t *  data,
const RectU rect 
)

Update a part of the texture from an array of pixels.

The data array is assumed to be in the right format (4 channels for colored texture and 1 channel for alpha texture) and its size must match the size of the rect argument.

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 data is nullptr or if the texture was not previously created.

Parameters
dataAn array of pixels to copy to the texture
rectThe region of the texture to update