String Height in Characters

This is a discussion on String Height in Characters within the C++ Programming forums, part of the General Programming Boards category; I've been working on a game in which I want to display recent events in a sort of action log ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    String Height in Characters

    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;
    }
    Last edited by timmeh; 10-07-2009 at 02:56 PM. Reason: Figured it out myself.
    My Dev Blog - The most up-to-date info on my current projects.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    If I understand what you want correctly, you have a fixed area in which you can write text. Having never worked with the various console graphics libraries, I will make the assumption that the area is defined in terms of characters. That is, say, 10 characters by 10 characters. If you use no line breaks and no words wrap, that would give, at most, 100 characters (adjust to the actual dimensions).

    My personal approach, after thinking for a few minutes, is that you must keep track of where the text will wrap. Count up the characters in the line before this point, and then count the characters after this count (because the line cannot be reused). Keep going until you get to a line break. FOr the line break, add the amount of characters that can be stored in a line to the total. Keep going until you either finish printing this event, or you run out of room. You could attempt doing this first before actually printing anything, and do this for all subsequent events.

    I think a potentially better method is, if your events are defined in some sort of file, pre count up the actual space they take up, and load that with the rest of the event. Then you just have to compare to see if it can fit.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Be careful with that solution. In Windows, there might be a \r before the \n, which won't be actually printed. Meaning that it returns one character more than the actual printed string would display.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by EVOEx View Post
    Be careful with that solution. In Windows, there might be a \r before the \n, which won't be actually printed. Meaning that it returns one character more than the actual printed string would display.
    Hmmm.. I didn't even know about the \d escape sequence.... well, thank's for the heads up, if I have any problems with it I know how to fix it
    My Dev Blog - The most up-to-date info on my current projects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21