A way to avoid copying data ?

Started by Nafffen, 11 July 2023, 14:24:07

Nafffen

Is there really no way to avoid data copying when create a Sprite from sf::Texture ?
I want to have a widget that embed a huge sf::Texture, but it takes some time to copy it into a tgui::Texture and I dont like that.
I thought I could do a trick by drawing a sfml sprite in the draw function of my custom widget but the clipping doesn't work anymore obviously.
Is there a way, even tricky with custom code, to have a reference to sfml texture data ?
Thank you

texus

There is no way to let TGUI reference an existing SFML texture without copying it. The best alternative depends on where exactly the sf::Texture is coming from.

QuoteIs there a way, even tricky with custom code, to have a reference to sfml texture data ?
TGUI uses an sf::Texture internally, which indirectly can be accessed, but TGUI needs ownership of the sf::Texture. So you can write data directly into TGUI's internal sf::Texture (with e.g. the sf::Texture::update function), but this method isn't useful if the sf::Texture needs to be created in your own code (e.g. if it is created from an sf::RenderTexture).

QuoteI thought I could do a trick by drawing a sfml sprite in the draw function of my custom widget
Have you tried drawing the sprite to a tgui::CanvasSFML (https://tgui.eu/tutorials/1.0/canvas/)?

Quotebut the clipping doesn't work anymore obviously
Which clipping? Some clipping from your own code? Because a custom widget can define a clipping region during its draw code.

QuoteI want to have a widget that embed a huge sf::Texture, but it takes some time to copy it into a tgui::Texture and I dont like that.
How big an how long are you talking about?
The slowest part is probably the call to texture.copyToImage() so that TGUI gets the pixel data from the texture. In most cases I actually don't think you need the pixel data itself. So if copying one sf::Texture to another sf::Texture is fast enough for your task then maybe I can provide you with a hack that skips the copyToImage() call.

Nafffen

QuoteWhich clipping? Some clipping from your own code? Because a custom widget can define a clipping region during its draw code.
Yes I was talking of my own custom clipping in my custom draw function

QuoteHave you tried drawing the sprite to a tgui::CanvasSFML (https://tgui.eu/tutorials/1.0/canvas/)?
My bad I didn't know this widget, it is quite useful indeed. Just one question, I have a canvas full screen, and most of it is transparent, but the whole canvas is triggering events so even if I click on transparent area, the event is consumed. I tried to define a custom isMouseOnWidget to return false (because I dont care about events on this widget) but the event is still consumed.

QuoteHow big an how long are you talking about?
I have a huge sf::RenderTexture somewhere, I want to do a "minimap" widget of it. I defined a custom widget to add two scrollbars and originally a tgui::sprite to draw the map. But of course the data is copied.
I should success to have something with canvasSFML

texus

Quotebut the whole canvas is triggering events so even if I click on transparent area, the event is consumed. I tried to define a custom isMouseOnWidget to return false (because I dont care about events on this widget) but the event is still consumed.
It's normal that is doesn't check whether the part is transparent or not, but if you override the isMouseOnWidget to always return false then the events should pass to the widget behind it.

Nafffen

Ok this is good with canvas, I could manage to have something cool
Quotebut if you override the isMouseOnWidget to always return false then the events should pass to the widget behind it
Yes my bad I was wrong on something,
Thank you