Hallo JGO,
I recreate my game in lwjgl (and fix some nasty design flaws) and got kinda stuck at drawing multilined strings. I found multiple resources for Java2D but none for Slick-Util/lwjgl.
So all I did was to “translate” it to gl-drawing:
public static void multiLinedString(float x, float y, String text, int width, float rotation, int color, float alpha, TrueTypeFont ttFont) {
AttributedCharacterIterator m_iterator;
int m_start;
int m_end;
AttributedString styledText = new AttributedString(text);
m_iterator = styledText.getIterator();
m_start = m_iterator.getBeginIndex();
m_end = m_iterator.getEndIndex();
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc);
measurer.setPosition(m_start);
int lastPos = m_start;
while (measurer.getPosition() < m_end){
TextLayout layout = measurer.nextLayout(width);
y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : width - layout.getAdvance();
string(x + dx, y, text.substring(lastPos, measurer.getPosition()), rotation, color, alpha, ttFont);
y += layout.getDescent() + layout.getLeading();
lastPos = measurer.getPosition();
}
}
public static void string(float x, float y, String text, float rotation, int color, float alpha, TrueTypeFont ttFont) {
float r = getRedFromHex(color);
float g = getGreenFromHex(color);
float b = getBlueFromHex(color);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
{
glTranslatef(x, y, 0);
glRotatef(rotation, 0, 0, 1);
ttFont.drawString(0, 0, text, new Color(r,g,b,alpha));
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
My question is now: Is there a more elegant way drawing multilined strings? Did I miss anything in the API or did I google the wrong keywords?