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() {
22  set(::GetTextureDefault());
23  }
24 
25  Texture(const ::Image& image) {
26  LoadFromImage(image);
27  }
28 
32  Texture(const std::string& fileName) {
33  Load(fileName);
34  }
35 
36  ~Texture() {
37  Unload();
38  }
39 
40  GETTERSETTER(unsigned int, Id, id)
41  GETTERSETTER(int, Width, width)
42  GETTERSETTER(int, Height, height)
43  GETTERSETTER(int, Mipmaps, mipmaps)
44  GETTERSETTER(int, Format, format)
45 
46  Texture& operator=(const ::Texture& texture) {
47  set(texture);
48  return *this;
49  }
50 
54  inline ::Vector2 GetSize() {
55  return {static_cast<float>(width), static_cast<float>(height)};
56  }
57 
61  void LoadFromImage(const ::Image& image) {
62  set(::LoadTextureFromImage(image));
63  }
64 
68  void LoadTextureCubemap(const ::Image& image, int layoutType) {
69  set(::LoadTextureCubemap(image, layoutType));
70  }
71 
75  void Load(const std::string& fileName) {
76  set(::LoadTexture(fileName.c_str()));
77  }
78 
82  inline void Unload() {
83  ::UnloadTexture(*this);
84  }
85 
89  inline Texture& Update(const void *pixels) {
90  ::UpdateTexture(*this, pixels);
91  return *this;
92  }
93 
97  inline Texture& UpdateRec(::Rectangle rec, const void *pixels) {
98  UpdateTextureRec(*this, rec, pixels);
99  return *this;
100  }
101 
105  inline ::Image GetData() const {
106  return ::GetTextureData(*this);
107  }
108 
112  inline operator raylib::Image() {
113  return GetData();
114  }
115 
119  inline Texture& GenMipmaps() {
120  ::GenTextureMipmaps(this);
121  return *this;
122  }
123 
127  inline Texture& SetFilter(int filterMode) {
128  ::SetTextureFilter(*this, filterMode);
129  return *this;
130  }
131 
135  inline Texture& SetWrap(int wrapMode) {
136  ::SetTextureWrap(*this, wrapMode);
137  return *this;
138  }
139 
143  inline Texture& Draw() {
144  return Draw(0, 0);
145  }
146 
150  inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) {
151  ::DrawTexture(*this, posX, posY, tint);
152  return *this;
153  }
154 
155  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
156  ::DrawTextureV(*this, position, tint);
157  return *this;
158  }
159 
160  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
161  ::Color tint = {255, 255, 255, 255}) {
162  ::DrawTextureEx(*this, position, rotation, scale, tint);
163  return *this;
164  }
165 
166  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
167  ::Color tint = {255, 255, 255, 255}) {
168  ::DrawTextureRec(*this, sourceRec, position, tint);
169  return *this;
170  }
171 
172  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
173  ::Color tint = {255, 255, 255, 255}) {
174  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
175  return *this;
176  }
177 
178  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
179  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
180  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
181  return *this;
182  }
183 
184  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
185  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
186  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
187  return *this;
188  }
189 
190  inline Texture& Draw(::Vector3 position, float width, float height, float length,
191  ::Color color = {255, 255, 255, 255}) {
192  ::DrawCubeTexture(*this, position, width, height, length, color);
193  return *this;
194  }
195 
196  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
197  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
198  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
199  return *this;
200  }
201 
205  inline Texture& SetMaterial(::Material *material, int mapType) {
206  ::SetMaterialTexture(material, mapType, *this);
207  return *this;
208  }
209 
213  static int GetPixelDataSize(int width, int height, int format) {
214  return ::GetPixelDataSize(width, height, format);
215  }
216 
220  inline Texture& SetShapes(::Rectangle source) {
221  ::SetShapesTexture(*this, source);
222  return *this;
223  }
224 
225  private:
226  inline void set(const ::Texture& texture) {
227  id = texture.id;
228  width = texture.width;
229  height = texture.height;
230  mipmaps = texture.mipmaps;
231  format = texture.format;
232  }
233 };
234 
235 // Create the Texture aliases.
236 typedef Texture Texture2D;
237 typedef Texture TextureCubemap;
238 
239 } // namespace raylib
240 
241 #endif // RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
raylib::Texture::GetPixelDataSize
static int GetPixelDataSize(int width, int height, int format)
Get pixel data size in bytes for certain format.
Definition: Texture.hpp:213
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:135
raylib::Texture::LoadTextureCubemap
void LoadTextureCubemap(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:68
raylib::Texture::Draw
Texture & Draw(int posX, int posY, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:150
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:54
raylib::Texture::SetShapes
Texture & SetShapes(::Rectangle source)
Define default texture used to draw shapes.
Definition: Texture.hpp:220
raylib::Texture::Load
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:75
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Texture::LoadFromImage
void LoadFromImage(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:61
raylib::Texture::Texture
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:32
raylib::Texture::SetFilter
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:127
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:105
raylib::Texture::Draw
Texture & Draw()
Draws the texture at the top left corner of the screen.
Definition: Texture.hpp:143
raylib::Texture::UpdateRec
Texture & UpdateRec(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:97
raylib::Texture
Texture type.
Definition: Texture.hpp:15
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:82
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:119
raylib::Material
Material type (generic)
Definition: Material.hpp:14
raylib::Texture::SetMaterial
Texture & SetMaterial(::Material *material, int mapType)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Texture.hpp:205
raylib::Texture::Update
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:89