Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Wiki] Using sf::View  (Read 30810 times)

0 Members and 1 Guest are viewing this topic.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
[Wiki] Using sf::View
« 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.
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

The tutorial covers:
  • What can sf::View do?
  • How does sf::View work?
  • The source rectangle
  • The size and the constructor
  • The viewport rectangle
  • The convertCoords(…) function
  • Complete demonstration

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!
« Last Edit: October 18, 2012, 03:40:22 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: [Wiki] Using sf::View
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Wiki] Using sf::View
« Reply #2 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: [Wiki] Using sf::View
« Reply #3 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:

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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Wiki] Using sf::View
« Reply #4 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.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Wiki] Using sf::View
« Reply #5 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: [Wiki] Using sf::View
« Reply #6 on: July 02, 2012, 11:23:59 am »
Ok, thanks to both of you for the tips!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Wiki] Using sf::View
« Reply #7 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

J

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Wiki] Using sf::View
« Reply #8 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>")

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Wiki] Using sf::View
« Reply #9 on: September 19, 2015, 10:13:22 pm »
No need to delete your other post.

So what's the error exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

J

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Wiki] Using sf::View
« Reply #10 on: September 19, 2015, 10:18:17 pm »
It says no matching function for call to sf::Text::Text(const char [16])

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [Wiki] Using sf::View
« Reply #11 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)

J

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Wiki] Using sf::View
« Reply #12 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

J

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Wiki] Using sf::View
« Reply #13 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

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Wiki] Using sf::View
« Reply #14 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?