raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
TextureUnmanaged.hpp
1#ifndef RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_HPP_
2#define RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_HPP_
3
4#include <string>
5#include <string_view>
6
7#include "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9#include "./Vector2.hpp"
10#include "./Material.hpp"
11#include "./RaylibException.hpp"
12#include "./Image.hpp"
13#include "./RadiansDegrees.hpp"
14
15namespace raylib {
23class TextureUnmanaged : public ::Texture {
24 public:
28 TextureUnmanaged() : ::Texture{0, 0, 0, 0, 0} {
29 // Nothing.
30 }
31
35 TextureUnmanaged(unsigned int id,
36 int width, int height,
37 int mipmaps = 1,
38 int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
39 : ::Texture{id, width, height, mipmaps, format} {
40 // Nothing.
41 }
42
46 TextureUnmanaged(const ::Texture& texture) :
47 ::Texture{texture.id, texture.width, texture.height, texture.mipmaps, texture.format} {
48 // Nothing.
49 }
50
56 TextureUnmanaged(const ::Image& image) {
57 Load(image);
58 }
59
67 TextureUnmanaged(const ::Image& image, int layout) {
68 Load(image, layout);
69 }
70
76 TextureUnmanaged(const std::string_view fileName) {
77 Load(fileName);
78 }
79
80 TextureUnmanaged(::Texture&& other) :
81 ::Texture{other.id, other.width, other.height, other.mipmaps, other.format} {
82 // Nothing.
83 }
84
85 GETTER(unsigned int, Id, id)
86 GETTER(int, Width, width)
87 GETTER(int, Height, height)
88 GETTER(int, Mipmaps, mipmaps)
89 GETTER(int, Format, format)
90
91 TextureUnmanaged& operator=(const ::Texture& texture) {
92 set(texture);
93 return *this;
94 }
95
99 Vector2 GetSize() const {
100 return {static_cast<float>(width), static_cast<float>(height)};
101 }
102
106 void Load(const ::Image& image) {
107 set(::LoadTextureFromImage(image));
108 if (!IsReady()) {
109 throw RaylibException("Failed to load Texture from Image");
110 }
111 }
112
116 void Load(const ::Image& image, int layoutType) {
117 set(::LoadTextureCubemap(image, layoutType));
118 if (!IsReady()) {
119 throw RaylibException("Failed to load Texture from Cubemap");
120 }
121 }
122
126 void Load(const std::string_view fileName) {
127 set(::LoadTexture(fileName.data()));
128 if (!IsReady()) {
129 throw RaylibException("Failed to load Texture from file: " + std::string(fileName));
130 }
131 }
132
136 void Unload() {
137 // Protect against calling UnloadTexture() twice.
138 if (id != 0) {
139 ::UnloadTexture(*this);
140 id = 0;
141 }
142 }
143
147 TextureUnmanaged& Update(const void *pixels) {
148 ::UpdateTexture(*this, pixels);
149 return *this;
150 }
151
155 TextureUnmanaged& Update(::Rectangle rec, const void *pixels) {
156 UpdateTextureRec(*this, rec, pixels);
157 return *this;
158 }
159
163 Image GetData() const {
164 return ::LoadImageFromTexture(*this);
165 }
166
170 operator Image() {
171 return GetData();
172 }
173
178 ::GenTextureMipmaps(this);
179 return *this;
180 }
181
185 TextureUnmanaged& SetFilter(int filterMode) {
186 ::SetTextureFilter(*this, filterMode);
187 return *this;
188 }
189
193 TextureUnmanaged& SetWrap(int wrapMode) {
194 ::SetTextureWrap(*this, wrapMode);
195 return *this;
196 }
197
203 void Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) const {
204 ::DrawTexture(*this, posX, posY, tint);
205 }
206
212 void Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) const {
213 ::DrawTextureV(*this, position, tint);
214 }
215
221 void Draw(::Vector2 position, Degree rotation, float scale = 1.0f,
222 ::Color tint = {255, 255, 255, 255}) const {
223 ::DrawTextureEx(*this, position, rotation, scale, tint);
224 }
225
231 void Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0},
232 ::Color tint = {255, 255, 255, 255}) const {
233 ::DrawTextureRec(*this, sourceRec, position, tint);
234 }
235
241 void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
242 Degree rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
243 ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
244 }
245
251 void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
252 Radian rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
253 ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
254 }
255
261 void DrawBillboard(const ::Camera& camera,
262 ::Vector3 position, float size,
263 ::Color tint = {255, 255, 255, 255}) const {
264 ::DrawBillboard(camera, *this, position, size, tint);
265 }
266
272 void DrawBillboard(const ::Camera& camera,
273 ::Rectangle source, ::Vector3 position, ::Vector2 size,
274 ::Color tint = {255, 255, 255, 255}) const {
275 DrawBillboardRec(camera, *this, source, position, size, tint);
276 }
277
283 void DrawBillboard(const ::Camera& camera,
284 ::Rectangle source, Vector3 position,
285 ::Vector3 up, Vector2 size, Vector2 origin, Degree rotation = 0.0f,
286 ::Color tint = {255, 255, 255, 255}) const {
287 DrawBillboardPro(camera, *this, source, position, up, size, origin, rotation, tint);
288 }
289
293 TextureUnmanaged& SetMaterial(::Material *material, int mapType = MATERIAL_MAP_NORMAL) {
294 ::SetMaterialTexture(material, mapType, *this);
295 return *this;
296 }
297
298 TextureUnmanaged& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
299 ::SetMaterialTexture((::Material*)(&material), mapType, *this);
300 return *this;
301 }
302
306 TextureUnmanaged& SetShapes(const ::Rectangle& source) {
307 ::SetShapesTexture(*this, source);
308 return *this;
309 }
310
314 TextureUnmanaged& SetShaderValue(const ::Shader& shader, int locIndex) {
315 ::SetShaderValueTexture(shader, locIndex, *this);
316 return *this;
317 }
318
324 bool IsReady() const {
325 return id != 0;
326 }
327
328 protected:
329 void set(const ::Texture& texture) {
330 id = texture.id;
331 width = texture.width;
332 height = texture.height;
333 mipmaps = texture.mipmaps;
334 format = texture.format;
335 }
336};
337
338// Create the TextureUnmanaged aliases.
339using Texture2DUnmanaged = TextureUnmanaged;
340using TextureCubemapUnmanaged = TextureUnmanaged;
341
342} // namespace raylib
343
347
348#endif // RAYLIB_CPP_INCLUDE_TEXTUREUNMANAGED_HPP_
Degree type (allows automatic worry free conversion between radians and degrees)
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:19
Radian type (allows automatic worry free conversion between radians and degrees)
Exception used for most raylib-related exceptions.
Texture type.
Definition: Texture.hpp:14
A Texture that is not managed by C++ RAII.
void DrawBillboard(const ::Camera &camera, ::Vector3 position, float size, ::Color tint={255, 255, 255, 255}) const
Draw a billboard texture.
void Load(const std::string_view fileName)
Load texture from file into GPU memory (VRAM)
TextureUnmanaged & SetShapes(const ::Rectangle &source)
Set texture and rectangle to be used on shapes drawing.
void Load(const ::Image &image, int layoutType)
Load cubemap from image, multiple image cubemap layouts supported.
void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin={0, 0}, Degree rotation=0, ::Color tint={255, 255, 255, 255}) const
Draw a part of a texture defined by a rectangle with 'pro' parameters.
TextureUnmanaged & SetFilter(int filterMode)
Set texture scaling filter mode.
TextureUnmanaged(const ::Image &image, int layout)
Load cubemap from image, multiple image cubemap layouts supported.
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.
TextureUnmanaged & SetWrap(int wrapMode)
Set texture wrapping mode.
TextureUnmanaged & Update(::Rectangle rec, const void *pixels)
Update GPU texture rectangle with new data.
TextureUnmanaged & SetMaterial(::Material *material, int mapType=MATERIAL_MAP_NORMAL)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
void Draw(int posX=0, int posY=0, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D.
Vector2 GetSize() const
Retrieve the width and height of the texture.
Image GetData() const
Get pixel data from GPU texture and return an Image.
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.
void Draw(::Vector2 position, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D with position defined as Vector2.
TextureUnmanaged()
Default texture constructor.
void Draw(::Vector2 position, Degree rotation, float scale=1.0f, ::Color tint={255, 255, 255, 255}) const
Draw a Texture2D with extended parameters.
void Unload()
Unload texture from GPU memory (VRAM)
TextureUnmanaged(const std::string_view fileName)
Load texture from file into GPU memory (VRAM)
void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin={0, 0}, Radian rotation=0, ::Color tint={255, 255, 255, 255}) const
Draws a texture (or part of it) that stretches or shrinks nicely.
TextureUnmanaged & Update(const void *pixels)
Update GPU texture with new data.
void DrawBillboard(const ::Camera &camera, ::Rectangle source, Vector3 position, ::Vector3 up, Vector2 size, Vector2 origin, Degree rotation=0.0f, ::Color tint={255, 255, 255, 255}) const
Draw a billboard texture defined by source and rotation.
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 & GenMipmaps()
Generate GPU mipmaps for a texture.
TextureUnmanaged & SetShaderValue(const ::Shader &shader, int locIndex)
Set shader uniform value for texture (sampler2d)
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:19
Vector3 type.
Definition: Vector3.hpp:19