Example usage for com.badlogic.gdx.graphics GL10 GL_PACK_ALIGNMENT

List of usage examples for com.badlogic.gdx.graphics GL10 GL_PACK_ALIGNMENT

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics GL10 GL_PACK_ALIGNMENT.

Prototype

int GL_PACK_ALIGNMENT

To view the source code for com.badlogic.gdx.graphics GL10 GL_PACK_ALIGNMENT.

Click Source Link

Usage

From source file:com.ridiculousRPG.GameBase.java

License:Apache License

/**
 * Takes a screenshot from the current frame buffer. The screenshot will be
 * stretched to fit into the Rectangle specified by dstW and dstH
 * //from w w  w.ja va2 s.  com
 * @param srcX
 * @param srcY
 * @param srcW
 * @param srcH
 * @param dstW
 * @param dstH
 * @return A Pixmap containing the screenshot. The screenshot will be
 *         flipped at the y-axis.
 * @throws IOException
 */
public Pixmap takeScreenshot(int srcX, int srcY, int srcW, int srcH, int dstW, int dstH) throws IOException {
    Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
    Pixmap pixmap = new Pixmap(srcW, srcH, Format.RGBA8888);
    Gdx.gl.glReadPixels(srcX, srcY, srcW, srcH, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixmap.getPixels());

    // scale the picture
    if (srcW != dstW || srcH != dstH) {
        Pixmap scale = new Pixmap(dstW, dstH, Format.RGBA8888);
        Blending old = Pixmap.getBlending();
        Pixmap.setBlending(Blending.None);
        scale.drawPixmap(pixmap, 0, 0, srcW, srcH, 0, 0, dstW, dstH);
        Pixmap.setBlending(old);
        pixmap.dispose();
        pixmap = scale;
    }

    return pixmap;
}

From source file:org.ege.utils.ScreenUtils.java

License:Apache License

/** Returns a portion of the default framebuffer contents specified by x, y, width and height as a {@link TextureRegion} with
 * the same dimensions. The base {@link Texture} always has {@link MathUtils#nextPowerOfTwo} dimensions and RGBA8888
 * {@link Format}. It can be accessed via {@link TextureRegion#getTexture}. This texture is not managed and has to be reloaded
 * manually on a context loss. If the width and height specified are larger than the framebuffer dimensions, the Texture will
 * be padded accordingly. Pixels that fall outside of the current screen will have RGBA values of 0.
 * /*from  w ww.  j a  va 2  s .com*/
 * @param x the x position of the framebuffer contents to capture
 * @param y the y position of the framebuffer contents to capture
 * @param w the width of the framebuffer contents to capture
 * @param h the height of the framebuffer contents to capture */
public static TextureRegion getFrameBufferTexture(int x, int y, int w, int h) {
    Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
    final int potW = MathUtils.nextPowerOfTwo(w);
    final int potH = MathUtils.nextPowerOfTwo(h);

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

    Texture texture = new Texture(pixmap);
    TextureRegion textureRegion = new TextureRegion(texture, 0, h, w, -h);
    pixmap.dispose();

    return textureRegion;
}

From source file:org.ege.utils.ScreenUtils.java

License:Apache License

/** Returns a portion of the default framebuffer contents specified by x, y, width and height, as a byte[] array with a length
 * equal to the specified width * height * 4. The byte[] will always contain RGBA8888 data. If the width and height specified
 * are larger than the framebuffer dimensions, the Texture will be padded accordingly. Pixels that fall outside of the current
 * screen will have RGBA values of 0. Because of differences in screen and image origins the framebuffer contents should be
 * flipped along the Y axis if you intend save them to disk as a bitmap. Flipping is not cheap operation, so use this
 * functionality wisely.// w  w  w .  j  av  a 2s  .c o  m
 * 
 * @param flipY whether to flip pixels along Y axis */
public static byte[] getFrameBufferPixels(int x, int y, int w, int h, boolean flipY) {
    Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
    final ByteBuffer pixels = BufferUtils.newByteBuffer(w * h * 4);
    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);
        }
    } else {
        pixels.clear();
        pixels.get(lines);
    }
    return lines;

}

From source file:org.ege.utils.ScreenUtils.java

License:Apache License

/** Returns a portion of the default framebuffer contents specified by x, y, width and height, as a byte[] array with a length
 * equal to the specified width * height * 4. The byte[] will always contain RGBA8888 data. If the width and height specified
 * are larger than the framebuffer dimensions, the Texture will be padded accordingly. Pixels that fall outside of the current
 * screen will have RGBA values of 0. Because of differences in screen and image origins the framebuffer contents should be
 * flipped along the Y axis if you intend save them to disk as a bitmap. Flipping is not cheap operation, so use this
 * functionality wisely.//from  w  w w. java  2 s .  c  o  m
 * 
 * @param flipY whether to flip pixels along Y axis */
public static byte[] getFrameBufferPixels(byte[] lines, int x, int y, int w, int h, boolean flipY) {
    final int numBytes = w * h * 4;

    if (lines.length != numBytes)
        throw new EasyGEngineRuntimeException("Your byte array must have the lenght: w*h*4");

    Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
    final ByteBuffer pixels = BufferUtils.newByteBuffer(numBytes);
    Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);

    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);
        }
    } else {
        pixels.clear();
        pixels.get(lines);
    }
    return lines;
}

From source file:org.ege.utils.ScreenUtils.java

License:Apache License

/** Returns a portion of the default framebuffer contents specified by x, y, width and height, as a byte[] array with a length
 * equal to the specified width * height * 4. The byte[] will always contain RGBA8888 data. If the width and height specified
 * are larger than the framebuffer dimensions, the Texture will be padded accordingly. Pixels that fall outside of the current
 * screen will have RGBA values of 0. Because of differences in screen and image origins the framebuffer contents should be
 * flipped along the Y axis if you intend save them to disk as a bitmap. Flipping is not cheap operation, so use this
 * functionality wisely.//from   w  ww .ja va  2s  . c  om
 * 
 * @param flipY whether to flip pixels along Y axis */
public static byte[] getFrameBufferPixels(final ByteBuffer pixels, byte[] lines, int x, int y, int w, int h,
        boolean flipY) {
    final int numBytes = w * h * 4;

    if (lines.length != numBytes || pixels.limit() != numBytes)
        throw new EasyGEngineRuntimeException("Your byte array and byte buffer must have the lenght: w*h*4");

    final int limit = pixels.limit();
    for (int i = 0; i < limit; i++)
        pixels.put(i, (byte) 0);
    pixels.rewind();

    Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
    Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);

    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);
        }
    } else {
        pixels.clear();
        pixels.get(lines);
    }
    return lines;
}