SFML community forums

Help => Graphics => Topic started by: 2ant on February 24, 2015, 03:33:29 pm

Title: Little questions/issue about view.move [SOLVED]
Post by: 2ant on February 24, 2015, 03:33:29 pm
Hi,i'm learning both c++ and sfml and i did a simple camera movement with 4 keyboard keys.

sf::View view(sf::FloatRect(0, 0, 640, 360));

window.setView(view);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))//move the camera up
                {
                    view.move(0, -32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))//move the camera down
                {
                    view.move(0, 32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))//move the camera left
                {
                    view.move(-32, 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))//move the camera right
                {
                    view.move(32, 0);
                }
 

My problem is that the camera move double the distance.

example : view.move(32, 0) will make the view/camera move 64 pixels to the right and not 32.

Is that normal ?
Why ?
Title: Re: Little questions/issue about view.move
Post by: Laurent on February 24, 2015, 03:50:34 pm
The keys will most likely remain pressed for more than one iteration of your game loop, so each key press will trigger multiple calls to move(). If you want to react to key press/release, and not to key down, use events.
Title: Re: Little questions/issue about view.move
Post by: 2ant on February 24, 2015, 04:27:02 pm
Thank you for your fast answer, i forgot to post the "event" lines :

I have this:

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

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                    view.move(0, -32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))
                {
                    view.move(0, 32);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                    view.move(-32, 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))
                {
                    view.move(32, 0);
                    title += " XX";
                    window.setTitle(title);
                }
 

I added this:
title += " XX";
window.setTitle(title);
And it briefly show four X each time the camera moves.

Should i post my entire (not so long) code ?
Title: Re: Little questions/issue about view.move
Post by: Laurent on February 24, 2015, 04:48:08 pm
Don't mix events and real-time input, it doesn't make sense. If you are inside an event loop, use the available event data.
Title: Re: Little questions/issue about view.move [SOLVED]
Post by: 2ant on February 24, 2015, 05:17:06 pm
Thank you Laurent !

I don't even remember why i did it this way  ::)

while (window.pollEvent(event))
            {
                switch (event.type)
                {
                    case sf::Event::Closed:
                        window.close();
                        break;

                    case sf::Event::KeyPressed:
                        {
                            if (event.key.code == sf::Keyboard::S)
                                {
                                    view.move(0, -32);
                                }
                            else if (event.key.code == sf::Keyboard::X)
                                {
                                    view.move(0, 32);
                                }
                            else if (event.key.code == sf::Keyboard::W)
                                {
                                    view.move(-32, 0);
                                }
                            else if (event.key.code == sf::Keyboard::C)
                                {
                                    view.move(32, 0);
                                }