raylib-cpp
C++ object-oriented wrapper library for raylib.
Texture.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
26 #define RAYLIB_CPP_INCLUDE_TEXTURE_HPP_
27 
28 #include <string>
29 
30 #include "./raylib.hpp"
31 #include "./raylib-cpp-utils.hpp"
32 #include "./Vector2.hpp"
33 #include "./Material.hpp"
34 
35 namespace raylib {
36 class Texture : public ::Texture {
37  public:
38  Texture(const ::Texture& texture) {
39  set(texture);
40  }
41 
42  Texture() {
43  set(::GetTextureDefault());
44  }
45 
46  Texture(const ::Image& image) {
47  LoadFromImage(image);
48  }
49 
53  Texture(const std::string& fileName) {
54  Load(fileName);
55  }
56 
58  Unload();
59  }
60 
61  GETTERSETTER(unsigned int, Id, id)
62  GETTERSETTER(int, Width, width)
63  GETTERSETTER(int, Height, height)
64  GETTERSETTER(int, Mipmaps, mipmaps)
65  GETTERSETTER(int, Format, format)
66 
67  Texture& operator=(const ::Texture& texture) {
68  set(texture);
69  return *this;
70  }
71 
75  inline ::Vector2 GetSize() {
76  return {static_cast<float>(width), static_cast<float>(height)};
77  }
78 
82  void LoadFromImage(const ::Image& image) {
83  set(::LoadTextureFromImage(image));
84  }
85 
89  void LoadTextureCubemap(const ::Image& image, int layoutType) {
90  set(::LoadTextureCubemap(image, layoutType));
91  }
92 
96  void Load(const std::string& fileName) {
97  set(::LoadTexture(fileName.c_str()));
98  }
99 
103  inline void Unload() {
104  ::UnloadTexture(*this);
105  }
106 
110  inline Texture& Update(const void *pixels) {
111  ::UpdateTexture(*this, pixels);
112  return *this;
113  }
114 
118  inline Texture& UpdateRec(::Rectangle rec, const void *pixels) {
119  UpdateTextureRec(*this, rec, pixels);
120  return *this;
121  }
122 
126  inline ::Image GetData() const {
127  return ::GetTextureData(*this);
128  }
129 
133  inline operator raylib::Image() {
134  return GetData();
135  }
136 
140  inline Texture& GenMipmaps() {
141  ::GenTextureMipmaps(this);
142  return *this;
143  }
144 
148  inline Texture& SetFilter(int filterMode) {
149  ::SetTextureFilter(*this, filterMode);
150  return *this;
151  }
152 
156  inline Texture& SetWrap(int wrapMode) {
157  ::SetTextureWrap(*this, wrapMode);
158  return *this;
159  }
160 
164  inline Texture& Draw() {
165  return Draw(0, 0);
166  }
167 
171  inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) {
172  ::DrawTexture(*this, posX, posY, tint);
173  return *this;
174  }
175 
176  inline Texture& Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) {
177  ::DrawTextureV(*this, position, tint);
178  return *this;
179  }
180 
181  inline Texture& Draw(::Vector2 position, float rotation, float scale = 1.0f,
182  ::Color tint = {255, 255, 255, 255}) {
183  ::DrawTextureEx(*this, position, rotation, scale, tint);
184  return *this;
185  }
186 
187  inline Texture& Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
188  ::Color tint = {255, 255, 255, 255}) {
189  ::DrawTextureRec(*this, sourceRec, position, tint);
190  return *this;
191  }
192 
193  inline Texture& Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
194  ::Color tint = {255, 255, 255, 255}) {
195  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
196  return *this;
197  }
198 
199  inline Texture& Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
200  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
201  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
202  return *this;
203  }
204 
205  inline Texture& Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
206  float rotation = 0, ::Color tint = {255, 255, 255, 255}) {
207  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
208  return *this;
209  }
210 
211  inline Texture& Draw(::Vector3 position, float width, float height, float length,
212  ::Color color = {255, 255, 255, 255}) {
213  ::DrawCubeTexture(*this, position, width, height, length, color);
214  return *this;
215  }
216 
217  inline Texture& DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
218  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) {
219  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
220  return *this;
221  }
222 
226  inline Texture& SetMaterial(::Material *material, int mapType) {
227  ::SetMaterialTexture(material, mapType, *this);
228  return *this;
229  }
230 
234  static int GetPixelDataSize(int width, int height, int format) {
235  return ::GetPixelDataSize(width, height, format);
236  }
237 
241  inline Texture& SetShapes(::Rectangle source) {
242  ::SetShapesTexture(*this, source);
243  return *this;
244  }
245 
246  protected:
247  inline void set(const ::Texture& texture) {
248  id = texture.id;
249  width = texture.width;
250  height = texture.height;
251  mipmaps = texture.mipmaps;
252  format = texture.format;
253  }
254 };
255 
256 // Create the Texture aliases.
259 
260 } // namespace raylib
261 
262 #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:234
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Texture::SetWrap
Texture & SetWrap(int wrapMode)
Set texture wrapping mode.
Definition: Texture.hpp:156
raylib::Texture::LoadTextureCubemap
void LoadTextureCubemap(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
Definition: Texture.hpp:89
raylib::Texture::set
void set(const ::Texture &texture)
Definition: Texture.hpp:247
raylib::Texture::Draw
Texture & Draw(int posX, int posY, ::Color tint={255, 255, 255, 255})
Draw a Texture2D.
Definition: Texture.hpp:171
raylib::Texture::Draw
Texture & Draw(::Vector3 position, float width, float height, float length, ::Color color={255, 255, 255, 255})
Definition: Texture.hpp:211
raylib::Image
Definition: Image.hpp:34
raylib::Texture::GetSize
inline ::Vector2 GetSize()
Retrieve the width and height of the texture.
Definition: Texture.hpp:75
raylib::Texture::Draw
Texture & Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:199
raylib::Texture::SetShapes
Texture & SetShapes(::Rectangle source)
Define default texture used to draw shapes.
Definition: Texture.hpp:241
raylib::Vector3
Definition: Vector3.hpp:37
raylib::Texture::Texture
Texture(const ::Image &image)
Definition: Texture.hpp:46
raylib::Texture::Draw
Texture & Draw(::Rectangle sourceRec, ::Vector2 position={0, 0}, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:187
raylib::Texture::Load
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:96
raylib::Rectangle
Definition: Rectangle.hpp:33
raylib::Texture::DrawTiled
Texture & DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, float scale=1, Color tint={255, 255, 255, 255})
Definition: Texture.hpp:217
raylib::Vector2
Definition: Vector2.hpp:37
raylib::Texture::LoadFromImage
void LoadFromImage(const ::Image &image)
Load texture from image data.
Definition: Texture.hpp:82
raylib::Texture::Texture
Texture(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
Definition: Texture.hpp:53
raylib::Texture::SetFilter
Texture & SetFilter(int filterMode)
Set texture scaling filter mode.
Definition: Texture.hpp:148
raylib::Color
Definition: Color.hpp:35
raylib::Texture::Draw
Texture & Draw(::Vector2 position, float rotation, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:181
raylib::Texture::~Texture
~Texture()
Definition: Texture.hpp:57
raylib::Texture::GetData
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
Definition: Texture.hpp:126
raylib::Texture2D
Texture Texture2D
Definition: Texture.hpp:257
raylib::Texture::Draw
Texture & Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:193
raylib::Texture::Draw
Texture & Draw()
Draws the texture at the top left corner of the screen.
Definition: Texture.hpp:164
raylib::Texture::UpdateRec
Texture & UpdateRec(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
Definition: Texture.hpp:118
raylib::Texture
Definition: Texture.hpp:36
raylib::Texture::Unload
void Unload()
Unload texture from GPU memory (VRAM)
Definition: Texture.hpp:103
raylib::Texture::Draw
Texture & Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:205
raylib::TextureCubemap
Texture TextureCubemap
Definition: Texture.hpp:258
raylib::Texture::GenMipmaps
Texture & GenMipmaps()
Generate GPU mipmaps for a texture.
Definition: Texture.hpp:140
raylib::Texture::Draw
Texture & Draw(::Vector2 position, ::Color tint={255, 255, 255, 255})
Definition: Texture.hpp:176
raylib::Texture::Texture
Texture(const ::Texture &texture)
Definition: Texture.hpp:38
raylib::Material
Definition: Material.hpp:35
raylib::Texture::SetMaterial
Texture & SetMaterial(::Material *material, int mapType)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Texture.hpp:226
raylib::Texture::Texture
Texture()
Definition: Texture.hpp:42
raylib::Texture::Update
Texture & Update(const void *pixels)
Update GPU texture with new data.
Definition: Texture.hpp:110