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

Author Topic: sf::View, Viewports and scaling - Default behavior  (Read 5771 times)

0 Members and 1 Guest are viewing this topic.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
sf::View, Viewports and scaling - Default behavior
« on: November 22, 2012, 06:51:05 am »
I have been doing a little experimenting with sf::View.  I'd wanted to use 2 sf::Views in combination with their viewports to map some content to different areas of the screen.  One area of the screen would be for the 'action' and one area would be for information related to what was occurring on the 'action' portion in a 75% - 25% split.  Because I don't need nearly the same size to render the action area and the information area, I defined the view for the information area to be much smaller than the view for the action area.

The following code/picture should illustrate the result I achieved.  The only difference in the code here and the code I used is that the background tiles I used to differentiate the two areas has been removed for brevity.  The text and the rectangle shape should be faithfully reproduced, although you'll have to supply your own font!

#include <SFML/Graphics.hpp>

const sf::Vector2f leftSize(1440.0f, 1080.0f) ;
const sf::Vector2f rightSize(480.0f, 1080.0f) ;
const sf::Vector2f origin(0.0f, 0.0f) ;

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "Text & View" ) ;

    sf::View leftView( sf::FloatRect(origin, leftSize) ) ;
    leftView.setViewport(sf::FloatRect(0.0f, 0.0f, 0.75f, 1.0f)) ;

    sf::View rightView( sf::FloatRect(origin, rightSize) ) ;
    rightView.setViewport(sf::FloatRect(0.75f, 0.0f, 1.0f, 1.0f)) ;

    sf::RectangleShape rect(sf::Vector2f(120.0f, 50.0f)) ;
    rect.setPosition(10, 15) ;
    rect.setFillColor(sf::Color(0,0,127)) ;

    sf::Font font ;
    font.loadFromFile("SketchFlow.ttf") ;

    sf::Text text("Word!", font);
    text.setColor(sf::Color(127, 255, 63, 255)) ;
    text.setPosition(20.0, 20.0) ;

    while ( window.isOpen() )
    {
        sf::Event event ;
        while ( window.pollEvent(event) )
        {
            if ( event.type == sf::Event::Closed )
                window.close() ;
        }

        window.clear() ;
       
        window.setView(leftView) ;
        window.draw(rect) ;
        window.draw(text) ;

        window.setView(rightView) ;
        window.draw(rect) ;
        window.draw(text) ;

        window.display() ;
    }
}

The result:


Someone correct me if I'm wrong:  the content in the sf::views that represent the right and left seems to be scaled from the view's respective sizes to the view's initial viewport size (in this case, 1280x720.)  This seems a little counter-intuitive to me.  I would've expected the content to be scaled from the size of the view to the size of the current viewport size.  I haven't yet been mucking about in the internals of SFML, so I don't know if this expectation is reasonable given the implementation.

I did try to implement a few solutions.  Unfortunately, the zoom can't be used to adjust the scaling factor, because the height to width ratio isn't the same, in other words I can't just zoom out from the x-axis without also zooming out from the y-axis.  Adjusting the size of the right view so the height/width ratio is correct and then using zoom to adjust the scaling factor does seem to work, although it feels very hackish.  The easiest solution I found was just to give the right view the same size as the left view, so that they both scaled the same.  Again, that still feels a little hackish.

So, I guess my question is whether or not this behavior is right?  The documentation states:

Quote
If the source rectangle has not the same size as the viewport, its contents will be stretched to fit in.

which seems to fit what my expectations were.

Any insights?  Is there something I'm missing?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: sf::View, Viewports and scaling - Default behavior
« Reply #2 on: November 22, 2012, 07:24:22 am »
FloatRect take Left, then Top, then Width, then Height.
So use FloatRect(0.75f, 0, 0.25f, 1.0f) instead of FloatRect(0.75f, 0, 1.0f, 1.0f) ;)

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::View, Viewports and scaling - Default behavior
« Reply #3 on: November 22, 2012, 07:58:16 am »
Gyscos.. you rock.  I don't know how long I was fussing around with that.

Thanks!

eXpl0it3r, I understood them well enough.  It seems I just made a simple mistake specifying the viewport.   :(


[edit:  Now that I think back on it, that's what I had originally, but I was experiencing a problem with a tiled texture displaying correctly and I must've accidentally changed it at some point -- live and learn.  All working correct now! :)]
« Last Edit: November 22, 2012, 08:10:23 am by cire »