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, int width, int height,
29  int mipmaps = 1, int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
30  : ::Texture{id, width, height, mipmaps, format} {
31  // Nothing.
32  }
33 
37  Texture(const ::Texture& texture) {
38  set(texture);
39  }
40 
46  Texture(const ::Image& image) {
47  Load(image);
48  }
49 
57  Texture(const ::Image& image, int layout) {
58  Load(image, layout);
59  }
60 
66  Texture(const std::string& fileName) {
67  Load(fileName);
68  }
69 
70  Texture(const Texture&) = delete;
71 
72  Texture(Texture&& other) {
73  set(other);
74 
75  other.id = 0;
76  other.width = 0;
77  other.height = 0;
78  other.mipmaps = 0;
79  other.format = 0;
80  }
81 
82  ~Texture() {
83  Unload();
84  }
85 
86  GETTERSETTER(unsigned int, Id, id)
87  GETTERSETTER(int, Width, width)
88  GETTERSETTER(int, Height, height)
89  GETTERSETTER(int, Mipmaps, mipmaps)
90  GETTERSETTER(int, Format, format)
91 
92  Texture& operator=(const ::Texture& texture) {
93  set(texture);
94  return *this;
95  }
96 
97  Texture& operator=(const Texture&) = delete;
98 
99  Texture& operator=(Texture&& other) noexcept {
100  if (this == &other) {
101  return *this;
102  }
103 
104  Unload();
105  set(other);
106 
107  other.id = 0;
108  other.width = 0;
109  other.height = 0;
110  other.mipmaps = 0;
111  other.format = 0;
112 
113  return *this;
114  }
115 
119  inline ::Vector2 GetSize() const {
120  return {static_cast<float>(width), static_cast<float>(height)};
121  }
122 
126  void Load(const ::Image& image) {
127  set(::LoadTextureFromImage(image));
128  if (!IsReady()) {
129  throw RaylibException("Failed to load Texture from Image");
130  }
131  }
132 
136  void Load(const ::Image& image, int layoutType) {
137  set(::LoadTextureCubemap(image, layoutType));
138  if (!IsReady()) {
139  throw RaylibException("Failed to load Texture from Cubemap");
140  }
141  }
142 
146  void Load(const std::string& fileName) {
147  set(::LoadTexture(fileName.c_str()));
148  if (!IsReady()) {
149  throw RaylibException("Failed to load Texture from file: " + fileName);
150  }
151  }
152 
156  inline void Unload() {
157  ::UnloadTexture(*this);
158  }
159 
163  inline Texture& Update(const void *pixels) {
164  ::UpdateTexture(*this, pixels);
165  return *this;
166  }
167 
171  inline Texture& Update(::Rectangle rec, const void *pixels) {
172  UpdateTextureRec(*this, rec, pixels);
173  return *this;
174  }
175 
179  inline ::Image GetData() const {
180  return ::LoadImageFromTexture(*this);
181  }
182 
186  inline operator raylib::Image() {
187  return GetData();
188  }
189 
193  inline Texture& GenMipmaps() {
194  ::GenTextureMipmaps(this);
195  return *this;
196  }
197 
201  inline Texture& SetFilter(int filterMode) {
202  ::SetTextureFilter(*this, filterMode);
203  return *this;
204  }
205 
209  inline Texture& SetWrap(int wrapMode) {
210  ::SetTextureWrap(*this, wrapMode);
211  return *this;
212  }
213 
217  inline Texture& Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) {
218  ::DrawTexture(*this, posX, posY, tint);
219  return *this;
220  }
221 
222  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
223  ::DrawTextureV(*this, position, tint);
224  return *this;
225  }
226 
227  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
228  ::Color tint = {255, 255, 255, 255}) {
229  ::DrawTextureEx(*this, position, rotation, scale, tint);
230  return *this;
231  }
232 
233  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
234  ::Color tint = {255, 255, 255, 255}) {
235  ::DrawTextureRec(*this, sourceRec, position, tint);
236  return *this;
237  }
238 
239  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
240  ::Color tint = {255, 255, 255, 255}) {
241  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
242  return *this;
243  }
244 
245  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
246  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
247  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
248  return *this;
249  }
250 
251  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
252  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
253  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
254  return *this;
255  }
256 
257  inline Texture& Draw(::Vector3 position, float width, float height, float length,
258  ::Color tint = {255, 255, 255, 255}) {
259  ::DrawCubeTexture(*this, position, width, height, length, tint);
260  return *this;
261  }
262 
263  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
264  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
265  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
266  return *this;
267  }
268 
269  inline Texture& DrawPoly(Vector2 center, Vector2 *points,
270  Vector2 *texcoords, int pointsCount,
271  Color tint = {255, 255, 255, 255}) {
272  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
273  return *this;
274  }
275 
279  inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
280  ::SetMaterialTexture(material, mapType, *this);
281  return *this;
282  }
283 
284  inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
285  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
286  return *this;
287  }
288 
292  inline Texture& SetShapes(const ::Rectangle& source) {
293  ::SetShapesTexture(*this, source);
294  return *this;
295  }
296 
300  inline Texture& SetShaderValue(const ::Shader& shader, int locIndex) {
301  ::SetShaderValueTexture(shader, locIndex, *this);
302  return *this;
303  }
304 
310  bool IsReady() const {
311  return id != 0;
312  }
313 
314  private:
315  void set(const ::Texture& texture) {
316  id = texture.id;
317  width = texture.width;
318  height = texture.height;
319  mipmaps = texture.mipmaps;
320  format = texture.format;
321  }
322 };
323 
324 // Create the Texture aliases.
325 typedef Texture Texture2D;
326 typedef Texture TextureCubemap;
327 
328 } // namespace raylib
329 using RTexture = raylib::Texture;
332 
333 #endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:16
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
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:156
void Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:136
Texture(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
Definition: Texture.hpp:37
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:209
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:126
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:201
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:193
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
Definition: Texture.hpp:119
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:179
Texture(const ::Image &image)
Creates a texture from the given Image.
Definition: Texture.hpp:46
Texture & Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:217
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:146
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:292
bool IsReady() const
Determines whether or not the Texture has been loaded and is ready.
Definition: Texture.hpp:310
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:279
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:57
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:66
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:163
Texture & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:171
Texture & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
Definition: Texture.hpp:300
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8