Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Word Wrapping formula.  (Read 1157 times) 0 Members and 1 Guest are viewing this topic.
SkyAphid
 « Posted 2012-05-23 21:07:49 »

So, I'm writing a word wrapping function for my text renderer in openGL, which, already works. Just my problem now is to get word wrapping working :S

So, here's the formula:

 1  2  3  4  5  6  7  8  9 `for (int a = 0; a < text.length(); a ++){         float frame = (float)((textValues[a] - 32) * charWidth) / fontWidth;         float charWidthf = (float) charWidth / fontWidth;                  int yy = y + ((a / boundWidth) * charHeight) + 1;         int xx = (x + (charWidth * (a)));                  //...OpenGL part      }`

charWidth and charHeight in this case are 5 and 7. fontWidth is the size of the image, which is the charWidth multiplied by 95, as we use 95 ASCII characters.

Now, yy works. It pushes the text downwards the further you get along with the text according to the boundWidth, which is how many chars are allowed horizontally before it needs to be pushed down for wrapping. The problem here is xx, which keeps going right regardless of the push of the wrap.

So, what I need is a mathematical formula so that xx will go back to the starting x position everytime it's wrapped.

Sorry if this a hard question, I'm terrible at explaining this stuff.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
BoBear2681

JGO Coder

Medals: 19

 « Reply #1 - Posted 2012-05-23 21:22:51 »

If you're rendering one character at a time, why not just increment "xx" by the character width for each char, and whenever you're going to go past the right-hand side, simply reset xx to the starting value and increment yy by the font height?  This doesn't break on word boundaries of course, but you don't appear to be doing that anyway.
davedes
 « Reply #2 - Posted 2012-05-23 21:24:28 »

Word wrapping generally depends on things like whitespace, word breaks (i.e. punctuation), and so forth. So it's not so much a "mathematical formula" you need, as a way to check characters and respond accordingly.

TWL is pretty solid with word wrapping. LibGDX also has some word wrapping code that might help. Both are open source so you can see how they did it.

If you just want to wrap text to a given width, with no regard to whitespace or word breaks, something really basic might look like:
 1  2  3  4  5  6  7  8  9  10  11  12 `startX = 50, startY = 50; //start positionx = startX;y = startY;for each glyph {     if (x + glyph.width > maxWidth || glyph.chr == '\n') {          //bump down a line          x = startX;          y += lineHeight + lineSpacing;     }     glyph.draw(x, y);     x += glyph.width; //also increase by xadvance, kerning info, etc.}`

Also IMO you shouldn't hard-code things like having 95 ASCII characters unless you are under some very serious constraints (i.e. Java4K). You'll regret it later, when you realize how restricted your hard-coded system is. For the sake of flexibility and easier rendering, just use a Glyph object that has its own x/y position in the glyph page, its own width/height, kerning info, etc.

SkyAphid
 « Reply #3 - Posted 2012-05-23 21:47:21 »

Word wrapping generally depends on things like whitespace, word breaks (i.e. punctuation), and so forth. So it's not so much a "mathematical formula" you need, as a way to check characters and respond accordingly.

TWL is pretty solid with word wrapping. LibGDX also has some word wrapping code that might help. Both are open source so you can see how they did it.

If you just want to wrap text to a given width, with no regard to whitespace or word breaks, something really basic might look like:
 1  2  3  4  5  6  7  8  9  10  11  12 `startX = 50, startY = 50; //start positionx = startX;y = startY;for each glyph {     if (x + glyph.width > maxWidth || glyph.chr == '\n') {          //bump down a line          x = startX;          y += lineHeight + lineSpacing;     }     glyph.draw(x, y);     x += glyph.width; //also increase by xadvance, kerning info, etc.}`

Also IMO you shouldn't hard-code things like having 95 ASCII characters unless you are under some very serious constraints (i.e. Java4K). You'll regret it later, when you realize how restricted your hard-coded system is. For the sake of flexibility and easier rendering, just use a Glyph object that has its own x/y position in the glyph page, its own width/height, kerning info, etc.

I hard coded 95 to a static integer so it's easy to recognize and change later, but the reason I did so is because I will only use the ASCII values 32-127, ever. lol. The first 31 are irrelevant to what I need. But I understand what you mean. Thank you.

I'll be extending my text splicing method so that it sees full words and makes sure they are split too, instead of the \n thing. I'm not really a fan of having to keep track of that kind of stuff.

Thank you both for input

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (16 views) 2015-05-23 20:26:16 MrMapcom (22 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (36 views) 2015-05-20 03:42:21 BurntPizza (71 views) 2015-05-10 15:53:18 FrozenShade (56 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (207 views) 2015-05-06 13:33:39 TheLopais (227 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x Drenius 12x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x princec 10x KevinWorkman 9x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org