1

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

LeSam
  • 1,235
  • 4
  • 18
  • 38
  • if its just for taking screen shot then use this. https://code.google.com/p/libgdx-users/wiki/Screenshots – Vikalp Jain Dec 22 '13 at 19:00
  • I know this code, and it's not good for 2 reason : 1. You must save the screenshot in a file. 2. Saving in a file and load the file in a Texture will be too long, it's waste a lot of time in the execution. Keep in mind that I want this screenshot in a Texture – LeSam Dec 22 '13 at 19:02
  • use texture = new texture(pixmap) instead of saving to a file – Vikalp Jain Dec 22 '13 at 19:18
  • I get a Fatal signal 11 (SIGSEGV) at 0x673b3000 (code=1), thread 5116 (Thread-19425) – LeSam Dec 22 '13 at 19:27
  • use the code below to convert to texture – Vikalp Jain Dec 22 '13 at 19:51

2 Answers2

2

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).

P.T.
  • 24,557
  • 7
  • 64
  • 95
1

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();
}
Vikalp Jain
  • 1,419
  • 1
  • 11
  • 20