| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- //
- // texture.cpp
- // graphics
- //
- // Created by Sam Jaffe on 7/5/16.
- //
- #include "game/graphics/texture.hpp"
- #include "scope_guard/scope_guard.hpp"
- #include <string>
- #include <unordered_map>
- #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 <typename T, typename Key = std::string> class private_factory {
- public:
- using key_t = Key;
- template <typename... Args> 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<key_t, T> instances;
- };
- namespace graphics { namespace detail { namespace texture {
- struct format {};
- std::unordered_map<std::string, ::graphics::texture> 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<graphics::texture>(id), size(sz) {}
- }
|