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(const ::Texture& texture) {
29  set(texture);
30  }
31 
37  Texture(const ::Image& image) {
38  LoadFromImage(image);
39  if (!IsReady()) {
40  throw RaylibException("Failed to load Texture from Image");
41  }
42  }
43 
51  Texture(const ::Image& image, int layout) {
52  LoadCubemap(image, layout);
53  if (!IsReady()) {
54  throw RaylibException("Failed to load Texture from Cubemap");
55  }
56  }
57 
63  Texture(const std::string& fileName) {
64  Load(fileName);
65  if (!IsReady()) {
66  throw RaylibException(TextFormat("Failed to load Texture from file: %s", fileName.c_str()));
67  }
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) {
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 LoadFromImage(const ::Image& image) {
127  set(::LoadTextureFromImage(image));
128  }
129 
133  void LoadCubemap(const ::Image& image, int layoutType) {
134  set(::LoadTextureCubemap(image, layoutType));
135  }
136 
140  void Load(const std::string& fileName) {
141  set(::LoadTexture(fileName.c_str()));
142  }
143 
147  inline void Unload() {
148  ::UnloadTexture(*this);
149  }
150 
154  inline Texture& Update(const void *pixels) {
155  ::UpdateTexture(*this, pixels);
156  return *this;
157  }
158 
162  inline Texture& Update(::Rectangle rec, const void *pixels) {
163  UpdateTextureRec(*this, rec, pixels);
164  return *this;
165  }
166 
170  inline ::Image GetData() const {
171  return ::LoadImageFromTexture(*this);
172  }
173 
177  inline operator raylib::Image() {
178  return GetData();
179  }
180 
184  inline Texture& GenMipmaps() {
185  ::GenTextureMipmaps(this);
186  return *this;
187  }
188 
192  inline Texture& SetFilter(int filterMode) {
193  ::SetTextureFilter(*this, filterMode);
194  return *this;
195  }
196 
200  inline Texture& SetWrap(int wrapMode) {
201  ::SetTextureWrap(*this, wrapMode);
202  return *this;
203  }
204 
208  inline Texture& Draw() {
209  return Draw(0, 0);
210  }
211 
215  inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) {
216  ::DrawTexture(*this, posX, posY, tint);
217  return *this;
218  }
219 
220  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
221  ::DrawTextureV(*this, position, tint);
222  return *this;
223  }
224 
225  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
226  ::Color tint = {255, 255, 255, 255}) {
227  ::DrawTextureEx(*this, position, rotation, scale, tint);
228  return *this;
229  }
230 
231  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
232  ::Color tint = {255, 255, 255, 255}) {
233  ::DrawTextureRec(*this, sourceRec, position, tint);
234  return *this;
235  }
236 
237  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
238  ::Color tint = {255, 255, 255, 255}) {
239  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
240  return *this;
241  }
242 
243  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
244  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
245  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
246  return *this;
247  }
248 
249  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
250  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
251  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
252  return *this;
253  }
254 
255  inline Texture& Draw(::Vector3 position, float width, float height, float length,
256  ::Color tint = {255, 255, 255, 255}) {
257  ::DrawCubeTexture(*this, position, width, height, length, tint);
258  return *this;
259  }
260 
261  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
262  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
263  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
264  return *this;
265  }
266 
267  inline Texture& DrawPoly(Vector2 center, Vector2 *points,
268  Vector2 *texcoords, int pointsCount,
269  Color tint = {255, 255, 255, 255}) {
270  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
271  return *this;
272  }
273 
277  inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
278  ::SetMaterialTexture(material, mapType, *this);
279  return *this;
280  }
281 
282  inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
283  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
284  return *this;
285  }
286 
290  inline Texture& SetShapes(const ::Rectangle& source) {
291  ::SetShapesTexture(*this, source);
292  return *this;
293  }
294 
298  inline Texture& SetShaderValue(const ::Shader& shader, int locIndex) {
299  ::SetShaderValueTexture(shader, locIndex, *this);
300  return *this;
301  }
302 
308  bool IsReady() {
309  return id != 0;
310  }
311 
312  private:
313  inline void set(const ::Texture& texture) {
314  id = texture.id;
315  width = texture.width;
316  height = texture.height;
317  mipmaps = texture.mipmaps;
318  format = texture.format;
319  }
320 };
321 
322 // Create the Texture aliases.
323 typedef Texture Texture2D;
324 typedef Texture TextureCubemap;
325 
326 } // namespace raylib
327 
328 #endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Texture::SetWrap
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:200
raylib::Texture::Draw
Texture & Draw(int posX, int posY, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:215
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:119
raylib::Texture::Texture
Texture(const ::Image &image)
Creates a texture from the given Image.
Definition: Texture.hpp:37
raylib::Texture::SetShapes
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:290
raylib::Texture::Load
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:140
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Texture::LoadFromImage
void LoadFromImage(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:126
raylib::Texture::Texture
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:63
raylib::Texture::Texture
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:51
raylib::Texture::SetFilter
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:192
raylib::Texture::LoadCubemap
void LoadCubemap(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:133
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Texture::GetData
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:170
raylib::Texture::Draw
Texture & Draw()
Draws the texture at the top left corner of the screen.
Definition: Texture.hpp:208
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:277
raylib::Texture
Texture type.
Definition: Texture.hpp:16
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:147
raylib::RaylibException
Exception used for most raylib-related exceptions.
Definition: RaylibException.hpp:13
raylib::Texture::IsReady
bool IsReady()
Determines whether or not the Texture has been loaded and is ready.
Definition: Texture.hpp:308
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:184
raylib::Texture::Texture
Texture(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
Definition: Texture.hpp:28
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:298
raylib::Texture::Texture
Texture()
Default constructor to create an empty Texture object.
Definition: Texture.hpp:21
raylib::Texture::Update
Texture & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:162
raylib::Texture::Update
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:154