我们终于在App Store上发布了我们的独立游戏,发现我们在iPhone 6s上存在重大的渲染问题。
游戏是使用LibGDX和RoboVM(v.1.12)
开发的我们使用iOS原生图形库将unicode文本渲染到纹理,以将其传递给LibGDX以显示为图像。 为此,我们首先创建要打印的字符串,然后调用其draw()方法:
final String tmp = text.substring(0, text.length() >= MAX_NAME_LEN ? MAX_NAME_LEN : text.length());
final NSMutableString string = new NSMutableString(tmp.length());
//Allocate space for the bitmap to draw on
int w = (int) width;
int h = (int) height;
final int bufferLen = w * h;
final CGBitmapContext context = new CGBitmapContext(
w, h, 8, 0, CGColorSpace.deviceRGB(), CGImageAlphaInfo.PremultipliedLast
);
final CGColor bgColor = UIColor.clear().getCGColor();
final CGRect rect = new CGRect(0, 0, width, height);
string.setString(tmp);
final UIColor textColor = new UIColor(color.r, color.g, color.b, color.a);
final UIFont font = UIFont.getBoldSystemFont(size);
final NSString tester = new NSString("a");
final CGSize textSize = tester.getSize(font);
int strLen = string.toString().length();
/**
* Computes the optimal length of the string and removes characters beyound the
* screen bounduaries.
* If the string has been reduced, appends an ellipsis at the end
*/
final NSRange range = new NSRange();
boolean removed = false;
while (strLen > 0 && textSize.getWidth() * strLen > width - textSize.getWidth() * 2) {
range.setLocation(strLen - 1);
range.setLength(1);
string.deleteCharacters(range);
strLen = string.toString().length();
removed = true;
}
//append ellipsis at the end if we removed a char
if (removed) {
string.append("\u2026");
}
context.setFillColor(bgColor);
context.fillRect(rect);
//set up drawing attributes
final NSAttributedStringAttributes attributes = new NSAttributedStringAttributes();
attributes.set(NSAttributedStringAttribute.Font, font);
attributes.set(NSAttributedStringAttribute.ForegroundColor, textColor);
final NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.setParagraphStyle(NSParagraphStyle.getDefaultParagraphStyle());
paragraphStyle.setLineBreakMode(NSLineBreakMode.TruncatingTail);
attributes.set(NSAttributedStringAttribute.ParagraphStyle, paragraphStyle);
final NSAttributedString finalStr = new NSAttributedString(string.toString(), attributes);
UIGraphics.pushContext(context);
context.setTextMatrix(CGAffineTransform.Identity());
context.translateCTM(0f, h);
context.scaleCTM(1, -1);
finalStr.draw(new CGPoint(rect.getX(), (rect.getY() + textSize.getHeight()) / 2));
UIGraphics.popContext();
return new BufferTextureData((int)context.getWidth(), (int)context.getHeight(), 0, GL20.GL_RGBA, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, context.getData().asIntBuffer(bufferLen));
然后,TextureBufferData以下列方式将Buffer绑定到纹理:
private Buffer data;
public BufferTextureData(int width, int height, int mipMapLevel, int internalFormat, int format, int type, Buffer data) {
this.width = width;
this.height = height;
this.mipLevel = mipMapLevel;
this.internalFormat = internalFormat;
this.format = format;
this.type = type;
this.data = data;
}
[...]
@Override
public void consumeCustomData(final int target) {
Gdx.app.debug("BufferTextureData", "consuming data for target: " + target);
Gdx.gl20.glTexImage2D(target, mipLevel, internalFormat, width, height, 0, format, type, data);
data = null;
}
我们在这些设备上测试了此代码没有任何问题: - iPhone 6 - iPad Air 2 - iPhone 5S 还有iPhone 6s模拟器。
问题是在iPhone 6s的实际设备上我们得到了这个:
正如您所看到的,文本完全失真。似乎在drawcall期间写入了包含绘图数据的缓冲区,但我们确保在绘图线程上调用该方法以避免GL上下文切换。
我们真的不知道在哪里寻找。 代码无缝地在iPhone6s模拟器(x64 CPU)和所有其他设备上运行的事实使我认为问题是硬件依赖或由RoboVM处理内存的方式引起的,但它只是一个猜测。 / p>
是否有人遇到过同样的问题或对可能导致此行为的原因有所了解?
提前致谢
答案 0 :(得分:1)
这是对齐和/或行跨步问题。我建议你阅读glPixelStore手册及其GL_UNPACK_ ...参数。在使用glTexImage
加载之前,您必须设置像素存储参数以匹配图像数据答案 1 :(得分:0)
感谢@datenwolf指出我们的方向。
问题出在某种程度上是由于iphone 6s的“已知错误”:
http://ios9news.net/fix-ios-9-apps-zoomed-in-on-iphone-6s/
也就是说,考虑到iphone 6的相同分辨率和ppi,6s认为他有点宽。 因此我们制作的POT倍数的游戏单元在6s设备中的长度是奇数(例如iphone 6上的宽度为480px,iphone 6s为561px)。
当我们使用CGContextCreate
创建图形上下文时,我们没有指定每行需要多少字节,我们让设备为我们决定。事实证明,操作系统正在为每一行添加填充,搞乱了所有的步幅和对齐。
解决方法是改变:
final CGBitmapContext context = new CGBitmapContext(
w, h, 8, 0, CGColorSpace.deviceRGB(), CGImageAlphaInfo.PremultipliedLast
);
为:
final CGBitmapContext context = new CGBitmapContext(
w, h, 8, 4 * w, CGColorSpace.deviceRGB(), CGImageAlphaInfo.PremultipliedLast
);