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

Author Topic: Where has my 'getGlobalBounds' gone?  (Read 5545 times)

0 Members and 1 Guest are viewing this topic.

jeremiawuzza

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Where has my 'getGlobalBounds' gone?
« on: June 17, 2015, 03:44:10 pm »
Hi all
I am trying to create a Button class but I'm trying to do it without a separate implementation file. I have coded the class and it works OK, but for some reason  'getGlobalBounds' is not available for the rectangle created. This is a shame because I want to use  a mouseover action with it. Here is my class and function:
#include <SFML/Graphics.hpp>
#include <iostream>

class Button : public sf::Drawable , public sf::Transformable
{
public:
    sf::RectangleShape recbutton;

    Button(float x, float y,sf::Color col)
    {   recbutton.setSize(sf::Vector2f(x,y));
        recbutton.setOrigin(x / 2, y / 2);
        recbutton.setFillColor(col); }
private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {   states.transform *= getTransform();
        target.draw(recbutton, states); } };



int main()
{   Button blue(75,75,sf::Color::Blue);
    blue.setPosition(125,125);

    sf::RenderWindow window;
    window.create(sf::VideoMode( 250,250), "DEMO");

    while (window.isOpen())
    {   sf::Event event;
        while (window.pollEvent(event))
        {   if(event.type == sf::Event::Closed)
                window.close(); }

        window.draw(blue);
        window.display(); }
        return 0}
 

What am I doing wrong? Any help would be appreciated ...

Jeremy
« Last Edit: June 20, 2015, 03:41:57 am by jeremiawuzza »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Where has my 'getGlobalBounds' gone?
« Reply #1 on: June 17, 2015, 03:51:37 pm »
And why do you think getGlobalBounds is not "available"? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Where has my 'getGlobalBounds' gone?
« Reply #2 on: June 17, 2015, 04:53:44 pm »
If you want a getGlobalBounds(...) function to be available on your own class then you need to define and implement the function yourself. Just because you inherit from sf::Transformable and sf::Drawable doesn't mean you magically get that function.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Where has my 'getGlobalBounds' gone?
« Reply #3 on: June 17, 2015, 07:01:03 pm »
As zsbzsb said, you could define the function yourself. It's inheriting from sf::Shape that gives you getGlobalBounds() (as eXpl0it3r said).

However, since your question stated this:
'getGlobalBounds' is not available for the rectangle created.
I'm going to answer it literally...
To access the rectangle's getGlobalBounds(), you need to specify the member first thus:
blue.rectangle.getGlobalBounds()
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jeremiawuzza

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Where has my 'getGlobalBounds' gone?
« Reply #4 on: June 18, 2015, 04:47:07 am »
I tried  'rectangle' as Hapax suggested and sure enough getGlobalBounds().contains(... compiled. But the mouse isbuttonpressed  won't work. Here is a code snip :
if (minute_page.rectangle.getGlobalBounds().contains(mouse_position.x,mouse_position.y)
                && sf::Mouse::isButtonPressed(sf::Mouse::Left)) click  =  2;
        if (second_page.getGlobalBounds().contains(mouse_position.x,mouse_position.y)
                && sf::Mouse::isButtonPressed(sf::Mouse::Left)) click = 3;
Note there are two different uses. The second_page uses a function I had previously defined in another file and the minute_page uses my class as attached above (except I changed 'recbutton' to 'rectangle').
What I am trying to do is pull all my functions from another file into classes in one header. It worked OK with vertexarray, but I seem to have hit a brick wall with my rectangleshape.
I can't seem to inherit from Shape without causing an ambiguity with  Drawable.

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Where has my 'getGlobalBounds' gone?
« Reply #5 on: June 18, 2015, 01:39:21 pm »
It should work and it would be more intuitive if you write it like this:


// if your "mouse_position" is already a sf::Vector2f, then you don't need to explicitly insert both x and y values
if (minute_page.rectangle.getGlobalBounds().contains(mouse_position)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse:Left)
        click = 2;
}
if (second_page.getGlobalBounds().contains(mouse_position)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse:Left)
        click = 3;
}
 

But I would recommend to check whether the mouse button is pressed inside the pollEvent loop, rather than checking it real time.

Btw, I am a c++ and a programer noob too, so take my suggestion with a grain of salt.  ;D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Where has my 'getGlobalBounds' gone?
« Reply #6 on: June 18, 2015, 03:17:42 pm »
second_page.getGlobalBounds()
Is 'second_page' a predefined SFML shape (such as rectangle, circle etc.)? If not, and you defined the getGlobalBounds function yourself, that means you already knew the answer to the rectangle problem  ;D

I can't seem to inherit from Shape without causing an ambiguity with  Drawable.
Shape itself already inherits from Drawable. Shape also has a specific way of working - it has virtual functions that need overriding. See this tutorial example.

It does look like you probably want to inherit from Drawable and then just add the getGlobalBounds functions yourself. They only need to return the rectangle's global bounds. In your original class, it would just be something like:
sf::FloatRect getGlobalBounds()
{
    return recButton.getGlobalBounds();
}

the mouse isbuttonpressed  won't work.
Why, what is it doing that you don't expect it to do?

If you're reading 'clicks', I would agree with Brax that it would be better to use events instead. isButtonPressed is better for times that depend on how long you hold the button e.g. dragging, charging weapon, firing bullets constantly until released. Basically, stuff that needs updating in real-time, rather than a one-off "event" (a single click).

When you test for the button being pressed using events, it also gives you the mouse position at the time of the click. Exactly the information you want when you're programming buttons, right?  ;)

Have a look at the tutorial on events if you need help with it/to refresh your memory of it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jeremiawuzza

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Where has my 'getGlobalBounds' gone?
« Reply #7 on: June 20, 2015, 03:41:06 am »
Hi all
Thanks for your help. I found my problem. In my class definition I set the origin of the rectangle before I set the position. Therefore the global bounds were wrong.
 To fix this I passed the position to the constructor. With the help of the floatrec suggested by Hapax I now have a working class.
class Rectangle : public sf::Drawable , public sf::Transformable
{
public:
     sf::RectangleShape rectangle;
     
sf::FloatRect getGlobalBounds(){ return rectangle.getGlobalBounds();}

    Rectangle(float x, float y,float u,float v, sf::Color col)
    {   rectangle.setSize(sf::Vector2f(x,y));
        rectangle.setPosition(u,v);
        rectangle.setOrigin(x/2, y/2);
        rectangle.setFillColor(col);
        }
        private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {   states.transform *= getTransform();
        target.draw(rectangle, states); } };

Now I will work on the mouse event as suggested.
Once again, thanks for all the help

Jeremy

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Where has my 'getGlobalBounds' gone?
« Reply #8 on: June 20, 2015, 04:00:18 pm »
If you're using the bounds to set the origin, it's likely that you actually wanted getLocalBounds().
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Where has my 'getGlobalBounds' gone?
« Reply #9 on: June 21, 2015, 12:27:52 pm »
You can also have a look at SFML's implementation of getGlobal/LocalBounds().

By the way: x,y for size and u,v for position are really bad variable names. Why don't you just pass two vectors position and size?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything