TGUI  1.0.0
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
26#ifndef TGUI_TEXTURE_HPP
27#define TGUI_TEXTURE_HPP
28
30
31#include <TGUI/TextureData.hpp>
32#include <TGUI/Vector2.hpp>
33#include <TGUI/String.hpp>
34#include <TGUI/Global.hpp>
35#include <TGUI/Color.hpp>
36#include <TGUI/Rect.hpp>
37
38#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
39 #include <functional>
40#endif
41
42#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_BUILD_AS_CXX_MODULE
43 namespace sf
44 {
45 class Texture;
46 class Shader;
47 }
48#endif
49
51
52TGUI_MODULE_EXPORT namespace tgui
53{
57 class TGUI_API Texture
58 {
59 public:
60
61 using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
62 using BackendTextureLoaderFunc = std::function<bool(BackendTexture&, const String&, bool smooth)>;
63 using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;
64
65
70 Texture() = default;
71
72
83 Texture(const char* id,
84 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
85 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
86 bool smooth = m_defaultSmooth)
87 : Texture(String{id}, partRect, middlePart, smooth)
88 {
89 }
90
91
104 Texture(const String& id,
105 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
106 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
107 bool smooth = m_defaultSmooth);
108
109#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
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
125
130
134 Texture(Texture&&) noexcept;
135
140
144 Texture& operator=(const Texture&);
145
149 Texture& operator=(Texture&&) noexcept;
150
151
162 void load(const String& id,
163 const UIntRect& partRect = {},
164 const UIntRect& middleRect = {},
165 bool smooth = m_defaultSmooth);
166
167#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
179 void load(const sf::Texture& texture,
180 const UIntRect& partRect = {},
181 const UIntRect& middleRect = {});
182#endif
183
195 void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
196
197
211 void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
212
213
226 void loadFromBase64(CharStringView imageAsBase64, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
227
228
235 TGUI_NODISCARD const String& getId() const;
236
237
244 TGUI_NODISCARD std::shared_ptr<TextureData> getData() const;
245
246
252 TGUI_NODISCARD Vector2u getImageSize() const;
253
254
261 TGUI_NODISCARD UIntRect getPartRect() const;
262
263
269 TGUI_NODISCARD bool isSmooth() const;
270
271
282 void setColor(const Color& color);
283
284
296 TGUI_NODISCARD const Color& getColor() const;
297
298#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
303 void setShader(sf::Shader* shader);
304
305
310 TGUI_NODISCARD sf::Shader* getShader() const;
311#endif
312
319 TGUI_NODISCARD UIntRect getMiddleRect() const;
320
321
330 TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const;
331
332
341 void setCopyCallback(const CallbackFunc& func);
342
343
352 void setDestructCallback(const CallbackFunc& func);
353
354
358 TGUI_NODISCARD bool operator==(const Texture& right) const;
359
360
364 TGUI_NODISCARD bool operator!=(const Texture& right) const;
365
366
377 static void setDefaultSmooth(bool smooth);
378
379
388 TGUI_NODISCARD static bool getDefaultSmooth();
389
390
400 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
401
402
410 TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader();
411
412
423 static void setTextureLoader(const TextureLoaderFunc& func);
424
425
434 TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader();
435
436
438 private:
439
448 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
449
450
452 private:
453
454#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
455 sf::Shader* m_shader = nullptr;
456#endif
457
458 std::shared_ptr<TextureData> m_data = nullptr;
459 Color m_color = Color::White;
460
461 UIntRect m_partRect;
462 UIntRect m_middleRect;
463 String m_id;
464
465 CallbackFunc m_copyCallback;
466 CallbackFunc m_destructCallback;
467
468 static bool m_defaultSmooth;
469
470 static TextureLoaderFunc m_textureLoader;
471 static BackendTextureLoaderFunc m_backendTextureLoader;
472 };
473
475}
476
478
479#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:58
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.
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:83
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.
Texture(const sf::Texture &texture, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0))
Constructor that created the texture from an existing sf::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 load(const sf::Texture &texture, const UIntRect &partRect={}, const UIntRect &middleRect={})
Creates the texture from an existing sf::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