TGUI  1.3-dev
Loading...
Searching...
No Matches
Texture.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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/TextureData.hpp>
31#include <TGUI/Vector2.hpp>
32#include <TGUI/String.hpp>
33#include <TGUI/Global.hpp>
34#include <TGUI/Color.hpp>
35#include <TGUI/Rect.hpp>
36
37#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
38 #include <functional>
39#endif
40
41#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_BUILD_AS_CXX_MODULE
42 namespace sf
43 {
44 class Texture;
45 class Shader;
46 }
47#endif
48
50
51TGUI_MODULE_EXPORT namespace tgui
52{
56 class TGUI_API Texture
57 {
58 public:
59
60 using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
61 using BackendTextureLoaderFunc = std::function<bool(BackendTexture&, const String&, bool smooth)>;
62 using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;
63
68 Texture() = default;
69
80 Texture(const char* id,
81 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
82 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
83 bool smooth = m_defaultSmooth)
84 : Texture(String{id}, partRect, middlePart, smooth)
85 {
86 }
87
100 Texture(const String& id,
101 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
102 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
103 bool smooth = m_defaultSmooth);
104
105#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
120 TGUI_DEPRECATED("Use Texture() and loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
121 Texture(const sf::Texture& texture,
122 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
123 const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
124#endif
129
133 Texture(Texture&&) noexcept;
134
139
143 Texture& operator=(const Texture&);
144
148 Texture& operator=(Texture&&) noexcept;
149
160 void load(const String& id,
161 const UIntRect& partRect = {},
162 const UIntRect& middleRect = {},
163 bool smooth = m_defaultSmooth);
164
165#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
180 TGUI_DEPRECATED("Use loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
181 void load(const sf::Texture& texture,
182 const UIntRect& partRect = {},
183 const UIntRect& middleRect = {});
184#endif
196 void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
197
211 void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
212
225 void loadFromBase64(CharStringView imageAsBase64, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
226
233 TGUI_NODISCARD const String& getId() const;
234
241 TGUI_NODISCARD std::shared_ptr<TextureData> getData() const;
242
248 TGUI_NODISCARD Vector2u getImageSize() const;
249
255 TGUI_NODISCARD UIntRect getPartRect() const;
256
262 TGUI_NODISCARD bool isSmooth() const;
263
274 void setColor(const Color& color);
275
287 TGUI_NODISCARD const Color& getColor() const;
288
289#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
294 void setShader(sf::Shader* shader);
295
296
301 TGUI_NODISCARD sf::Shader* getShader() const;
302#endif
310 void setMiddleRect(const UIntRect& middleRect);
311
317 TGUI_NODISCARD UIntRect getMiddleRect() const;
318
327 TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const;
328
337 void setCopyCallback(const CallbackFunc& func);
338
347 void setDestructCallback(const CallbackFunc& func);
348
352 TGUI_NODISCARD bool operator==(const Texture& right) const;
353
357 TGUI_NODISCARD bool operator!=(const Texture& right) const;
358
369 static void setDefaultSmooth(bool smooth);
370
379 TGUI_NODISCARD static bool getDefaultSmooth();
380
390 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
391
399 TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader();
400
411 static void setTextureLoader(const TextureLoaderFunc& func);
412
421 TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader();
422
424 private:
425
434 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
435
437 private:
438
439#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
440 sf::Shader* m_shader = nullptr;
441#endif
442
443 std::shared_ptr<TextureData> m_data = nullptr;
444 Color m_color = Color::White;
445
446 UIntRect m_partRect;
447 UIntRect m_middleRect;
448 String m_id;
449
450 CallbackFunc m_copyCallback;
451 CallbackFunc m_destructCallback;
452
453 static bool m_defaultSmooth;
454
455 static TextureLoaderFunc m_textureLoader;
456 static BackendTextureLoaderFunc m_backendTextureLoader;
457 };
458
460}
461
463
464#endif // TGUI_TEXTURE_HPP
Base class for texture implementations that depend on the backend.
Definition BackendTexture.hpp:44
Wrapper for colors.
Definition Color.hpp:72
Wrapper class to store strings.
Definition String.hpp:101
Texture wrapper that internally reuses resources when multiple Texture objects are loaded from the sa...
Definition Texture.hpp:57
static void setTextureLoader(const TextureLoaderFunc &func)
Sets a different texture loader.
static TGUI_NODISCARD const TextureLoaderFunc & getTextureLoader()
Returns the used texture loader.
Texture(const Texture &)
Copy constructor.
static void setDefaultSmooth(bool smooth)
Changes whether textures are smoothed by default or not.
TGUI_NODISCARD bool isSmooth() const
Tells whether the smooth filter is enabled 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)
static TGUI_NODISCARD bool getDefaultSmooth()
Returns whether textures are smoothed by default or not.
Texture(Texture &&) noexcept
Move constructor.
TGUI_NODISCARD 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:80
TGUI_NODISCARD const String & getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename)
TGUI_NODISCARD std::shared_ptr< TextureData > getData() const
Returns the texture data.
void setShader(sf::Shader *shader)
Sets the shader used to draw the texture.
void loadFromBase64(CharStringView imageAsBase64, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from a base64 string.
void setColor(const Color &color)
Sets the global color of the texture.
TGUI_NODISCARD UIntRect getPartRect() const
Returns which part of the image was loaded.
static TGUI_NODISCARD const BackendTextureLoaderFunc & getBackendTextureLoader()
Returns the used backend texture loader.
TGUI_NODISCARD const Color & getColor() const
Returns the global color of the texture.
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.
TGUI_NODISCARD bool operator==(const Texture &right) const
Compares the texture with another one.
TGUI_NODISCARD UIntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
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.
TGUI_NODISCARD 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...
TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const
Checks if a certain pixel is transparent.
void setCopyCallback(const CallbackFunc &func)
Sets a callback function for when this texture is copied.
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.
Texture()=default
Default constructor.
TGUI_NODISCARD sf::Shader * getShader() const
Returns the shader used to draw the texture.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39