SFML community forums

Help => Graphics => Topic started by: JunkerKun on December 07, 2012, 06:54:11 pm

Title: Why is it necessary to call setView every tick?
Post by: JunkerKun on December 07, 2012, 06:54:11 pm
So, I tried to use view to have it to follow player:
void Player::Step() {
        /*if (CollisionPoint(viewXview+Mouse.getPosition(window).x,viewYview+Mouse.getPosition(window).y,ObjMan.GetObject(id))) {
                sprite.setColor(sf::Color(sf::Color::Red));
        } else sprite.setColor(sf::Color(sf::Color::White));*/

        if (Keyboard.isKeyPressed(sf::Keyboard::Key::Right)) {
        speed=2;
        animXdir=1;
        };
        if (Keyboard.isKeyPressed(sf::Keyboard::Key::Left)) {
        speed=-2;
        animXdir=-1;
        };
        if (!Keyboard.isKeyPressed(sf::Keyboard::Key::Right) &&
                !Keyboard.isKeyPressed(sf::Keyboard::Key::Left)) {
        speed=0;
        };
        x+=speed;
        viewXview=x;
        viewYview=y;
        view.setCenter(viewXview,viewYview);
        viewXview-=320;
        viewYview-=240;
        Update();
};
And after a while I noticed that it is slitly shifted to the side when player is moving. And it was fixed only after I made it to call setView every tick (in main cycle, before all rendering). Is there a reason for that or am I just being dumb?
Title: Re: Why is it necessary to call setView every tick?
Post by: eXpl0it3r on December 07, 2012, 07:02:49 pm
Yes the view is pass by value and not by reference, thus it will only change if you call setView. ;)
Title: Re: Why is it necessary to call setView every tick?
Post by: JunkerKun on December 07, 2012, 07:09:56 pm
Yes the view is pass by value and not by reference, thus it will only change if you call setView. ;)
Oh. So in other words it needs to be updated manualy?
Title: Re: Why is it necessary to call setView every tick?
Post by: Nexus on December 07, 2012, 07:16:41 pm
Oh. So in other words it needs to be updated manualy?
Yes. This has the advantage that the sf::View has no restrictions with respect to lifetime. You can pass temporary objects to setView(), for example.
Title: Re: Why is it necessary to call setView every tick?
Post by: JunkerKun on December 07, 2012, 07:28:05 pm
Okay, thanks for help!