I'm writing a text wrapping function that takes a string and breaks it up in to multiple strings based on it's position in the window. I'm using a for loop and sf::Text.getCharacterpos().x to find the first character that is outside of the text box.
here's the problem I'm having.
lines that are composed of mostly large character's (like "k") are longer then lines that are composed of mostly smaller characters(like "j").
hopefully someone can help me understand what i'm doing wrong.
here's the function I'm working on,
//text wrapping function
//this function takes 3 arguments
// #1 input_text : this argument passes in the original string via an sf::text object which will
// need to be broken up into into smaller strings based on the size of the intended text box.
// #2 output_string: this argument will pass in a reference to the desired string vector so the function
// can push back as many individual lines as needed.
// #3 line_width : use this argument to pass in the desired line length in pixels.
void wrapText(sf::Text input_text, std::vector<std::string> &output_string, int string_width)
{
std::string string = input_text.getString();
std::string temp_string;
for(int i = 0; i < string.size(); i++)
{
if(input_text.findCharacterPos(i).x > string_width)
{
for(int j = 0; j < i; j++)
{
temp_string += string[j];
}
output_string.push_back(temp_string);
temp_string = "";
for(int k = i; k < string.size(); k++)
{
temp_string += string[k];
}
string = temp_string;
temp_string = "";
i = 0;
}
}
output_string.push_back(string);
};
thanks in advance for any help