SFML

each time i press A key to move the ball i dont see any move while in console i can see the coordinates changing.
secondly when i click on console menu and then again select(click) the graphic menu i could see the ball(circle) have changed its position .
why i am unable to see the change when i press key "A"..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
      while (win.isOpen())
    {
        sf::Event event;
        while (win.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                win.close();
            }
        }
        win.clear();
        win.draw(circle);
        win.display();

        sf::Vector2f pos = circle.getPosition();
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
        {
            cout<<" a pressed :";
            cout<<pos.x<<" "<<pos.y;
            circle.move(0.1,0.1);
        }
    }
On line 1, change the code to while (true).
This will allow the loop to be executed repeatedly and move the ball. This is because win.isOpen() will always be false. So you could also change it to while (!win.isOpen()).
Last edited on
while (win.isOpen()) {...} is a proper way of setting up a main loop in SFML. The problem is something else. (What exactly, I don't know.) Note that you are only moving 0.1 each frame, so it takes 10 frames to move a pixel.

OP, can give you give a simplified version (if needed) of your full, compilable code, that still replicates the problem?
Last edited on
10 frames isnt a problem the problem is that is press a few times then i minimize the graphic window and after maximizing it back i could see the ball at a new position ..
What type of Drawable is circle? Sounds almost like win.display() isn't called.
Again, you should post a complete, minimal example of your problem in code.
I didn't say that was the problem, I just wanted to confirm you were aware of that.

Your actual problem seems to be more related to how isKeyPressed works in itself.
From https://www.sfml-dev.org/tutorials/2.4/window-inputs.php
Keyboard
The class that provides access to the keyboard state is sf::Keyboard. It only contains one function, isKeyPressed, which checks the current state of a key (pressed or released). It is a static function, so you don't need to instantiate sf::Keyboard to use it.

---> This function directly reads the keyboard state, ignoring the focus state of your window. This means that isKeyPressed may return true even if your window is inactive. <---

1
2
3
4
5
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
    // left key is pressed: move our character
    character.move(1, 0);
}


If you don't want any actions to be able to happen while the window is inactive, I think you might have to use the something such as the window's hasFocus() check.
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Window.php#ac4dce670f07c5039a732ba0903ce3a77

1
2
3
4
5
6
7
8
9
if (win.hasFocus())
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
        {
            cout<<" a pressed :";
            cout<<pos.x<<" "<<pos.y;
            circle.move(0.1,0.1);
        }
}
Last edited on
win.display() is called inside the while loop check the very first code ..
also i have a video clip which can better explain this problem ..
but i dont know how to share that here
Have you tried running the program without using cout inside the loop?
Last edited on
what is alternate for cleardevice( graphics.h ) in sfml?
Topic archived. No new replies allowed.