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

Author Topic: mouse position relative to window  (Read 10091 times)

0 Members and 1 Guest are viewing this topic.

Kalicliq

  • Newbie
  • *
  • Posts: 3
    • View Profile
mouse position relative to window
« 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);
}
 
« Last Edit: February 14, 2021, 04:30:34 pm by Kalicliq »

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: mouse position relative to window
« Reply #1 on: February 07, 2021, 06:16:54 pm »
Could you provide minimal but full code for reproducing the behaviour?

Kalicliq

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: mouse position relative to window
« Reply #2 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;
}

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: mouse position relative to window
« Reply #3 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: mouse position relative to window
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kalicliq

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: mouse position relative to window
« Reply #5 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.

 

anything