return value of a rect in c++

May 9, 2013 at 5:26pm
I have got a function in sfml which gets the co-ordinates of a rectangle bounding box for a circle shape. The return value is described as FloatRect and the value is:
circle1 = 1026324980834960100

This is for a circle with the following dimensions:

circle1
x = 395.11
y = 344.97
width = 49.78
height = 50.06


I may be being stupid but I have tried looking in all the ref books I can find and I still can't work out what the return value has got to do with the 4 co-ordinates.

Has anyone got any idea?

Thanks for any advice
May 9, 2013 at 5:44pm
What does the function look like? Or are you using something like .getGlobalBounds()?
May 9, 2013 at 6:38pm
A FloatRect has 4 members. It does not have a value, per se. The values it holds are contained in its data members.


I may be being stupid but I have tried looking in all the ref books I can find and I still can't work out what the return value has got to do with the 4 co-ordinates.

What return value and what 4 coordinates? width/height are not coordinates.

If you could supply some minimal code that reproduced the situation you are speaking about instead of using English mumbo jumbo, that would be helpful.
May 9, 2013 at 6:45pm
Edit: lol I thought cire was BillH xD

I think you are confused, at least I think I am. (You should supply code that shows us 1026324980834960100)

How did you get that value? Did you just directly std::cout << the floatrect? width/height are not coordinates, but top and left are. You can thus get the other corners by decrementing top by height, incrementing left by width and doing both for the bottom right corner.
Last edited on May 9, 2013 at 6:46pm
May 9, 2013 at 7:43pm
To all

Sorry, what I have is this:
This is where the original circle1 = 1026324980834960100 came from:

1
2
sprintf(buffer,"\ncircle1 = %.0f\n",circle.getGlobalBounds());
std::cout<<buffer,"\n";


At the moment I am getting the boundary co-ordinates from the following code

1
2
3
4
5
x=circle.getGlobalBounds().left;
y=circle.getGlobalBounds().top;
w=circle.getGlobalBounds().width;
h=circle.getGlobalBounds().height;


I then use this to find out whether the mouse is within that box area, in order to drag and drop. The actual program works fine. What I would like to do is cut these 4 lines down into a single one, if possible. I am fairly new to this so I hope you will bear with me.

Thanks again.
May 9, 2013 at 7:54pm
You are printing an object directly, I think behaviour is undefined. It's like saying std::cout << circle.getGlobalBounds().

You say you want to cut it down?

 
circle.getGlobalBounds().contains(sf::Mouse::getPosition());


SFML documentation here http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php
May 9, 2013 at 9:18pm
1
2
sprintf(buffer,"\ncircle1 = %.0f\n",circle.getGlobalBounds());
std::cout<<buffer,"\n";


The sprintf call is undefined behavior. An sf::FloatRect is not a float.

The following line is equivalent to: (std::cout << buffer), "\n" ; which is to say that the "\n" is pointless. Use std::cout << buffer << '\n'; to chain calls to operator<<.

Using events is more appropriate for dragging/dropping stuff than using sf::Mouse.

Try out the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Drag it");

    const sf::Color hoverColor(255, 0, 0) ;
    const sf::Color regularColor(127, 0, 0) ;

    sf::CircleShape circle(25.0f) ;
    circle.setOrigin(25.0f, 25.0f) ;
    circle.setPosition(400.0f, 300.0f) ;
    circle.setFillColor(regularColor) ;

    bool hovering = false ;
    bool dragging = false ;

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch( event.type )
            {
            case sf::Event::Closed:
                window.close() ;
                break;

            case sf::Event::MouseMoved:
                if ( dragging )
                {
                    circle.setPosition(event.mouseMove.x, event.mouseMove.y) ;
                }
                else if ( circle.getGlobalBounds().contains(event.mouseMove.x, event.mouseMove.y) )
                {
                    hovering = true ;
                    circle.setFillColor(hoverColor) ;
                }
                else if ( hovering == true )
                {
                    hovering = false ;
                    circle.setFillColor(regularColor) ;
                }      
                break ;

            case sf::Event::MouseButtonPressed:
                if ( circle.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) 
                     && event.mouseButton.button == sf::Mouse::Left  )
                {
                    dragging = true ;
                    circle.setPosition(event.mouseButton.x, event.mouseButton.y) ;
                }
                break ;

            case sf::Event::MouseButtonReleased:
                if ( event.mouseButton.button == sf::Mouse::Left )
                    dragging = false ;
                break ;
            }
        }

        window.clear();
        window.draw(circle) ;
        window.display();
    }
}

May 10, 2013 at 7:06am
Thanks Bourgond Aries and cire, I was obviously thinking along the wrong lines. Your advice has solved my problem. Thanks again
Topic archived. No new replies allowed.