SFML community forums

Help => Window => Topic started by: BagOfBones on February 12, 2017, 01:37:43 pm

Title: Centering mouse
Post by: BagOfBones on February 12, 2017, 01:37:43 pm
Hi. Im using sfml for implementing fps style camera and Ive recently noticed some strange behaviour. Code structure looks like this

int main()
{
  sf::RenderWindow window(sf::VideoMode(1280, 720), "test");
  // ...
  // ...

  // main loop
  while(!exitLoop)
  {
      //...
     sf::Vector mousePosition = sf::Mouse::getPosition(window);
     sf::Mouse::setPosition(sf::Vector2i(640, 360), window);

     update(mousePosition);
    // ...
  }
}
 

The thing is I use laptop with another monitor. When I set laptop window as a main screen and run an app, everything works fine. But when I set my second monitor as a main screen, sometimes setting the mouse into the center of a screen actually sets the mouse position into slightly diffrent values like (639, 360), (640, 359) etc...which causes some small offset against the center and constantly moves my camera, even when Im not touching my mouse. Sorry if its some stupid mistake. Thanks for help.
Title: Centering mouse
Post by: eXpl0it3r on February 13, 2017, 01:40:11 am
And how do you check that it's not at the specified position?
What OS are you using? What SFML version are you using?
Title: Re: Centering mouse
Post by: BagOfBones on February 13, 2017, 09:45:28 pm
immediately after setPosition
std::cout << sf::Mouse::getPosition(window).x << " " << sf::Mouse::getPosition(window).y << std::endl;
 

Working on Windows 10 and sfml 2.4.0
Title: Re: Centering mouse
Post by: Hiura on February 14, 2017, 04:01:31 pm
I think to understand the issue we need a minimal, yet complete, program that reproduces this issue, as well as specifics about your setups. Also, update your SFML version.  ;)

In this discussion (https://github.com/SFML/SFML/pull/614#issuecomment-81080916) an alternative API for mouse relative movements was discussed ("fps-style"). I'm not saying that we need it -- maybe there's a slight bug in your code or we need to tweak SFML implementation -- but if it appears from the minimal & complete example that it cannot be solved with the current API, we might need to consider discussing this alternative API a bit more.
Title: Re: Centering mouse
Post by: BagOfBones on February 14, 2017, 09:03:59 pm
Updated to latest version, now Im using 2.4.2 64 bit version compiled under VS 2015...and its definitely not a problem of my code, which manages my camera...made this little program:

#include <SFML\Graphics.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1280, 720), "Test", sf::Style::Default);

        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                window.close();
                }

                sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
                sf::Mouse::setPosition(sf::Vector2i(640, 360), window);
                std::cout << sf::Mouse::getPosition(window).x << " " << sf::Mouse::getPosition(window).y << std::endl;

                window.clear();
                window.display();
        }
       
        return EXIT_SUCCESS;
}
 

...and bug is still there...on my laptop monitor it works fine but when I switch to second monitor and moves a mouse a little, it is centered at wrong position and stays there while not moving mouse.

(https://s23.postimg.org/natvikh9n/Bez_n_zvu.png)