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.