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

Author Topic: Viewports  (Read 4435 times)

0 Members and 1 Guest are viewing this topic.

Bross

  • Newbie
  • *
  • Posts: 3
    • View Profile
Viewports
« on: August 01, 2014, 11:47:36 pm »
Is there method to set pixel perfect viewport position? For example I have 1024x768 window and I want set viewport on 50x50 with 200 width and 200 height. And in that viewport I do not want any zoom, any streching, just plain 1:1 display as in default view but clipped.

It is not intuitive that viewport use ratios and not absolute pixels as any other SFML functions. It is very confusing.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Viewports
« Reply #1 on: August 02, 2014, 01:34:28 am »
What does "on 50x50" mean?

Wouldn't setting the viewport to (0, 0, viewWidth / windowWidth, viewHeight / windowHeight) give you the size you want, assuming that your view is of size 200x200.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bross

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Viewports
« Reply #2 on: August 02, 2014, 01:39:43 am »
What does "on 50x50" mean?

Absolute position in main window, which is 1024x768, so is in upper left.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Viewports
« Reply #3 on: August 02, 2014, 02:12:16 am »
What does "on 50x50" mean?
Absolute position in main window, which is 1024x768, so is in upper left.
So... the top-left of the viewport is at (50,50)?
If so, you could apply the same calculations to find out 50 pixels. e.g. viewportOffsetX = 50 / windowWidth.
Then, you could just add it to both of the x ratios.
Same for y.

Let me know if this works. Not certain of my calculations  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bross

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Viewports
« Reply #4 on: August 02, 2014, 01:22:10 pm »
So... the top-left of the viewport is at (50,50)?
If so, you could apply the same calculations to find out 50 pixels. e.g. viewportOffsetX = 50 / windowWidth.
Then, you could just add it to both of the x ratios.
Same for y.

Thanks! This works.

        int viewportOffsetX = 50;
        int viewportOffsetY = 50;
        int viewportWidth = 200;
        int viewportHeight = 200;
       
        int windowWidth = window.getSize().x;
        int windowHeight = window.getSize().y;
       
        sf::View view(sf::FloatRect(static_cast<float>(viewportOffsetX), static_cast<float>(viewportOffsetY),
                                                                        static_cast<float>(viewportWidth), static_cast<float>(viewportHeight)));

        float viewportOffsetXRatio = static_cast<float>(viewportOffsetX) / windowWidth;
        float viewportOffsetYRatio = static_cast<float>(viewportOffsetY) / windowHeight;

        float viewportWidthRatio = static_cast<float>(viewportWidth) / windowWidth;
        float viewportHeightRatio = static_cast<float>(viewportHeight) / windowHeight;

        view.setViewport(sf::FloatRect(viewportOffsetXRatio, viewportOffsetYRatio,
                                                                viewportWidthRatio, viewportHeightRatio));

        window.setView(view);
 

Let me know if this works. Not certain of my calculations  :P

This is why i call it unintutive.  There will be method for set viewport without this calculation (IMHO setViewport internally doing exact reverse calculations, so there are round errors).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Viewports
« Reply #5 on: August 03, 2014, 02:55:21 pm »
You're welcome. Glad I could help.

You may be right about it seemingly needing more work than necessary but it's probably more common to use it needing the ratio (your case is possibly more rare) and it's not like it's not easy to create a simple function that does these calculations for you when you need them  ;)
I don't think that floats are inaccurate enough to lose that much precision in two calculations i.e. a multiplication and a division. If you were constantly doing these, then it may do, but why would you?  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Viewports
« Reply #6 on: August 03, 2014, 04:00:47 pm »
When you need a certain entity to be rendered pixel-perfect, it's better to just set the entity's size and position accordingly.  Views are best for when you want large numbers of entities (eg, the entire game world) to be in a particular region of the screen without having to add a zillion if/switch statements to deal with every possible resolution or game mode.

If you need large numbers of entities to be displayed in different window regions in a pixel-perfect way, sooner or later you'll probably have to do this kind of adjustment at the application level to make it work properly, be it on every individual entity or on a single view.  SFML can't really hide that from you., since upscaling/downscaling (which is what views often do) simply cannot be done pixel-perfectly.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Viewports
« Reply #7 on: August 03, 2014, 05:56:09 pm »
In a flash of (probably misguided and incorrect) genius, I realised that you can also use a render texture for this. Create one of size 200x200 and set its position at (50, 50).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*