TGUI  0.9.1
Texture.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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/Color.hpp>
35 #include <TGUI/Rect.hpp>
36 #include <functional>
37 
38 #if TGUI_HAS_BACKEND_SFML
39  #include <SFML/Graphics/Texture.hpp>
40  #include <SFML/Graphics/Shader.hpp>
41 #endif
42 
44 
45 namespace tgui
46 {
47  class TGUI_API Texture
48  {
49  public:
50 
51  using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
52  using BackendTextureLoaderFunc = std::function<bool(BackendTextureBase&, const String&)>;
53  using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;
54 
55 
60  Texture() {}
61 
62 
73  Texture(const char* id,
74  const UIntRect& partRect = UIntRect(0, 0, 0, 0),
75  const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
76  bool smooth = true)
77  : Texture(String{id}, partRect, middlePart, smooth)
78  {
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 = true);
98 
99 #if TGUI_HAS_BACKEND_SFML
111  Texture(const sf::Texture& texture,
112  const UIntRect& partRect = UIntRect(0, 0, 0, 0),
113  const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
114 #endif
115 
119  Texture(const Texture&);
120 
124  Texture(Texture&&) noexcept;
125 
130 
134  Texture& operator=(const Texture&);
135 
139  Texture& operator=(Texture&&) noexcept;
140 
141 
152  void load(const String& id,
153  const UIntRect& partRect = {},
154  const UIntRect& middleRect = {},
155  bool smooth = true);
156 
157 #if TGUI_HAS_BACKEND_SFML
169  void load(const sf::Texture& texture,
170  const UIntRect& partRect = {},
171  const UIntRect& middleRect = {});
172 #endif
173 
184  TGUI_DEPRECATED("Use loadFromPixelData instead") bool load(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {});
185 
186 
198  void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = true);
199 
200 
214  void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = true);
215 
216 
223  const String& getId() const;
224 
225 
232  std::shared_ptr<TextureData> getData() const;
233 
234 
241 
242 
250 
251 
257  bool isSmooth() const;
258 
259 
270  void setColor(const Color& color);
271 
272 
284  const Color& getColor() const;
285 
286 #if TGUI_HAS_BACKEND_SFML
291  void setShader(sf::Shader* shader);
292 
293 
298  sf::Shader* getShader() const;
299 #endif
300 
308 
309 
318  bool isTransparentPixel(Vector2u pos) const;
319 
320 
329  void setCopyCallback(const CallbackFunc& func);
330 
331 
340  void setDestructCallback(const CallbackFunc& func);
341 
342 
346  bool operator==(const Texture& right) const;
347 
348 
352  bool operator!=(const Texture& right) const;
353 
354 
364  static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
365 
366 
374  static const BackendTextureLoaderFunc& getBackendTextureLoader();
375 
376 
387  static void setTextureLoader(const TextureLoaderFunc& func);
388 
389 
398  static const TextureLoaderFunc& getTextureLoader();
399 
400 
402  private:
403 
412  void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
413 
414 
416  private:
417 
418 #if TGUI_HAS_BACKEND_SFML
419  sf::Shader* m_shader = nullptr;
420 #endif
421 
422  std::shared_ptr<TextureData> m_data = nullptr;
423  Color m_color = Color::White;
424 
425  UIntRect m_partRect;
426  UIntRect m_middleRect;
427  String m_id;
428 
429  CallbackFunc m_copyCallback;
430  CallbackFunc m_destructCallback;
431 
432  static TextureLoaderFunc m_textureLoader;
433  static BackendTextureLoaderFunc m_backendTextureLoader;
434  };
435 
437 }
438 
440 
441 #endif // TGUI_TEXTURE_HPP
Base class for texture implementations that depend on the backend.
Definition: BackendTexture.hpp:41
Wrapper for colors.
Definition: Color.hpp:52
static const Color White
White predefined color.
Definition: Color.hpp:237
Wrapper class to store strings.
Definition: String.hpp:74
Definition: Texture.hpp:48
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.
static const TextureLoaderFunc & getTextureLoader()
Returns the used texture loader.
Texture(const Texture &)
Copy constructor.
const String & getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename)
bool isTransparentPixel(Vector2u pos) const
Checks if a certain pixel is transparent.
Texture(Texture &&) noexcept
Move constructor.
static const BackendTextureLoaderFunc & getBackendTextureLoader()
Returns the used backend texture loader.
bool operator==(const Texture &right) const
Compares the texture with another one.
std::shared_ptr< TextureData > getData() const
Returns the texture data.
void loadFromPixelData(Vector2u size, const std::uint8_t *pixels, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=true)
Loads the texture from an array of 32-bits RGBA pixels.
bool operator!=(const Texture &right) const
Compares the texture with another one.
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:60
bool isSmooth() const
Tells whether the smooth filter is enabled or not.
static void setBackendTextureLoader(const BackendTextureLoaderFunc &func)
Sets a different backend texture loader.
Texture(const char *id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=true)
Constructor that created the texture.
Definition: Texture.hpp:73
void setCopyCallback(const CallbackFunc &func)
Sets a callback function for when this texture is copied.
Texture(const String &id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=true)
Constructor that created the texture.
void setDestructCallback(const CallbackFunc &func)
Sets a callback function for when this texture is destroyed.
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...
void loadFromMemory(const std::uint8_t *data, std::size_t dataSize, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=true)
Loads the texture from memory (data in memory should contain the entire file, not just the pixels)
const Color & getColor() const
Returns the global color of the texture.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36