raylib-cpp
C++ object-oriented wrapper library for raylib.
Texture.hpp
1#ifndef RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
2#define RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
3
4#include <string>
5
6#include "./raylib.hpp"
7#include "./raylib-cpp-utils.hpp"
8#include "./Vector2.hpp"
9#include "./Material.hpp"
10#include "./RaylibException.hpp"
11
12namespace raylib {
16class Texture : public ::Texture {
17 public:
21 Texture(unsigned int id = 0,
22 int width = 0,
23 int height = 0,
24 int mipmaps = 0,
25 int format = 0) : ::Texture{id, width, height, mipmaps, format} {
26 // Nothing.
27 }
28
32 Texture(const ::Texture& texture) {
33 set(texture);
34 }
35
41 Texture(const ::Image& image) {
42 if (!Load(image)) {
43 throw RaylibException("Failed to load Texture from Image");
44 }
45 }
46
54 Texture(const ::Image& image, int layout) {
55 if (!Load(image, layout)) {
56 throw RaylibException("Failed to load Texture from Cubemap");
57 }
58 }
59
65 Texture(const std::string& fileName) {
66 if (!Load(fileName)) {
67 throw RaylibException(TextFormat("Failed to load Texture from file: %s", fileName.c_str()));
68 }
69 }
70
71 Texture(const Texture&) = delete;
72
73 Texture(Texture&& other) {
74 set(other);
75
76 other.id = 0;
77 other.width = 0;
78 other.height = 0;
79 other.mipmaps = 0;
80 other.format = 0;
81 }
82
84 Unload();
85 }
86
87 GETTERSETTER(unsigned int, Id, id)
88 GETTERSETTER(int, Width, width)
89 GETTERSETTER(int, Height, height)
90 GETTERSETTER(int, Mipmaps, mipmaps)
91 GETTERSETTER(int, Format, format)
92
93 Texture& operator=(const ::Texture& texture) {
94 set(texture);
95 return *this;
96 }
97
98 Texture& operator=(const Texture&) = delete;
99
100 Texture& operator=(Texture&& other) noexcept {
101 if (this == &other) {
102 return *this;
103 }
104
105 Unload();
106 set(other);
107
108 other.id = 0;
109 other.width = 0;
110 other.height = 0;
111 other.mipmaps = 0;
112 other.format = 0;
113
114 return *this;
115 }
116
120 inline ::Vector2 GetSize() const {
121 return {static_cast<float>(width), static_cast<float>(height)};
122 }
123
127 bool Load(const ::Image& image) {
128 set(::LoadTextureFromImage(image));
129 return IsReady();
130 }
131
135 bool Load(const ::Image& image, int layoutType) {
136 set(::LoadTextureCubemap(image, layoutType));
137 return IsReady();
138 }
139
143 bool Load(const std::string& fileName) {
144 set(::LoadTexture(fileName.c_str()));
145 return IsReady();
146 }
147
151 inline void Unload() {
152 ::UnloadTexture(*this);
153 }
154
158 inline Texture& Update(const void *pixels) {
159 ::UpdateTexture(*this, pixels);
160 return *this;
161 }
162
166 inline Texture& Update(::Rectangle rec, const void *pixels) {
167 UpdateTextureRec(*this, rec, pixels);
168 return *this;
169 }
170
174 inline ::Image GetData() const {
175 return ::LoadImageFromTexture(*this);
176 }
177
181 inline operator raylib::Image() {
182 return GetData();
183 }
184
188 inline Texture& GenMipmaps() {
189 ::GenTextureMipmaps(this);
190 return *this;
191 }
192
196 inline Texture& SetFilter(int filterMode) {
197 ::SetTextureFilter(*this, filterMode);
198 return *this;
199 }
200
204 inline Texture& SetWrap(int wrapMode) {
205 ::SetTextureWrap(*this, wrapMode);
206 return *this;
207 }
208
212 inline Texture& Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) {
213 ::DrawTexture(*this, posX, posY, tint);
214 return *this;
215 }
216
217 inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
218 ::DrawTextureV(*this, position, tint);
219 return *this;
220 }
221
222 inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
223 ::Color tint = {255, 255, 255, 255}) {
224 ::DrawTextureEx(*this, position, rotation, scale, tint);
225 return *this;
226 }
227
228 inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
229 ::Color tint = {255, 255, 255, 255}) {
230 ::DrawTextureRec(*this, sourceRec, position, tint);
231 return *this;
232 }
233
234 inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
235 ::Color tint = {255, 255, 255, 255}) {
236 ::DrawTextureQuad(*this, tiling, offset, quad, tint);
237 return *this;
238 }
239
240 inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
241 float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
242 ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
243 return *this;
244 }
245
246 inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
247 float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
248 ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
249 return *this;
250 }
251
252 inline Texture& Draw(::Vector3 position, float width, float height, float length,
253 ::Color tint = {255, 255, 255, 255}) {
254 ::DrawCubeTexture(*this, position, width, height, length, tint);
255 return *this;
256 }
257
258 inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
259 float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
260 ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
261 return *this;
262 }
263
264 inline Texture& DrawPoly(Vector2 center, Vector2 *points,
265 Vector2 *texcoords, int pointsCount,
266 Color tint = {255, 255, 255, 255}) {
267 ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
268 return *this;
269 }
270
274 inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
275 ::SetMaterialTexture(material, mapType, *this);
276 return *this;
277 }
278
279 inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
280 ::SetMaterialTexture((::Material*)(&material), mapType, *this);
281 return *this;
282 }
283
287 inline Texture& SetShapes(const ::Rectangle& source) {
288 ::SetShapesTexture(*this, source);
289 return *this;
290 }
291
295 inline Texture& SetShaderValue(const ::Shader& shader, int locIndex) {
296 ::SetShaderValueTexture(shader, locIndex, *this);
297 return *this;
298 }
299
305 bool IsReady() const {
306 return id != 0;
307 }
308
309 private:
310 inline void set(const ::Texture& texture) {
311 id = texture.id;
312 width = texture.width;
313 height = texture.height;
314 mipmaps = texture.mipmaps;
315 format = texture.format;
316 }
317};
318
319// Create the Texture aliases.
322
323} // namespace raylib
324
325#endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:16
Exception used for most raylib-related exceptions.
Texture type.
Definition: Texture.hpp:16
Texture & Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:212
Texture & operator=(Texture &&other) noexcept
Definition: Texture.hpp:100
Texture(unsigned int id=0, int width=0, int height=0, int mipmaps=0, int format=0)
Default constructor to create an empty Texture object.
Definition: Texture.hpp:21
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:151
bool Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:143
Texture & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:166
Texture(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
Definition: Texture.hpp:32
Texture & Draw(::Vector2 position, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:217
Texture & DrawPoly(Vector2 center, Vector2 *points, Vector2 *texcoords, int pointsCount, Color tint={255, 255, 255, 255})
Definition: Texture.hpp:264
Texture & Draw(::Rectangle sourceRec, ::Vector2 position={0, 0}, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:228
Texture & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
Definition: Texture.hpp:295
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
Definition: Texture.hpp:120
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:174
Texture(const ::Image &image)
Creates a texture from the given Image.
Definition: Texture.hpp:41
bool Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:135
Texture(const Texture &)=delete
Texture & DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, float scale=1, Color tint={255, 255, 255, 255})
Definition: Texture.hpp:258
bool IsReady() const
Determines whether or not the Texture has been loaded and is ready.
Definition: Texture.hpp:305
Texture(Texture &&other)
Definition: Texture.hpp:73
Texture & Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:246
Texture & operator=(const Texture &)=delete
Texture & SetMaterial(const ::Material &material, int mapType=MATERIAL_MAP_NORMAL)
Definition: Texture.hpp:279
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:188
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:54
Texture & SetMaterial(::Material *material, int mapType=MATERIAL_MAP_NORMAL)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Texture.hpp:274
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:204
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:65
Texture & Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:240
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:287
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:158
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:196
Texture & Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:234
Texture & Draw(::Vector2 position, float rotation, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:222
Texture & Draw(::Vector3 position, float width, float height, float length, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:252
bool Load(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:127
Vector2 type.
Definition: Vector2.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
Texture TextureCubemap
Definition: Texture.hpp:321
Texture Texture2D
Definition: Texture.hpp:320