当我想要在我的关卡编辑器中创建一个关卡的PNG时,遇到了类似的问题。 级别由创建者放置的图块制作而成,然后将整个级别保存为.png以用于游戏。
解决了P.T.解释。我希望它对遇到同样问题的其他人有用。
而在你的应用程序配置中:cfg.useGL20 = true; GL20需要能够建立帧缓冲
public boolean exportLevel(){
//width and height in pixels
int width = (int)lba.getPrefWidth();
int height = (int)lba.getPrefHeight();
//Create a SpriteBatch to handle the drawing.
SpriteBatch sb = new SpriteBatch();
//Set the projection matrix for the SpriteBatch.
Matrix4 projectionMatrix = new Matrix4();
//because Pixmap has its origin on the topleft and everything else in LibGDX has the origin left bottom
//we flip the projection matrix on y and move it -height. So it will end up side up in the .png
projectionMatrix.setToOrtho2D(0, -height, width, height).scale(1,-1,1);
//Set the projection matrix on the SpriteBatch
sb.setProjectionMatrix(projectionMatrix);
//Create a frame buffer.
FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
//Call begin(). So all next drawing will go to the new FrameBuffer.
fb.begin();
//Set up the SpriteBatch for drawing.
sb.begin();
//Draw all the tiles.
BuildTileActor[][] btada = lba.getTiles();
for(BuildTileActor[] btaa: btada){
for(BuildTileActor bta: btaa){
bta.drawTileOnly(sb);
}
}
//End drawing on the SpriteBatch. This will flush() any sprites remaining to be drawn as well.
sb.end();
//Then retrieve the Pixmap from the buffer.
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Close the FrameBuffer. Rendering will resume to the normal buffer.
fb.end();
//Save the pixmap as png to the disk.
FileHandle levelTexture = Gdx.files.local("levelTexture.png");
PixmapIO.writePNG(levelTexture, pm);
//Dispose of the resources.
fb.dispose();
sb.dispose();
注:我是新来LibGDX所以这可能不会是这样做的最巧妙的方法。
纹理是由用户在应用程序中创建的一个简单的类似油漆的界面,所以我手边没有任何数据。如果我可以从FrameBuffer中提取信息来创建完美的纹理,但我的印象是,您只能从绘制到屏幕的默认FrameBuffer中执行此操作。在我的情况下,纹理可能会超出屏幕的分辨率,所以它会切断或扭曲此路线上的图像。不幸的是,使用Pixmaps自己绘图也不是一种选择。 – Shamrock 2013-02-25 02:33:50