|
@@ -9,8 +9,8 @@
|
|
|
|
|
|
|
|
#include "scope_guard/scope_guard.hpp"
|
|
#include "scope_guard/scope_guard.hpp"
|
|
|
|
|
|
|
|
-#include <unordered_map>
|
|
|
|
|
#include <string>
|
|
#include <string>
|
|
|
|
|
+#include <unordered_map>
|
|
|
|
|
|
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic push
|
|
|
#pragma clang diagnostic ignored "-Wcomma"
|
|
#pragma clang diagnostic ignored "-Wcomma"
|
|
@@ -18,56 +18,54 @@
|
|
|
#include "stb/stb_image.h"
|
|
#include "stb/stb_image.h"
|
|
|
#pragma clang diagnostic pop
|
|
#pragma clang diagnostic pop
|
|
|
|
|
|
|
|
-unsigned char * stbi_load( char const *, int *, int *, int *, int );
|
|
|
|
|
-void stbi_image_free( void * );
|
|
|
|
|
|
|
+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 {
|
|
|
|
|
|
|
+template <typename T, typename Key = std::string> class private_factory {
|
|
|
public:
|
|
public:
|
|
|
using key_t = Key;
|
|
using key_t = Key;
|
|
|
-
|
|
|
|
|
- template <typename... Args>
|
|
|
|
|
- static T create(key_t const & key) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ template <typename... Args> static T create(key_t const & key) {
|
|
|
auto found = instances.find(key);
|
|
auto found = instances.find(key);
|
|
|
if (found == instances.end()) {
|
|
if (found == instances.end()) {
|
|
|
found = instances.emplace(key, T::create(key)).first;
|
|
found = instances.emplace(key, T::create(key)).first;
|
|
|
}
|
|
}
|
|
|
return found->second;
|
|
return found->second;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
private:
|
|
private:
|
|
|
static std::unordered_map<key_t, T> instances;
|
|
static std::unordered_map<key_t, T> instances;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
namespace graphics { namespace detail { namespace texture {
|
|
namespace graphics { namespace detail { namespace texture {
|
|
|
struct format {};
|
|
struct format {};
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
std::unordered_map<std::string, ::graphics::texture> g_textures;
|
|
std::unordered_map<std::string, ::graphics::texture> g_textures;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
unsigned int init(format = {}, math::vec2i = {}, unsigned char * = {}) {
|
|
unsigned int init(format = {}, math::vec2i = {}, unsigned char * = {}) {
|
|
|
throw; // TODO implement
|
|
throw; // TODO implement
|
|
|
}
|
|
}
|
|
|
-} } }
|
|
|
|
|
|
|
+}}}
|
|
|
|
|
|
|
|
namespace graphics {
|
|
namespace graphics {
|
|
|
- texture texture::create( std::string const & imagefile ) {
|
|
|
|
|
|
|
+ texture texture::create(std::string const & imagefile) {
|
|
|
using detail::texture::g_textures;
|
|
using detail::texture::g_textures;
|
|
|
auto found = g_textures.find(imagefile);
|
|
auto found = g_textures.find(imagefile);
|
|
|
if (found != g_textures.end()) { return found->second; }
|
|
if (found != g_textures.end()) { return found->second; }
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
int components = 0;
|
|
int components = 0;
|
|
|
math::vec2i size;
|
|
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 };
|
|
|
|
|
|
|
+ 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;
|
|
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) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 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) {}
|
|
|
}
|
|
}
|