Accessing textures

Started by starkhorn, 04 July 2015, 04:45:21

starkhorn

In the theme conf file for child window, there is the setting for title bar where you specify the titlebar image file. I'm assuming tgui loads that in and stores it as a texture?

Now say I wanted to access that same texture again. For example say I want to sub divide the child window so I want to draw that titlebar again. Is there a way I can access that texture so I can assign it to a sf::sprite? Note I list childwindow as an example. I'd also be interested on how to access say the button png texture or radio button etc.


texus

You can't get access to the texture from the widget directly.

You could get it through the texture manager, but you will have to know the filename of the texture. So this method works if you want access to a specific texture, but can't be used to get the same texture as a widget without knowing what it was loaded with. The "(0, 0, 300, 25)" in this code is the part of the image to load, in this case the child window title bar.
Code (cpp) Select
tgui::Texture temp;
tgui::TGUI_TextureManager.getTexture("TGUI/widgets/Black.conf", temp, sf::IntRect(0, 0, 300, 25));
sf::Texture& texture = temp.data->texture; // you can assign this to a sprite
sf::Sprite& sprite = temp.sprite; // or just use the sprite that already exists


(you will actually have to store the 'temp' variable, because you have to call tgui::TGUI_TextureManager.removeTexture(temp) to release the texture again, otherwise you will create a memory leak)

The only advantage you get from using the above code instead of the following is that the texture manager will reuse the same texture if it was already loaded.
Code (cpp) Select
sf::Texture texture;
texture.loadFromFile("TGUI/widgets/Black.conf", sf::IntRect(0, 0, 300, 25));
sf::Sprite sprite(texture);


But why would you want to access the textures that the widgets use?