Simulant  21.12-1303
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
material_property_overrider.h
1 #pragma once
2 
3 #include <unordered_map>
4 #include <list>
5 #include <string>
6 
7 #include "../../../generic/containers/contiguous_map.h"
8 #include "../property_value.h"
9 #include "core_material.h"
10 #include "material_value_pool.h"
11 
12 namespace smlt {
13 
14 /* All materials and passes inherit the properties and
15  * values of the core material. Overriders allow two things:
16  *
17  * 1. Overriding the value of the core material
18  * 2. Adding additional property values (e.g. shader uniforms)
19  */
20 
21 bool valid_name(const char* name);
22 
24 public:
25  MaterialPropertyOverrider() = default;
27  parent_(parent) {}
28 
29 public:
30  virtual bool set_property_value(MaterialPropertyNameHash hsh,
31  const char* name, const bool& value) = 0;
32  virtual bool set_property_value(MaterialPropertyNameHash hsh,
33  const char* name, const float& value) = 0;
34  virtual bool set_property_value(MaterialPropertyNameHash hsh,
35  const char* name, const int32_t& value) = 0;
36  virtual bool set_property_value(MaterialPropertyNameHash hsh,
37  const char* name, const Vec2& value) = 0;
38  virtual bool set_property_value(MaterialPropertyNameHash hsh,
39  const char* name, const Vec3& value) = 0;
40  virtual bool set_property_value(MaterialPropertyNameHash hsh,
41  const char* name, const Vec4& value) = 0;
42  virtual bool set_property_value(MaterialPropertyNameHash hsh,
43  const char* name, const Mat3& value) = 0;
44  virtual bool set_property_value(MaterialPropertyNameHash hsh,
45  const char* name, const Mat4& value) = 0;
46  virtual bool set_property_value(MaterialPropertyNameHash hsh,
47  const char* name,
48  const TexturePtr& value) = 0;
49 
50  virtual bool property_value(const MaterialPropertyNameHash hsh,
51  const bool*& out) const = 0;
52  virtual bool property_value(const MaterialPropertyNameHash hsh,
53  const float*& out) const = 0;
54  virtual bool property_value(const MaterialPropertyNameHash hsh,
55  const int32_t*& out) const = 0;
56  virtual bool property_value(const MaterialPropertyNameHash hsh,
57  const Vec2*& out) const = 0;
58  virtual bool property_value(const MaterialPropertyNameHash hsh,
59  const Vec3*& out) const = 0;
60  virtual bool property_value(const MaterialPropertyNameHash hsh,
61  const Vec4*& out) const = 0;
62  virtual bool property_value(const MaterialPropertyNameHash hsh,
63  const Mat3*& out) const = 0;
64  virtual bool property_value(const MaterialPropertyNameHash hsh,
65  const Mat4*& out) const = 0;
66  virtual bool property_value(const MaterialPropertyNameHash hsh,
67  const TexturePtr*& out) const = 0;
68 
69  virtual bool set_property_value(MaterialPropertyNameHash hsh,
70  const char* name, const Color& value) {
71  return set_property_value(
72  hsh, name,
73  reinterpret_cast<const Vec4&>(
74  value)); // FIXME: dirty cast, add to_vec4() to Color
75  }
76 
77  virtual bool property_value(const MaterialPropertyNameHash hsh,
78  const Color*& out) const {
79  return property_value(
80  hsh, reinterpret_cast<const Vec4*&>(out)); // FIXME: dirty cast
81  }
82 
83  /* Helpers for std::string */
84  template<typename T>
85  void set_property_value(const std::string& str, const T& v) {
86  MaterialPropertyNameHash hsh = material_property_hash(str.c_str());
87  set_property_value(hsh, str.c_str(), v);
88  }
89 
90  template<typename T>
91  void set_property_value(const char* name, const T& v) {
92  MaterialPropertyNameHash hsh = material_property_hash(name);
93  set_property_value(hsh, name, v);
94  }
95 
96  template<typename T>
97  bool property_value(const std::string& str, const T*& out) const {
98  return property_value(str.c_str(), out);
99  }
100 
101  template<typename T>
102  bool property_value(const char* name, const T*& out) const {
103  auto hsh = material_property_hash(name);
104  return property_value(hsh, out);
105  }
106 
107  bool clear_override(const char* name) {
108  return on_clear_override(material_property_hash(name));
109  }
110 
111  bool clear_override(MaterialPropertyNameHash hsh) {
112  return on_clear_override(hsh);
113  }
114 
115  bool check_existance(const char* property_name) const {
116  return on_check_existence(material_property_hash(property_name));
117  }
118 
119  virtual bool property_type(const char* property_name,
120  MaterialPropertyType* type) const = 0;
121 
122  virtual bool on_check_existence(MaterialPropertyNameHash hsh) const = 0;
123 
124 protected:
125  virtual void on_override(MaterialPropertyNameHash hsh, const char* name,
126  MaterialPropertyType type) {
127  _S_UNUSED(hsh);
128  _S_UNUSED(name);
129  _S_UNUSED(type);
130  }
131 
132  virtual bool on_clear_override(MaterialPropertyNameHash hsh) = 0;
133  const MaterialPropertyOverrider* parent_ = nullptr;
134 };
135 
136 } // namespace smlt
smlt::Mat4
Definition: mat4.h:35
smlt::Vec3
Definition: vec3.h:25
smlt::Mat3
Definition: mat3.h:14
smlt
Definition: animation.cpp:25
smlt::Color
Definition: color.h:32
smlt::MaterialPropertyOverrider
Definition: material_property_overrider.h:23
smlt::Vec4
Definition: vec4.h:15
smlt::Vec2
Definition: vec2.h:16