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

Author Topic: window resize mouse position wrong  (Read 1213 times)

0 Members and 2 Guests are viewing this topic.

BitMaNip

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
window resize mouse position wrong
« 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!


eXpl0it3r

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

BitMaNip

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: window resize mouse position wrong
« Reply #2 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?

eXpl0it3r

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