From: badlogic Date: Sun, 6 Oct 2013 14:35:32 +0000 (+0200) Subject: added new method to ScreenUtils to get a pixmap, not the fastest X-Git-Url: http://git.osdn.net/view?p=mikumikustudio%2Flibgdx-mikumikustudio.git;a=commitdiff_plain;h=1f5d9e98f65e9b91656bf1f0010f61de76b2eb31 added new method to ScreenUtils to get a pixmap, not the fastest --- diff --git a/gdx/src/com/badlogic/gdx/utils/ScreenUtils.java b/gdx/src/com/badlogic/gdx/utils/ScreenUtils.java index 2c09035a5..fce422855 100644 --- a/gdx/src/com/badlogic/gdx/utils/ScreenUtils.java +++ b/gdx/src/com/badlogic/gdx/utils/ScreenUtils.java @@ -67,6 +67,18 @@ public class ScreenUtils { return textureRegion; } + + public static Pixmap getFrameBufferPixmap(int x, int y, int w, int h) { + Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1); + + final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888); + ByteBuffer pixels = pixmap.getPixels(); + byte[] bytes = getFrameBufferPixels(true); + pixels.put(bytes); + pixels.flip(); + + return pixmap; + } /** Returns the default framebuffer contents as a byte[] array with a length equal to screen width * height * 4. The byte[] will * always contain RGBA8888 data. Because of differences in screen and image origins the framebuffer contents should be flipped diff --git a/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/Test2.java b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/Test2.java new file mode 100644 index 000000000..f03bebbb5 --- /dev/null +++ b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/Test2.java @@ -0,0 +1,58 @@ + +package com.badlogic.gdx.tests.lwjgl; + +import com.badlogic.gdx.ApplicationAdapter; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.backends.lwjgl.LwjglApplication; +import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.GL10; +import com.badlogic.gdx.graphics.Pixmap; +import com.badlogic.gdx.graphics.Pixmap.Format; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.utils.ScreenUtils; + +public class Test2 extends ApplicationAdapter { + SpriteBatch batch; + TextureRegion region; + Pixmap screenCap; + Texture screenCapTex; + + public void create () { + int size =1; + + batch = new SpriteBatch(); + Pixmap p = new Pixmap(64, 64, Format.RGBA8888); + p.setColor(Color.RED); + p.fillRectangle(32, 32, size, size); + region = new TextureRegion(new Texture(p), 32, 32, size, size); + + Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + Gdx.gl.glClearColor(0, 0, 0, 1); + batch.begin(); + batch.draw(region, 1, 1, 256, 256); + batch.end(); + screenCap = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + screenCapTex = new Texture(screenCap); + System.out.println(screenCap.getPixel(0, 0)); + System.out.println(screenCap.getPixel(1, 1)); + System.out.println(screenCap.getPixel(256, 256)); + System.out.println(screenCap.getPixel(257, 257)); + } + + public void render () { + Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + Gdx.gl.glClearColor(0, 0, 0, 1); + batch.begin(); + batch.draw(screenCapTex, 0, 0); + batch.end(); + } + + public static void main (String[] args) { + LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); + config.useGL20 = true; + new LwjglApplication(new Test2(), config); + } +}