TGUI 1.13
Loading...
Searching...
No Matches
Texture.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_TEXTURE_HPP
26#define TGUI_TEXTURE_HPP
27
29
30#include <TGUI/Global.hpp>
31
32#include <TGUI/Color.hpp>
33#include <TGUI/Rect.hpp>
34#include <TGUI/String.hpp>
35#include <TGUI/TextureData.hpp>
36#include <TGUI/Vector2.hpp>
37
38#include <functional>
39
40#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
41 #include <SFML/Graphics/Shader.hpp>
42 #include <SFML/Graphics/Texture.hpp>
43#endif
44
46
47namespace tgui
48{
52 class TGUI_API Texture
53 {
54 public:
55 using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
56 using BackendTextureLoaderFunc = std::function<bool(BackendTexture&, const String&, bool smooth)>;
57 using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;
58
62 Texture() = default;
63
74 Texture(const char* id,
75 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
76 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
77 bool smooth = m_defaultSmooth) :
78 Texture(String{id}, partRect, middlePart, smooth)
79 {
80 }
81
94 Texture(const String& id,
95 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
96 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
97 bool smooth = m_defaultSmooth);
98
99#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
114 TGUI_DEPRECATED("Use Texture() and loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
115 Texture(const sf::Texture& texture,
116 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
117 const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
118#endif
123
127 Texture(Texture&&) noexcept;
128
133
137 Texture& operator=(const Texture&);
138
142 Texture& operator=(Texture&&) noexcept;
143
154 void load(const String& id, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
155
156#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
171 TGUI_DEPRECATED("Use loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
172 void load(const sf::Texture& texture, const UIntRect& partRect = {}, const UIntRect& middleRect = {});
173#endif
185 void loadFromMemory(const std::uint8_t* data,
186 std::size_t dataSize,
187 const UIntRect& partRect = {},
188 const UIntRect& middleRect = {},
189 bool smooth = m_defaultSmooth);
190
204 void loadFromPixelData(Vector2u size,
205 const std::uint8_t* pixels,
206 const UIntRect& partRect = {},
207 const UIntRect& middleRect = {},
208 bool smooth = m_defaultSmooth);
209
222 void loadFromBase64(CharStringView imageAsBase64,
223 const UIntRect& partRect = {},
224 const UIntRect& middleRect = {},
225 bool smooth = m_defaultSmooth);
226
232 [[nodiscard]] const String& getId() const;
233
239 [[nodiscard]] std::shared_ptr<TextureData> getData() const;
240
246 [[nodiscard]] Vector2u getImageSize() const;
247
253 [[nodiscard]] UIntRect getPartRect() const;
254
260 [[nodiscard]] bool isSmooth() const;
261
272 void setColor(const Color& color);
273
284 [[nodiscard]] const Color& getColor() const;
285
286#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
291 void setShader(sf::Shader* shader);
292
297 [[nodiscard]] sf::Shader* getShader() const;
298#endif
306 void setMiddleRect(const UIntRect& middleRect);
307
313 [[nodiscard]] UIntRect getMiddleRect() const;
314
329 void setScaledNineSlice(bool scaled);
330
340 [[nodiscard]] bool getScaledNineSlice() const;
341
349 [[nodiscard]] bool isTransparentPixel(Vector2u pos) const;
350
358 void setCopyCallback(const CallbackFunc& func);
359
367 void setDestructCallback(const CallbackFunc& func);
368
372 [[nodiscard]] bool operator==(const Texture& right) const;
373
377 [[nodiscard]] bool operator!=(const Texture& right) const;
378
389 static void setDefaultSmooth(bool smooth);
390
399 [[nodiscard]] static bool getDefaultSmooth();
400
410 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
411
419 [[nodiscard]] static const BackendTextureLoaderFunc& getBackendTextureLoader();
420
430 static void setTextureLoader(const TextureLoaderFunc& func);
431
439 [[nodiscard]] static const TextureLoaderFunc& getTextureLoader();
440
442
443 private:
451 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
452
454
455 private:
456#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
457 sf::Shader* m_shader = nullptr;
458#endif
459
460 std::shared_ptr<TextureData> m_data = nullptr;
461 Color m_color = Color::White;
462
463 UIntRect m_partRect;
464 UIntRect m_middleRect;
465 String m_id;
466 bool m_scaledNineSlice = false;
467
468 CallbackFunc m_copyCallback;
469 CallbackFunc m_destructCallback;
470
471 static bool m_defaultSmooth;
472
473 static TextureLoaderFunc m_textureLoader;
474 static BackendTextureLoaderFunc m_backendTextureLoader;
475 };
476} // namespace tgui
477
478#endif // TGUI_TEXTURE_HPP
Base class for texture implementations that depend on the backend.
Definition BackendTexture.hpp:41
Wrapper for colors.
Definition Color.hpp:63
static const Color White
White predefined color.
Definition Color.hpp:245
Wrapper class to store strings.
Definition String.hpp:94
void setScaledNineSlice(bool scaled)
Sets whether the corners of the image can still scale with the image size when 9-slice scaling is use...
UIntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
static void setTextureLoader(const TextureLoaderFunc &func)
Sets a different texture loader.
Texture(const Texture &)
Copy constructor.
static void setDefaultSmooth(bool smooth)
Changes whether textures are smoothed by default or not.
void setMiddleRect(const UIntRect &middleRect)
Sets the middle part of the image for 9-slice scaling (relative to the part defined by partRect).
bool isTransparentPixel(Vector2u pos) const
Checks if a certain pixel is transparent.
std::shared_ptr< TextureData > getData() const
Returns the texture data.
sf::Shader * getShader() const
Returns the shader used to draw the texture.
Texture(Texture &&) noexcept
Move constructor.
void load(const String &id, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Creates the texture.
static const BackendTextureLoaderFunc & getBackendTextureLoader()
Returns the used backend texture loader.
static bool getDefaultSmooth()
Returns whether textures are smoothed by default or not.
bool operator==(const Texture &right) const
Compares the texture with another one.
Texture(const char *id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=m_defaultSmooth)
Constructor that created the texture.
Definition Texture.hpp:74
void setShader(sf::Shader *shader)
Sets the shader used to draw the texture.
bool operator!=(const Texture &right) const
Compares the texture with another one.
void loadFromBase64(CharStringView imageAsBase64, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from a base64 string.
static const TextureLoaderFunc & getTextureLoader()
Returns the used texture loader.
void setColor(const Color &color)
Sets the global color of the texture.
UIntRect getPartRect() const
Returns which part of the image was loaded.
bool isSmooth() const
Tells whether the smooth filter is enabled or not.
void loadFromPixelData(Vector2u size, const std::uint8_t *pixels, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from an array of 32-bits RGBA pixels.
bool getScaledNineSlice() const
Returns whether the corners of the image can still scale with the image size when 9-slice scaling is ...
void loadFromMemory(const std::uint8_t *data, std::size_t dataSize, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from memory (data in memory should contain the entire file, not just the pixels).
static void setBackendTextureLoader(const BackendTextureLoaderFunc &func)
Sets a different backend texture loader.
void setCopyCallback(const CallbackFunc &func)
Sets a callback function for when this texture is copied.
const Color & getColor() const
Returns the global color of the texture.
const String & getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename).
void setDestructCallback(const CallbackFunc &func)
Sets a callback function for when this texture is destroyed.
Texture(const String &id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=m_defaultSmooth)
Constructor that created the texture.
Vector2u getImageSize() const
Returns the size that the loaded image, or the size of the part if only a part of the image is loaded...
Texture()=default
Default constructor.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37