Use an enum for texture setup
This commit is contained in:
parent
06e7e308cc
commit
89c8589c21
|
@ -25,7 +25,7 @@ Quad::Quad(std::span<float> verts, const Uniform &uniform_data)
|
||||||
color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
tex_id = TextureLoader::get_instance()
|
tex_id = TextureLoader::get_instance()
|
||||||
->load_texture("./res/tex/util/missing.png")
|
->load_texture(TextureID::Tex_Util_Missing)
|
||||||
->get_texture_id();
|
->get_texture_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,16 @@
|
||||||
|
|
||||||
#define PNG_HEADER_BYTES_TO_CHECK 8
|
#define PNG_HEADER_BYTES_TO_CHECK 8
|
||||||
|
|
||||||
|
std::string TextureLoader::get_path_from_id(TextureID id) {
|
||||||
|
switch (id) {
|
||||||
|
case Tex_Util_Missing:
|
||||||
|
return "./res/tex/util/missing.png";
|
||||||
|
default:
|
||||||
|
spdlog::warn("Encountered request for invalid texture {}", id);
|
||||||
|
return "./res/tex/util/missing.png";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TextureLoader *TextureLoader::m_instance = NULL;
|
TextureLoader *TextureLoader::m_instance = NULL;
|
||||||
|
|
||||||
TextureLoader *TextureLoader::get_instance() {
|
TextureLoader *TextureLoader::get_instance() {
|
||||||
|
@ -27,26 +37,30 @@ TextureLoader *TextureLoader::get_instance() {
|
||||||
TextureLoader::TextureLoader() { m_texture_cache.clear(); }
|
TextureLoader::TextureLoader() { m_texture_cache.clear(); }
|
||||||
|
|
||||||
TextureLoader::~TextureLoader() {
|
TextureLoader::~TextureLoader() {
|
||||||
for (std::pair<std::string, Texture *> st : m_texture_cache) {
|
for (std::pair<TextureID, Texture *> st : m_texture_cache) {
|
||||||
delete (st.second);
|
delete (st.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture *TextureLoader::load_texture(const std::string &path) {
|
Texture *TextureLoader::load_texture(TextureID id) {
|
||||||
// Cache Hit
|
// Cache Hit
|
||||||
if (m_texture_cache.contains(path)) {
|
if (m_texture_cache.contains(id)) {
|
||||||
return m_texture_cache.at(path);
|
return m_texture_cache.at(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache miss - we have to load it and create it
|
// Cache miss - we have to load it and create it
|
||||||
m_texture_cache.emplace(std::make_pair(path, new Texture(path)));
|
m_texture_cache.emplace(
|
||||||
|
std::make_pair(id, new Texture(get_path_from_id(id))));
|
||||||
|
|
||||||
return m_texture_cache.at(path);
|
return m_texture_cache.at(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Figure out why png_sig_cmp never worked and replace this garbage
|
// FIXME: Figure out why png_sig_cmp never worked and replace this garbage
|
||||||
static bool validate_png_sig(
|
static bool validate_png_sig(
|
||||||
const std::array<std::uint8_t, PNG_HEADER_BYTES_TO_CHECK> &bytes) {
|
const std::array<std::uint8_t, PNG_HEADER_BYTES_TO_CHECK> &bytes) {
|
||||||
|
#ifndef texture_use_own_validation
|
||||||
|
return png_sig_cmp(bytes.data(), 0, 8) == 0;
|
||||||
|
#else
|
||||||
std::array<std::uint8_t, 8> valid_header = {0x89, 0x50, 0x4e, 0x47,
|
std::array<std::uint8_t, 8> valid_header = {0x89, 0x50, 0x4e, 0x47,
|
||||||
0x0d, 0x0a, 0x1a, 0x0a};
|
0x0d, 0x0a, 0x1a, 0x0a};
|
||||||
|
|
||||||
|
@ -60,6 +74,7 @@ static bool validate_png_sig(
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::Texture(const std::string &path) {
|
Texture::Texture(const std::string &path) {
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
enum TextureID { Tex_Util_Missing = 0 };
|
||||||
|
|
||||||
class Texture {
|
class Texture {
|
||||||
private:
|
private:
|
||||||
GLuint m_gl_texture_id;
|
GLuint m_gl_texture_id;
|
||||||
|
@ -22,13 +24,15 @@ class TextureLoader {
|
||||||
private:
|
private:
|
||||||
static TextureLoader *m_instance;
|
static TextureLoader *m_instance;
|
||||||
|
|
||||||
std::unordered_map<std::string, Texture *> m_texture_cache;
|
std::unordered_map<TextureID, Texture *> m_texture_cache;
|
||||||
|
|
||||||
TextureLoader();
|
TextureLoader();
|
||||||
~TextureLoader();
|
~TextureLoader();
|
||||||
|
|
||||||
|
static std::string get_path_from_id(TextureID);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Texture *load_texture(const std::string &path);
|
Texture *load_texture(TextureID id);
|
||||||
static TextureLoader *get_instance();
|
static TextureLoader *get_instance();
|
||||||
|
|
||||||
TextureLoader(const TextureLoader &tl) = delete;
|
TextureLoader(const TextureLoader &tl) = delete;
|
||||||
|
|
Loading…
Reference in New Issue