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(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 
83  ~Texture() {
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) {
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.
320 typedef Texture Texture2D;
321 typedef Texture TextureCubemap;
322 
323 } // namespace raylib
324 
325 #endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Texture::SetWrap
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:204
raylib::Texture::Load
bool Load(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:127
raylib::Image
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:16
raylib::Texture::GetSize
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
Definition: Texture.hpp:120
raylib::Texture::Texture
Texture(const ::Image &image)
Creates a texture from the given Image.
Definition: Texture.hpp:41
raylib::Texture::IsReady
bool IsReady() const
Determines whether or not the Texture has been loaded and is ready.
Definition: Texture.hpp:305
raylib::Texture::SetShapes
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:287
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Texture::Draw
Texture & Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:212
raylib::Texture::Texture
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:65
raylib::Texture::Texture
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:54
raylib::Texture::SetFilter
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:196
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Texture::Load
bool Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:143
raylib::Texture::GetData
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:174
raylib::Texture::Load
bool Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:135
raylib::Texture::SetMaterial
Texture & SetMaterial(::Material *material, int mapType=MATERIAL_MAP_NORMAL)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Texture.hpp:274
raylib::Texture
Texture type.
Definition: Texture.hpp:16
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:151
raylib::RaylibException
Exception used for most raylib-related exceptions.
Definition: RaylibException.hpp:13
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:188
raylib::Texture::Texture
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
raylib::Texture::Texture
Texture(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
Definition: Texture.hpp:32
raylib::Material
Material type (generic)
Definition: Material.hpp:14
raylib::Texture::SetShaderValue
Texture & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
Definition: Texture.hpp:295
raylib::Texture::Update
Texture & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:166
raylib::Texture::Update
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:158