Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Word Wrapping formula.  (Read 1007 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline 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.
Offline 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 position
x = 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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 position
x = 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 Smiley

“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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!