raylib-cpp
C++ object-oriented wrapper library for raylib.
Material.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
2#define RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
3
4#include <string>
5#include <vector>
6
7#include "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9
10namespace raylib {
14class Material : public ::Material {
15 public:
16 Material(const ::Material& material) {
17 set(material);
18 }
19
24 set(LoadMaterialDefault());
25 }
26
27 Material(const Material&) = delete;
28
29 Material(Material&& other) {
30 set(other);
31
32 other.maps = nullptr;
33 other.shader = {};
34 }
35
37 Unload();
38 }
39
43 static std::vector<Material> Load(const std::string& fileName) {
44 int count = 0;
45 // TODO(RobLoach): Material::Load() possibly leaks the materials array.
46 ::Material* materials = ::LoadMaterials(fileName.c_str(), &count);
47 return std::vector<Material>(materials, materials + count);
48 }
49
50 GETTERSETTER(::Shader, Shader, shader)
51 GETTERSETTER(::MaterialMap*, Maps, maps)
52 // TODO(RobLoach): Resolve the Material params being a float[4].
53 // GETTERSETTER(float[4], Params, params)
54
55 Material& operator=(const ::Material& material) {
56 set(material);
57 return *this;
58 }
59
60 Material& operator=(const Material&) = delete;
61
62 Material& operator=(Material&& other) noexcept {
63 if (this == &other) {
64 return *this;
65 }
66
67 Unload();
68 set(other);
69
70 other.maps = nullptr;
71 other.shader = {};
72
73 return *this;
74 }
75
79 inline void Unload() {
80 if (maps != nullptr) {
81 ::UnloadMaterial(*this);
82 maps = nullptr;
83 }
84 }
85
89 inline Material& SetTexture(int mapType, const ::Texture2D& texture) {
90 ::SetMaterialTexture(this, mapType, texture);
91 return *this;
92 }
93
97 inline const Material& DrawMesh(const ::Mesh& mesh, ::Matrix transform) const {
98 ::DrawMesh(mesh, *this, transform);
99 return *this;
100 }
101
105 inline const Material& DrawMesh(const ::Mesh& mesh, ::Matrix* transforms, int instances) const {
106 ::DrawMeshInstanced(mesh, *this, transforms, instances);
107 return *this;
108 }
109
110 private:
111 inline void set(const ::Material& material) {
112 shader = material.shader;
113 maps = material.maps;
114 params[0] = material.params[0];
115 params[1] = material.params[1];
116 params[2] = material.params[2];
117 params[3] = material.params[3];
118 }
119};
120} // namespace raylib
121
122#endif // RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
Material type (generic)
Definition: Material.hpp:14
static std::vector< Material > Load(const std::string &fileName)
Load materials from model file.
Definition: Material.hpp:43
const Material & DrawMesh(const ::Mesh &mesh, ::Matrix *transforms, int instances) const
Draw multiple mesh instances with material and different transforms.
Definition: Material.hpp:105
Material & SetTexture(int mapType, const ::Texture2D &texture)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Material.hpp:89
void Unload()
Unload material from memory.
Definition: Material.hpp:79
Material(const Material &)=delete
Material()
Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
Definition: Material.hpp:23
const Material & DrawMesh(const ::Mesh &mesh, ::Matrix transform) const
Draw a 3d mesh with material and transform.
Definition: Material.hpp:97
Material(const ::Material &material)
Definition: Material.hpp:16
Material & operator=(Material &&other) noexcept
Definition: Material.hpp:62
Material & operator=(const Material &)=delete
Material(Material &&other)
Definition: Material.hpp:29
Shader type (generic)
Definition: Shader.hpp:14
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8