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

Author Topic: sf::text size and 'highlighting'.  (Read 4623 times)

0 Members and 2 Guests are viewing this topic.

Hourand

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::text size and 'highlighting'.
« on: January 13, 2014, 06:51:45 pm »
Hello  :)

I am trying to write a basic function for printing highlighted text to the screen. Eventually I wont be loading the font every time I want to print text, but this is just a test:

void mvprintsp(sf::RenderWindow* window, const char* string, const int x, const int y, sf::Color fgColour, sf::Color bgColour){
   
    static sf::Font font;
    font.loadFromFile("fonts/Courier New Bold.ttf");
   
    sf::Text text(string, font, 24);
    text.setColor(fgColour);
    text.setPosition(x, y*text.getGlobalBounds().height-text.getGlobalBounds().top);
   
    sf::RectangleShape rectangle(sf::Vector2f(text.getGlobalBounds().width, text.getGlobalBounds().height));
    rectangle.setPosition(text.getGlobalBounds().left, text.getGlobalBounds().top);
    rectangle.setFillColor(bgColour);

    window->draw(rectangle);
    window->draw(text);
}

But this doesn’t work. The background rectangle is not high enough, and some characters (capital 'A') stick out the side of the highlighted area. I thought It might have something to do with each character having different coordinates so I tried this:

int c = 0;
int offsetY=0;
while(string[c]!='\0') {
    if(text.findCharacterPos(c).y>y*24){
        offsetY=y*24-text.findCharacterPos(c).y;
    }
    c++;
}
rectangle.setSize(sf::Vector2f(text.getGlobalBounds().width, text.getGlobalBounds().height+offsetY));

But that didn’t change anything. This is what I get:



There must be something i'm not getting here.  ???
Any help would be appreciated.
« Last Edit: January 14, 2014, 01:08:23 pm by Hourand »

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::text size and 'highlighting'.
« Reply #1 on: January 13, 2014, 07:25:29 pm »
I had issue with correcting text to a position, the issue was with the font.

To try:
Try with already known good font "arial.ttf" and see if it works as intended, when it does switch font to desired font.
Position the font rectangle correctly.
sf::Text txt;
sf::RectangleShape rec;
rec.setPosition(txt.getPosition().x + txt.getGlobalBounds().left + txt.getGlobalBounds().width / 2 - rec.getSize().x,
txt.getPosition().y + txt.getGlobalBounds().top + txt.getGlobalBounds().height / 2 - rec.getSize().y);
 
« Last Edit: January 13, 2014, 08:38:54 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::text size and 'highlighting'.
« Reply #2 on: January 13, 2014, 07:43:54 pm »
Please read the documentation of the classes you are using. There is a sf::Text::findCharacterPos() function, and the sf::Glyph class provides all the character metrics. There have also been some thread about aligning/positioning text, for example the links here.

By the way, use [code=cpp] code tags, not [code] or [tt]. Also you, BaneTrapper ;)
« Last Edit: January 13, 2014, 07:45:39 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::text size and 'highlighting'.
« Reply #3 on: January 13, 2014, 08:39:32 pm »
By the way, use [code=cpp] code tags, not [code] or [tt]. Also you, BaneTrapper ;)
The shiny armor knight!!! i didn't know that.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Hourand

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::text size and 'highlighting'.
« Reply #4 on: January 16, 2014, 09:22:46 pm »

Thanks for the replies!
Ive found something that works, although i'm not completely sure how. I guess whats referred to as the 'baseline' in the documentation is the bottom of the text bounding box.

static sf::Font font;
    font.loadFromFile("fonts/Arial.ttf");
   
    sf::Text text(string, font, 30);
    text.setColor(fgColour);
    text.setPosition(x, y*text.getGlobalBounds().height-text.getGlobalBounds().top);
   
    int c = 0;
        while(string[c]!='\0') {
            sf::RectangleShape rect;
            rect.setPosition(text.findCharacterPos(c).x+font.getGlyph(string[c], 30, 0).bounds.left, text.getGlobalBounds().top+(text.getGlobalBounds().height+font.getGlyph(string[c], 30, 0).bounds.top));
            rect.setSize(sf::Vector2f(font.getGlyph(string[c], 30, 0).bounds.width, font.getGlyph(string[c], 30, 0).bounds.height));
            rect.setFillColor(bgColour);
            window->draw(rect);
            c++;
        }
   
    window->draw(text);


BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::text size and 'highlighting'.
« Reply #5 on: January 17, 2014, 11:18:41 pm »
Hello.
I did not finish my post but i will attempt to help.
Solution 1:
Change color?
Solution 2:
Draw a rectangle with sf::Color(255,255,255,x) where x represents the amount you want to highlight
Will highlight a square around text tho, but it is fast to highlight whole button and be lazy  :P.

My code bellow does not work... sorry & i cant remember how i did that text centering also i cant find it in old project  :P

IMO you are doing many draw calls to highlight the text like that witch is not necessary.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: sf::text size and 'highlighting'.
« Reply #6 on: January 18, 2014, 05:19:12 am »
Here is a drawing to brievly explain the "bounds".

The central vertical Line represent the  Text.getPosition.x position.

The | | are absolute value ( This mean -1 * (A negative number) )
« Last Edit: January 18, 2014, 05:57:24 am by math1992 »

 

anything