nngn
Loading...
Searching...
No Matches
nngn::Textures Class Reference

Texture manager, loads and caches image data from files/buffers. More...

#include <texture.h>

Collaboration diagram for nngn::Textures:

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

Graphicsgraphics = nullptr
 
std::vector< Hashhashes = {{}}
 
std::vector< u32counts = {1}
 
std::vector< std::string > names = {{}}
 
u64 gen = 0
 

Detailed Description

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.

Member Enumeration Documentation

◆ Format

Enumerator
PNG 
PPM 

Member Function Documentation

◆ add_ref() [1/2]

void nngn::Textures::add_ref ( const char * filename,
std::size_t n = 1 )

Increments the reference count of a texture by name.

Here is the call graph for this function:

◆ add_ref() [2/2]

void nngn::Textures::add_ref ( u32 i,
std::size_t n = 1 )

Increments the reference count of a texture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump()

std::vector< std::tuple< std::string_view, u32 > > nngn::Textures::dump ( void ) const

Dumps name/ref_count pairs for all loaded textures.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flip_y()

void nngn::Textures::flip_y ( std::span< std::byte > s)
static

Reverses the rows of a buffer containing raw RGBA image data.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generation()

u64 nngn::Textures::generation ( void ) const
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.

Here is the caller graph for this function:

◆ insert()

u32 nngn::Textures::insert ( u32 i,
std::string_view name,
const std::byte * p )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

u32 nngn::Textures::load ( const char * filename)

Loads texture data from a file.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_data()

u32 nngn::Textures::load_data ( const char * name,
const std::byte * p )

Loads RGBA texture data from a buffer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ max()

u32 nngn::Textures::max ( void ) const
inline

Maximum number of textures that can be loaded, see set_max.

Here is the caller graph for this function:

◆ n()

u32 nngn::Textures::n ( void ) const

Number of active images in the cache.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

std::vector< std::byte > nngn::Textures::read ( const char * filename)
static

Reads raw RGBA image data from a file.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ red_to_rgba()

void nngn::Textures::red_to_rgba ( std::span< std::byte > dst,
std::span< const std::byte > src )
static

Expands single-channel data into full RGBA.

Each pixel value is replicated three times, one for each channel.

Parameters
dstBuffer of nngn::Graphics::TEXTURE_SIZE size.
srcSource buffer of size dst.size() / 4 containing the single-channel values to be replicated.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reload()

bool nngn::Textures::reload ( u32 i)

Reloads data for a single texture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reload_all()

bool nngn::Textures::reload_all ( void )

Reloads all texture data.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove()

void nngn::Textures::remove ( u32 i)

Decrements a texture's reference count and cleans up if necessary.

Here is the caller graph for this function:

◆ set_graphics()

void nngn::Textures::set_graphics ( Graphics * g)
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).

Here is the caller graph for this function:

◆ set_max()

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.

Here is the caller graph for this function:

◆ update_data()

bool nngn::Textures::update_data ( u32 i,
const std::byte * p ) const

Updates a previously loaded texture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

bool nngn::Textures::write ( const char * filename,
std::span< const std::byte > s,
Format fmt )
static

Writes raw RGBA image data to a file.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ counts

std::vector<u32> nngn::Textures::counts = {1}
private

◆ gen

u64 nngn::Textures::gen = 0
private

◆ graphics

Graphics* nngn::Textures::graphics = nullptr
private

◆ hashes

std::vector<Hash> nngn::Textures::hashes = {{}}
private

◆ names

std::vector<std::string> nngn::Textures::names = {{}}
private

The documentation for this class was generated from the following files: