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

Author Topic: How do I convert a getBlobalBounds() to a int like thing  (Read 1342 times)

0 Members and 1 Guest are viewing this topic.

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
How do I convert a getBlobalBounds() to a int like thing
« on: August 10, 2012, 03:38:03 am »
I'm trying to make it so when I click on a shape it disapears:

      sf::FloatRect rectBounds = enemy.getGlobalBounds();

      if (mouseX == rectBounds && mouseY == rectBounds)
      {
         hoverEnemy = true;
      }

      if (clickEnemy == true && hoverEnemy)
      {
         EnemyObject.die();
         clickEnemy = false;
      }

This ^ obviously doenst work. Is there a way I can do it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: How do I convert a getBlobalBounds() to a int like thing
« Reply #1 on: August 10, 2012, 03:47:31 am »
Please make use of the code=cpp tag! ;)

This will work, but will throw you a warning at you up on compiling.
sf::FloatRect rectBounds = enemy.getGlobalBounds();

if (mouseX == rectBounds.left && mouseY == rectBounds.top)
{
    hoverEnemy = true;
}

This won't throw a warning:
sf::FloatRect rectBounds = enemy.getGlobalBounds();

if (mouseX == static_cast<int>(rectBounds.left) && mouseY == static_cast<int>(rectBounds.top))
{
    hoverEnemy = true;
}

But the coordinates won't get rounded up, so you could use:
sf::FloatRect rectBounds = enemy.getGlobalBounds();

if (mouseX == static_cast<int>(rectBounds.left+0.5f) && mouseY == static_cast<int>(rectBounds.top+0.5f))
{
    hoverEnemy = true;
}

Then again this all seems a bit wrong and since you probably want some sort of collision test you'd better check if the mouse cursor is inside the rectangle:
if (enemy.getGlobalBounds().contains(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window))))
{
    hoverEnemy = true;
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: eXpl0it3r
« Reply #2 on: August 10, 2012, 06:07:06 am »
Thanks it works for the most part. Somethings don't work though like:

Hover is always true even when the mouse is away from the square so when you click it always works no matter where you are.

I can't draw an object when it's hovered or clicked:

                sf::FloatRect rectBounds = enemy.getGlobalBounds();

                if (enemy.getGlobalBounds().contains(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window))))
                {
                        hoverEnemy = true;
                }

                if (hoverEnemy = true)
                {
                        std::cout << "Hover\n";
                        window.draw(enemyHover);
                }
                if (clickEnemy == true && hoverEnemy)
                {
                        EnemyObject.die();
                        window.draw(enemyClicked);
                        std::cout << "Clicked";
                        clickEnemy = false;
                }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: How do I convert a getBlobalBounds() to a int like thing
« Reply #3 on: August 10, 2012, 09:19:02 am »
Because hoverEnemy = true is an assignment which will always be true. ;)
Don't use == true checks for booleans a simple if(hoverEnemy) is enough.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/