我有一个文字游戏的动态纹理,该纹理是我创建并保留在FrameBuffer上的。然后,我使用该FrameBuffer支持的TextureRegions绘制到屏幕上。这段代码在Android上运行良好,但在iOS上却遇到问题。
您可以在下面的屏幕截图中看到,在左下方绘制了FrameBuffer内容-一切都很好。
但是,如果返回到iOS主屏幕,则会丢弃FrameBuffer,然后根据需要重新创建所有内容并将其重新绘制到FrameBuffer。然后,FrameBuffer的内容似乎是屏幕的部分翻转镜像。
I tried disposing and recreating the FrameBuffer on every frame to see what happens. Again, this works perfectly unless I return to the iOS home screen, after which the contents of the FrameBuffer are incorrect.
I then tried reducing the size of the FrameBuffer and drawing across mutliple FrameBuffers. If I do this it again works fine unless I return to the iOS home screen. However, in this instance only the first FrameBuffer created becomes corrupted, all subsequent buffers are fine.
I then tried always creating a 1x1px FrameBuffer first that I never draw anything to and this fixes my problem.... but why!? It seems the first FrameBuffer I create after returning from the iOS home screen is not working as expected, but every FrameBuffer after that is fine. It looks like it's always trying to draw the screen over the first FrameBuffer I create.
FrameBuffer的创建和绘制代码如下:
private static FrameBuffer getFrameBuffer(boolean newRequired) {
if (newRequired || frameBuffers.size == 0) {
FrameBuffer frameBuffer;
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, getFrameBufferSize(), getFrameBufferSize(), false);
} catch (Exception e) {
frameBuffer = new FrameBuffer(Format.RGBA4444, getFrameBufferSize(), getFrameBufferSize(), false);
}
frameBuffer.begin();
Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
frameBuffer.end();
frameBuffers.add(frameBuffer);
// Set up the camera correctly for the frame buffer
OrthographicCamera camera = new OrthographicCamera(frameBuffer.getWidth(), frameBuffer.getHeight());
camera.position.set(frameBuffer.getWidth() * 0.5f, frameBuffer.getHeight() * 0.5f, 0);
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
}
return frameBuffers.get(frameBuffers.size - 1);
}
private static TextureRegion getTextureUsingGpu(String letter, Bubble.BubbleType bubbleType) {
if (!enabled)
return null;
TextureRegion tx = getBlockImage(letter, bubbleType);
int width = (int) (tx.getRegionWidth() * LayoutManager.getShortEdge() / 1080 * LayoutManager.getScaling());
int height = (int) (tx.getRegionHeight() * LayoutManager.getShortEdge() / 1080 * LayoutManager.getScaling());
if (nextTextureX + width + PADDING > getFrameBufferSize()) {
nextTextureX = PADDING;
nextTextureY = nextRowY + PADDING;
if (nextTextureY + height + PADDING > getFrameBufferSize()) {
getFrameBuffer(true);
nextRowY = PADDING;
nextTextureY = PADDING;
}
}
FrameBuffer fb = getFrameBuffer(false);
fb.begin();
spriteBatch.begin();
tx.flip(false, !tx.isFlipY());
spriteBatch.disableBlending();
spriteBatch.draw(tx, nextTextureX, nextTextureY, width, height);
spriteBatch.enableBlending();
// Drawing Code Removed
spriteBatch.end();
fb.end();
TextureRegion textureRegion = new TextureRegion(fb.getColorBufferTexture(), nextTextureX, nextTextureY, width, height);
textureRegion.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
cacheTexture(letter, textureRegion);
nextTextureX += width;
nextRowY = Math.max(nextRowY, nextTextureY + height);
return textureRegion;
}
如果您正在调用getFrameBuffer
或getTextureUsingGpu
通过您的resize
或resume
方法进行调用,那就是问题所在。在iOS上,resize
而resume
不必调用之前OpenGL是准备恢复,所以它不是安全的做任何事情直接关系到OpenGL的出现,如创建ShaderPrograms,装载纹理,或创建帧缓冲区。我在游戏中通过resume
设置boolean成员来解决此问题,并使用该成员将新帧缓冲区的创建推迟到该render
方法中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句