SFML community forums

General => SFML wiki => Topic started by: eXpl0it3r on June 23, 2012, 12:36:18 pm

Title: [Wiki] Using sf::View
Post by: eXpl0it3r on June 23, 2012, 12:36:18 pm
In the past I've had problems understanding how sf::View really works. A few weeks ago I sat down and started to experiment with it and ended up writing a tutorial or informational article on my blog (http://dev.my-gate.net/2012/06/using-sfview/).
To share the tutorial even more and keep some order I've now also converted my post to a wiki entry on GitHub.

Using sf::View (https://github.com/SFML/SFML/wiki/Tutorial:-Using-View)

The tutorial covers:

Everything is illustrated with some images and the final demonstration gives you something you can directly temper with.

I hope it will be usefull for some of you. :)

If you have any questions/suggestions, just answer to this thread and feel free to edit the wiki post if you find any mistakes!
Title: Re: [Wiki] Using sf::View
Post by: thePyro_13 on June 23, 2012, 03:15:33 pm
That's an amazing tutorial. Great work!

I did a quick read though, I feel like you should address the connection between view resolution and window resolution. As this can allow you to have a game that runs at a very low resolution, but can still let the window go fullscreen and use a high resolution without mucking up the game size.
Title: Re: [Wiki] Using sf::View
Post by: eXpl0it3r on June 23, 2012, 03:30:58 pm
That's an amazing tutorial. Great work!

I'm glad you like it! Took me some weeks to get it done. :)

I did a quick read though, I feel like you should address the connection between view resolution and window resolution. As this can allow you to have a game that runs at a very low resolution, but can still let the window go fullscreen and use a high resolution without mucking up the game size.

I understand what you're suggesting, but I feel it doesn't really fit into the tutorial. 'Using sf::View' should just give you a overview what the sf::View can do, but it isn't there to explain some rather specific tasks.
You could easily create a new tutorial focused on that topic, so it doen't even get lost in the hugh overiew tutorial. ;)
Title: Re: [Wiki] Using sf::View
Post by: Haze on June 26, 2012, 09:07:03 pm
That's a really good tutorial, I didn't know views have so many usages, and I've learnt a couple of things about viewports (that's a new feature, right?).

Until now, for split screens, I used a very different approach: a parent container inheriting sf::Transformable & sf::Drawable, and I apply the container's transformations on its children, in the overridden draw method.

class Container: public sf::Transformable, public sf::Drawable
{
    void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= getTransform();
        for each child
        {
            target.draw(child, states);
        }
    }
};

Should I use views instead? Is one method faster than the other?

Also, why the viewport rectangle is defined using ratio instead of dimensions in pixels? This puzzles me.
For example, if I want to split my screen between a game zone and an HUD zone like this:
(https://legacy.sfmluploads.org/cache/pics/245_views.png)
This leads to the following:
float h = (screen_height - 60) / screen_height;

sf::View game_view(sf::FloatRect(0, 0, screen_width, screen_height - 60));
player_view.setViewport(sf::FloatRect(0, 1 - h, 1, h));

sf::View hud_view(sf::FloatRect(0, 0, screen_width, 60));
hud_view.setViewport(sf::FloatRect(0, 0, 1, 1 - h));

So the ratio values are rounded, and I'm afraid this may result in non-pixel perfect positioning.
Title: Re: [Wiki] Using sf::View
Post by: Laurent on June 26, 2012, 09:18:26 pm
Quote
Should I use views instead? Is one method faster than the other?
Viewports are easier to define and use, and provide clipping. They might be a little faster, but that's probably not noticeable.

Quote
Also, why the viewport rectangle is defined using ratio instead of dimensions in pixels? This puzzles me.
The reason is that with ratios, the viewport can stretch automatically when the window is resized. Otherwise users would have to handle the Resized event and adjust their viewport accordingly.
Title: Re: [Wiki] Using sf::View
Post by: eXpl0it3r on June 26, 2012, 09:21:54 pm
That's a really good tutorial, I didn't know views have so many usages, and I've learnt a couple of things about viewports (that's a new feature, right?).

