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

Author Topic: sf::Text & sf::Rectangleshape display at diffrent positions?  (Read 3624 times)

0 Members and 1 Guest are viewing this topic.

replaceits

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
sf::Text & sf::Rectangleshape display at diffrent positions?
« on: December 27, 2012, 04:50:01 am »
So I've trying to create a simple menu for a test project, however I can not get a rectangle to sit exactly behind the text, the text is always lower than the shape when using the same position, size, and origin.

    sf::Text StartButton(sf::String("Start"),DefFont,50);
    StartButton.setColor(sf::Color(255,255,255));
    StartButton.setOrigin(StartButton.getLocalBounds().width / 2, StartButton.getLocalBounds().height / 2);
    StartButton.setPosition(App.getSize().x / 2, App.getSize().y / 2);

    sf::RectangleShape StartRec;
    StartRec.setSize(sf::Vector2f(StartButton.getLocalBounds().width, StartButton.getLocalBounds().height));
    StartRec.setOrigin(StartRec.getLocalBounds().width / 2, StartRec.getLocalBounds().height / 2);
    StartRec.setPosition(sf::Vector2f(StartButton.getPosition().x, StartButton.getPosition().y));
    StartRec.setFillColor(sf::Color(50,50,50));
    StartRec.setOutlineColor(sf::Color(40,40,40));
    StartRec.setOutlineThickness(4);

That declares the two, the size is correct however the position is not, any idea what's going on here?
I've attached a pic of what I'm talking about and a copy of the src (no where near done with it).


[attachment deleted by admin]

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Text & sf::Rectangleshape display at diffrent positions?
« Reply #1 on: December 27, 2012, 10:03:23 am »
I played around with this a little and I'm not sure what the exact issue is, but there are two things that kind of stuck out at me. 

The first is that for a character size of 50, the glyph returned for a capital 'S' was 36 pixels in height.  That's a difference of 14 pixels.  Not coincidentally, that's the "top" value I got if I inspected the local bounding box for the text.  Because there are no vertices in that area at the top of the glyph, the bounding box returned by the VertexArray isn't what I would normally expect in a local bounding box, which is what sf::Text returns when asked for one.  A simple fix library-wise might be to have sf::Text::setPosition() and sf::Text::getPosition() account for the offset automatically.

Anyway, the long and short of it is that you need to apply an offset to counter the empty vertical space in the glyph.

Following is code which illustrates the issue (build as a console app to see the bounding box output:)

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

void print(const sf::FloatRect & rect)
{
    std::cout << '{' << rect.left << ", " << rect.top << ", " << rect.width << ", " << rect.height << "}\n" ;
}

int main()
{
    sf::VideoMode vMode(800, 600) ;
    sf::RenderWindow window(vMode, "Blocks", sf::Style::Close);
 
    sf::Font font;
    if(!font.loadFromFile("sketchflow.ttf")){return 1;};

    sf::Vector2f center( floor(window.getSize().x / 2.0f), floor(window.getSize().y / 2.0f)) ;
    sf::Text text(sf::String("Text"),font);

    sf::FloatRect textBounds = text.getLocalBounds() ;

    // offset the text position.   text.setPosition(center) won't work correctly!
    text.setPosition(center.x-textBounds.left, center.y-textBounds.top) ;
 
    print(textBounds) ;

    sf::RectangleShape textRec(sf::Vector2f(textBounds.width, textBounds.height)) ;
    textRec.setPosition(center) ;

    textRec.setFillColor(sf::Color(0, 127, 63)) ;

    sf::FloatRect textRecBounds = textRec.getLocalBounds() ;

    print(textRecBounds) ;

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

        window.clear( sf::Color(63,0, 63) ) ;
        window.draw(textRec) ;
        window.draw(text) ;
        window.display() ;
    }
}

The console output I get when i run this:
{-1, 8, 61, 25}
{0, 0, 61, 25}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text & sf::Rectangleshape display at diffrent positions?
« Reply #2 on: December 27, 2012, 10:21:55 am »
It's not a bug, it's a feature :P

The height of the first line is the maximum height (ie. the character size), so that if you add a taller character in the first line of text, your bounding box won't move and your alignment won't be broken.
Laurent Gomila - SFML developer

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Text & sf::Rectangleshape display at diffrent positions?
« Reply #3 on: December 27, 2012, 10:51:05 am »
It's not a bug, it's a feature :P

Heheh.  Yes, I've found a couple other posts on the subject now.  Thank you, google.

I have to say, though, it is a bit counter-intuitive to have rect.setPosition(text.getPosition()) work the way it does!