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 "ShaderUnmanaged.hpp"
9#include "Texture.hpp"
10#include "./raylib-cpp-utils.hpp"
11#include "./raylib.hpp"
12
13namespace raylib {
17class Shader : public ShaderUnmanaged {
18public:
19 using ShaderUnmanaged::ShaderUnmanaged;
20
21 Shader(const std::string_view vsFileName, const std::string_view fsFileName) {
22 set(::LoadShader(vsFileName.data(), fsFileName.data()));
23 }
24
25 Shader(const char* vsFileName, const char* fsFileName) {
26 set(::LoadShader(vsFileName, fsFileName));
27 }
28
29 Shader(const Shader&) = delete;
30
31 Shader(Shader&& other) noexcept {
32 set(other);
33
34 other.id = 0;
35 other.locs = nullptr;
36 }
37
43 static ::Shader Load(const std::string_view vsFileName, const std::string_view fsFileName) {
44 return ::LoadShader(vsFileName.data(), fsFileName.data());
45 }
46
47 static ::Shader Load(const char* vsFileName, const char* fsFileName) {
48 return ::LoadShader(vsFileName, fsFileName);
49 }
50
56 static ::Shader LoadFromMemory(const std::string_view vsCode, const std::string_view fsCode) {
57 return ::LoadShaderFromMemory(vsCode.data(), fsCode.data());
58 }
59
60 static ::Shader LoadFromMemory(const char* vsCode, const char* fsCode) {
61 return ::LoadShaderFromMemory(vsCode, fsCode);
62 }
63
64 GETTER(unsigned int, Id, id)
65 GETTER(int*, Locs, locs)
66
67 Shader& operator=(const ::Shader& shader) {
68 set(shader);
69 return *this;
70 }
71
72 Shader& operator=(const Shader&) = delete;
73
74 Shader& operator=(Shader&& other) noexcept {
75 if (this == &other) {
76 return *this;
77 }
78
79 Unload();
80 set(other);
81
82 other.id = 0;
83 other.locs = nullptr;
84
85 return *this;
86 }
87
91 ~Shader() { Unload(); }
92
96 void Unload() {
97 if (locs != nullptr) {
98 ::UnloadShader(*this);
99 }
100 }
101
106 ::BeginShaderMode(*this);
107 return *this;
108 }
109
114 ::EndShaderMode();
115 return *this;
116 }
117
123 int GetLocation(const std::string_view uniformName) const {
124 return ::GetShaderLocation(*this, uniformName.data());
125 }
126
132 int GetLocationAttrib(const std::string_view attribName) const {
133 return ::GetShaderLocationAttrib(*this, attribName.data());
134 }
135
141 Shader& SetValue(int uniformLoc, const void* value, int uniformType) {
142 ::SetShaderValue(*this, uniformLoc, value, uniformType);
143 return *this;
144 }
145
152 inline Shader& SetValue(std::string_view uniformName, const void* value, int uniformType) {
153 ::SetShaderValue(*this, GetLocation(uniformName), value, uniformType);
154 return *this;
155 }
156
162 template<typename T>
163 inline Shader& SetValue(int uniformLoc, const T& value, int uniformType) {
164 ::SetShaderValue(*this, uniformLoc, &value, uniformType);
165 return *this;
166 }
167
174 template<typename T>
175 inline Shader& SetValue(std::string_view uniformName, const T& value, int uniformType) {
176 ::SetShaderValue(*this, GetLocation(uniformName), &value, uniformType);
177 return *this;
178 }
179
185 Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) {
186 ::SetShaderValueV(*this, uniformLoc, value, uniformType, count);
187 return *this;
188 }
189
196 inline Shader& SetValue(std::string_view uniformName, const void* value, int uniformType, int count) {
197 ::SetShaderValueV(*this, GetLocation(uniformName), value, uniformType, count);
198 return *this;
199 }
200
206 template<typename T>
207 inline Shader& SetValue(int uniformLoc, std::span<T> data, int uniformType) {
208 ::SetShaderValueV(*this, uniformLoc, data.value(), uniformType, data.count());
209 return *this;
210 }
211
218 template<typename T>
219 inline Shader& SetValue(std::string_view uniformName, std::span<T> data, int uniformType) {
220 ::SetShaderValueV(*this, GetLocation(uniformName), data.value(), uniformType, data.count());
221 return *this;
222 }
223
229 Shader& SetValue(int uniformLoc, const ::Matrix& mat) {
230 ::SetShaderValueMatrix(*this, uniformLoc, mat);
231 return *this;
232 }
233
240 inline Shader& SetValue(std::string_view uniformName, const ::Matrix& mat) {
241 ::SetShaderValueMatrix(*this, GetLocation(uniformName), mat);
242 return *this;
243 }
244
250 Shader& SetValue(int uniformLoc, const ::Texture2D& texture) {
251 ::SetShaderValueTexture(*this, uniformLoc, texture);
252 return *this;
253 }
254
261 inline Shader& SetValue(std::string_view uniformName, const ::Texture2D& texture) {
262 ::SetShaderValueTexture(*this, GetLocation(uniformName), texture);
263 return *this;
264 }
265
269 bool IsValid() const {
270 return id != 0 && locs != nullptr;
271 }
272
273 protected:
274 void set(const ::Shader& shader) {
275 id = shader.id;
276 locs = shader.locs;
277 }
278};
279} // namespace raylib
280
281using RShader = raylib::Shader;
282
283#endif // RAYLIB_CPP_INCLUDE_SHADER_HPP_
Shader type (generic)
Definition: Shader.hpp:17
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:175
Shader & SetValue(int uniformLoc, const ::Texture2D &texture)
Set shader uniform value for texture.
Definition: Shader.hpp:250
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:152
int GetLocationAttrib(const std::string_view attribName) const
Get shader attribute location.
Definition: Shader.hpp:132
Shader & SetValue(int uniformLoc, std::span< T > data, int uniformType)
Set shader uniform value vector (represented as a std::span)
Definition: Shader.hpp:207
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:261
bool IsValid() const
Retrieves whether or not the shader is ready.
Definition: Shader.hpp:269
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:240
void Unload()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:96
~Shader()
Unload shader from GPU memory (VRAM)
Definition: Shader.hpp:91
Shader & BeginMode()
Begin custom shader drawing.
Definition: Shader.hpp:105
Shader & SetValue(int uniformLoc, const void *value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:141
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:219
Shader & EndMode()
End custom shader drawing (use default shader).
Definition: Shader.hpp:113
Shader & SetValue(int uniformLoc, const ::Matrix &mat)
Set shader uniform value (matrix 4x4)
Definition: Shader.hpp:229
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:196
::Shader Load(const std::string_view vsFileName, const std::string_view fsFileName)
Load shader from files and bind default locations.
Definition: Shader.hpp:43
int GetLocation(const std::string_view uniformName) const
Get shader uniform location.
Definition: Shader.hpp:123
Shader & SetValue(int uniformLoc, const void *value, int uniformType, int count)
Set shader uniform value vector.
Definition: Shader.hpp:185
::Shader LoadFromMemory(const std::string_view vsCode, const std::string_view fsCode)
Load a shader from memory.
Definition: Shader.hpp:56
Shader & SetValue(int uniformLoc, const T &value, int uniformType)
Set shader uniform value.
Definition: Shader.hpp:163
Shader type (generic), not managed by C++ RAII.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8