I have a libgdx screen's render method where I am executing lots of for loops and drawing various shapes on screen in thoese loops. Also I am manupulating (x,y) of each shape at runtime to acheive a smooth animation.Everything was ok until,

Issue: Recently I had added few more shape's at some positions in my screen. Due to some mess up, I am getting flicker, and some shapes are getting drawn out of my game world.

I tried to debug my code many times but there are too many loops and shapes so I am unable to pin point the exact loop or frame where shapes are getting drawn out of my game world.

Need Input on following approach to debug:

So I was thinking to save each frame of my render method as an image locally. Is it possible? Please provide suggestions or sample code to acheive it.

I will hook up some debug variables in my loops to calculate exact frame where things are getting messed up. That I will take care of. Please advice on above question.

Thanks in advance.


1 Answer 1


It is absolutly possible. Here's a simple class that, when called, will save a screenshot of the main graphical context:

public class ScreenshotFactory {

    private static int counter = 1;
    public static void saveScreenshot(){
            FileHandle fh;
                fh = new FileHandle("screenshot" + counter++ + ".png");
            }while (fh.exists());
            Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
            PixmapIO.writePNG(fh, pixmap);
        }catch (Exception e){           

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
        final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

        if (yDown) {
            // Flip the pixmap upside down
            ByteBuffer pixels = pixmap.getPixels();
            int numBytes = w * h * 4;
            byte[] lines = new byte[numBytes];
            int numBytesPerLine = w * 4;
            for (int i = 0; i < h; i++) {
                pixels.position((h - i - 1) * numBytesPerLine);
                pixels.get(lines, i * numBytesPerLine, numBytesPerLine);

        return pixmap;

The only thing you have to do if you have this class in your project is to call the following line in each of your render loop:


It will save the screenshot as screenshotX.png, X being the smallest number available, starting with 1.

Just make sure you draw the frame number or an identifier on the image to be able to match it with all your debug messages.


  • \$\begingroup\$ Thank you very much, I will try this and get back to you in case of any doubt. \$\endgroup\$
    – kingAm
    Commented Jun 18, 2015 at 12:55
  • \$\begingroup\$ @kingAm If you don't have any problems with this code and it answered your question, feel free to accept it using the gray checkmark below the up and down vote arrows. \$\endgroup\$ Commented Jun 19, 2015 at 12:55
  • \$\begingroup\$ I know that, i will try this code first and award you, don't worry.Thanks again. :D \$\endgroup\$
    – kingAm
    Commented Jun 19, 2015 at 13:18
  • \$\begingroup\$ @kingAm My bad, I tought you had plenty of time yesterday to test it, and you didn't come back with any feedback... \$\endgroup\$ Commented Jun 19, 2015 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .