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

Author Topic: [Solved]Window, possibly mouse. Possible bug when resizing  (Read 3453 times)

0 Members and 1 Guest are viewing this topic.

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
[Solved]Window, possibly mouse. Possible bug when resizing
« on: June 12, 2014, 01:57:46 pm »
I can not find anything else about this strange bug, leaving me to believe I've stumbled upon something weird. I use a simple function comparing a sprite's position to the mouse to see if the mouse is hovering over the sprite. I've found that when you resize the window, the images stretch. I do like this feature, but what I do not like about it is, the sprite's original x,y are kept and it's not updated when the window is stretched. I am getting the mouses coords relative to the window too. Here's my code for detecting if the mouse is over the sprite. Please correct any errors you see, or give me tips.

I create a RenderWindow object:
sf::RenderWindow myWindow;
 

I create a simple function that returns true if the mouse is within the sprite's x and y boundries and its width + height.
bool isMouseOverSprite(const sf::RenderWindow& myWindow, const sf::Sprite& Sprite)
{
        return  (sf::Mouse::getPosition(myWindow).x < (Sprite.getPosition().x + Sprite.getGlobalBounds().width)) && (Sprite.getPosition().x < sf::Mouse::getPosition(myWindow).x) &&
                        (sf::Mouse::getPosition(myWindow).y < (Sprite.getPosition().y + Sprite.getGlobalBounds().height)) && (Sprite.getPosition().y < sf::Mouse::getPosition(myWindow).y);
}
 

Maybe I missed something in the forums or documentation or tutorials, but this code works when my window is set to a small 900x600 window. I realize this is an odd resolution and I don't plan on keeping it. I'm in very early testing stages of a lot of things at this point. The point is, when I resize the window, the sprites report their original x,y coordinates while the mouse reports its NEW x,y coordinates.

What I'm trying to say is, the sprite class accounts for stretching, the mouse class does not. Maybe this is intended, I do not know. I may have missed a detail reading up for this issue.
« Last Edit: June 12, 2014, 03:14:42 pm by destrugter »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
AW: Window, possibly mouse. Possible bug when resizing
« Reply #1 on: June 12, 2014, 02:23:42 pm »
You're looking for window.mapPixelToCoords or window.mapCoordsToPixel so you can convert between the "window pixel" (at which your mouse is pointing) and "world coordinates" (where your mouse is currently located in the scene) or the other way around.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Window, possibly mouse. Possible bug when resizing
« Reply #2 on: June 12, 2014, 03:14:21 pm »
Thanks eXpl0it3r. Not sure how or why I missed those functions. I must have just misread or was reading too fast. This did solve the problem for anyone wondering =)

Although I do wonder if this should be done automatically by SFML when the window is resized. I could be wrong about this though.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Window, possibly mouse. Possible bug when resizing
« Reply #3 on: June 12, 2014, 03:21:23 pm »
Although I do wonder if this should be done automatically by SFML when the window is resized. I could be wrong about this though.

You should know that it is not about the window resizing, rather the view is changing.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor