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

Author Topic: RenderWindow getView doesnt return the view ?  (Read 1946 times)

0 Members and 1 Guest are viewing this topic.

de5cartes

  • Newbie
  • *
  • Posts: 9
    • View Profile
RenderWindow getView doesnt return the view ?
« on: April 28, 2020, 06:14:20 pm »
Hi

First, I loved SFML.
Thank you for that !


Can you tell me why this doesnt work ?

// ... before game loop
sf::View cameraView(sf::FloatRect(0, 0, GamePreferences::WINDOW_RESOLUTION_WIDTH, GamePreferences::WINDOW_RESOLUTION_HEIGHT));


// ... inside game loop

window.setView(cameraView);
handleInputEvents(&window);

// ... end of game loop method


// method handleInputEvents

void Director::handleInputEvents(sf::RenderWindow *window) {
    sf::Event event{};
    sf::View view = window->getView();

    while (window->pollEvent(event))
    {
        if (event.type == sf::Event::MouseWheelScrolled) {
            if (event.mouseWheelScroll.delta == 1)
                view->zoom(0.95f);
            else if (event.mouseWheelScroll.delta == -1)
                view->zoom(1.05f);
        }
    }
}

 

The zoom does not change.
However, if I provide cameraView to handleInputEvents as a parameter, it does work.
Also if I copy the handleInputEvents to the game loop (instead of using a method).

What am I doing wrong here ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderWindow getView doesnt return the view ?
« Reply #1 on: April 28, 2020, 06:20:54 pm »
You copy the window's view, then modify the copy. You miss a call to window->setView(view) to apply your modifications.
Laurent Gomila - SFML developer

de5cartes

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: RenderWindow getView doesnt return the view ?
« Reply #2 on: April 28, 2020, 08:09:32 pm »
Thanks for the reply.

Why do I get a copy instead of a pointer to the actual view?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderWindow getView doesnt return the view ?
« Reply #3 on: April 28, 2020, 09:02:19 pm »
Because that's how the SFML API is designed. sf::View is a lightweight object that can be copied without performance loss, therefore it is much safer to take/return copies that avoid dangling pointers and ownership issues. The only drawback is to remember to call RenderTarget::setView whenever you want to apply a modified view.
Laurent Gomila - SFML developer

de5cartes

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: RenderWindow getView doesnt return the view ?
« Reply #4 on: April 28, 2020, 09:32:06 pm »
O.K, Thanks !