// // texture.cpp // graphics // // Created by Sam Jaffe on 7/5/16. // #include "game/graphics/texture.hpp" #include "scope_guard/scope_guard.hpp" #include #include #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wcomma" #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" #pragma clang diagnostic pop unsigned char * stbi_load(char const *, int *, int *, int *, int); void stbi_image_free(void *); template class private_factory { public: using key_t = Key; template static T create(key_t const & key) { auto found = instances.find(key); if (found == instances.end()) { found = instances.emplace(key, T::create(key)).first; } return found->second; } private: static std::unordered_map instances; }; namespace graphics { namespace detail { namespace texture { struct format {}; std::unordered_map g_textures; unsigned int init(format = {}, math::vec2i = {}, unsigned char * = {}) { throw; // TODO implement } }}} namespace graphics { texture texture::create(std::string const & imagefile) { using detail::texture::g_textures; auto found = g_textures.find(imagefile); if (found != g_textures.end()) { return found->second; } int components = 0; math::vec2i size; unsigned char * data = stbi_load(imagefile.c_str(), &size.x(), &size.y(), &components, 0); scope(exit) { stbi_image_free(data); }; texture tex{detail::texture::init({}, size, data), size}; return g_textures.emplace(imagefile, std::move(tex)).first->second; } texture texture::create(unsigned char * data, math::vec2i size) { return {detail::texture::init({}, size, data), size}; } texture::texture(unsigned int id, math::vec2i sz) : identity(id), size(sz) {} }