Texture class

Started by Garwin, 15 November 2023, 20:13:43

Garwin

I have just recently updated my old project from TGUI 0.9 to TGUI 1.1

I get some warning:
src\game_state_game.cpp|62|warning: 'tgui::Texture::Texture(const sf::Texture&, const tgui::UIntRect&, const tgui::UIntRect&)' is deprecated: Use Texture() and loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead [-Wdeprecated-declarations]|

Do I get it correctly, that the change to TGUI 1.1 is depreciation of direct construction of tgui::Texture from sf::Texture and shows more clearly the intention that TGUI copy that texture internally (slow) so using function loadFromPixelData is more appropriate? And having correct non-depreciated code is than:
tgui::Texture tguiPicture;
tguiPicture.loadFromPixelData(sfPicture.getSize(), sfPicture.copyToImage().getPixelsPtr());
auto picture = tgui::Picture::create(std::move(tguiPicture));
Do I have it correct that if my texture manager using sf::Texture to store all textures, than there is no way to avoid copies from sf::Texture to tgui::Texture?

texus

#1
Correct.

The reason why this change was made is because a copy was always being made before as well, the constructor overload that accepted an sf::Texture object is calling the slow copyToImage function internally. The deprecation makes it a lot more clear that this method shouldn't be used.

TGUI requires ownership of the sf::Texture, so you can't load one in your own code and then share it with TGUI.

If you load a tgui::Texture with a filename, TGUI's internal texture manager will already handle sharing the image between all tgui::Texture objects that were loaded with the same filename (and the image will be released once there are no more tgui::Texture objects with that image).