nngn
Loading...
Searching...
No Matches
texture.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_TEXTURE_H
2#define NNGN_GRAPHICS_TEXTURE_H
3
4#include <cassert>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include "math/hash.h"
10#include "utils/def.h"
11
12namespace nngn {
13
14struct Graphics;
15
34class Textures {
35public:
36 enum class Format : u8 {
37 PNG = 1,
38 PPM,
39 };
41 static std::vector<std::byte> read(const char *filename);
43 static bool write(
44 const char *filename, std::span<const std::byte> s, Format fmt);
53 static void red_to_rgba(
54 std::span<std::byte> dst, std::span<const std::byte> src);
56 static void flip_y(std::span<std::byte> s);
58 u32 max(void) const { return static_cast<u32>(this->counts.capacity()); }
60 u32 n(void) const;
66 u64 generation(void) const { return this->gen; }
72 void set_graphics(Graphics *g) { this->graphics = g; }
79 void set_max(u32 n);
81 u32 load_data(const char *name, const std::byte *p);
83 u32 load(const char *filename);
85 bool reload(u32 i);
87 bool reload_all(void);
89 bool update_data(u32 i, const std::byte *p) const;
91 void remove(u32 i);
93 void add_ref(u32 i, std::size_t n = 1);
95 void add_ref(const char *filename, std::size_t n = 1);
97 std::vector<std::tuple<std::string_view, u32>> dump(void) const;
98private:
99 Graphics *graphics = nullptr;
100 std::vector<Hash> hashes = {{}};
101 std::vector<u32> counts = {1};
102 std::vector<std::string> names = {{}};
103 u64 gen = 0;
104 u32 insert(u32 i, std::string_view name, const std::byte *p);
105};
106
107}
108
109#endif
Texture manager, loads and caches image data from files/buffers.
Definition: texture.h:34
Format
Definition: texture.h:36
std::vector< u32 > counts
Definition: texture.h:101
Graphics * graphics
Definition: texture.h:99
static bool write(const char *filename, std::span< const std::byte > s, Format fmt)
Writes raw RGBA image data to a file.
Definition: texture.cpp:116
u32 load_data(const char *name, const std::byte *p)
Loads RGBA texture data from a buffer.
Definition: texture.cpp:188
void remove(u32 i)
Decrements a texture's reference count and cleans up if necessary.
Definition: texture.cpp:238
bool reload(u32 i)
Reloads data for a single texture.
Definition: texture.cpp:218
void set_max(u32 n)
Sets the maximum number of textures that can be loaded.
Definition: texture.cpp:182
u32 max(void) const
Maximum number of textures that can be loaded, see set_max.
Definition: texture.h:58
u64 generation(void) const
Counter incremented every time the cache is changed.
Definition: texture.h:66
u64 gen
Definition: texture.h:103
bool update_data(u32 i, const std::byte *p) const
Updates a previously loaded texture.
Definition: texture.cpp:233
void add_ref(u32 i, std::size_t n=1)
Increments the reference count of a texture.
Definition: texture.cpp:247
std::vector< std::string > names
Definition: texture.h:102
static void red_to_rgba(std::span< std::byte > dst, std::span< const std::byte > src)
Expands single-channel data into full RGBA.
Definition: texture.cpp:153
void set_graphics(Graphics *g)
Sets the graphics back end where textures will be uploaded.
Definition: texture.h:72
std::vector< Hash > hashes
Definition: texture.h:100
static void flip_y(std::span< std::byte > s)
Reverses the rows of a buffer containing raw RGBA image data.
Definition: texture.cpp:163
bool reload_all(void)
Reloads all texture data.
Definition: texture.cpp:226
u32 insert(u32 i, std::string_view name, const std::byte *p)
Definition: texture.cpp:142
std::vector< std::tuple< std::string_view, u32 > > dump(void) const
Dumps name/ref_count pairs for all loaded textures.
Definition: texture.cpp:263
static std::vector< std::byte > read(const char *filename)
Reads raw RGBA image data from a file.
Definition: texture.cpp:91
u32 n(void) const
Number of active images in the cache.
Definition: texture.cpp:177
function fmt(name, size, n, max)
Definition: debug.h:45
std::uint64_t u64
Definition: def.h:15
std::uint8_t u8
Definition: def.h:12
std::uint32_t u32
Definition: def.h:14
name
Definition: old_man.lua:1
load
Definition: entity.lua:5
Definition: graphics.h:99
std::chrono::seconds s
Definition: timing.cpp:6