(Libgdx 1.6.1)由于索引超出范围,BitmapFontCache.draw崩溃((Libgdx 1.6.1) BitmapFontCache.draw crashing due to in

编程入门 行业动态 更新时间:2024-10-19 19:52:52
(Libgdx 1.6.1)由于索引超出范围,BitmapFontCache.draw崩溃((Libgdx 1.6.1) BitmapFontCache.draw crashing due to index out of bounds)

我刚刚将我的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.

更多推荐

本文发布于:2023-08-06 03:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   BitmapFontCache   Libgdx   draw   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!