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) noexcept {
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 
84  ~Shader() {
85  Unload();
86  }
87 
91  void Unload() {
92  if (locs != nullptr) {
93  ::UnloadShader(*this);
94  }
95  }
96 
100  inline Shader& BeginMode() {
101  ::BeginShaderMode(*this);
102  return *this;
103  }
104 
108  inline Shader& EndMode() {
109  ::EndShaderMode();
110  return *this;
111  }
112 
118  inline int GetLocation(const std::string& uniformName) const {
119  return ::GetShaderLocation(*this, uniformName.c_str());
120  }
121 
127  inline int GetLocationAttrib(const std::string& attribName) const {
128  return ::GetShaderLocationAttrib(*this, attribName.c_str());
129  }
130 
136  inline Shader& SetValue(int uniformLoc, const void* value, int uniformType) {
137  ::SetShaderValue(*this, uniformLoc, value, uniformType);
138  return *this;
139  }
140 
146  inline Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) {
147  ::SetShaderValueV(*this, uniformLoc, value, uniformType, count);
148  return *this;
149  }
150 
156  inline Shader& SetValue(int uniformLoc, const ::Matrix& mat) {
157  ::SetShaderValueMatrix(*this, uniformLoc, mat);
158  return *this;
159  }
160 
166  inline Shader& SetValue(int uniformLoc, const ::Texture2D& texture) {
167  ::SetShaderValueTexture(*this, uniformLoc, texture);
168  return *this;
169  }
170 
174  bool IsReady() const {
175  return id != 0 && locs != nullptr;
176  }
177 
178  private:
179  void set(const ::Shader& shader) {
180  id = shader.id;
181  locs = shader.locs;
182  }
183 };
184 } // namespace raylib
185 
186 using RShader = raylib::Shader;
187 
188 #endif // RAYLIB_CPP_INCLUDE_SHADER_HPP_
Shader type (generic)
Definition: Shader.hpp:14
Shader & SetValue(int uniformLoc, const void *value, int uniformType, int count)
Set shader uniform value vector.
Definition: Shader.hpp:146
Shader & EndMode()
End custom shader drawing (use default shader).
Definition: Shader.hpp:108
void Unload()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:91
~Shader()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:84
Shader & BeginMode()
Begin custom shader drawing.
Definition: Shader.hpp:100
::Shader Load(const std::string &vsFileName, const std::string &fsFileName)
Load shader from files and bind default locations.
Definition: Shader.hpp:44
Shader & SetValue(int uniformLoc, const ::Texture2D &texture)
Set shader uniform value for texture.
Definition: Shader.hpp:166
::Shader LoadFromMemory(const std::string &vsCode, const std::string &fsCode)
Load a shader from memory.
Definition: Shader.hpp:53
int GetLocation(const std::string &uniformName) const
Get shader uniform location.
Definition: Shader.hpp:118
int GetLocationAttrib(const std::string &attribName) const
Get shader attribute location.
Definition: Shader.hpp:127
bool IsReady() const
Retrieves whether or not the shader is ready.
Definition: Shader.hpp:174
Shader & SetValue(int uniformLoc, const ::Matrix &mat)
Set shader uniform value (matrix 4x4)
Definition: Shader.hpp:156
Shader & SetValue(int uniformLoc, const void *value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:136
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8