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());

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

  • \$\begingroup\$ Have you considered keeping your buffer of frames GPU-side (VRAM permitting), so you don't need to read it back to the CPU each frame? \$\endgroup\$ – DMGregory Apr 7 '20 at 18:35
  • \$\begingroup\$ Hey @DMGregory , thanks for your reply. I have thought about that but im pretty new to game-dev and libgdx. Do you have a link or maybe a class for me to look into that would allow me to achieve that in libgdx? \$\endgroup\$ – Kasuyakema Apr 7 '20 at 19:13
  • \$\begingroup\$ I think @DMGregory refers to FrameBufferObjects? You can render your scene to textures that are then stored in a FrameBufferObject. In LibGDX there is an API for that: github.com/mattdesl/lwjgl-basics/wiki/FrameBufferObjects. These FrameBufferObjects can then be converted to Texture objects where you then can get the image data. \$\endgroup\$ – mrdlink Apr 11 '20 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.