raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Shader.hpp
1#ifndef RAYLIB_CPP_INCLUDE_SHADER_HPP_
2#define RAYLIB_CPP_INCLUDE_SHADER_HPP_
3
4#include <string>
5#include <string_view>
6#include <span>
7
8#include "./raylib.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "Texture.hpp"
11
12namespace raylib {
16class Shader : public ::Shader {
17 public:
18 Shader(const ::Shader& shader) {
19 set(shader);
20 }
21
22 Shader(unsigned int id, int* locs = nullptr) : ::Shader{id, locs} {}
23 Shader(unsigned int id, std::span<int> locs) : ::Shader{id, locs.data()} {}
24
25 Shader(const std::string_view vsFileName, const std::string_view fsFileName) {
26 set(::LoadShader(vsFileName.data(), fsFileName.data()));
27 }
28
29 Shader(const char* vsFileName, const char* fsFileName) {
30 set(::LoadShader(vsFileName, fsFileName));
31 }
32
33 Shader(const Shader&) = delete;
34
35 Shader(Shader&& other) {
36 set(other);
37
38 other.id = 0;
39 other.locs = nullptr;
40 }
41
47 static ::Shader Load(const std::string_view vsFileName, const std::string_view fsFileName) {
48 return ::LoadShader(vsFileName.data(), fsFileName.data());
49 }
50
51 static ::Shader Load(const char* vsFileName, const char* fsFileName) {
52 return ::LoadShader(vsFileName, fsFileName);
53 }
54
60 static ::Shader LoadFromMemory(const std::string_view vsCode, const std::string_view fsCode) {
61 return ::LoadShaderFromMemory(vsCode.data(), fsCode.data());
62 }
63
64 static ::Shader LoadFromMemory(const char* vsCode, const char* fsCode) {
65 return ::LoadShaderFromMemory(vsCode, fsCode);
66 }
67
68 GETTER(unsigned int, Id, id)
69 GETTER(int*, Locs, locs)
70
71 Shader& operator=(const ::Shader& shader) {
72 set(shader);
73 return *this;
74 }
75
76 Shader& operator=(const Shader&) = delete;
77
78 Shader& operator=(Shader&& other) noexcept {
79 if (this == &other) {
80 return *this;
81 }
82
83 Unload();
84 set(other);
85
86 other.id = 0;
87 other.locs = nullptr;
88
89 return *this;
90 }
91
96 Unload();
97 }
98
102 void Unload() {
103 if (locs != nullptr) {
104 ::UnloadShader(*this);
105 }
106 }
107
112 ::BeginShaderMode(*this);
113 return *this;
114 }
115
120 ::EndShaderMode();
121 return *this;
122 }
123
129 int GetLocation(const std::string_view uniformName) const {
130 return ::GetShaderLocation(*this, uniformName.data());
131 }
132
138 int GetLocationAttrib(const std::string_view attribName) const {
139 return ::GetShaderLocationAttrib(*this, attribName.data());
140 }
141
147 Shader& SetValue(int uniformLoc, const void* value, int uniformType) {
148 ::SetShaderValue(*this, uniformLoc, value, uniformType);
149 return *this;
150 }
151
158 inline Shader& SetValue(std::string_view uniformName, const void* value, int uniformType) {
159 ::SetShaderValue(*this, GetLocation(uniformName), value, uniformType);
160 return *this;
161 }
162
168 template<typename T>
169 inline Shader& SetValue(int uniformLoc, const T& value, int uniformType) {
170 ::SetShaderValue(*this, uniformLoc, &value, uniformType);
171 return *this;
172 }
173
180 template<typename T>
181 inline Shader& SetValue(std::string_view uniformName, const T& value, int uniformType) {
182 ::SetShaderValue(*this, GetLocation(uniformName), &value, uniformType);
183 return *this;
184 }
185
191 Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) {
192 ::SetShaderValueV(*this, uniformLoc, value, uniformType, count);
193 return *this;
194 }
195
202 inline Shader& SetValue(std::string_view uniformName, const void* value, int uniformType, int count) {
203 ::SetShaderValueV(*this, GetLocation(uniformName), value, uniformType, count);
204 return *this;
205 }
206
212 template<typename T>
213 inline Shader& SetValue(int uniformLoc, std::span<T> data, int uniformType) {
214 ::SetShaderValueV(*this, uniformLoc, data.value(), uniformType, data.count());
215 return *this;
216 }
217
224 template<typename T>
225 inline Shader& SetValue(std::string_view uniformName, std::span<T> data, int uniformType) {
226 ::SetShaderValueV(*this, GetLocation(uniformName), data.value(), uniformType, data.count());
227 return *this;
228 }
229
235 Shader& SetValue(int uniformLoc, const ::Matrix& mat) {
236 ::SetShaderValueMatrix(*this, uniformLoc, mat);
237 return *this;
238 }
239
246 inline Shader& SetValue(std::string_view uniformName, const ::Matrix& mat) {
247 ::SetShaderValueMatrix(*this, GetLocation(uniformName), mat);
248 return *this;
249 }
250
256 Shader& SetValue(int uniformLoc, const ::Texture2D& texture) {
257 ::SetShaderValueTexture(*this, uniformLoc, texture);
258 return *this;
259 }
260
267 inline Shader& SetValue(std::string_view uniformName, const ::Texture2D& texture) {
268 ::SetShaderValueTexture(*this, GetLocation(uniformName), texture);
269 return *this;
270 }
271
275 bool IsReady() const {
276 return id != 0 && locs != nullptr;
277 }
278
279 protected:
280 void set(const ::Shader& shader) {
281 id = shader.id;
282 locs = shader.locs;
283 }
284};
285} // namespace raylib
286
287using RShader = raylib::Shader;
288
289#endif // RAYLIB_CPP_INCLUDE_SHADER_HPP_
Shader type (generic)
Definition: Shader.hpp:16
Shader & SetValue(std::string_view uniformName, const T &value, int uniformType)
Set shader uniform value Automatically finds the location of the uniform given its name.
Definition: Shader.hpp:181
Shader & SetValue(int uniformLoc, const ::Texture2D &texture)
Set shader uniform value for texture.
Definition: Shader.hpp:256
Shader & SetValue(std::string_view uniformName, const void *value, int uniformType)
Set shader uniform value Automatically finds the location of the uniform given its name.
Definition: Shader.hpp:158
int GetLocationAttrib(const std::string_view attribName) const
Get shader attribute location.
Definition: Shader.hpp:138
Shader & SetValue(int uniformLoc, std::span< T > data, int uniformType)
Set shader uniform value vector (represented as a std::span)
Definition: Shader.hpp:213
Shader & SetValue(std::string_view uniformName, const ::Texture2D &texture)
Set shader uniform value for texture Automatically finds the location of the uniform given its name.
Definition: Shader.hpp:267
Shader & SetValue(std::string_view uniformName, const ::Matrix &mat)
Set shader uniform value (matrix 4x4) Automatically finds the location of the uniform given its name.
Definition: Shader.hpp:246
void Unload()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:102
~Shader()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:95
Shader & BeginMode()
Begin custom shader drawing.
Definition: Shader.hpp:111
Shader & SetValue(int uniformLoc, const void *value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:147
Shader & SetValue(std::string_view uniformName, std::span< T > data, int uniformType)
Set shader uniform value vector (represented as a std::span) Automatically finds the location of the ...
Definition: Shader.hpp:225
Shader & EndMode()
End custom shader drawing (use default shader).
Definition: Shader.hpp:119
Shader & SetValue(int uniformLoc, const ::Matrix &mat)
Set shader uniform value (matrix 4x4)
Definition: Shader.hpp:235
Shader & SetValue(std::string_view uniformName, const void *value, int uniformType, int count)
Set shader uniform value vector Automatically finds the location of the uniform given its name.
Definition: Shader.hpp:202
::Shader Load(const std::string_view vsFileName, const std::string_view fsFileName)
Load shader from files and bind default locations.
Definition: Shader.hpp:47
bool IsReady() const
Retrieves whether or not the shader is ready.
Definition: Shader.hpp:275
int GetLocation(const std::string_view uniformName) const
Get shader uniform location.
Definition: Shader.hpp:129
Shader & SetValue(int uniformLoc, const void *value, int uniformType, int count)
Set shader uniform value vector.
Definition: Shader.hpp:191
::Shader LoadFromMemory(const std::string_view vsCode, const std::string_view fsCode)
Load a shader from memory.
Definition: Shader.hpp:60
Shader & SetValue(int uniformLoc, const T &value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:169