Textbox wrapping
Displaying 1-7 of 7 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
miky

Hey, it's me again :)

After a very long break, I've started on my textbox engine again, and wrapping won't work.

//These are the font and background images.
int font = LoadFont("font1.png");
int box = LoadImage("box.png");

int vborder = 5; //The vertical and horizontal borders.
int hborder = 5;
int linespacing = FontHeight(font) * 2; //The spacing between lines.

void Textbox(string text, int x, int y)
{
int speed = 2; //This is how long it waits between letters.
int i = 0;
Blit(x, y, box, screen);
int origx = x;
int origy = y;
y += vborder;
x += hborder;
int linenum = 1;
int another = 0;
int wordlen = 0;
int wordstart;

while (i < len(text)) //Loop through the text, blitting it letter by letter
{
if (strcmp(mid(text, i, 2), "\n") == 0) //Go to the next line if we encounter a \n
{
i += 2;
y += linespacing;
x = origx + hborder;
}

if (strcmp(mid(text, i, 1), " ") != 0 && strcmp(mid(text, i - 1, 1), " ") == 0)
{
wordstart = i;

while(strcmp(mid(text, wordstart + wordlen, 1), " ") != 0)
{
wordlen += 1;
}

if (x + TextWidth(font, mid(text, wordstart, wordlen)) > origx + ImageWidth(box) - hborder)
{
y += FontHeight(font) * 2;
x = origx + hborder;
}

wordlen = 0;
}

PrintString(x, y, screen, font, mid(text, i, 1));
ShowPage();
x += TextWidth(font, mid(text, i, 1));
i++;

if (b1) //Go to the end if the button is pressed
speed = 0;
Unpress(b1);

Wait(speed);
ShowPage();
}

while (!b1 && !another)
UpdateControls();

Unpress(b1);
}

void Wait(int delay)
{
int timestamp = systemtime;
while (systemtime - timestamp < delay)
{
UpdateControls();
}

}


There's the source. It's just not wrapping at all. Thanks in advance to anyone who can help me.

EDIT: Read the last post.

Posted on 2008-12-11 21:38:00 (last edited on 2008-12-13 11:00:45)

Overkill

Hey miky!

The reason your code probably isn't working is because you need to compare to chr('\n') not "\n".

Why? Because character constants have escape characters, but strings do not. This could be added to Verge, but I fear lots of people will have broken code because I've seen plenty of code that uses a single \ as a delimiter in strings for menu systems and stuff. And when compiled with escape characters introduced, it'd make a lot of compiler errors.

(Maybe I should add this and make a "stresc 1" or something in verge.cfg. I'll think about it)

Posted on 2008-12-11 22:59:30

Overkill

Secondly, why aren't you using the builtin WrapText function to pre-wrap your text! Then you don't need a slow loop with lots of function calls on strings in VC. :D

Posted on 2008-12-11 23:23:33

miky

The reason I'm not using the built-in function is that I'm drawing the text letter-by-letter rather than all at once. Even if I did draw the whole thing at once, it would look silly, because while the first part of a word fits on a line, when the word is drawn enough to grow long enough to wrap, it would disappear and reappear on the second line.

And also, that wasn't my problem. "\n" worked fine for me, but what I'm trying to do is make a word go to the next line if it's too long.

EDIT: I am silly. I accidentally put "pos" instead of "i" at some parts. Now it freezes when it gets to a space.

Posted on 2008-12-12 17:54:54 (last edited on 2008-12-12 18:21:44)

Kildorf

You can get around the problem of it disappearing and appearing on the next line by just ordering your wrap correctly. Instead of:


string toprint = "...";
string chunk = "";
int pos = 0;
int wrapwidth = 150; // or whatever
int font = default_font; // or whatever

while (pos < len(toprint))
{
pos++;
chunk = mid(toprint,0,pos);
printString(0,0,screen,font,wrapText(font,chunk,wrapwidth));
}



instead use...


string toprint = "...";
string chunk = "";
int pos = 0;
int wrapwidth = 150; // or whatever
int font = default_font; // or whatever

toprint = wrapText(font,toprint,wrapwidth); // wrap it HERE instead

while (pos < len(toprint))
{
pos++;
chunk = mid(toprint,0,pos);
printString(0,0,screen,font,chunk); // and don't wrap here
}



Does that make any sense? (Also I'm not certain this code runs exactly as written; it's been a while since I've written any VergeC. Hopefully you can get the idea though.)

Posted on 2008-12-12 18:34:03

miky

Instead of going to a new line, when I PrintString a string with chr('\n') in it, it draws the second line on top of the first line only a little to the right.

And WrapText didn't do anything at all. :(

EDIT: Uh, never mind. I changed it to draw the whole string instead of letter-by-letter, and it works perfectly now. Thanks :)

Posted on 2008-12-13 13:13:40 (last edited on 2008-12-13 13:19:23)

Overkill

Hooray!

Oh, and just for sake of reference, people should check out the textbox system that's included with Molasses Meow (and was originally designed for the remake of Rubbish Rogue).

You can customize the box colors (and shading style -- it doesn't need to be solid colors even though that's how I made the settings in Molasses Meow), the font, how the textbox handles overflow, the position, and other things.

All you should need to do to include the stuff is:
#include "graphics.vc"
#include "textbox.vc"
The only other setup required is as follows:

Make an global int variable called "font_main" somewhere that you set to whatever font handle your textbox uses.

And put the following line in your system.vc's autoexec:
DefaultTextBox();
Enjoy!

Posted on 2008-12-13 16:15:43 (last edited on 2008-12-13 16:16:51)


Displaying 1-7 of 7 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.