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

Author Topic: Mouse Coordinates and Different Window Resolutions  (Read 1462 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Mouse Coordinates and Different Window Resolutions
« on: September 29, 2017, 06:57:59 pm »
Let's say I have this code where I have a 1920x1080 window and a 50x50 button sprite at position.x 1800  position.y 1000

V = sf::Mouse::getPosition(WINDOW);

if ((V.x >= 1800) && (V.x <= 1850) && (V.y >= 1000) && (V.y <= 1050)) { press_button(); }
 

It works as intended at the original resolution; but if I were to change the window's resolution to something else like 1280x720, it won't work because even though my mouse is hovering over the same area on the resized window, the mouse-window coordinates are not the same.

Checking for whether the button's bounds contains the mouse is not always an option because let's say I want to use a single sprite and get different results when I click on different parts of that one sprite.  (or because I've already coded the whole thing like this  :-[ >:( :'()

Is there a way to make this code for a window of any size without making the sprite bounds check for the mouse position?
« Last Edit: September 29, 2017, 06:59:47 pm by NGM88 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: Mouse Coordinates and Different Window Resolutions
« Reply #1 on: September 29, 2017, 07:56:23 pm »
You want mapPixelToCoords or mapCoordsToPixel functions. That way you can convert between screen space and world space.

It's also mentioned in the official tutorials. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything