Libgdx save SpriteBatch in a texture

蹲街弑〆低调 提交于 2019-12-10 11:42:58

问题


I would like to know if it possible to save a spriteBatch in a texture.

 SpriteBatch batch = new SpriteBatch();

After drawing a few thing inside the batch, I would like to save all thing that contains the SpriteBatch in One texture (something like a screenshot).

I have no idea to how to do it, I searched on the web and on the libgdx doc but didn't found.

Thanks you


回答1:


You can render to a FrameBufferObject (FBO). See https://github.com/mattdesl/lwjgl-basics/wiki/FrameBufferObjects

An FBO will work if you are okay with making the decision to render to a texture in advance. One side-effect is that the image is not rendered to the screen, but only to the texture. (Its easy enough to render the texture to the screen afterwards, of course).

As the other answer suggested, you can scrape the bytes off the screen buffer, and make a Texture from the resulting Pixmap (you do not need to go all the way to the filesystem). See https://code.google.com/p/libgdx-users/wiki/Screenshots (just use the getScreenshot method to get a Pixmap of the bytes).




回答2:


Use the conversion to texture like this:

final Pixmap pmap = new Pixmap(bytes, 0, bytes.length);
try{
    Gdx.app.postRunnable(new Runnable(){
        public void run(){
        texture=new Texture(pmap);
        }
    });
}catch(Exception e){
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/20731463/libgdx-save-spritebatch-in-a-texture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!