I've been working on a game in which I want to display recent events in a sort of action log on the character's info screen. The log is a box on the right-hand side of the screen, with a fixed height and width. The log is stored as a vector of strings, with each element representing the text output for a single event. I use PDCurses, so I can add the pieces of the vector together, and output that into the box and it will wrap (by character, as in a normal console window) to fit the width of the window automatically, and keeps it inside the box.
The problem here, is that it takes the string passed to it, and outputs as much of it as will fit, starting from the beginning. What I need to do is display as many of the most recent events as will fit. My plan is to start at the end of the vector and add each event in reverse, making sure it doesn't make it too long.
What I need to know is how to tell when it gets too long. What would be the best (read: easiest) way to get a height (in characters) from a string and a maximum width, counting both where the text would wrap, and any line breaks (\n) in the string?
I'm not going to ask for code, as I'll learn more if I figure it out myself, but what I would really appreciate is a direction to take as far as the logic goes, and relevant functions or other things I should look up.
Thank you in advance,
Timothy
[EDIT]: I feel stupid now... the solution was rather simple. Here is the function I ended up with...
Code:
int get_string_height(string mystring, int maxwide)
{
int height = 0;
int wide=0;
int stringsize = mystring.size();
for(int pos=0; pos<stringsize; pos++)
{
if(mystring.at(pos)=='\n')
{
height++;
wide=0;
}
else if(wide>maxwide)
{
height++;
wide=0;
}
else
{
wide++;
}
}
return height;
}