Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Centering mouse  (Read 1710 times)

0 Members and 1 Guest are viewing this topic.

BagOfBones

  • Newbie
  • *
  • Posts: 4
    • View Profile
Centering mouse
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10044
    • View Profile
    • development blog
    • Email
Centering mouse
« Reply #1 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

BagOfBones

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Centering mouse
« Reply #2 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

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Centering mouse
« Reply #3 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 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.
SFML / OS X developer

BagOfBones

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Centering mouse
« Reply #4 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.


 

anything