SFML community forums

General => General discussions => Topic started by: BitMaNip on October 06, 2024, 03:56:39 pm

Title: window resize mouse position wrong
Post by: BitMaNip on October 06, 2024, 03:56:39 pm
Hi all

and thank you for any help in advance.

I create my window and view as follows:
window(sf::VideoMode(l_videoMode.x, l_videoMode.y), l_windowName, sf::Style::Close | sf::Style::Resize)

m_View.setSize(sf::Vector2f(window.getSize().x, window.getSize().y));
m_View.setCenter(window.getSize().x/2, window.getSize().y/2);

 

In the Update method of my window class I get the mouse position as follows:

m_winMousePos = sf::Mouse::getPosition(window);

//I have also tried the following
/*
//sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
//sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);
/*

//The window resize event as sfml website
if (event.type == sf::Event::Resized)
{
        sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
        window.setView(sf::View(visibleArea));
}

//My draw code:
void Window::Draw(const sf::Drawable& l_drawable)
{
        window.setView(m_View);

        window.draw(l_drawable);
}

When I maximise the window, the mouse coords are not correct and I cannot get what I am doing wrong.

Any pointers [no pun intended] would be a great help!

Title: Re: window resize mouse position wrong
Post by: eXpl0it3r on October 06, 2024, 09:59:12 pm
mapPixelToCoords has a second parameter, where you can pass in the view, see: https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1RenderTarget.php#a2d3e9d7c4a1f5ea7e52b06f53e3011f9

Since you change the view and don't necessarily have it applied at the time you're requesting the mouse location, you'll get the wrong location reported.
Title: Re: window resize mouse position wrong
Post by: BitMaNip on October 08, 2024, 12:58:35 pm
I tried the code as suggested and it still gives wrong values.

I have tried with and without using a view.

Can anyone suggest what I might be doing wrong or what to do that is right?
Title: Re: window resize mouse position wrong
Post by: eXpl0it3r on October 08, 2024, 01:27:47 pm
Can you provide a simple example of what you expect should happen and what actually happens?

Plus the complete code that you're using.
From the posted code, I'm not sure what is actually being used now.