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

Author Topic: strange behaviour with View and Viewport  (Read 3079 times)

0 Members and 1 Guest are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
strange behaviour with View and Viewport
« on: October 25, 2019, 02:52:03 pm »
hello
I'm having trouble understanding this Views and Viewport situation:
in this code, the text appears while the mouse cursor is inside view_one, which occupies 100% of the height and 80% of the width (because the Viewport is (0, 0, 0,8, 1)):
#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Views");
    sf::View view_one, view_two;
    view_one.setViewport(sf::FloatRect(0, 0, 0.8, 1));
    view_two.setViewport(sf::FloatRect(0.8, 0, 0.2, 1));
    sf::Font font;
    font.loadFromFile("LondrinaSolid-Regular.otf");
    sf::Text text("Cursor is inside View One", font);
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed) window.close();
        }
        sf::Vector2f pos_in_view = window.mapPixelToCoords(sf::Mouse::getPosition(window), view_one);
        window.clear();
        if (sf::IntRect(view_one.getCenter().x-view_one.getSize().x/2, view_one.getCenter().y-view_one.getSize().y/2, view_one.getCenter().x+view_one.getSize().x/2, view_one.getCenter().y+view_one.getSize().y/2).contains(sf::Vector2i(pos_in_view))){
            window.setView(view_one);
            window.draw(text);
        }
        window.display();
    }
    return 0;
}


it works. but if I resize the view_one,
view_one.setSize(3000, 3000);
the viewport seems to be resized too




is that supposed to happen? how do I keep the correct proportions, so I can always check if the cursor is inside a given sf::View?

EDIT: I know that the VIEW should be resized, and with it the text inside. but the Viewport should not, I think. notice how the viewport shrinks to almost half the window size in the second image

EDIT 2: it works if I use
view_one.reset(sf::FloatRect(0, 0, 3000, 3000));
is this a bug? I checked the source, and apart from resizing the view (like setSize() does), reset() just recalculates the view center and set the rotation to 0;
« Last Edit: October 25, 2019, 06:38:54 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: strange behaviour with View and Viewport
« Reply #1 on: October 26, 2019, 09:40:59 am »
The difference between reset and setSize is the origin: with reset (and the args you pass) it's relative to the top-left corner, while with setSize it's relative to the center. Therefore, you only see 1/4 of the view when you use setSize, the rest is outside the window.

By the way, this kind of things is easy to check by simply printing the various coordinates involved (and by reading the doc ;)).
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: strange behaviour with View and Viewport
« Reply #2 on: October 26, 2019, 10:08:13 am »
I did that. It doesn't say anything about the view center; just states that the reset() function resets the rotation angle.
About the coordinates, I find it hard to believe that it's intended. Changing the view size should really change the viewport position in the screen!?
I mean, if you set the viewport and then zoom, the viewport is messed up.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: strange behaviour with View and Viewport
« Reply #3 on: October 26, 2019, 03:08:55 pm »
The viewport and view size are (should be) totally independant of each other. I find it a little difficult to understand your test and interpret the results. Could you give more details about what happens, and what makes you think that the viewport is changed?

And you're right, I couldn't find a mention of the resize origin in the doc, although I can swear there is one ;D
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: strange behaviour with View and Viewport
« Reply #4 on: October 26, 2019, 05:34:19 pm »
hi! after your response, I was looking what I could be doing wrong.
you are right, the view size and viewport are not interfering with each other. turns out I was making the calculation of the viewport in a wrong way. I was using these lines:
sf::Vector2f pos_in_view = window.mapPixelToCoords(sf::Mouse::getPosition(window), view_one);
if (sf::IntRect(
view_one.getCenter().x-view_one.getSize().x/2,
view_one.getCenter().y-view_one.getSize().y/2,
view_one.getCenter().x+view_one.getSize().x/2, view_one.getCenter().y+view_one.getSize().y/2).contains(sf::Vector2i(pos_in_view))){
but this temporary IntRect if not the same size the viewport occupies in the window; it just has the size of the View, BEFORE being looked at by the view itself, what is short for "doesn't make any sense at all".


but since I'm already here let me ask, then: how do I check if the cursor is inside a particular Viewport?
I tried this:
sf::Vector2f pos_in_view = window.mapPixelToCoords(sf::Mouse::getPosition(window), view_one);

if (sf::FloatRect(0, 0, view_one.getViewport().width*window.getSize().x, view_one.getViewport().height*window.getSize().y).contains(pos_in_view)){
but it didn't worked, and now I have no idea at all on how to do it  :P
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: strange behaviour with View and Viewport
« Reply #5 on: October 26, 2019, 09:17:11 pm »
Looks good, as long as your viewport's top-left corner is (0, 0).

Again, the easiest way to debug this kind of code is to print both the mouse position and the rectangle you're testing it against, and figure out what coordinate doesn't look correct.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3372
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: strange behaviour with View and Viewport
« Reply #6 on: October 28, 2019, 01:28:21 am »
how do I check if the cursor is inside a particular Viewport?
I tried this:
sf::Vector2f pos_in_view = window.mapPixelToCoords(sf::Mouse::getPosition(window), view_one);

if (sf::FloatRect(0, 0, view_one.getViewport().width*window.getSize().x, view_one.getViewport().height*window.getSize().y).contains(pos_in_view)){
but it didn't worked, and now I have no idea at all on how to do it  :P
Since pos_in_view is a mapped (view) co-ordinate, it should be compared to the view size, not the window size.

Or, you could compare the un-mapped (pixel) co-ordinate with the window size.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: strange behaviour with View and Viewport
« Reply #7 on: October 28, 2019, 02:34:35 pm »
thank very much to both of you
i got up with this:

if (sf::IntRect(view_one.getCenter().x-view_one.getSize().x/2, view_one.getCenter().y-view_one.getSize().y/2, view_one.getSize().x, view_one.getSize().y).contains(sf::Vector2i(pos_in_view))){


I don't know if this would be used frequently, but wouldn't it be interesting to add such function in the sf::View class?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: strange behaviour with View and Viewport
« Reply #8 on: October 28, 2019, 03:01:54 pm »
What about

sf::FloatRect(view_one.getCenter() - view_one.getSize() / 2.f, view_one.getSize()).contains(pos_in_view)

(not tested)
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: strange behaviour with View and Viewport
« Reply #9 on: October 28, 2019, 04:43:06 pm »
works perfectly! thanks again!  ;D
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything