2013-12-09 35 views
2

我试图做一个截图保护程序例程。我正在使用的代码here作为基础,因此产生的代码是这样的:截图例程给出一个LibGdx空白图像

public void update(float deltaTime) { 
     if(Gdx.input.isKeyPressed(Keys.ESCAPE)) { 
      Gdx.app.exit(); 
     } 
     if(Gdx.input.isKeyPressed(Keys.F10)) { 
      this.saveScreenshot(new FileHandle(new File("screenshots/screenShot001.png"))); 
     } 
    } 

    public void saveScreenshot(FileHandle file) { 
     Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); 

     PixmapIO.writePNG(file, pixmap); 
     pixmap.dispose(); 
    } 

    public Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY) { 
     Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1); 

     final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888); 
     ByteBuffer pixels = pixmap.getPixels(); 
     Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels); 

     final int numBytes = w * h * 4; 
     byte[] lines = new byte[numBytes]; 
     if (flipY) { 
      final int numBytesPerLine = w * 4; 
      for (int i = 0; i < h; i++) { 
       pixels.position((h - i - 1) * numBytesPerLine); 
       pixels.get(lines, i * numBytesPerLine, numBytesPerLine); 
      } 
      pixels.clear(); 
      pixels.put(lines); 
     } else { 
      pixels.clear(); 
      pixels.get(lines); 
     } 

     return pixmap; 
    } 

的文件被创建,它似乎是一个正确的尺寸正确的PNG图像,但它是一个空白的。该应用程序是setup-ui制作的示例,并显示libGDX徽标。任何想法的问题?

+0

你还在某处呈现标志吗? (), – noone

+0

是的,我在我的渲染方法渲染标志@Override \t public void render(){ \t \t Gdx.gl.gl.glClearColor(1,1,1,1); \t \t Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); \t \t controller.update(Gdx.graphics.getDeltaTime()); \t \t batch.setProjectionMatrix(camera.combined); \t \t batch.begin(); \t \t sprite.draw(batch);batch.end(); \t} – Killrazor

回答

3

从您的评论摘自:

@Override public void render() { 
    Gdx.gl.glClearColor(1, 1, 1, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    controller.update(Gdx.graphics.getDeltaTime()); 
    batch.setProjectionMatrix(camera.combined); 
    batch.begin(); 
    sprite.draw(batch); 
    batch.end(); 
} 

的问题是,您清除颜色,然后检查输入(并进行截图),然后渲染标志。

移动controller.update(Gdx.graphics.getDeltaTime());render方法结束,后您呈现的标志(batch.end())。

+0

是的!这解决了问题:)感谢您的帮助! – Killrazor