(Libgdx 1.6.1) BitmapFontCache.draw crash due to index out of bounds

I recently upgraded my Libgdx project from 1.4.x to 1.6.1. I am using BitmapFontCache for my dialog in my game, drawing a string character with a character using BitmapFontCache.draw (start, end). This worked fine in 1.4.x, but after making the necessary changes to get 1.6.1 to build, it seems to crash when the wrapper is enabled after the last character is displayed. Oddly enough, this is not a single line of line issue.

This is how I add my text:

fontCache.addText( message, fontPosX, fontPosY, fontWidth, Align.left, true);

      

Then I increase the number of characters and draw. currentCharacter stops when the end of the string is reached, depending on its length:

fontCache.draw( batch, 0, currentCharacter );

      

This worked fine in 1.4.x even with multi-line wrapped strings, but seems to throw an out-of-bounds exception if the lines are wrapped in a second line (crash after drawing the last character). Here is the line causing the SpriteBatch to fail.

System.arraycopy(spriteVertices, offset, vertices, idx, copyCount);

      

Is there a new way to calculate line length for drawing? Should I be using the returned GlyphLayout in some way? Or could it be a bug?

+3


source to share


1 answer


Ok I know where the problem is and I'm pretty sure it's a bug in libgdx.

I also have a workaround, although it's a bit of a hack.

Problem When GlyphLayout

trailing a string with a space character, it optimizes trailing space. Thus, with the space removed, the total number of glyphs in the layout is now less than the number of characters per line. The more lines are wrapped around the space character, the more the difference will be between them.



Workaround To figure out what length to use to display the full text, we need to count the number of glyphs in the GlyphLayout instead of the number of characters in the String. Here's some code that does it ...

private int calcLength(GlyphLayout glyphLayout) {

    int length = 0;
    for(GlyphLayout.GlyphRun run : glyphLayout.runs) {
        length += run.glyphs.size;
    }
    return length;
}

      

The transfer GlyphLayout

will be the one returned by the method BitmapFontCache.addText()

.

+2


source







All Articles