SFML community forums

Help => Window => Topic started by: Kalicliq on February 06, 2021, 10:50:50 pm

Title: mouse position relative to window
Post by: Kalicliq on February 06, 2021, 10:50:50 pm
Hi i want to center the mouse on the previously created window. But something went wrong and i dont know why.
you can see where the mouse is positioned on the screenshot.

Edit: Im on Fedora 33.

void Window::create(const std::string windowName, const sf::VideoMode resolution, const bool fullScreen)
{
    sf::VideoMode screenSize = sf::VideoMode::getDesktopMode();

    _window.create(resolution, windowName, fullScreen ? 8 : 7);
    _window.setVerticalSyncEnabled(true);
    if (!fullScreen)
        _window.setPosition(sf::Vector2i((screenSize.width / 2) - (resolution.width / 2), (screenSize.height / 2) - (resolution.height / 2)));

    sf::Mouse::setPosition(sf::Vector2i(_window.getSize().x / 2, _window.getSize().y / 2), _window);
}
 
(https://media.discordapp.net/attachments/796046442560487455/807728944576462868/unknown.png?width=1173&height=660)
Title: Re: mouse position relative to window
Post by: Kvaz1r on February 07, 2021, 06:16:54 pm
Could you provide minimal but full code for reproducing the behaviour?
Title: Re: mouse position relative to window
Post by: Kalicliq on February 10, 2021, 03:53:18 pm
this is what i approximately do:

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/Window/Event.hpp>

class _window
{
    public:
        void create(std::string windowName, const sf::VideoMode resolution, const bool fullScreen)
        {
            sf::VideoMode screenSize = sf::VideoMode::getDesktopMode();

            _rw.create(resolution, windowName, fullScreen ? 8 : 7);
            _rw.setVerticalSyncEnabled(true);
            if (!fullScreen)
                _rw.setPosition(sf::Vector2i((screenSize.width / 2) - (resolution.width / 2), (screenSize.height / 2) - (resolution.height / 2)));

            sf::Mouse::setPosition(sf::Vector2i(_rw.getSize().x / 2, _rw.getSize().y / 2), _rw);
        }

        sf::RenderWindow& get_rw()
        {
            return _rw;
        }

        sf::Event& get_event()
        {
            return _event;
        }

    private:
        sf::RenderWindow _rw;
        sf::Event _event;
};

int main()
{
    _window w;

    w.create("test", sf::VideoMode(800, 600, 32), false);

    while (w.get_rw().isOpen()) {
        while (w.get_rw().pollEvent(w.get_event()))
            switch (w.get_event().type)
            {
                case sf::Event::Closed:
                    w.get_rw().close();
                    break;
                default:
                    break;
            }
        w.get_rw().display();
        w.get_rw().clear();
    }
    return 0;
}
Title: Re: mouse position relative to window
Post by: Kvaz1r on February 13, 2021, 03:15:53 pm
Code works for me(Windows), I think it's bug so should be reported - https://github.com/SFML/SFML/issues
Title: Re: mouse position relative to window
Post by: eXpl0it3r on February 15, 2021, 08:37:41 am
If you deal in screen resolution, everything will be relative to the top left corner of the screen. What you want to do is, calculate the position relative to the window position.
As such take the window position, calculate the wanted offset to your window and set the mouse cursor position to that.
Title: Re: mouse position relative to window
Post by: Kalicliq on February 16, 2021, 02:58:22 pm
Code works for me(Windows), I think it's bug so should be reported - https://github.com/SFML/SFML/issues
Hi, ok thx for testing.
I'm on Fedora 33.

If you deal in screen resolution, everything will be relative to the top left corner of the screen. What you want to do is, calculate the position relative to the window position.
As such take the window position, calculate the wanted offset to your window and set the mouse cursor position to that.
As Kvaz1r said my code works on windows so its not a code problem its an OS problem since i'm on fedora 33.