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
Format
Definition: texture.h:36
u32 load_data(const char *name, const std::byte *p)
Loads RGBA texture data from a buffer.
void remove(u32 i)
Decrements a texture's reference count and cleans up if necessary.
bool reload(u32 i)
Reloads data for a single texture.
void set_max(u32 n)
Sets the maximum number of textures that can be loaded.
std::vector< Hash > hashes
Definition: texture.h:100
u32 max(void) const
Maximum number of textures that can be loaded, see set_max.
Definition: texture.h:58
static std::vector< std::byte > read(const char *filename)
Reads raw RGBA image data from a file.
u64 generation(void) const
Counter incremented every time the cache is changed.
Definition: texture.h:66
void add_ref(const char *filename, std::size_t n=1)
Increments the reference count of a texture by name.
Graphics * graphics
Definition: texture.h:99
u64 gen
Definition: texture.h:103
bool update_data(u32 i, const std::byte *p) const
Updates a previously loaded texture.
void add_ref(u32 i, std::size_t n=1)
Increments the reference count of a texture.
void set_graphics(Graphics *g)
Sets the graphics back end where textures will be uploaded.
Definition: texture.h:72
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.
bool reload_all(void)
Reloads all texture data.
u32 insert(u32 i, std::string_view name, const std::byte *p)
u32 load(const char *filename)
Loads texture data from a file.
std::vector< std::tuple< std::string_view, u32 > > dump(void) const
Dumps name/ref_count pairs for all loaded textures.
std::vector< u32 > counts
Definition: texture.h:101
std::vector< std::string > names
Definition: texture.h:102
static void flip_y(std::span< std::byte > s)
Reverses the rows of a buffer containing raw RGBA image data.
u32 n(void) const
Number of active images in the cache.
for i
Definition: font.lua:5
g
Definition: gamma.lua:7
name
Definition: cathedral.lua:11
p
Definition: input.lua:29
std::chrono::seconds s
Definition: timing.cpp:6
Definition: audio.cpp:7
std::uint32_t u32
Definition: def.h:14
std::string fmt(auto &&...args)
Definition: string.h:11
std::uint64_t u64
Definition: def.h:15
std::uint8_t u8
Definition: def.h:12
Definition: graphics.h:138