فهرست منبع

Add data members to texture/shader/shader_program. Use flyweight<T> to get quick-use versions.

Sam Jaffe 6 سال پیش
والد
کامیت
37d4075d48

+ 4 - 1
graphics/include/game/graphics/shader.hpp

@@ -20,6 +20,9 @@ namespace graphics {
     static flyweight<shader> create(shaders::type tp, std::string const & path);
 
   private:
-    shader(unsigned int);
+    shader(unsigned int, shaders::type, std::string const &);
+
+    shaders::type type;
+    std::string path;
   };
 }

+ 4 - 1
graphics/include/game/graphics/shader_program.hpp

@@ -11,6 +11,8 @@
 
 #include "game/util/flyweight.hpp"
 
+#include "shader.hpp"
+
 namespace graphics {
   class shader_program : public identity<shader_program> {
   public:
@@ -19,6 +21,7 @@ namespace graphics {
     void activate() const;
 
   private:
-    shader_program(unsigned int);
+    shader_program(unsigned int, flyweight<shader>, flyweight<shader>);
+    flyweight<shader> fragment_shader, vertex_shader;
   };
 }

+ 13 - 12
graphics/src/shader.cpp

@@ -11,20 +11,21 @@
 
 #include "helper.hpp"
 
+using namespace graphics;
+
 namespace {
-  using key_t = std::pair<graphics::shaders::type, std::string>;
-  std::unordered_map<key_t, ::graphics::shader> g_shaders;
+  using key_t = std::pair<shaders::type, std::string>;
+  std::unordered_map<key_t, shader> g_shaders;
 }
 
-namespace graphics {
-  flyweight<shader> shader::create(shaders::type tp, std::string const & path) {
-    auto key = std::make_pair(tp, path);
-    auto found = g_shaders.find(key);
-    if (found != g_shaders.end()) { return found->second; }
-
-    shader shad{shaders::init(tp, path)};
-    return g_shaders.emplace(key, std::move(shad)).first->second;
-  }
+flyweight<shader> shader::create(shaders::type tp, std::string const & path) {
+  auto key = std::make_pair(tp, path);
+  auto found = g_shaders.find(key);
+  if (found != g_shaders.end()) { return found->second; }
 
-  shader::shader(unsigned int id) : identity<shader>(id) {}
+  shader shad{shaders::init(tp, path), tp, path};
+  return g_shaders.emplace(key, std::move(shad)).first->second;
 }
+
+shader::shader(unsigned int id, shaders::type type, std::string const & path)
+    : identity<shader>(id), type(type), path(path) {}

+ 19 - 17
graphics/src/shader_program.cpp

@@ -13,28 +13,30 @@
 #include "game/graphics/shader.hpp"
 #include "helper.hpp"
 
+using namespace graphics;
+
 namespace {
   using key_t = std::pair<std::string, std::string>;
-  std::unordered_map<key_t, flyweight<::graphics::shader_program>>
-      g_shader_programs;
+  std::unordered_map<key_t, flyweight<shader_program>> g_shader_programs;
 }
 
-namespace graphics {
-  flyweight<shader_program> shader_program::create(std::string const & frag,
-                                                   std::string const & vert) {
-    auto key = std::make_pair(frag, vert);
-    auto found = g_shader_programs.find(key);
-    if (found != g_shader_programs.end()) { return found->second; }
-
-    auto fragment_shader = shader::create(shaders::type::FRAGMENT, frag);
-    auto vertex_shader = shader::create(shaders::type::VERTEX, vert);
+flyweight<shader_program> shader_program::create(std::string const & frag,
+                                                 std::string const & vert) {
+  auto key = std::make_pair(frag, vert);
+  auto found = g_shader_programs.find(key);
+  if (found != g_shader_programs.end()) { return found->second; }
 
-    shader_program prog{shaders::init(fragment_shader, vertex_shader)};
-    return g_shader_programs.emplace(key, std::move(prog)).first->second;
-  }
+  auto fragment_shader = shader::create(shaders::type::FRAGMENT, frag);
+  auto vertex_shader = shader::create(shaders::type::VERTEX, vert);
 
-  shader_program::shader_program(unsigned int id)
-      : identity<shader_program>(id) {}
+  shader_program prog{shaders::init(fragment_shader, vertex_shader),
+                      fragment_shader, vertex_shader};
+  return g_shader_programs.emplace(key, std::move(prog)).first->second;
+}
 
-  void shader_program::activate() const { shaders::activate(id); }
+shader_program::shader_program(unsigned int id, flyweight<shader> frag,
+                               flyweight<shader> vert)
+    : identity<shader_program>(id), fragment_shader(frag), vertex_shader(vert) {
 }
+
+void shader_program::activate() const { shaders::activate(id); }

+ 33 - 33
graphics/src/texture.cpp

@@ -22,45 +22,45 @@
 unsigned char * stbi_load(char const *, int *, int *, int *, int);
 void stbi_image_free(void *);
 
+using namespace graphics;
+
 namespace {
-  std::unordered_map<std::string, flyweight<graphics::texture>> g_textures;
+  std::unordered_map<std::string, flyweight<texture>> g_textures;
 }
 
-namespace graphics {
-  static textures::format format(int comps) {
-    switch (comps) {
-    case 3:
-      return textures::format::RGB;
-    case 4:
-      return textures::format::RGBA;
-    default:
-      throw;
-    }
+static textures::format format(int comps) {
+  switch (comps) {
+  case 3:
+    return textures::format::RGB;
+  case 4:
+    return textures::format::RGBA;
+  default:
+    throw;
   }
+}
 
-  flyweight<texture> texture::create(std::string const & imagefile) {
-    auto found = g_textures.find(imagefile);
-    if (found != g_textures.end()) { return found->second; }
+flyweight<texture> texture::create(std::string const & imagefile) {
+  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{textures::init(format(components), size, data), size};
-    return g_textures.emplace(imagefile, std::move(tex)).first->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{textures::init(format(components), size, data), size};
+  return g_textures.emplace(imagefile, std::move(tex)).first->second;
+}
 
-  texture texture::create(char const * data, math::vec2i size) {
-    return {textures::init(format(4), size, data), size};
-  }
+texture texture::create(char const * data, math::vec2i size) {
+  return {textures::init(format(4), size, data), size};
+}
 
-  texture::texture(unsigned int id, math::vec2i sz)
-      : identity<texture>(id), size(sz) {}
+texture::texture(unsigned int id, math::vec2i sz)
+    : identity<texture>(id), size(sz) {}
 
-  texture const texture::WHITE = texture::create("\xFF\xFF\xFF\xFF", {{1, 1}});
-  texture const texture::DARK_YELLOW =
-      texture::create("\x80\x80\x00\xFF", {{1, 1}});
-  texture const texture::LIGHT_BLUE =
-      texture::create("\x80\x80\xFF\xFF", {{1, 1}});
-}
+texture const texture::WHITE = texture::create("\xFF\xFF\xFF\xFF", {{1, 1}});
+texture const texture::DARK_YELLOW =
+    texture::create("\x80\x80\x00\xFF", {{1, 1}});
+texture const texture::LIGHT_BLUE =
+    texture::create("\x80\x80\xFF\xFF", {{1, 1}});