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() {
42  Unload();
43  }
44 
45  GETTERSETTER(unsigned int, Id, id)
46  GETTERSETTER(int, Width, width)
47  GETTERSETTER(int, Height, height)
48  GETTERSETTER(int, Mipmaps, mipmaps)
49  GETTERSETTER(int, Format, format)
50 
51  Texture& operator=(const ::Texture& texture) {
52  set(texture);
53  return *this;
54  }
55 
59  inline ::Vector2 GetSize() {
60  return {static_cast<float>(width), static_cast<float>(height)};
61  }
62 
66  void LoadFromImage(const ::Image& image) {
67  set(::LoadTextureFromImage(image));
68  }
69 
73  void LoadCubemap(const ::Image& image, int layoutType) {
74  set(::LoadTextureCubemap(image, layoutType));
75  }
76 
80  void Load(const std::string& fileName) {
81  set(::LoadTexture(fileName.c_str()));
82  }
83 
87  inline void Unload() {
88  ::UnloadTexture(*this);
89  }
90 
94  inline Texture& Update(const void *pixels) {
95  ::UpdateTexture(*this, pixels);
96  return *this;
97  }
98 
102  inline Texture& UpdateRec(::Rectangle rec, const void *pixels) {
103  UpdateTextureRec(*this, rec, pixels);
104  return *this;
105  }
106 
110  inline ::Image GetData() const {
111  return ::GetTextureData(*this);
112  }
113 
117  inline operator raylib::Image() {
118  return GetData();
119  }
120 
124  inline Texture& GenMipmaps() {
125  ::GenTextureMipmaps(this);
126  return *this;
127  }
128 
132  inline Texture& SetFilter(int filterMode) {
133  ::SetTextureFilter(*this, filterMode);
134  return *this;
135  }
136 
140  inline Texture& SetWrap(int wrapMode) {
141  ::SetTextureWrap(*this, wrapMode);
142  return *this;
143  }
144 
148  inline Texture& Draw() {
149  return Draw(0, 0);
150  }
151 
155  inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) {
156  ::DrawTexture(*this, posX, posY, tint);
157  return *this;
158  }
159 
160  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
161  ::DrawTextureV(*this, position, tint);
162  return *this;
163  }
164 
165  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
166  ::Color tint = {255, 255, 255, 255}) {
167  ::DrawTextureEx(*this, position, rotation, scale, tint);
168  return *this;
169  }
170 
171  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
172  ::Color tint = {255, 255, 255, 255}) {
173  ::DrawTextureRec(*this, sourceRec, position, tint);
174  return *this;
175  }
176 
177  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
178  ::Color tint = {255, 255, 255, 255}) {
179  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
180  return *this;
181  }
182 
183  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
184  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
185  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
186  return *this;
187  }
188 
189  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
190  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
191  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
192  return *this;
193  }
194 
195  inline Texture& Draw(::Vector3 position, float width, float height, float length,
196  ::Color tint = {255, 255, 255, 255}) {
197  ::DrawCubeTexture(*this, position, width, height, length, tint);
198  return *this;
199  }
200 
201  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
202  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
203  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
204  return *this;
205  }
206 
207  inline Texture& DrawPoly(Vector2 center, Vector2 *points,
208  Vector2 *texcoords, int pointsCount,
209  Color tint = {255, 255, 255, 255}) {
210  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
211  return *this;
212  }
213 
217  inline Texture& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
218  ::SetMaterialTexture(material, mapType, *this);
219  return *this;
220  }
221 
222  inline Texture& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
223  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
224  return *this;
225  }
226 
227  private:
228  inline void set(const ::Texture& texture) {
229  id = texture.id;
230  width = texture.width;
231  height = texture.height;
232  mipmaps = texture.mipmaps;
233  format = texture.format;
234  }
235 };
236 
237 // Create the Texture aliases.
238 typedef Texture Texture2D;
239 typedef Texture TextureCubemap;
240 
241 } // namespace raylib
242 
243 #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:140
raylib::Texture::Draw
Texture & Draw(int posX, int posY, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:155
raylib::Image
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:15
raylib::Texture::GetSize
inline ::Vector2 GetSize()
Retrieve the width and height of the texture.
Definition: Texture.hpp:59
raylib::Texture::Load
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:80
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Texture::LoadFromImage
void LoadFromImage(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:66
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:132
raylib::Texture::LoadCubemap
void LoadCubemap(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:73
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:110
raylib::Texture::Draw
Texture & Draw()
Draws the texture at the top left corner of the screen.
Definition: Texture.hpp:148
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:217
raylib::Texture::UpdateRec
Texture & UpdateRec(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:102
raylib::Texture
Texture type.
Definition: Texture.hpp:15
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:87
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:124
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:94