TGUI  0.10-beta
Texture.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#include <functional>
38
39#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
40 namespace sf
41 {
42 class Texture;
43 class Shader;
44 }
45#endif
46
48
49namespace tgui
50{
51 class TGUI_API Texture
52 {
53 public:
54
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
59
65
66
77 Texture(const char* id,
78 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
79 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
80 bool smooth = m_defaultSmooth)
81 : Texture(String{id}, partRect, middlePart, smooth)
82 {
83 }
84
85
98 Texture(const String& id,
99 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
100 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
101 bool smooth = m_defaultSmooth);
102
103#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
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
119
124
128 Texture(Texture&&) noexcept;
129
134
138 Texture& operator=(const Texture&);
139
143 Texture& operator=(Texture&&) noexcept;
144
145
156 void load(const String& id,
157 const UIntRect& partRect = {},
158 const UIntRect& middleRect = {},
159 bool smooth = m_defaultSmooth);
160
161#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
173 void load(const sf::Texture& texture,
174 const UIntRect& partRect = {},
175 const UIntRect& middleRect = {});
176#endif
177
189 void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
190
191
205 void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
206
207
214 const String& getId() const;
215
216
223 std::shared_ptr<TextureData> getData() const;
224
225
232
233
241
242
248 bool isSmooth() const;
249
250
261 void setColor(const Color& color);
262
263
275 const Color& getColor() const;
276
277#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
282 void setShader(sf::Shader* shader);
283
284
289 sf::Shader* getShader() const;
290#endif
291
299
300
310
311
320 void setCopyCallback(const CallbackFunc& func);
321
322
331 void setDestructCallback(const CallbackFunc& func);
332
333
337 bool operator==(const Texture& right) const;
338
339
343 bool operator!=(const Texture& right) const;
344
345
356 static void setDefaultSmooth(bool smooth);
357
358
367 static bool getDefaultSmooth();
368
369
379 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
380
381
389 static const BackendTextureLoaderFunc& getBackendTextureLoader();
390
391
402 static void setTextureLoader(const TextureLoaderFunc& func);
403
404
413 static const TextureLoaderFunc& getTextureLoader();
414
415
417 private:
418
427 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
428
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:41
Wrapper for colors.
Definition: Color.hpp:63
static const Color White
White predefined color.
Definition: Color.hpp:254
Wrapper class to store strings.
Definition: String.hpp:79
Definition: Texture.hpp:52
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.
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.
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:77
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.
bool operator!=(const Texture &right) const
Compares the texture with another one.
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.
Texture()
Default constructor.
Definition: Texture.hpp:64
bool isSmooth() const
Tells whether the smooth filter is enabled or not.
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.
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...
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36