I have implemented a gif-button for my libgdx game that allows the player to store the last few seconds of gameplay as a gif.
It works by storing frames in memory, and encoding them into a gif if the player presses a Button.
The problem is, that my method of storing the frames takes around 20ms, far to slow for my 60fps game. This leads to stuttering.
Currently im storing the frames like this:
private static LinkedList<Pixmap> frames = new LinkedList<>();
private static float delay = 0;
static Pixmap pixmap;
static int width = Gdx.graphics.getWidth();
static int height = Gdx.graphics.getHeight();
// called every frame
public static void act(float delta) {
delay -= delta;
if (delay <= 0f) {
// the actual storing of the frame. ~20ms
Gdx.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
Gdx.gl.glReadPixels(0, 0, width, height, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixmap.getPixels());
frames.add(pixmap);
// remove old frames and reset delay
}
}
I have tried to allocate as little memory as possible but its still way to slow.
Concurrency is not an option for this approach since the gl methods need to be called on the rendering thread.
I would be very thankful for a solution or ideas for other possible ways to achieve my goal.