Browse Source

Construct the basic format of a shader.
TODO: impl code.
ALSO:
- Perform some minor cleanup.
- std::hash for pairs

Sam Jaffe 6 years ago
parent
commit
08f987bf58

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

@@ -9,9 +9,13 @@
 
 #include "util/identity.hpp"
 
+#include <string>
+
 namespace graphics {
   class shader : public identity<shader> {
   public:
+    static shader create(unsigned int type, std::string const & path);
+
   private:
     shader(unsigned int);
   };

+ 18 - 5
graphics/src/helper.hpp

@@ -9,11 +9,24 @@
 #pragma once
 
 #include <string>
-#include <unordered_map>
+#include <utility>
 
 #include "game/math/math_fwd.hpp"
 
-namespace graphics { namespace textures {
-  enum class format { RGB, RGBA };
-  unsigned int init(format, math::vec2i, unsigned char *);
-}}
+namespace std {
+  template <typename T, typename S> struct hash<std::pair<T, S>> {
+    std::size_t operator()(std::pair<T, S> const & pair) const {
+      return std::hash<T>()(pair.first) ^ std::hash<S>()(pair.second);
+    }
+  };
+}
+
+namespace graphics {
+  namespace textures {
+    enum class format { RGB, RGBA };
+    unsigned int init(format, math::vec2i, unsigned char *);
+  }
+  namespace shaders {
+    unsigned int init(unsigned int, std::string const &);
+  }
+}

+ 4 - 0
graphics/src/opengl_helper.cxx

@@ -90,3 +90,7 @@ namespace graphics { namespace textures {
     return id;
   }
 }}
+
+namespace graphics { namespace shaders {
+  unsigned int init(unsigned int type, std::string const & path) { return 0; }
+}}

+ 22 - 0
graphics/src/shader.cpp

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

+ 3 - 0
graphics/src/texture.cpp

@@ -7,6 +7,8 @@
 
 #include "game/graphics/texture.hpp"
 
+#include <unordered_map>
+
 #include "scope_guard/scope_guard.hpp"
 
 #pragma clang diagnostic push
@@ -35,6 +37,7 @@ namespace graphics {
       throw;
     }
   }
+
   texture texture::create(std::string const & imagefile) {
     auto found = g_textures.find(imagefile);
     if (found != g_textures.end()) { return found->second; }