我刚刚将我的Libgdx项目从1.4.x更新到1.6.1。 我在我的游戏中使用BitmapFontCache进行对话,使用BitmapFontCache.draw(开始,结束)按字符绘制字符串字符。 这在1.4.x中工作正常,但在进行必要的更改以获得1.6.1构建之后,在显示最后一个字符后启用换行时,似乎会导致崩溃。 奇怪的是,这似乎不是一个线串的问题。
以下是我添加文字的方法:
fontCache.addText( message, fontPosX, fontPosY, fontWidth, Align.left, true);然后我增加字符数并绘制。 currentCharacter在根据字符串长度到达字符串末尾时停止:
fontCache.draw( batch, 0, currentCharacter );这在1.4.x中运行良好,即使使用多行换行,但如果行换行到第二行(在绘制最后一个字符后崩溃),似乎会导致出界异常。 这是导致SpriteBatch崩溃的行。
System.arraycopy(spriteVertices, offset, vertices, idx, copyCount);我需要一种新的方法来计算绘制字符串的长度吗? 我是否需要以某种方式使用返回GlyphLayout? 或者这可能是一个错误?
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 BitmapFontCache.draw(start, end). This was working fine in 1.4.x but after making the necessary changes to get 1.6.1 to build, it seems to cause a crash when wrapping is enabled after the last character is displayed. Strangely this does not seem to be a problem with one line strings.
Here is how I add my text:
fontCache.addText( message, fontPosX, fontPosY, fontWidth, Align.left, true);Then I increment the character count and draw. currentCharacter stops when reaching the end of the string based on its length:
fontCache.draw( batch, 0, currentCharacter );This worked fine in 1.4.x even with multi-line wrapped strings but seems to cause an out of bounds exception if the lines wraps to a second line (crashes after drawing the last character). Here is the line causing crash in SpriteBatch.
System.arraycopy(spriteVertices, offset, vertices, idx, copyCount);Is there a new way I need to be calculating the length of the string for drawing? Do I need to use the return GlyphLayout in some way? Or is this perhaps a bug?
最满意答案
好的,我知道问题出在哪里,我很确定这是libgdx中的一个错误。
我也有一个解决方法,虽然它有点hacky。
问题当GlyphLayout在空格字符上包裹一行时,它会优化终止空间。 因此,删除空格后,布局中的字形总数现在小于字符串中的字符数。 包裹在空格字符上的线越多,两者之间的差异就越大。
解决方法为了计算出渲染全文的长度,我们需要计算GlyphLayout中的字形数而不是字符串中的字符数。 这里有一些代码可以做到......
private int calcLength(GlyphLayout glyphLayout) { int length = 0; for(GlyphLayout.GlyphRun run : glyphLayout.runs) { length += run.glyphs.size; } return length; }传入的GlyphLayout将是BitmapFontCache.addText()方法返回的BitmapFontCache.addText() 。
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.
更多推荐
发布评论