From 89c8589c213d508695a02af24c8721d4d566f871 Mon Sep 17 00:00:00 2001 From: Archie Hilton Date: Mon, 27 May 2024 15:31:14 +0100 Subject: [PATCH] Use an enum for texture setup --- src/Quad.cc | 2 +- src/texture.cc | 27 +++++++++++++++++++++------ src/texture.hh | 8 ++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Quad.cc b/src/Quad.cc index 56c1ece..ec30ce6 100644 --- a/src/Quad.cc +++ b/src/Quad.cc @@ -25,7 +25,7 @@ Quad::Quad(std::span verts, const Uniform &uniform_data) color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); tex_id = TextureLoader::get_instance() - ->load_texture("./res/tex/util/missing.png") + ->load_texture(TextureID::Tex_Util_Missing) ->get_texture_id(); } diff --git a/src/texture.cc b/src/texture.cc index 963547f..700b824 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -14,6 +14,16 @@ #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::get_instance() { @@ -27,26 +37,30 @@ TextureLoader *TextureLoader::get_instance() { TextureLoader::TextureLoader() { m_texture_cache.clear(); } TextureLoader::~TextureLoader() { - for (std::pair st : m_texture_cache) { + for (std::pair st : m_texture_cache) { delete (st.second); } } -Texture *TextureLoader::load_texture(const std::string &path) { +Texture *TextureLoader::load_texture(TextureID id) { // Cache Hit - if (m_texture_cache.contains(path)) { - return m_texture_cache.at(path); + if (m_texture_cache.contains(id)) { + return m_texture_cache.at(id); } // 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 static bool validate_png_sig( const std::array &bytes) { +#ifndef texture_use_own_validation + return png_sig_cmp(bytes.data(), 0, 8) == 0; +#else std::array valid_header = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; @@ -60,6 +74,7 @@ static bool validate_png_sig( } return true; +#endif } Texture::Texture(const std::string &path) { diff --git a/src/texture.hh b/src/texture.hh index 3c66799..01d954a 100644 --- a/src/texture.hh +++ b/src/texture.hh @@ -6,6 +6,8 @@ #include #include +enum TextureID { Tex_Util_Missing = 0 }; + class Texture { private: GLuint m_gl_texture_id; @@ -22,13 +24,15 @@ class TextureLoader { private: static TextureLoader *m_instance; - std::unordered_map m_texture_cache; + std::unordered_map m_texture_cache; TextureLoader(); ~TextureLoader(); + static std::string get_path_from_id(TextureID); + public: - Texture *load_texture(const std::string &path); + Texture *load_texture(TextureID id); static TextureLoader *get_instance(); TextureLoader(const TextureLoader &tl) = delete;