Gamedev Framework (gf)
0.3.0
A C++11 framework for 2D games
|
An image that lives in the graphic memory that can be used for drawing. More...
#include <gf/Texture.h>
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... | |
BareTexture & | operator= (const BareTexture &)=delete |
Deleted copy assignment. More... | |
BareTexture (BareTexture &&other) | |
Move constructor. More... | |
BareTexture & | operator= (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... | |
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.
gf::BareTexture::BareTexture | ( | Format | format | ) |
Constructor.
format | The format of the texture |
Once set, the format can not be changed.
gf::BareTexture::~BareTexture | ( | ) |
Destructor.
|
delete |
Deleted copy constructor.
gf::BareTexture::BareTexture | ( | BareTexture && | other | ) |
Move constructor.
|
static |
Bind a texture for rendering.
This function is for internal use only.
texture | Pointer to the texture to bind, can be nullptr to use no texture |
Compute normalized texture coordinates.
rect | The rectangle in the texture, in pixels |
|
protected |
Create the texture.
If this function fails, the texture is left unchanged.
size | Size of the texture |
data | Initial pixels of the texture (can be nullptr ) |
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.
|
inline |
Get the format of the texture.
|
inline |
Get the internal representation of the texture.
This function is for internal use only.
|
inline |
Return the size of the texture.
|
inlinenoexcept |
Check if the texture is repeated or not.
|
inlinenoexcept |
Check if the smooth filter is enabled or not.
|
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.
repeated | True to repeat the texture, false to disable repeating |
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.
smooth | True to enable smoothing, false to disable it |
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.
data | An 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.
data | An array of pixels to copy to the texture |
rect | The region of the texture to update |