TGUI  1.1
Loading...
Searching...
No Matches
Texture.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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)
117 TGUI_DEPRECATED("Use Texture() and loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
118 Texture(const sf::Texture& texture,
119 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
120 const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
121#endif
126
130 Texture(Texture&&) noexcept;
131
136
140 Texture& operator=(const Texture&);
141
145 Texture& operator=(Texture&&) noexcept;
146
157 void load(const String& id,
158 const UIntRect& partRect = {},
159 const UIntRect& middleRect = {},
160 bool smooth = m_defaultSmooth);
161
162#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
174 TGUI_DEPRECATED("Use loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
175 void load(const sf::Texture& texture,
176 const UIntRect& partRect = {},
177 const UIntRect& middleRect = {});
178#endif
190 void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
191
205 void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
206
219 void loadFromBase64(CharStringView imageAsBase64, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
220
227 TGUI_NODISCARD const String& getId() const;
228
235 TGUI_NODISCARD std::shared_ptr<TextureData> getData() const;
236
242 TGUI_NODISCARD Vector2u getImageSize() const;
243
249 TGUI_NODISCARD UIntRect getPartRect() const;
250
256 TGUI_NODISCARD bool isSmooth() const;
257
268 void setColor(const Color& color);
269
281 TGUI_NODISCARD const Color& getColor() const;
282
283#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
288 void setShader(sf::Shader* shader);
289
290
295 TGUI_NODISCARD sf::Shader* getShader() const;
296#endif
304 void setMiddleRect(const UIntRect& middleRect);
305
311 TGUI_NODISCARD UIntRect getMiddleRect() const;
312
321 TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const;
322
331 void setCopyCallback(const CallbackFunc& func);
332
341 void setDestructCallback(const CallbackFunc& func);
342
346 TGUI_NODISCARD bool operator==(const Texture& right) const;
347
351 TGUI_NODISCARD bool operator!=(const Texture& right) const;
352
363 static void setDefaultSmooth(bool smooth);
364
373 TGUI_NODISCARD static bool getDefaultSmooth();
374
384 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
385
393 TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader();
394
405 static void setTextureLoader(const TextureLoaderFunc& func);
406
415 TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader();
416
418 private:
419
428 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
429
431 private:
432
433#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
434 sf::Shader* m_shader = nullptr;
435#endif
436
437 std::shared_ptr<TextureData> m_data = nullptr;
438 Color m_color = Color::White;
439
440 UIntRect m_partRect;
441 UIntRect m_middleRect;
442 String m_id;
443
444 CallbackFunc m_copyCallback;
445 CallbackFunc m_destructCallback;
446
447 static bool m_defaultSmooth;
448
449 static TextureLoaderFunc m_textureLoader;
450 static BackendTextureLoaderFunc m_backendTextureLoader;
451 };
452
454}
455
457
458#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