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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Hourand

Pages: [1]
1
Graphics / Re: sf::text size and 'highlighting'.
« 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);


2
Graphics / 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.

Pages: [1]