|
|
@@ -7,7 +7,7 @@
|
|
|
|
|
|
#include "texture.hpp"
|
|
|
|
|
|
-#include "util/scope_exit.hpp"
|
|
|
+#include "scope_guard/scope_guard.hpp"
|
|
|
|
|
|
#include <unordered_map>
|
|
|
#include <string>
|
|
|
@@ -26,28 +26,26 @@ public:
|
|
|
template <typename... Args>
|
|
|
static T create(key_t const & key) {
|
|
|
auto found = instances.find(key);
|
|
|
- if (found != instances.end()) { return found->second; }
|
|
|
- else {
|
|
|
- return instances.emplace(key, T::create(key)).first->second;
|
|
|
+ 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 {};
|
|
|
+namespace graphics { namespace detail { namespace texture {
|
|
|
+ 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
|
|
|
}
|
|
|
-
|
|
|
+} } }
|
|
|
+
|
|
|
+namespace graphics {
|
|
|
texture texture::create( std::string const & imagefile ) {
|
|
|
using detail::texture::g_textures;
|
|
|
auto found = g_textures.find(imagefile);
|
|
|
@@ -55,17 +53,18 @@ namespace graphics {
|
|
|
|
|
|
int components = 0;
|
|
|
math::vec2i size;
|
|
|
- unsigned char * data = stbi_load( imagefile.c_str(), &size.x(), &size.y(), &components, 0 );
|
|
|
+ unsigned char * data = stbi_load(imagefile.c_str(), &size.x(), &size.y(),
|
|
|
+ &components, 0);
|
|
|
scope(exit) { stbi_image_free( data ); };
|
|
|
- return g_textures.emplace(imagefile, texture{ detail::texture::init({}, size, data), size }).first->second;
|
|
|
+ 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) {
|
|
|
-
|
|
|
+ texture::texture( unsigned int id, math::vec2i sz )
|
|
|
+ : identity<graphics::texture>(id), size(sz) {
|
|
|
}
|
|
|
-
|
|
|
}
|