raylib-cpp
C++ object-oriented wrapper library for raylib.
Shader.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_SHADER_HPP_
2 #define RAYLIB_CPP_INCLUDE_SHADER_HPP_
3 
4 #include <string>
5 
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 #include "Texture.hpp"
9 
10 namespace raylib {
14 class Shader : public ::Shader {
15  public:
16  Shader(const ::Shader& shader) {
17  set(shader);
18  }
19 
20  Shader(unsigned int id, int* locs = nullptr) : ::Shader{id, locs} {}
21 
22  Shader(const std::string& vsFileName, const std::string& fsFileName) {
23  set(::LoadShader(vsFileName.c_str(), fsFileName.c_str()));
24  }
25 
26  Shader(const char* vsFileName, const char* fsFileName) {
27  set(::LoadShader(vsFileName, fsFileName));
28  }
29 
30  Shader(const Shader&) = delete;
31 
32  Shader(Shader&& other) {
33  set(other);
34 
35  other.id = 0;
36  other.locs = nullptr;
37  }
38 
44  static ::Shader Load(const std::string& vsFileName, const std::string& fsFileName) {
45  return ::LoadShader(vsFileName.c_str(), fsFileName.c_str());
46  }
47 
53  static ::Shader LoadFromMemory(const std::string& vsCode, const std::string& fsCode) {
54  return ::LoadShaderFromMemory(vsCode.c_str(), fsCode.c_str());
55  }
56 
57  GETTERSETTER(unsigned int, Id, id)
58  GETTERSETTER(int*, Locs, locs)
59 
60  Shader& operator=(const ::Shader& shader) {
61  set(shader);
62  return *this;
63  }
64 
65  Shader& operator=(const Shader&) = delete;
66 
67  Shader& operator=(Shader&& other) {
68  if (this != &other) {
69  return *this;
70  }
71 
72  Unload();
73  set(other);
74 
75  other.id = 0;
76  other.locs = nullptr;
77 
78  return *this;
79  }
80 
81  ~Shader() {
82  Unload();
83  }
84 
85  void Unload() {
86  if (locs != nullptr) {
87  ::UnloadShader(*this);
88  }
89  }
90 
94  inline Shader& BeginMode() {
95  ::BeginShaderMode(*this);
96  return *this;
97  }
98 
102  inline Shader& EndMode() {
103  ::EndShaderMode();
104  return *this;
105  }
106 
112  inline int GetLocation(const std::string& uniformName) const {
113  return ::GetShaderLocation(*this, uniformName.c_str());
114  }
115 
121  inline int GetLocationAttrib(const std::string& attribName) const {
122  return ::GetShaderLocationAttrib(*this, attribName.c_str());
123  }
124 
130  inline Shader& SetValue(int uniformLoc, const void* value, int uniformType) {
131  ::SetShaderValue(*this, uniformLoc, value, uniformType);
132  return *this;
133  }
134 
140  inline Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) {
141  ::SetShaderValueV(*this, uniformLoc, value, uniformType, count);
142  return *this;
143  }
144 
150  inline Shader& SetValue(int uniformLoc, const ::Matrix& mat) {
151  ::SetShaderValueMatrix(*this, uniformLoc, mat);
152  return *this;
153  }
154 
160  inline Shader& SetValue(int uniformLoc, const ::Texture2D& texture) {
161  ::SetShaderValueTexture(*this, uniformLoc, texture);
162  return *this;
163  }
164 
168  bool IsReady() const {
169  return id != 0 && locs != nullptr;
170  }
171 
172  private:
173  inline void set(const ::Shader& shader) {
174  id = shader.id;
175  locs = shader.locs;
176  }
177 };
178 } // namespace raylib
179 
180 #endif // RAYLIB_CPP_INCLUDE_SHADER_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Shader::SetValue
Shader & SetValue(int uniformLoc, const void *value, int uniformType, int count)
Set shader uniform value vector.
Definition: Shader.hpp:140
raylib::Shader::LoadFromMemory
::Shader LoadFromMemory(const std::string &vsCode, const std::string &fsCode)
Load a shader from memory.
Definition: Shader.hpp:53
raylib::Shader::SetValue
Shader & SetValue(int uniformLoc, const ::Matrix &mat)
Set shader uniform value (matrix 4x4)
Definition: Shader.hpp:150
raylib::Shader::GetLocation
int GetLocation(const std::string &uniformName) const
Get shader uniform location.
Definition: Shader.hpp:112
raylib::Shader::EndMode
Shader & EndMode()
End custom shader drawing (use default shader).
Definition: Shader.hpp:102
raylib::Shader
Shader type (generic)
Definition: Shader.hpp:14
raylib::Shader::BeginMode
Shader & BeginMode()
Begin custom shader drawing.
Definition: Shader.hpp:94
raylib::Shader::GetLocationAttrib
int GetLocationAttrib(const std::string &attribName) const
Get shader attribute location.
Definition: Shader.hpp:121
raylib::Shader::Load
::Shader Load(const std::string &vsFileName, const std::string &fsFileName)
Load shader from files and bind default locations.
Definition: Shader.hpp:44
raylib::Shader::SetValue
Shader & SetValue(int uniformLoc, const ::Texture2D &texture)
Set shader uniform value for texture.
Definition: Shader.hpp:160
raylib::Shader::SetValue
Shader & SetValue(int uniformLoc, const void *value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:130
raylib::Shader::IsReady
bool IsReady() const
Retrieves whether or not the shader is ready.
Definition: Shader.hpp:168