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

Author Topic: Check if sf::RectangleShape contains mouse global bounds?  (Read 9548 times)

0 Members and 1 Guest are viewing this topic.

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Check if sf::RectangleShape contains mouse global bounds?
« on: May 30, 2016, 10:44:37 pm »
Hi,
I want to check if an sf::Shape contains the mouse's global bounds (and if left mouse button is clicked), and created the function below:
bool isRectClicked(sf::RectangleShape &rect, sf::RenderWindow &window) {
        sf::IntRect rect(rect.getPosition().x, rect.getPosition().y, rect.getGlobalBounds().width,  
        rect.getGlobalBounds().height);
        if (rect.contains(sf::Mouse::getPosition(window)) && (sf::Mouse::isButtonPressed(sf::Mouse::Left))) {
                return true;
        }
        return false;
}
 

However it says sf::RectangleShape has no member contains. How would I do this or do I have to do it the longer way by checking if the cursor is within the shape dimensions?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Check if sf::RectangleShape contains mouse global bounds?
« Reply #1 on: May 30, 2016, 11:04:40 pm »
You should avoid calling 2 different variables with the same name.
"sf::RectangleShape &rect" and "sf::IntRect rect"

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check if sf::RectangleShape contains mouse global bounds?
« Reply #2 on: May 30, 2016, 11:18:29 pm »
Why don't you just use rect.getGlobalBounds() directly anyway?
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Check if sf::RectangleShape contains mouse global bounds?
« Reply #3 on: June 02, 2016, 12:42:57 am »
Remember to map the mouse position into the view's co-ordinates.

"Luckily" that returns a Vector2f so you can easily check to see if the rectangle shape's global bounds contains that point.

(click to show/hide)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*