raylib-cpp
C++ object-oriented wrapper library for raylib.
TextureUnmanaged.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_HPP_
2 #define RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_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 #include "./Image.hpp"
12 
13 namespace raylib {
21 class TextureUnmanaged : public ::Texture {
22  public:
26  TextureUnmanaged() : ::Texture{0, 0, 0, 0, 0} {
27  // Nothing.
28  }
29 
33  TextureUnmanaged(unsigned int id,
34  int width, int height,
35  int mipmaps = 1,
36  int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
37  : ::Texture{id, width, height, mipmaps, format} {
38  // Nothing.
39  }
40 
44  TextureUnmanaged(const ::Texture& texture) :
45  ::Texture{texture.id, texture.width, texture.height, texture.mipmaps, texture.format} {
46  // Nothing.
47  }
48 
54  TextureUnmanaged(const ::Image& image) {
55  Load(image);
56  }
57 
65  TextureUnmanaged(const ::Image& image, int layout) {
66  Load(image, layout);
67  }
68 
74  TextureUnmanaged(const std::string& fileName) {
75  Load(fileName);
76  }
77 
78  TextureUnmanaged(::Texture&& other) :
79  ::Texture{other.id, other.width, other.height, other.mipmaps, other.format} {
80  // Nothing.
81  }
82 
83  GETTERSETTER(unsigned int, Id, id)
84  GETTERSETTER(int, Width, width)
85  GETTERSETTER(int, Height, height)
86  GETTERSETTER(int, Mipmaps, mipmaps)
87  GETTERSETTER(int, Format, format)
88 
89  TextureUnmanaged& operator=(const ::Texture& texture) {
90  set(texture);
91  return *this;
92  }
93 
97  inline ::Vector2 GetSize() const {
98  return {static_cast<float>(width), static_cast<float>(height)};
99  }
100 
104  void Load(const ::Image& image) {
105  set(::LoadTextureFromImage(image));
106  if (!IsReady()) {
107  throw RaylibException("Failed to load Texture from Image");
108  }
109  }
110 
114  void Load(const ::Image& image, int layoutType) {
115  set(::LoadTextureCubemap(image, layoutType));
116  if (!IsReady()) {
117  throw RaylibException("Failed to load Texture from Cubemap");
118  }
119  }
120 
124  void Load(const std::string& fileName) {
125  set(::LoadTexture(fileName.c_str()));
126  if (!IsReady()) {
127  throw RaylibException("Failed to load Texture from file: " + fileName);
128  }
129  }
130 
134  inline void Unload() {
135  ::UnloadTexture(*this);
136  }
137 
141  inline TextureUnmanaged& Update(const void *pixels) {
142  ::UpdateTexture(*this, pixels);
143  return *this;
144  }
145 
149  inline TextureUnmanaged& Update(::Rectangle rec, const void *pixels) {
150  UpdateTextureRec(*this, rec, pixels);
151  return *this;
152  }
153 
157  inline ::Image GetData() const {
158  return ::LoadImageFromTexture(*this);
159  }
160 
164  inline operator Image() {
165  return GetData();
166  }
167 
172  ::GenTextureMipmaps(this);
173  return *this;
174  }
175 
179  inline TextureUnmanaged& SetFilter(int filterMode) {
180  ::SetTextureFilter(*this, filterMode);
181  return *this;
182  }
183 
187  inline TextureUnmanaged& SetWrap(int wrapMode) {
188  ::SetTextureWrap(*this, wrapMode);
189  return *this;
190  }
191 
197  inline void Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) const {
198  ::DrawTexture(*this, posX, posY, tint);
199  }
200 
206  inline void Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) const {
207  ::DrawTextureV(*this, position, tint);
208  }
209 
215  inline void Draw(::Vector2 position, float rotation, float scale = 1.0f,
216  ::Color tint = {255, 255, 255, 255}) const {
217  ::DrawTextureEx(*this, position, rotation, scale, tint);
218  }
219 
225  inline void Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
226  ::Color tint = {255, 255, 255, 255}) const {
227  ::DrawTextureRec(*this, sourceRec, position, tint);
228  }
229 
235  inline void Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
236  ::Color tint = {255, 255, 255, 255}) const {
237  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
238  }
239 
245  inline void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
246  float rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
247  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
248  }
249 
255  inline void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
256  float rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
257  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
258  }
259 
265  inline void DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
266  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) const {
267  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
268  }
269 
275  inline void DrawPoly(::Vector2 center, ::Vector2 *points,
276  ::Vector2 *texcoords, int pointsCount,
277  ::Color tint = {255, 255, 255, 255}) const {
278  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
279  }
280 
286  inline void DrawBillboard(const ::Camera& camera,
287  ::Vector3 position, float size,
288  ::Color tint = {255, 255, 255, 255}) const {
289  ::DrawBillboard(camera, *this, position, size, tint);
290  }
291 
297  inline void DrawBillboard(const ::Camera& camera,
298  ::Rectangle source, ::Vector3 position, ::Vector2 size,
299  ::Color tint = {255, 255, 255, 255}) const {
300  DrawBillboardRec(camera, *this, source, position, size, tint);
301  }
302 
308  inline void DrawBillboard(const ::Camera& camera,
309  ::Rectangle source, Vector3 position,
310  ::Vector3 up, Vector2 size, Vector2 origin, float rotation = 0.0f,
311  ::Color tint = {255, 255, 255, 255}) const {
312  DrawBillboardPro(camera, *this, source, position, up, size, origin, rotation, tint);
313  }
314 
320  inline void DrawCube(::Vector3 position, float width, float height, float length,
321  ::Color color = {255, 255, 255, 255}) const {
322  ::DrawCubeTexture(*this, position, width, height, length, color);
323  }
324 
330  inline void DrawCube(::Vector3 position, ::Vector3 dimensions,
331  ::Color color = {255, 255, 255, 255}) const {
332  ::DrawCubeTexture(*this, position, dimensions.x, dimensions.y, dimensions.z, color);
333  }
334 
340  inline void DrawCube(::Rectangle source, ::Vector3 position, float width, float height, float length,
341  ::Color color = {255, 255, 255, 255}) const {
342  ::DrawCubeTextureRec(*this, source, position, width, height, length, color);
343  }
344 
350  inline void DrawCube(::Rectangle source, ::Vector3 position, ::Vector3 dimensions,
351  ::Color color = {255, 255, 255, 255}) const {
352  ::DrawCubeTextureRec(*this, source, position, dimensions.x, dimensions.y, dimensions.z, color);
353  }
354 
358  inline TextureUnmanaged& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
359  ::SetMaterialTexture(material, mapType, *this);
360  return *this;
361  }
362 
363  inline TextureUnmanaged& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
364  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
365  return *this;
366  }
367 
371  inline TextureUnmanaged& SetShapes(const ::Rectangle& source) {
372  ::SetShapesTexture(*this, source);
373  return *this;
374  }
375 
379  inline TextureUnmanaged& SetShaderValue(const ::Shader& shader, int locIndex) {
380  ::SetShaderValueTexture(shader, locIndex, *this);
381  return *this;
382  }
383 
389  bool IsReady() const {
390  return id != 0;
391  }
392 
393  protected:
394  void set(const ::Texture& texture) {
395  id = texture.id;
396  width = texture.width;
397  height = texture.height;
398  mipmaps = texture.mipmaps;
399  format = texture.format;
400  }
401 };
402 
403 // Create the TextureUnmanaged aliases.
404 typedef TextureUnmanaged Texture2DUnmanaged;
405 typedef TextureUnmanaged TextureCubemapUnmanaged;
406 
407 } // namespace raylib
408 
412 
413 #endif // RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:17
Material type (generic)
Definition: Material.hpp:14
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:12
Texture type.
Definition: Texture.hpp:14
A Texture that is not managed by the C++ garbage collector.
void DrawBillboard(const ::Camera &camera, ::Vector3 position, float size, ::Color tint={255, 255, 255, 255}) const
Draw a billboard texture.
TextureUnmanaged & SetFilter(int filterMode)
Set texture scaling filter mode.
TextureUnmanaged & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
void Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
TextureUnmanaged & Update(const void *pixels)
Update GPU texture with new data.
inline ::Image GetData() const
Get pixel data from GPU texture and return an Image.
TextureUnmanaged & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
void DrawPoly(::Vector2 center, ::Vector2 *points, ::Vector2 *texcoords, int pointsCount, ::Color tint={255, 255, 255, 255}) const
Draw a textured polygon.
TextureUnmanaged(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
void DrawCube(::Vector3 position, ::Vector3 dimensions, ::Color color={255, 255, 255, 255}) const
Draw cube textured, with dimensions.
void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255}) const
Draws a texture (or part of it) that stretches or shrinks nicely.
TextureUnmanaged(unsigned int id, int width, int height, int mipmaps=1, int format=PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
Move/Create a texture structure manually.
bool IsReady() const
Determines whether or not the Texture has been loaded and is ready.
void Load(const ::Image &image)
Load texture from image data.
void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, ::Color tint={255, 255, 255, 255}) const
Draw a part of a texture defined by a rectangle with 'pro' parameters.
void DrawBillboard(const ::Camera &camera, ::Rectangle source, Vector3 position, ::Vector3 up, Vector2 size, Vector2 origin, float rotation=0.0f, ::Color tint={255, 255, 255, 255}) const
Draw a billboard texture defined by source and rotation.
void Load(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
void Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D.
TextureUnmanaged & SetMaterial(::Material *material, int mapType=MATERIAL_MAP_NORMAL)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
TextureUnmanaged & GenMipmaps()
Generate GPU mipmaps for a texture.
void Draw(::Rectangle sourceRec, ::Vector2 position={0, 0}, ::Color tint={255, 255, 255, 255}) const
Draw a part of a texture defined by a rectangle.
inline ::Vector2 GetSize() const
Retrieve the width and height of the texture.
void Draw(::Vector2 position, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D with position defined as Vector2.
void Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad, ::Color tint={255, 255, 255, 255}) const
Draw texture quad with tiling and offset parameters.
void Draw(::Vector2 position, float rotation, float scale=1.0f, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D with extended parameters.
TextureUnmanaged()
Default texture constructor.
void DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, float rotation=0, float scale=1, Color tint={255, 255, 255, 255}) const
Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
TextureUnmanaged & SetWrap(int wrapMode)
Set texture wrapping mode.
void DrawCube(::Rectangle source, ::Vector3 position, ::Vector3 dimensions, ::Color color={255, 255, 255, 255}) const
Draw cube with a region of a texture, with dimensions.
TextureUnmanaged(const std::string &fileName)
Load texture from file into GPU memory (VRAM)
void Unload()
Unload texture from GPU memory (VRAM)
void DrawCube(::Rectangle source, ::Vector3 position, float width, float height, float length, ::Color color={255, 255, 255, 255}) const
Draw cube with a region of a texture.
void DrawCube(::Vector3 position, float width, float height, float length, ::Color color={255, 255, 255, 255}) const
Draw cube textured.
TextureUnmanaged(const ::Texture &texture)
Creates a texture object based on the given Texture struct data.
TextureUnmanaged(const ::Image &image)
Creates a texture from the given Image.
TextureUnmanaged & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
void DrawBillboard(const ::Camera &camera, ::Rectangle source, ::Vector3 position, ::Vector2 size, ::Color tint={255, 255, 255, 255}) const
Draw a billboard texture defined by source.
Vector2 type.
Definition: Vector2.hpp:16
Vector3 type.
Definition: Vector3.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8