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  // Protect against calling UnloadTexture() twice.
136  if (id != 0) {
137  ::UnloadTexture(*this);
138  id = 0;
139  }
140  }
141 
145  inline TextureUnmanaged& Update(const void *pixels) {
146  ::UpdateTexture(*this, pixels);
147  return *this;
148  }
149 
153  inline TextureUnmanaged& Update(::Rectangle rec, const void *pixels) {
154  UpdateTextureRec(*this, rec, pixels);
155  return *this;
156  }
157 
161  inline ::Image GetData() const {
162  return ::LoadImageFromTexture(*this);
163  }
164 
168  inline operator Image() {
169  return GetData();
170  }
171 
176  ::GenTextureMipmaps(this);
177  return *this;
178  }
179 
183  inline TextureUnmanaged& SetFilter(int filterMode) {
184  ::SetTextureFilter(*this, filterMode);
185  return *this;
186  }
187 
191  inline TextureUnmanaged& SetWrap(int wrapMode) {
192  ::SetTextureWrap(*this, wrapMode);
193  return *this;
194  }
195 
201  inline void Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) const {
202  ::DrawTexture(*this, posX, posY, tint);
203  }
204 
210  inline void Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) const {
211  ::DrawTextureV(*this, position, tint);
212  }
213 
219  inline void Draw(::Vector2 position, float rotation, float scale = 1.0f,
220  ::Color tint = {255, 255, 255, 255}) const {
221  ::DrawTextureEx(*this, position, rotation, scale, tint);
222  }
223 
229  inline void Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
230  ::Color tint = {255, 255, 255, 255}) const {
231  ::DrawTextureRec(*this, sourceRec, position, tint);
232  }
233 
239  inline void Draw(::Vector2 tiling, ::Vector2 offset, ::Rectangle quad,
240  ::Color tint = {255, 255, 255, 255}) const {
241  ::DrawTextureQuad(*this, tiling, offset, quad, tint);
242  }
243 
249  inline void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
250  float rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
251  ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
252  }
253 
259  inline void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
260  float rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
261  ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
262  }
263 
269  inline void DrawTiled(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
270  float rotation = 0, float scale = 1, Color tint = {255, 255, 255, 255}) const {
271  ::DrawTextureTiled(*this, sourceRec, destRec, origin, rotation, scale, tint);
272  }
273 
279  inline void DrawPoly(::Vector2 center, ::Vector2 *points,
280  ::Vector2 *texcoords, int pointsCount,
281  ::Color tint = {255, 255, 255, 255}) const {
282  ::DrawTexturePoly(*this, center, points, texcoords, pointsCount, tint);
283  }
284 
290  inline void DrawBillboard(const ::Camera& camera,
291  ::Vector3 position, float size,
292  ::Color tint = {255, 255, 255, 255}) const {
293  ::DrawBillboard(camera, *this, position, size, tint);
294  }
295 
301  inline void DrawBillboard(const ::Camera& camera,
302  ::Rectangle source, ::Vector3 position, ::Vector2 size,
303  ::Color tint = {255, 255, 255, 255}) const {
304  DrawBillboardRec(camera, *this, source, position, size, tint);
305  }
306 
312  inline void DrawBillboard(const ::Camera& camera,
313  ::Rectangle source, Vector3 position,
314  ::Vector3 up, Vector2 size, Vector2 origin, float rotation = 0.0f,
315  ::Color tint = {255, 255, 255, 255}) const {
316  DrawBillboardPro(camera, *this, source, position, up, size, origin, rotation, tint);
317  }
318 
324  inline void DrawCube(::Vector3 position, float width, float height, float length,
325  ::Color color = {255, 255, 255, 255}) const {
326  ::DrawCubeTexture(*this, position, width, height, length, color);
327  }
328 
334  inline void DrawCube(::Vector3 position, ::Vector3 dimensions,
335  ::Color color = {255, 255, 255, 255}) const {
336  ::DrawCubeTexture(*this, position, dimensions.x, dimensions.y, dimensions.z, color);
337  }
338 
344  inline void DrawCube(::Rectangle source, ::Vector3 position, float width, float height, float length,
345  ::Color color = {255, 255, 255, 255}) const {
346  ::DrawCubeTextureRec(*this, source, position, width, height, length, color);
347  }
348 
354  inline void DrawCube(::Rectangle source, ::Vector3 position, ::Vector3 dimensions,
355  ::Color color = {255, 255, 255, 255}) const {
356  ::DrawCubeTextureRec(*this, source, position, dimensions.x, dimensions.y, dimensions.z, color);
357  }
358 
362  inline TextureUnmanaged& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
363  ::SetMaterialTexture(material, mapType, *this);
364  return *this;
365  }
366 
367  inline TextureUnmanaged& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
368  ::SetMaterialTexture((::Material*)(&material), mapType, *this);
369  return *this;
370  }
371 
375  inline TextureUnmanaged& SetShapes(const ::Rectangle& source) {
376  ::SetShapesTexture(*this, source);
377  return *this;
378  }
379 
383  inline TextureUnmanaged& SetShaderValue(const ::Shader& shader, int locIndex) {
384  ::SetShaderValueTexture(shader, locIndex, *this);
385  return *this;
386  }
387 
393  bool IsReady() const {
394  return id != 0;
395  }
396 
397  protected:
398  void set(const ::Texture& texture) {
399  id = texture.id;
400  width = texture.width;
401  height = texture.height;
402  mipmaps = texture.mipmaps;
403  format = texture.format;
404  }
405 };
406 
407 // Create the TextureUnmanaged aliases.
408 typedef TextureUnmanaged Texture2DUnmanaged;
409 typedef TextureUnmanaged TextureCubemapUnmanaged;
410 
411 } // namespace raylib
412 
416 
417 #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