SFML community forums

Help => Window => Topic started by: destrugter on June 12, 2014, 01:57:46 pm

Title: [Solved]Window, possibly mouse. Possible bug when resizing
Post by: destrugter 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.
Title: AW: Window, possibly mouse. Possible bug when resizing
Post by: eXpl0it3r 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.
Title: Re: Window, possibly mouse. Possible bug when resizing
Post by: destrugter 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.
Title: Re: Window, possibly mouse. Possible bug when resizing
Post by: zsbzsb 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.