nngn
|
Texture manager, loads and caches image data from files/buffers. More...
#include <texture.h>
Public Types | |
enum class | Format : u8 { PNG = 1 , PPM } |
Public Member Functions | |
u32 | max (void) const |
Maximum number of textures that can be loaded, see set_max. | |
u32 | n (void) const |
Number of active images in the cache. | |
u64 | generation (void) const |
Counter incremented every time the cache is changed. | |
void | set_graphics (Graphics *g) |
Sets the graphics back end where textures will be uploaded. | |
void | set_max (u32 n) |
Sets the maximum number of textures that can be loaded. | |
u32 | load_data (const char *name, const std::byte *p) |
Loads RGBA texture data from a buffer. | |
u32 | load (const char *filename) |
Loads texture data from a file. | |
bool | reload (u32 i) |
Reloads data for a single texture. | |
bool | reload_all (void) |
Reloads all texture data. | |
bool | update_data (u32 i, const std::byte *p) const |
Updates a previously loaded texture. | |
void | remove (u32 i) |
Decrements a texture's reference count and cleans up if necessary. | |
void | add_ref (u32 i, std::size_t n=1) |
Increments the reference count of a texture. | |
void | add_ref (const char *filename, std::size_t n=1) |
Increments the reference count of a texture by name. | |
std::vector< std::tuple< std::string_view, u32 > > | dump (void) const |
Dumps name/ref_count pairs for all loaded textures. | |
Static Public Member Functions | |
static std::vector< std::byte > | read (const char *filename) |
Reads raw RGBA image data from a file. | |
static bool | write (const char *filename, std::span< const std::byte > s, Format fmt) |
Writes raw RGBA image data to a file. | |
static void | red_to_rgba (std::span< std::byte > dst, std::span< const std::byte > src) |
Expands single-channel data into full RGBA. | |
static void | flip_y (std::span< std::byte > s) |
Reverses the rows of a buffer containing raw RGBA image data. | |
Private Member Functions | |
u32 | insert (u32 i, std::string_view name, const std::byte *p) |
Private Attributes | |
Graphics * | graphics = nullptr |
std::vector< Hash > | hashes = {{}} |
std::vector< u32 > | counts = {1} |
std::vector< std::string > | names = {{}} |
u64 | gen = 0 |
Texture manager, loads and caches image data from files/buffers.
All images are assumed to be rectangular and of Graphics::TEXTURE_EXTENT size. Attempting to load images with the wrong size from a file will fail. If a graphics back end is set (via set_graphics), texture data is uploaded to it via Graphics::load_textures.
Image data is associated with a string ID (usually the file name) and a reference count. Future loads with the same ID will simply increment the reference count (image data, if provided, is ignored). When a texture is removed, the reference count is decremented. No special action is taken when it reaches zero, but the slot is then considered unused and the ID will be reused in future loads.
The index 0
is special: it is pre-allocated when the object is initialized and returned in case of errors. It is a texture with no associated data.
|
strong |
void nngn::Textures::add_ref | ( | const char * | filename, |
std::size_t | n = 1 ) |
Increments the reference count of a texture by name.
void nngn::Textures::add_ref | ( | u32 | i, |
std::size_t | n = 1 ) |
Increments the reference count of a texture.
std::vector< std::tuple< std::string_view, u32 > > nngn::Textures::dump | ( | void | ) | const |
Dumps name/ref_count pairs for all loaded textures.
|
static |
Reverses the rows of a buffer containing raw RGBA image data.
|
inline |
Counter incremented every time the cache is changed.
Can be used to quickly track changes across frames, e.g. by an external tool to know when to call dump.
Loads texture data from a file.
Loads RGBA texture data from a buffer.
|
inline |
Maximum number of textures that can be loaded, see set_max.
u32 nngn::Textures::n | ( | void | ) | const |
Number of active images in the cache.
|
static |
Reads raw RGBA image data from a file.
|
static |
Expands single-channel data into full RGBA.
Each pixel value is replicated three times, one for each channel.
dst | Buffer of nngn::Graphics::TEXTURE_SIZE size. |
src | Source buffer of size dst.size() / 4 containing the single-channel values to be replicated. |
Reloads data for a single texture.
bool nngn::Textures::reload_all | ( | void | ) |
Reloads all texture data.
void nngn::Textures::remove | ( | u32 | i | ) |
Decrements a texture's reference count and cleans up if necessary.
|
inline |
Sets the graphics back end where textures will be uploaded.
This method does not load any data, any pre-existing textures have to be reloaded (see reload
and reload_all
).
void nngn::Textures::set_max | ( | u32 | n | ) |
Sets the maximum number of textures that can be loaded.
Attempting to perform a load beyond this limit will fail, unless the texture is already in the cache. It is assumed that the graphics back end has been configured to hold at least n
textures.
Updates a previously loaded texture.
|
static |
Writes raw RGBA image data to a file.
|
private |
|
private |
|
private |
|
private |
|
private |