Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created September 6, 2017 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom-Ski/d8b0be1c8b19cc735e5404c93c2e70a6 to your computer and use it in GitHub Desktop.
Save Tom-Ski/d8b0be1c8b19cc735e5404c93c2e70a6 to your computer and use it in GitHub Desktop.
public FileHandle saveFrameBuffer (FrameBuffer frameBuffer) {
FileHandle handle = Gdx.files.external("myscreenshot.png");
frameBuffer.bind();
Pixmap pixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Pixmap.Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(0, 0, frameBuffer.getWidth(), frameBuffer.getHeight(), GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);
try {
PixmapIO.PNG writer = new PixmapIO.PNG((int)(pixmap.getWidth() * pixmap.getHeight() * 1.5f)); // Guess at deflated size.
try {
writer.setFlipY(true);
writer.write(handle, pixmap);
} finally {
writer.dispose();
}
} catch (IOException ex) {
throw new GdxRuntimeException("Error writing PNG: " + handle, ex);
}
frameBuffer.dispose();
pixmap.dispose();
return handle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment