Advertisement

Word Wrapping

Started by December 29, 2000 04:20 PM
3 comments, last by Fresh 24 years, 1 month ago
I know it''s been asked before - but how do I word wrap?? not just char wrap, I can do that, but actual word wrapping? my function thus far:

	if (strlen(string) < horizontal_capacity)
		ConsoleFont.DrawStringAt(string, x_offset, y_offset);
	else {
		while (string_remaining) {
			string_remaining -= horizontal_capacity;
			if (string_remaining < 0) string_remaining = 0;
			_snprintf(buf, horizontal_capacity, string);
			string += horizontal_capacity;
			ConsoleFont.DrawStringAt(buf, x_offset, y_offset);
			y_offset += char_height + 2;
		}
	}
 
Thanks in advance "The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."
How about this: Everytime you finish drawing a letter, check if the next letter is a space (Ascii value 32), if it is a space, start looking for the next space right after it.
Then calculate the width of the letters between those 2 spaces (Which is actually a word), and if it''s too big to fit, move to the next line.

Something like this (pseudo code):

while (string is not over)
{
dds->DrawLetter();
if (next letter is a space)
{
while (string is not over)
{
find next space, or end of string
}
if (length of word is bigger than what you need)
{
move to next line
}
}
}



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Advertisement
You might also chop the original string into different strings.
If you''re going letter by letter, record the position of last space or hyphen. When you have a word that is too long, blank the line from the position of last space or hyphen to the marghin and then reprint the too-long word from the start of the next line.


ZoomBoy
Developing a 2D RPG with skills, weapons, and adventure.
See my character editor, old Hex-Tile editor, diary, 3D Art resources at
Check out my web-site
Write the text a word at a time. For each word, calculate the word''s length. If the word''s length + your current cursor position will be too much, start a new line. Then draw the word.
ok I did it
Not the way you guys suggested (overly excessive array parsing and isspace''ing
What I do is:
1. check to see if strlen(string) > horizontal capacity.
2 If no, draw and return, if yes draw horizontal capacity much of the string, advance the string pointer by horizontal capacity, backtrack to to the last space, and procede.

3. do the above with error checking.

Probably not the fastest way of doing things, but faster than the ideas suggested. I could just store how I draw it, but I redo this every frame, which kind of beats the purpose I know. but they I hope that helps anybody else who wanted to know

r.

"The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

This topic is closed to new replies.

Advertisement