SFML community forums

Help => Graphics => Topic started by: de5cartes on April 28, 2020, 06:14:20 pm

Title: RenderWindow getView doesnt return the view ?
Post by: de5cartes 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 ?
Title: Re: RenderWindow getView doesnt return the view ?
Post by: Laurent 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.
Title: Re: RenderWindow getView doesnt return the view ?
Post by: de5cartes 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?
Title: Re: RenderWindow getView doesnt return the view ?
Post by: Laurent 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.
Title: Re: RenderWindow getView doesnt return the view ?
Post by: de5cartes on April 28, 2020, 09:32:06 pm
O.K, Thanks !