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 
12 namespace raylib {
16 class Texture : public ::Texture {
17  public:
21  Texture() : ::Texture{0, 0, 0, 0, 0} {
22  // Nothing.
23  }
24 
28  Texture(unsigned int id,
29  int width, int height,
30  int mipmaps = 1,
31  int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
32  : ::Texture{id, width, height, mipmaps, format} {
33  // Nothing.
34  }
35 
39  Texture(const ::Texture& texture) {
40  set(texture);
41  }
42 
48  Texture(const ::Image& image) {
49  Load(image);
50  }
51 
59  Texture(const ::Image& image, int layout) {
60  Load(image, layout);
61  }
62 
68  Texture(const std::string& fileName) {
69  Load(fileName);
70  }
71 
72  Texture(const Texture&) = delete;
73 
74  Texture(Texture&& other) {
75  set(other);
76 
77  other.id = 0;
78  other.width = 0;
79  other.height = 0;
80  other.mipmaps = 0;
81  other.format = 0;
82  }
83 
84  ~Texture() {
85  Unload();
86  }
87 
88  GETTERSETTER(unsigned int, Id, id)
89  GETTERSETTER(int, Width, width)
90  GETTERSETTER(int, Height, height)
91  GETTERSETTER(int, Mipmaps, mipmaps)
92  GETTERSETTER(int, Format, format)
93 
94  Texture& operator=(const ::Texture& texture) {
95  set(texture);
96  return *this;
97  }
98 
99  Texture& operator=(const Texture&) = delete;
100 
101  Texture& operator=(Texture&& other) noexcept {
102  if (this == &other) {
103  return *this;
104  }
105 
106  Unload();
107  set(other);
108 
109  other.id = 0;
110  other.width = 0;
111  other.height = 0;
112  other.mipmaps = 0;
113  other.format = 0;
114 
115  return *this;
116  }
117 
121  inline ::Vector2 GetSize() const {
122  return {static_cast<float>(width), static_cast<float>(height)};
123  }
124 
128  void Load(const ::Image& image) {
129  set(::LoadTextureFromImage(image));
130  if (!IsReady()) {
131  throw RaylibException("Failed to load Texture from Image");
132  }
133  }
134 
138  void Load(const ::Image& image, int layoutType) {
139  set(::LoadTextureCubemap(image, layoutType));
140  if (!IsReady()) {
141  throw RaylibException("Failed to load Texture from Cubemap");
142  }
143  }
144 
148  void Load(const std::string& fileName) {
149  set(::LoadTexture(fileName.c_str()));
150  if (!IsReady()) {
151  throw RaylibException("Failed to load Texture from file: " + fileName);
152  }
153  }
154 
158  inline void Unload() {
159  ::UnloadTexture(*this);
160  }
161 
165  inline Texture& Update(const void *pixels) {
166  ::UpdateTexture(*this, pixels);
167  return *this;
168  }
169 
173  inline Texture& Update(::Rectangle rec, const void *pixels) {
174  UpdateTextureRec(*this, rec, pixels);
175  return *this;
176  }
177 
181  inline ::Image GetData() const {
182  return ::LoadImageFromTexture(*this);
183  }
184 
188  inline operator raylib::Image() {
189  return GetData();
190  }
191 
195  inline Texture& GenMipmaps() {
196  ::GenTextureMipmaps(this);
197  return *this;
198  }
199 
203  inline Texture& SetFilter(int filterMode) {
204  ::SetTextureFilter(*this, filterMode);
205  return *this;
206  }
207 
211  inline Texture& SetWrap(int wrapMode) {
212  ::SetTextureWrap(*this, wrapMode);
213  return *this;
214  }
215 
219  inline Texture& Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) {
220  ::DrawTexture(*this, posX, posY, tint);
221  return *this;
222  }
223 
224  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
225  ::DrawTextureV(*this, position, tint);
226  return *this;
227  }
228 
229  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
230  ::Color tint = {255, 255, 255, 255}) {
231  ::DrawTextureEx(*this, position, rotation, scale, tint);
232  return *this;
233  }
234 
235  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
236  ::Color tint = {255, 255, 255, 255}) {
237  ::DrawTextureRec(*this, sourceRec, position, tint);
238  return *this;
239  }
240 
241  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
242  ::Color tint = {255, 255, 255, 255}) {
243  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
244  return *this;
245  }
246 
247  inline Texture& Draw(::Vector3 position, float width, float height, float length,
248  ::Color tint = {255, 255, 255, 255}) {
249  ::DrawCubeTexture(*this, position, width, height, length, tint);
250  return *this;
251  }
252 
253  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
254  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
255  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
256  return *this;
257  }
258 
259  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
260  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
261  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
262  return *this;
263  }
264 
265  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
266  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
267  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
268  return *this;
269  }
270 
271  inline Texture& DrawPoly(::Vector2 center, ::Vector2 *points,
272  ::Vector2 *texcoords, int pointsCount,
273  ::Color tint = {255, 255, 255, 255}) {
274  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
275  return *this;
276  }
277 
281  inline Texture& DrawBillboard(const ::Camera& camera,
282  ::Vector3 position, float size,
283  ::Color tint = {255, 255, 255, 255}) {
284  ::DrawBillboard(camera, *this, position, size, tint);
285  return *this;
286  }
287 
291  inline Texture& DrawBillboard(const ::Camera& camera,
292  ::Rectangle source, ::Vector3 position, ::Vector2 size,
293  ::Color tint = {255, 255, 255, 255}) {
294  DrawBillboardRec(camera, *this, source, position, size, tint);
295  return *this;
296  }
297 
301  inline Texture& DrawBillboard(const ::Camera& camera,
302  ::Rectangle source, Vector3 position,
303  ::Vector3 up, Vector2 size, Vector2 origin, float rotation,
304  ::Color tint = {255, 255, 255, 255}) {
305  DrawBillboardPro(camera, *this, source, position, up, size, origin, rotation, tint);
306  return *this;
307  }
308 
312  inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
313  ::SetMaterialTexture(material, mapType, *this);
314  return *this;
315  }
316 
317  inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
318  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
319  return *this;
320  }
321 
325  inline Texture& SetShapes(const ::Rectangle& source) {
326  ::SetShapesTexture(*this, source);
327  return *this;
328  }
329 
333  inline Texture& SetShaderValue(const ::Shader& shader, int locIndex) {
334  ::SetShaderValueTexture(shader, locIndex, *this);
335  return *this;
336  }
337 
343  bool IsReady() const {
344  return id != 0;
345  }
346 
347  private:
348  void set(const ::Texture& texture) {
349  id = texture.id;
350  width = texture.width;
351  height = texture.height;
352  mipmaps = texture.mipmaps;
353  format = texture.format;
354  }
355 };
356 
357 // Create the Texture aliases.
358 typedef Texture Texture2D;
359 typedef Texture TextureCubemap;
360 
361 } // namespace raylib
362 
363 using RTexture = raylib::Texture;
366 
367 #endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:17
Material type (generic)
Definition: Material.hpp:14
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:12
Texture type.
Definition: Texture.hpp:16
Texture & DrawBillboard(const ::Camera &camera, ::Rectangle source, Vector3 position, ::Vector3 up, Vector2 size, Vector2 origin, float rotation, ::Color tint={255, 255, 255, 255})
Draw a billboard texture defined by source and rotation.
Definition: Texture.hpp:301
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:158
void Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:138
Texture(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
Definition: Texture.hpp:39
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:211
Texture(unsigned int id, int width, int height, int mipmaps=1, int format=PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
Move/Create a texture structure manually.
Definition: Texture.hpp:28
void Load(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:128
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:203
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:195
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
Definition: Texture.hpp:121
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:181
Texture(const ::Image &image)
Creates a texture from the given Image.
Definition: Texture.hpp:48
Texture & Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:219
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:148
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:325
bool IsReady() const
Determines whether or not the Texture has been loaded and is ready.
Definition: Texture.hpp:343
Texture()
Default texture constructor.
Definition: Texture.hpp:21
Texture & SetMaterial(::Material *material, int mapType=MATERIAL_MAP_NORMAL)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Texture.hpp:312
Texture & DrawBillboard(const ::Camera &camera, ::Vector3 position, float size, ::Color tint={255, 255, 255, 255})
Draw a billboard texture.
Definition: Texture.hpp:281
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:59
Texture & DrawBillboard(const ::Camera &camera, ::Rectangle source, ::Vector3 position, ::Vector2 size, ::Color tint={255, 255, 255, 255})
Draw a billboard texture defined by source.
Definition: Texture.hpp:291
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:68
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:165
Texture & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:173
Texture & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
Definition: Texture.hpp:333
Vector2 type.
Definition: Vector2.hpp:16
Vector3 type.
Definition: Vector3.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8