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

前端 未结 1 1989
无人及你
无人及你 2021-01-03 09:56

I just recently updated my Libgdx project from 1.4.x to 1.6.1. I use BitmapFontCache for my dialogue in my game, drawing a string character by character using BitmapFontCach

相关标签:
1条回答
  • 2021-01-03 10:20

    OK, I know where the issue lies, and I'm pretty certain it's a bug in libgdx.

    I also have a workaround, although it's a little hacky.

    The Problem When GlyphLayout wraps a line on a space character, it optimises out the terminating space. So with the space removed, the total number of glyphs in the layout is now less than the number of characters in the string. The more lines that get wrapped on a space character, the bigger the discrepency will be between the two.

    The Workaround In order to work out what length to use for rendering the full text therefore, 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 that...

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

    The GlyphLayout to pass in will be the one that was returned by the BitmapFontCache.addText() method.

    0 讨论(0)
提交回复
热议问题