-
Hi,
I'm new to smfl and I was wondering how I can scale my content to fit into the window without having a global scale variable which has to be used in every frame. I tried a simple approach with RenderTextures, but calling the GetTexture() method every frame seems to be VERY slow. Any ideas?
-
Did you try Views?
-
No, but i found a tutorial doing EXACTLY what I'm doing, which is weird, since this method reduces the performance of my program to about 5 frames per second:
window.clear();
renderTarget.clear();
RenderFrame();
renderTarget.display();
contentTexture = renderTarget.getTexture();
auto spr = sf::Sprite(contentTexture);
window.draw(spr);
window.display();
edit: I will use views now, I'm still interested why this does kill the performance.
-
window.clear();
renderTarget.clear();
RenderFrame();
renderTarget.display();
//contentTexture = renderTarget.getTexture(); // NO-NO
auto spr = sf::Sprite(renderTarget.getTexture());//Yes!
window.draw(spr);//could be window.draw(sf::Sprite(renderTarget.getTexture()));
window.display();
You're making a roundtrip to CPU and then back to GPU every frame now, which is terrible really and takes a long time.
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Texture.cpp#L82
-
There is no way to render to a texture on the GPU ?
-
Your question makes no sense, everything is rendered on GPU but what you are doing is copying texture every frame(instead of using the render texture's texture directly) which takes a long time because it goes from GPU to CPU then back to GPU, you should do that instead:
window.clear();
renderTarget.clear();
RenderFrame();
renderTarget.display();
auto spr = sf::Sprite(renderTarget.getTexture());
window.draw(spr);
window.display();
-
I did that first, it makes absolutely no difference. GetTexture does not return a pointer, so the texture will be copied anyway, won't it?
-
It returns a const reference and Sprite takes a const reference so there is no copy then.
-
okay thanks.
-
The roudtrip via RenderTexture isn't really needed either (unless I didn't understand what you're trying to do), because as Ghosa pointed out, you can simply use an sf::View to make the content fit your window. The official tutorials explain views quite well, you should read it! :)
-
Yeah but views can't be nested right ?
-
Also how do I draw a sprite with a fixed size? My sprite is always rendered fullscreen? I looked into the tutorials, they all describe relative scaling, but none shows how to draw s sprite with an absolute size.
-
Not sure what you mean with nested, but you can use multiple views.
There's no absolute scaling. A sprite simple represents a texture' axis aligned bounding box. If you want it absolute scaled you need to calculate the scaling yourself. ;)