Explorar el Código

Refactor out flyweight object.
TODO: Move members out to the object.

Sam Jaffe hace 6 años
padre
commit
6583de4d13

+ 3 - 3
graphics/include/game/graphics/shader.hpp

@@ -7,17 +7,17 @@
 
 #pragma once
 
-#include "game/util/identity.hpp"
-
 #include <string>
 
+#include "game/util/flyweight.hpp"
+
 namespace graphics {
   namespace shaders {
     enum class type : unsigned int;
   }
   class shader : public identity<shader> {
   public:
-    static shader create(shaders::type tp, std::string const & path);
+    static flyweight<shader> create(shaders::type tp, std::string const & path);
 
   private:
     shader(unsigned int);

+ 3 - 3
graphics/include/game/graphics/shader_program.hpp

@@ -9,13 +9,13 @@
 
 #include <string>
 
-#include "game/util/identity.hpp"
+#include "game/util/flyweight.hpp"
 
 namespace graphics {
   class shader_program : public identity<shader_program> {
   public:
-    static shader_program create(std::string const & frag,
-                                 std::string const & vert);
+    static flyweight<shader_program> create(std::string const & frag,
+                                            std::string const & vert);
     void activate() const;
 
   private:

+ 2 - 2
graphics/include/game/graphics/texture.hpp

@@ -8,13 +8,13 @@
 #pragma once
 
 #include "game/math/math_fwd.hpp"
-#include "game/util/identity.hpp"
+#include "game/util/flyweight.hpp"
 #include "vector/vector.hpp"
 
 namespace graphics {
   class texture : public identity<texture> {
   public:
-    static texture create(std::string const & imagefile);
+    static flyweight<texture> create(std::string const & imagefile);
 
     static texture const WHITE;
     static texture const DARK_YELLOW;

+ 4 - 2
graphics/src/helper.hpp

@@ -13,6 +13,8 @@
 
 #include "game/math/math_fwd.hpp"
 
+template <typename> class flyweight;
+
 namespace graphics {
   class shader;
   namespace textures {
@@ -22,8 +24,8 @@ namespace graphics {
   namespace shaders {
     enum class type : unsigned int { FRAGMENT, VERTEX };
     unsigned int init(type, std::string const &);
-    unsigned int init_program(shader const & fragmentShader,
-                              shader const & vertexShader);
+    unsigned int init(flyweight<shader> const & fragmentShader,
+                      flyweight<shader> const & vertexShader);
     void activate(unsigned int id);
   }
 }

+ 2 - 2
graphics/src/opengl_helper.cxx

@@ -235,8 +235,8 @@ namespace graphics { namespace shaders {
     return id;
   }
 
-  unsigned int init_program(shader const & fragmentShader,
-                            shader const & vertexShader) {
+  unsigned int init(flyweight<shader> const & fragmentShader,
+                    flyweight<shader> const & vertexShader) {
     // 9. Create a new shader program ID
     unsigned int id = glCreateProgram();
 

+ 1 - 1
graphics/src/shader.cpp

@@ -17,7 +17,7 @@ namespace {
 }
 
 namespace graphics {
-  shader shader::create(shaders::type tp, std::string const & path) {
+  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; }

+ 7 - 6
graphics/src/shader_program.cpp

@@ -15,20 +15,21 @@
 
 namespace {
   using key_t = std::pair<std::string, std::string>;
-  std::unordered_map<key_t, ::graphics::shader_program> g_shader_programs;
+  std::unordered_map<key_t, flyweight<::graphics::shader_program>>
+      g_shader_programs;
 }
 
 namespace graphics {
-  shader_program shader_program::create(std::string const & frag,
-                                        std::string const & 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 fragment_shader = shader::create(shaders::type::FRAGMENT, frag);
-    shader vertex_shader = shader::create(shaders::type::VERTEX, vert);
+    auto fragment_shader = shader::create(shaders::type::FRAGMENT, frag);
+    auto vertex_shader = shader::create(shaders::type::VERTEX, vert);
 
-    shader_program prog{shaders::init_program(fragment_shader, vertex_shader)};
+    shader_program prog{shaders::init(fragment_shader, vertex_shader)};
     return g_shader_programs.emplace(key, std::move(prog)).first->second;
   }
 

+ 3 - 3
graphics/src/texture.cpp

@@ -23,7 +23,7 @@ unsigned char * stbi_load(char const *, int *, int *, int *, int);
 void stbi_image_free(void *);
 
 namespace {
-  std::unordered_map<std::string, ::graphics::texture> g_textures;
+  std::unordered_map<std::string, flyweight<graphics::texture>> g_textures;
 }
 
 namespace graphics {
@@ -38,7 +38,7 @@ namespace graphics {
     }
   }
 
-  texture texture::create(std::string const & imagefile) {
+  flyweight<texture> texture::create(std::string const & imagefile) {
     auto found = g_textures.find(imagefile);
     if (found != g_textures.end()) { return found->second; }
 
@@ -56,7 +56,7 @@ namespace graphics {
   }
 
   texture::texture(unsigned int id, math::vec2i sz)
-      : identity<graphics::texture>(id), size(sz) {}
+      : identity<texture>(id), size(sz) {}
 
   texture const texture::WHITE = texture::create("\xFF\xFF\xFF\xFF", {{1, 1}});
   texture const texture::DARK_YELLOW =

+ 28 - 0
util/include/game/util/flyweight.hpp

@@ -0,0 +1,28 @@
+//
+//  flyweight.hpp
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//
+
+#pragma once
+
+#include <unordered_map>
+
+#include "identity.hpp"
+
+template <typename T> class flyweight : public identity<T> {
+private:
+  static std::unordered_map<unsigned int, T> actual_;
+
+public:
+  flyweight(unsigned int id, T actual) : identity<T>(id) {
+    actual_.emplace(id, std::move(actual));
+  }
+  flyweight(T actual) : identity<T>(actual) {
+    actual_.emplace(this->id, std::move(actual));
+  }
+  T const & actual() const { return actual_.find(this->id)->second; }
+};
+
+template <typename T> std::unordered_map<unsigned int, T> flyweight<T>::actual_;