SFML community forums

Help => Graphics => Topic started by: pixartist on November 23, 2013, 04:35:14 pm

Title: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 04:35:14 pm
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?
Title: Re: Resolution-Independent Rendering?
Post by: Rhimlock on November 23, 2013, 04:55:44 pm
Did you try Views?
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 05:00:11 pm
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.
Title: Re: Resolution-Independent Rendering?
Post by: FRex on November 23, 2013, 05:17:41 pm
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
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 05:21:07 pm
There is no way to render to a texture on the GPU ?
Title: Re: Resolution-Independent Rendering?
Post by: FRex on November 23, 2013, 05:23:57 pm
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();
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 05:42:35 pm
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?
Title: Re: Resolution-Independent Rendering?
Post by: FRex on November 23, 2013, 05:46:55 pm
It returns a const reference and Sprite takes a const reference so there is no copy then.
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 05:57:00 pm
okay thanks.
Title: Re: Resolution-Independent Rendering?
Post by: eXpl0it3r on November 23, 2013, 05:57:25 pm
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! :)
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 06:37:32 pm
Yeah but views can't be nested right ?
Title: Re: Resolution-Independent Rendering?
Post by: pixartist on November 23, 2013, 07:51:54 pm
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.
Title: AW: Resolution-Independent Rendering?
Post by: eXpl0it3r on November 23, 2013, 10:34:01 pm
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. ;)