Thanks! :)
Yes, viewports are new in SFML 2.0.

Should I use views instead? Is one method faster than the other?

It's always hard to say what's better, since there are no benchmarkings on anything. But afaik the whole view/viewport stuff does kind of exist in OpenGL too, so it probably is better optimized there.

Also, why the viewport rectangle is defined using ratio instead of dimensions in pixels? This puzzles me.
...
So the ratio values are rounded, and I'm afraid this may result in non-pixel perfect positioning.

Yeah, I wondered a first too, but I guess they need to use ration, otherwise you can't resize the window with the viewport, since your viewport would be stuck at a certain pixel height/width.
Since all window resolutions are dividable by two, you should end up with a pretty good result if you choose your wanted height/width good enough.

Yeah Laurent was faster... ;D
Title: Re: [Wiki] Using sf::View
Post by: Haze on July 02, 2012, 11:23:59 am
Ok, thanks to both of you for the tips!
Title: Re: [Wiki] Using sf::View
Post by: eXpl0it3r on September 19, 2015, 09:56:49 pm
The text file("<0,0> -<0,0>") doesn't seem to exist.
I've no idea what you're trying to say. Can you be a bit more specific?
Title: Re: [Wiki] Using sf::View
Post by: J on September 19, 2015, 10:01:28 pm
Sorry for not being more specific. I seem to be getting an error on the line that says sf::Text position ("<0,0>-<0,0>")
Title: Re: [Wiki] Using sf::View
Post by: eXpl0it3r on September 19, 2015, 10:13:22 pm
No need to delete your other post.

So what's the error exactly?
Title: Re: [Wiki] Using sf::View
Post by: J on September 19, 2015, 10:18:17 pm
It says no matching function for call to sf::Text::Text(const char [16])
Title: Re: [Wiki] Using sf::View
Post by: G. on September 19, 2015, 10:26:44 pm
http://en.sfml-dev.org/forums/index.php?topic=18195.msg130950#msg130950
"sf::Text doesn't have a constructor with only one string parameter."
Use setString. The code on the wiki is correct, but the one inside the "code package" is outdated/wrong.
(and don't forget to load a font)
Title: Re: [Wiki] Using sf::View
Post by: J on September 20, 2015, 06:34:00 am
Thanks that works now and thanks for the quick responses. Just  one more question, why doesn't the sprite get displayed on the minimap
Title: Re: [Wiki] Using sf::View
Post by: J on September 20, 2015, 01:43:47 pm
So I'm making a game where everything that occurs on the screen needs to be displayed on the minimap. Now the background of the screen is displayed on the minimap(using your code), but I loaded a sprite that is moving and shooting and that's not showing on the minimap
Title: Re: [Wiki] Using sf::View
Post by: Jesper Juhl on September 20, 2015, 01:49:14 pm
Just a guess since you don't provide many details.
Maybe your sprite is very small and your minimap is also very small, so when scaling the sprite down to the size of the minimap it ends up with a size smaller than one pixel?
Title: [Wiki] Using sf::View
Post by: eXpl0it3r on September 20, 2015, 01:51:40 pm
Or the sprite isn't rendered to the minimap view. Be a while since I wrote that code...
Title: Re: [Wiki] Using sf::View
Post by: J on September 20, 2015, 02:17:40 pm
How do i render the sprite to the minimap. Sorry it's just that it's my first time using sfml
Title: Re: [Wiki] Using sf::View
Post by: Jesper Juhl on September 20, 2015, 02:33:05 pm
That (how to render to a view) is explained both in the wiki tutorial (https://github.com/SFML/SFML/wiki/Tutorial:-Using-View) and the official sf::View tutorial (http://www.sfml-dev.org/tutorials/2.3/graphics-view.php).
Read them again.
Title: Re: [Wiki] Using sf::View
Post by: J on September 21, 2015, 04:43:04 am
i get it now thank you