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 
11 namespace raylib {
15 class Texture : public ::Texture {
16  public:
17  Texture(const ::Texture& texture) {
18  set(texture);
19  }
20 
21  Texture(const ::Image& image) {
22  LoadFromImage(image);
23  }
24 
30  Texture(const ::Image& image, int layout) {
31  LoadCubemap(image, layout);
32  }
33 
37  Texture(const std::string& fileName) {
38  Load(fileName);
39  }
40 
41  Texture(const Texture&) = delete;
42 
43  Texture(Texture&& other) {
44  set(other);
45 
46  other.id = 0;
47  other.width = 0;
48  other.height = 0;
49  other.mipmaps = 0;
50  other.format = 0;
51  }
52 
53  ~Texture() {
54  Unload();
55  }
56 
57  GETTERSETTER(unsigned int, Id, id)
58  GETTERSETTER(int, Width, width)
59  GETTERSETTER(int, Height, height)
60  GETTERSETTER(int, Mipmaps, mipmaps)
61  GETTERSETTER(int, Format, format)
62 
63  Texture& operator=(const ::Texture& texture) {
64  set(texture);
65  return *this;
66  }
67 
68  Texture& operator=(const Texture&) = delete;
69 
70  Texture& operator=(Texture&& other) {
71  if (this == &other) {
72  return *this;
73  }
74 
75  Unload();
76  set(other);
77 
78  other.id = 0;
79  other.width = 0;
80  other.height = 0;
81  other.mipmaps = 0;
82  other.format = 0;
83 
84  return *this;
85  }
86 
90  inline ::Vector2 GetSize() const {
91  return {static_cast<float>(width), static_cast<float>(height)};
92  }
93 
97  void LoadFromImage(const ::Image& image) {
98  set(::LoadTextureFromImage(image));
99  }
100 
104  void LoadCubemap(const ::Image& image, int layoutType) {
105  set(::LoadTextureCubemap(image, layoutType));
106  }
107 
111  void Load(const std::string& fileName) {
112  set(::LoadTexture(fileName.c_str()));
113  }
114 
118  inline void Unload() {
119  ::UnloadTexture(*this);
120  }
121 
125  inline Texture& Update(const void *pixels) {
126  ::UpdateTexture(*this, pixels);
127  return *this;
128  }
129 
133  inline Texture& UpdateRec(::Rectangle rec, const void *pixels) {
134  UpdateTextureRec(*this, rec, pixels);
135  return *this;
136  }
137 
141  inline ::Image GetData() const {
142  return ::LoadImageFromTexture(*this);
143  }
144 
148  inline operator raylib::Image() {
149  return GetData();
150  }
151 
155  inline Texture& GenMipmaps() {
156  ::GenTextureMipmaps(this);
157  return *this;
158  }
159 
163  inline Texture& SetFilter(int filterMode) {
164  ::SetTextureFilter(*this, filterMode);
165  return *this;
166  }
167 
171  inline Texture& SetWrap(int wrapMode) {
172  ::SetTextureWrap(*this, wrapMode);
173  return *this;
174  }
175 
179  inline Texture& Draw() {
180  return Draw(0, 0);
181  }
182 
186  inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) {
187  ::DrawTexture(*this, posX, posY, tint);
188  return *this;
189  }
190 
191  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
192  ::DrawTextureV(*this, position, tint);
193  return *this;
194  }
195 
196  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
197  ::Color tint = {255, 255, 255, 255}) {
198  ::DrawTextureEx(*this, position, rotation, scale, tint);
199  return *this;
200  }
201 
202  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
203  ::Color tint = {255, 255, 255, 255}) {
204  ::DrawTextureRec(*this, sourceRec, position, tint);
205  return *this;
206  }
207 
208  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
209  ::Color tint = {255, 255, 255, 255}) {
210  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
211  return *this;
212  }
213 
214  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
215  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
216  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
217  return *this;
218  }
219 
220  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
221  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
222  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
223  return *this;
224  }
225 
226  inline Texture& Draw(::Vector3 position, float width, float height, float length,
227  ::Color tint = {255, 255, 255, 255}) {
228  ::DrawCubeTexture(*this, position, width, height, length, tint);
229  return *this;
230  }
231 
232  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
233  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
234  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
235  return *this;
236  }
237 
238  inline Texture& DrawPoly(Vector2 center, Vector2 *points,
239  Vector2 *texcoords, int pointsCount,
240  Color tint = {255, 255, 255, 255}) {
241  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
242  return *this;
243  }
244 
248  inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
249  ::SetMaterialTexture(material, mapType, *this);
250  return *this;
251  }
252 
253  inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
254  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
255  return *this;
256  }
257 
261  inline Texture& SetShapes(const ::Rectangle& source) {
262  ::SetShapesTexture(*this, source);
263  return *this;
264  }
265 
266  private:
267  inline void set(const ::Texture& texture) {
268  id = texture.id;
269  width = texture.width;
270  height = texture.height;
271  mipmaps = texture.mipmaps;
272  format = texture.format;
273  }
274 };
275 
276 // Create the Texture aliases.
277 typedef Texture Texture2D;
278 typedef Texture TextureCubemap;
279 
280 } // namespace raylib
281 
282 #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:171
raylib::Texture::Draw
Texture & Draw(int posX, int posY, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:186
raylib::Image
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:15
raylib::Texture::GetSize
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
Definition: Texture.hpp:90
raylib::Texture::SetShapes
Texture & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
Definition: Texture.hpp:261
raylib::Texture::Load
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:111
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Texture::LoadFromImage
void LoadFromImage(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:97
raylib::Texture::Texture
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:37
raylib::Texture::Texture
Texture(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:30
raylib::Texture::SetFilter
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:163
raylib::Texture::LoadCubemap
void LoadCubemap(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:104
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:141
raylib::Texture::Draw
Texture & Draw()
Draws the texture at the top left corner of the screen.
Definition: Texture.hpp:179
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:248
raylib::Texture::UpdateRec
Texture & UpdateRec(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:133
raylib::Texture
Texture type.
Definition: Texture.hpp:15
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:118
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:155
raylib::Material
Material type (generic)
Definition: Material.hpp:14
raylib::Texture::Update
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:125