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 "./Image.hpp"
8#include "./Material.hpp"
9#include "./RaylibException.hpp"
10#include "./Image.hpp"
11#include "./RadiansDegrees.hpp"
12
13namespace raylib {
21class TextureUnmanaged : public ::Texture {
22public:
26 TextureUnmanaged() : ::Texture{0, 0, 0, 0, 0} {
27 // Nothing.
28 }
29
34 unsigned int id,
35 int width,
36 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) { Load(image); }
57
65 TextureUnmanaged(const ::Image& image, int layout) { Load(image, layout); }
66
72 TextureUnmanaged(const std::string_view fileName) {
73 Load(fileName);
74 }
75
76 TextureUnmanaged(::Texture&& other) : ::Texture{other.id, other.width, other.height, other.mipmaps, other.format} {
77 // Nothing.
78 }
79
80 GETTER(unsigned int, Id, id)
81 GETTER(int, Width, width)
82 GETTER(int, Height, height)
83 GETTER(int, Mipmaps, mipmaps)
84 GETTER(int, Format, format)
85
86 TextureUnmanaged& operator=(const ::Texture& texture) {
87 set(texture);
88 return *this;
89 }
90
94 Vector2 GetSize() const {
95 return {static_cast<float>(width), static_cast<float>(height)};
96 }
97
101 void Load(const ::Image& image) {
102 set(::LoadTextureFromImage(image));
103 if (!IsValid()) {
104 throw RaylibException("Failed to load Texture from Image");
105 }
106 }
107
111 void Load(const ::Image& image, int layoutType) {
112 set(::LoadTextureCubemap(image, layoutType));
113 if (!IsValid()) {
114 throw RaylibException("Failed to load Texture from Cubemap");
115 }
116 }
117
121 void Load(const std::string_view fileName) {
122 set(::LoadTexture(fileName.data()));
123 if (!IsValid()) {
124 throw RaylibException("Failed to load Texture from file: " + std::string(fileName));
125 }
126 }
127
131 void Unload() {
132 // Protect against calling UnloadTexture() twice.
133 if (id != 0) {
134 ::UnloadTexture(*this);
135 id = 0;
136 }
137 }
138
142 TextureUnmanaged& Update(const void* pixels) {
143 ::UpdateTexture(*this, pixels);
144 return *this;
145 }
146
150 TextureUnmanaged& Update(::Rectangle rec, const void* pixels) {
151 UpdateTextureRec(*this, rec, pixels);
152 return *this;
153 }
154
158 Image GetData() const {
159 return ::LoadImageFromTexture(*this);
160 }
161
165 operator Image() { return GetData(); }
166
171 ::GenTextureMipmaps(this);
172 return *this;
173 }
174
178 TextureUnmanaged& SetFilter(int filterMode) {
179 ::SetTextureFilter(*this, filterMode);
180 return *this;
181 }
182
186 TextureUnmanaged& SetWrap(int wrapMode) {
187 ::SetTextureWrap(*this, wrapMode);
188 return *this;
189 }
190
196 void Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) const {
197 ::DrawTexture(*this, posX, posY, tint);
198 }
199
205 void Draw(::Vector2 position, ::Color tint = {255, 255, 255, 255}) const { ::DrawTextureV(*this, position, tint); }
206
212 void Draw(::Vector2 position, Degree rotation, float scale = 1.0f,
213 ::Color tint = {255, 255, 255, 255}) const {
214 ::DrawTextureEx(*this, position, rotation, scale, tint);
215 }
216
222 void Draw(::Rectangle sourceRec, ::Vector2 position = {0, 0}, ::Color tint = {255, 255, 255, 255}) const {
223 ::DrawTextureRec(*this, sourceRec, position, tint);
224 }
225
231 void Draw(::Rectangle sourceRec, ::Rectangle destRec, ::Vector2 origin = {0, 0},
232 Degree rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
233 ::DrawTexturePro(*this, sourceRec, destRec, origin, rotation, tint);
234 }
235
241 void Draw(::NPatchInfo nPatchInfo, ::Rectangle destRec, ::Vector2 origin = {0, 0},
242 Radian rotation = 0, ::Color tint = {255, 255, 255, 255}) const {
243 ::DrawTextureNPatch(*this, nPatchInfo, destRec, origin, rotation, tint);
244 }
245
251 void
252 DrawBillboard(const ::Camera& camera, ::Vector3 position, float size, ::Color tint = {255, 255, 255, 255}) const {
253 ::DrawBillboard(camera, *this, position, size, tint);
254 }
255
262 const ::Camera& camera,
263 ::Rectangle source,
264 ::Vector3 position,
265 ::Vector2 size,
266 ::Color tint = {255, 255, 255, 255}) const {
267 DrawBillboardRec(camera, *this, source, position, size, tint);
268 }
269
275 void DrawBillboard(const ::Camera& camera,
276 ::Rectangle source, Vector3 position,
277 ::Vector3 up, Vector2 size, Vector2 origin, Degree rotation = 0.0f,
278 ::Color tint = {255, 255, 255, 255}) const {
279 DrawBillboardPro(camera, *this, source, position, up, size, origin, rotation, tint);
280 }
281
285 TextureUnmanaged& SetMaterial(::Material* material, int mapType = MATERIAL_MAP_NORMAL) {
286 ::SetMaterialTexture(material, mapType, *this);
287 return *this;
288 }
289
290 TextureUnmanaged& SetMaterial(const ::Material& material, int mapType = MATERIAL_MAP_NORMAL) {
291 ::SetMaterialTexture(const_cast<::Material*>(&material), mapType, *this);
292 return *this;
293 }
294
298 TextureUnmanaged& SetShapes(const ::Rectangle& source) {
299 ::SetShapesTexture(*this, source);
300 return *this;
301 }
302
306 TextureUnmanaged& SetShaderValue(const ::Shader& shader, int locIndex) {
307 ::SetShaderValueTexture(shader, locIndex, *this);
308 return *this;
309 }
310
316 [[nodiscard]] bool IsValid() const { return IsTextureValid(*this); }
317protected:
318 void set(const ::Texture& texture) {
319 id = texture.id;
320 width = texture.width;
321 height = texture.height;
322 mipmaps = texture.mipmaps;
323 format = texture.format;
324 }
325};
326
327// Create the TextureUnmanaged aliases.
328using Texture2DUnmanaged = TextureUnmanaged;
329using TextureCubemapUnmanaged = TextureUnmanaged;
330
331} // namespace raylib
332
336
337#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
Material type (generic)
Definition: Material.hpp:16
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.
bool IsValid() const
Determines whether or not the Texture has been loaded and is ready.
TextureUnmanaged(unsigned int id, int width, int height, int mipmaps=1, int format=PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
Move/Create a texture structure manually.
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:20
Vector3 type.
Definition: Vector3.hpp:20
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8