Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ugh! Problem with Word Wrap!

Started by Zeraan Apr 14, 2002 at 9:40 PM 3 replies 500+ views
Original Post
Zeraan
Zeraan
I''m currently working on a remake of the classic NES Dragon Warrior. I''m in process of adding Text Message where all the conversations happen. But I''m stumped by this stupid little problem called Word Wrap. I''m making my own Font engine. Anyway, here''s the code that I have for word wrap: for(int j = 0+(currentLine*68); j <= 68+(currentLine*68); j++) { if(Msg[j] == '' '') { tracker = j; } } Each line is 69 letters long, and this is how it works: It takes the last space in the line and ends there, and starts another line. The problem is that the word wrap works perfectly in the first line, but fails miserably after that. For example This is a testing. | This word shouldn''t| be wrapped. | actually is This is a testing. | This wor | d shouldn''t | be wrapped. | or something like that. I don''t know what''s wrong with it. To debug... Or not to debug... That is the question.
TheCray
TheCray
I''m not sure what''s causing those problems, but if your lines are 69 letters long, then you may want to use this instead :
for(int j = currentLine*69; j < 69+(currentLine*69); j++) {
...
JJXtra
JJXtra
Hopefully your font is fixed width for all characters, otherwise making word wrap will be harder. You might parse your string until it goes past the text box, then go back until you hit a '' '' character. The characters past the '' '' character will go to the next line, and so on. Does that make any sense? Or did you already know that?

-Jeff
Jeff Johnson"He who questions training, only trains himself at asking questions" - Sphynx
Kylotan
Kylotan
Instead of looping through the whole 69 characters, why not just start at the end and count backwards until you find a space? You can ''break'' at that point and speed up the system.

I think the problem is that you''re using variable-sized lines, but always stepping forward 69 each time! So if you found the space after 66 letters, you''d end the line at position 66 in the original string but the next line wouldn''t start until position 70... meaning you lose letters 68 and 69.

The initialisation value for j each time should be ''tracker+1'', ie. just past the end of the previous line.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.