public static void takeScreenshot() { byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true); Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888); BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH-mm-ss"); PixmapIO.writePNG( Gdx.files.external(dateFormat.format(new Date()) + ".png"), pixmap); pixmap.dispose(); }
/** * Get the current scissor rectangle */ public Rect getScissorRect() { if(!Engine.instance().isGLThread()) { // 不能在非gl线程中访问 CCLog.error(TAG, "GLThread error"); return null; } FloatBuffer fb = BufferUtils.newFloatBuffer(16); //4 * 4 Gdx.gl.glGetFloatv(GL20.GL_SCISSOR_BOX, fb); float[] params = new float[4]; fb.get(params); float x = (params[0] - _viewPortRect.x) / _scaleX; float y = (params[1] - _viewPortRect.y) / _scaleY; float w = params[2] / _scaleX; float h = params[3] / _scaleY; return (Rect) poolRect.set(x, y, w, h); }
/** * @return Whether the device supports anisotropic filtering. */ public static boolean isSupported () { GL20 gl = Gdx.gl; if (gl != null) { if (Gdx.graphics.supportsExtension("GL_EXT_texture_filter_anisotropic")) { anisotropySupported = true; FloatBuffer buffer = BufferUtils.newFloatBuffer(16); buffer.position(0); buffer.limit(buffer.capacity()); Gdx.gl20.glGetFloatv(GL20.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, buffer); maxAnisotropySupported = buffer.get(0); } checkComplete = true; } return anisotropySupported; }
/** * * @param image * @return * @see http://stackoverflow.com/a/10872080 */ private ByteBuffer loadTexture(PNGDecoder image) { int bytesPerComponent = chooseBitsPerComponent() / 8; int bytesPerPixel = chooseComponentCount() * bytesPerComponent; int imageBufSize = image.getWidth() * image.getHeight() * bytesPerPixel; ByteBuffer buffer = BufferUtils.newByteBuffer(imageBufSize); Format format = pngDecodingFormatFromComponentCount(); try { image.decode(buffer, image.getWidth() * bytesPerPixel, format); } catch (IOException e) { throw new ResourceException(e); } buffer.flip(); // FOR THE LOVE OF GOD DO NOT FORGET THIS return buffer; }
public ByteBuffer getBuffer () { if (getRows() == 0) // Issue #768 - CheckJNI frowns upon env->NewDirectByteBuffer with NULL buffer or capacity 0 // "JNI WARNING: invalid values for address (0x0) or capacity (0)" // FreeType sets FT_Bitmap::buffer to NULL when the bitmap is empty (e.g. for ' ') // JNICheck is on by default on emulators and might have a point anyway... // So let's avoid this and just return a dummy non-null non-zero buffer return BufferUtils.newByteBuffer(1); int offset = getBufferAddress(address); int length = getBufferSize(address); Int8Array as = getBuffer(address, offset, length); ArrayBuffer aBuf = as.buffer(); ByteBuffer buf = FreeTypeUtil.newDirectReadWriteByteBuffer(aBuf, length, offset); return buf; }
public Pixmap getFrameBufferPixmap(Viewport viewport) { int w = viewport.getScreenWidth(); int h = viewport.getScreenHeight(); int x = viewport.getScreenX(); int y = viewport.getScreenY(); final ByteBuffer pixelBuffer = BufferUtils.newByteBuffer(w * h * 4); Gdx.gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, fbo.getFramebufferHandle()); Gdx.gl.glReadPixels(x, y, w, h, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE, pixelBuffer); Gdx.gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, 0); final int numBytes = w * h * 4; byte[] imgLines = new byte[numBytes]; final int numBytesPerLine = w * 4; for (int i = 0; i < h; i++) { pixelBuffer.position((h - i - 1) * numBytesPerLine); pixelBuffer.get(imgLines, i * numBytesPerLine, numBytesPerLine); } Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888); BufferUtils.copy(imgLines, 0, pixmap.getPixels(), imgLines.length); return pixmap; }
/** * Copies the contents of {@code src} to {@code dst}. * * @throws IllegalArgumentException If the pixmaps are different sizes or different formats. */ public static void copy(Pixmap src, Pixmap dst) { Format srcFmt = src.getFormat(); Format dstFmt = dst.getFormat(); Checks.checkArgument(srcFmt == dstFmt, "Formats not equal: src=" + srcFmt + ", dst=" + dstFmt); int srcW = src.getWidth(); int dstW = dst.getWidth(); Checks.checkArgument(srcW == dstW, "Widths not equal: src.w=" + srcW + ", dst.w=" + dstW); int srcH = src.getHeight(); int dstH = src.getHeight(); Checks.checkArgument(srcH == dstH, "Heights not equal: src.h=" + srcH + ", dst.h=" + dstH); ByteBuffer srcPixels = src.getPixels(); ByteBuffer dstPixels = dst.getPixels(); BufferUtils.copy(srcPixels, dstPixels, srcPixels.limit()); srcPixels.clear(); dstPixels.clear(); }
@Override public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) { if (cls.getName().equals(BufferUtils.class.getName())) { transformBufferUtils(cls, innerSource); } else if (cls.getName().equals(TextureData.Factory.class.getName())) { transformTextureData(cls, innerSource); } else if (cls.getName().equals(FileHandle.class.getName())) { transformFileHandle(cls); } else if (cls.getName().equals(Pixmap.class.getName())) { replaceClass(cls, innerSource.get(PixmapEmulator.class.getName())); } else if (cls.getName().equals(Matrix4.class.getName())) { transformMatrix(cls, innerSource); } else if (cls.getName().equals(VertexArray.class.getName()) || cls.getName().equals(VertexBufferObject.class.getName())) { replaceClass(cls, innerSource.get(VertexArrayEmulator.class.getName())); } else if (cls.getName().equals(IndexArray.class.getName()) || cls.getName().equals(IndexBufferObject.class.getName())) { replaceClass(cls, innerSource.get(IndexArrayEmulator.class.getName())); } }
/** Constructs a new ShaderProgram and immediately compiles it. * * @param vertexShader the vertex shader * @param fragmentShader the fragment shader */ public ShaderProgram (String vertexShader, String fragmentShader) { if (vertexShader == null) throw new IllegalArgumentException("vertex shader must not be null"); if (fragmentShader == null) throw new IllegalArgumentException("fragment shader must not be null"); this.vertexShaderSource = vertexShader; this.fragmentShaderSource = fragmentShader; this.matrix = BufferUtils.newFloatBuffer(16); compileShaders(vertexShader, fragmentShader); if (isCompiled()) { fetchAttributes(); fetchUniforms(); addManagedShader(Gdx.app, this); } }
private int loadShader (int type, String source) { GL20 gl = Gdx.gl20; IntBuffer intbuf = BufferUtils.newIntBuffer(1); int shader = gl.glCreateShader(type); if (shader == 0) return -1; gl.glShaderSource(shader, source); gl.glCompileShader(shader); gl.glGetShaderiv(shader, GL20.GL_COMPILE_STATUS, intbuf); int compiled = intbuf.get(0); if (compiled == 0) { // gl.glGetShaderiv(shader, GL20.GL_INFO_LOG_LENGTH, intbuf); // int infoLogLength = intbuf.get(0); // if (infoLogLength > 1) { String infoLog = gl.glGetShaderInfoLog(shader); log += infoLog; // } return -1; } return shader; }
@Override public void setVertices (float[] vertices, int offset, int count) { isDirty = true; if (isDirect) { BufferUtils.copy(vertices, byteBuffer, count, offset); buffer.position(0); buffer.limit(count); } else { buffer.clear(); buffer.put(vertices, offset, count); buffer.flip(); byteBuffer.position(0); byteBuffer.limit(buffer.limit() << 2); } bufferChanged(); }
/** Releases all resources associated with the FrameBuffer. */ public void dispose () { GL20 gl = Gdx.gl20; IntBuffer handle = BufferUtils.newIntBuffer(1); colorTexture.dispose(); if (hasDepth) gl.glDeleteRenderbuffer(depthbufferHandle); if (hasStencil) gl.glDeleteRenderbuffer(stencilbufferHandle); gl.glDeleteFramebuffer(framebufferHandle); if (buffers.get(Gdx.app) != null) buffers.get(Gdx.app).removeValue(this, true); }
public ETC1Data (FileHandle pkmFile) { byte[] buffer = new byte[1024 * 10]; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new GZIPInputStream(pkmFile.read()))); int fileSize = in.readInt(); compressedData = BufferUtils.newUnsafeByteBuffer(fileSize); int readBytes = 0; while ((readBytes = in.read(buffer)) != -1) { compressedData.put(buffer, 0, readBytes); } compressedData.position(0); compressedData.limit(compressedData.capacity()); } catch (Exception e) { throw new GdxRuntimeException("Couldn't load pkm file '" + pkmFile + "'", e); } finally { StreamUtils.closeQuietly(in); } width = getWidthPKM(compressedData, 0); height = getHeightPKM(compressedData, 0); dataOffset = PKM_HEADER_SIZE; compressedData.position(dataOffset); checkNPOT(); }
private void create (int width, int height, Format format2) { this.width = width; this.height = height; this.format = Format.RGBA8888; canvas = Canvas.createIfSupported(); canvas.getCanvasElement().setWidth(width); canvas.getCanvasElement().setHeight(height); context = canvas.getContext2d(); context.setGlobalCompositeOperation(getComposite()); buffer = BufferUtils.newIntBuffer(1); id = nextId++; buffer.put(0, id); pixmaps.put(id, this); }
public void init(Bundle owner, int x, int y, int z) { this.owner = owner; this.x = x; this.y = y; this.z = z; transform.idt(); transform.translate(x * Config.GRID_SIZE, y * Config.GRID_SIZE, z * Config.GRID_SIZE); inverseTransform.set(transform); inverseTransform.inv(); transformBuffer.rewind(); transformBuffer.put(transform.val); transformBuffer.rewind(); inverseTransformBuffer.rewind(); inverseTransformBuffer.put(inverseTransform.val); inverseTransformBuffer.rewind(); this.matrix.clear(); BufferUtils.copy(transform.val, this.matrix, transform.val.length, 0); }
private void updateConstantBuffer() { baseModelViewMatrix.set(viewMatrix).translate(0.0f, 0.0f, 5f).rotate(1, 1, 1, rotation); ByteBuffer constant_buffer = dynamicConstantBuffer[constantDataBufferIndex].getContents(); constant_buffer.order(ByteOrder.nativeOrder()); for (int i = 0; i < kNumberOfBoxes; i++) { int multiplier = ((i % 2) == 0?1: -1); modelViewMatrix.set(baseModelViewMatrix).translate(0, 0, multiplier * 1.5f).rotate(1, 1, 1, rotation); normalMatrix.set(modelViewMatrix).tra().inv(); constant_buffer.position((sizeOfConstantT *i) + normalMatrixOffset); BufferUtils.copy(normalMatrix.getValues(), 0, 16, constant_buffer); combinedMatrix.set(projectionMatrix).mul(modelViewMatrix); constant_buffer.position((sizeOfConstantT * i) + modelViewProjectionMatrixOffset); BufferUtils.copy(combinedMatrix.getValues(), 0, 16, constant_buffer); } rotation += 0.01; }
public ShaderProgram(String paramString1, String paramString2) { if (paramString1 == null) throw new IllegalArgumentException("vertex shader must not be null"); if (paramString2 == null) throw new IllegalArgumentException("fragment shader must not be null"); this.vertexShaderSource = paramString1; this.fragmentShaderSource = paramString2; this.matrix = BufferUtils.newFloatBuffer(16); compileShaders(paramString1, paramString2); if (isCompiled()) { fetchAttributes(); fetchUniforms(); addManagedShader(Gdx.app, this); } }
private int loadShader(int paramInt, String paramString) { GL20 localGL20 = Gdx.graphics.getGL20(); IntBuffer localIntBuffer = BufferUtils.newIntBuffer(1); int i = localGL20.glCreateShader(paramInt); if (i == 0) return -1; localGL20.glShaderSource(i, paramString); localGL20.glCompileShader(i); localGL20.glGetShaderiv(i, 35713, localIntBuffer); if (localIntBuffer.get(0) == 0) { String str = localGL20.glGetShaderInfoLog(i); this.log += str; return -1; } return i; }
public VertexBufferObjectSubData(boolean paramBoolean, int paramInt, VertexAttribute[] paramArrayOfVertexAttribute) { this.isStatic = paramBoolean; this.attributes = new VertexAttributes(paramArrayOfVertexAttribute); this.byteBuffer = BufferUtils.newByteBuffer(paramInt * this.attributes.vertexSize); this.isDirect = true; if (paramBoolean); for (int i = 35044; ; i = 35048) { this.usage = i; this.buffer = this.byteBuffer.asFloatBuffer(); this.bufferHandle = createBufferObject(); this.buffer.flip(); this.byteBuffer.flip(); return; } }
public void dispose() { GL20 localGL20 = Gdx.graphics.getGL20(); IntBuffer localIntBuffer = BufferUtils.newIntBuffer(1); this.colorTexture.dispose(); if (this.hasDepth) { localIntBuffer.put(this.depthbufferHandle); localIntBuffer.flip(); localGL20.glDeleteRenderbuffers(1, localIntBuffer); } localIntBuffer.clear(); localIntBuffer.put(this.framebufferHandle); localIntBuffer.flip(); localGL20.glDeleteFramebuffers(1, localIntBuffer); if (buffers.get(Gdx.app) != null) ((List)buffers.get(Gdx.app)).remove(this); }
public VertexBufferObject(boolean paramBoolean, int paramInt, VertexAttributes paramVertexAttributes) { this.isStatic = paramBoolean; this.attributes = paramVertexAttributes; this.attributeIndexCache = new int[paramVertexAttributes.size()]; this.byteBuffer = BufferUtils.newUnsafeByteBuffer(paramInt * this.attributes.vertexSize); this.buffer = this.byteBuffer.asFloatBuffer(); this.buffer.flip(); this.byteBuffer.flip(); if (paramBoolean); for (int i = 35044; ; i = 35048) { this.usage = i; this.bufferHandle = createBufferObject(); return; } }
public void dispose() { if (Gdx.gl20 != null) { tmpHandle.clear(); tmpHandle.put(this.bufferHandle); tmpHandle.flip(); GL20 localGL20 = Gdx.gl20; localGL20.glBindBuffer(34962, 0); localGL20.glDeleteBuffers(1, tmpHandle); } for (this.bufferHandle = 0; ; this.bufferHandle = 0) { BufferUtils.disposeUnsafeByteBuffer(this.byteBuffer); return; tmpHandle.clear(); tmpHandle.put(this.bufferHandle); tmpHandle.flip(); GL11 localGL11 = Gdx.gl11; localGL11.glBindBuffer(34962, 0); localGL11.glDeleteBuffers(1, tmpHandle); } }
/** * Takes a screenshot of the current game state and saves it in the assets directory */ public void takeScreenshot() { byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true); Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888); BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); PixmapIO.writePNG(Gdx.files.local("screenshots/screenshot.png"), pixmap); pixmap.dispose(); }
public void link() { Gdx.gl.glLinkProgram( handle ); IntBuffer status = BufferUtils.newIntBuffer(1); Gdx.gl.glGetProgramiv( handle, GL20.GL_LINK_STATUS, status ); if (status.get() == GL20.GL_FALSE) { throw new Error( Gdx.gl.glGetProgramInfoLog( handle ) ); } }
public void compile() { Gdx.gl.glCompileShader( handle ); IntBuffer status = BufferUtils.newIntBuffer(1); Gdx.gl.glGetShaderiv( handle, GL20.GL_COMPILE_STATUS, status); if (status.get() == GL20.GL_FALSE) { throw new Error( Gdx.gl.glGetShaderInfoLog( handle ) ); } }
public static void takeScreenShot(final String filePath) { byte[] pixels = ScreenUtils.getFrameBufferPixels( 0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true ); Pixmap pixmap = new Pixmap( Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888 ); BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); PixmapIO.writePNG(Gdx.files.external(filePath), pixmap); pixmap.dispose(); }
public boolean loadFile(final com.badlogic.gdx.files.FileHandle fileHandle) { final int len = (int) fileHandle.length(); if (len <= 0) throw new com.badlogic.gdx.utils.GdxRuntimeException("Incorrect file specified"); java.nio.ByteBuffer buff = BufferUtils.newByteBuffer(len); buff.put(fileHandle.readBytes()); buff.position(0); boolean result = loadFileFromMemory(buff, len); // com.badlogic.gdx.utils.BufferUtils.disposeUnsafeByteBuffer(buff); return result; }
public void takeScreenshot() { byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true); Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888); BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); PixmapIO.writePNG(Gdx.files.external(UUID.randomUUID().toString() + ".png"), pixmap); pixmap.dispose(); }
protected int loadShader(int type, String source) throws Exception { IntBuffer intbuf = BufferUtils.newIntBuffer(1); int shader = Gdx.gl.glCreateShader(type); if(shader == 0) return -1; Gdx.gl.glShaderSource(shader, source); Gdx.gl.glCompileShader(shader); Gdx.gl.glGetShaderiv(shader, GL20.GL_COMPILE_STATUS, intbuf); int compiled = intbuf.get(0); if(compiled == 0) { $setValue_log($getValue__log() + Gdx.gl.glGetShaderInfoLog(shader)); return -1; } return shader; }
/** Creates a new generator from the given font file. Uses {@link FileHandle#length()} to determine the file size. If the file * length could not be determined (it was 0), an extra copy of the font bytes is performed. Throws a * {@link GdxRuntimeException} if loading did not succeed. */ public FreeTypeFontGenerator (FileHandle fontFile) { name = fontFile.pathWithoutExtension(); int fileSize = (int)fontFile.length(); library = FreeType.initFreeType(); if (library == null) throw new GdxRuntimeException("Couldn't initialize FreeType"); ByteBuffer buffer; InputStream input = fontFile.read(); try { if (fileSize == 0) { // Copy to a byte[] to get the file size, then copy to the buffer. byte[] data = StreamUtils.copyStreamToByteArray(input, fileSize > 0 ? (int)(fileSize * 1.5f) : 1024 * 16); buffer = BufferUtils.newByteBuffer(data.length); BufferUtils.copy(data, 0, buffer, data.length); } else { // Trust the specified file size. buffer = BufferUtils.newByteBuffer(fileSize); StreamUtils.copyStream(input, buffer); } } catch (IOException ex) { throw new GdxRuntimeException(ex); } finally { StreamUtils.closeQuietly(input); } face = library.newMemoryFace(buffer, 0); if (face == null) throw new GdxRuntimeException("Couldn't create face for font: " + fontFile); if (checkForBitmapFont()) return; setPixelSizes(0, 15); }
public void link() { Gdx.gl.glLinkProgram(handle); IntBuffer status = BufferUtils.newIntBuffer(1); Gdx.gl.glGetProgramiv(handle, GL20.GL_LINK_STATUS, status); if (status.get() == GL20.GL_FALSE) { throw new Error(Gdx.gl.glGetProgramInfoLog(handle)); } }
public void compile() { Gdx.gl.glCompileShader(handle); IntBuffer status = BufferUtils.newIntBuffer(1); Gdx.gl.glGetShaderiv(handle, GL20.GL_COMPILE_STATUS, status); if (status.get() == GL20.GL_FALSE) { throw new Error(Gdx.gl.glGetShaderInfoLog(handle)); } }
@Override public void setVertices(float[] vertices, int offset, int count) { isDirty = true; BufferUtils.copy(vertices, buffer, count, offset); buffer.position(0); buffer.limit(count); bufferChanged(); }
@Override public void updateVertices(int targetOffset, float[] vertices, int sourceOffset, int count) { isDirty = true; final int pos = buffer.position(); buffer.position(targetOffset); BufferUtils.copy(vertices, sourceOffset, count, buffer); buffer.position(pos); bufferChanged(); }
/** * Creates a new IndexBufferObject to be used with vertex arrays. * * @param maxIndices * the maximum number of indices this buffer can hold */ public IndexArrayEmulator(int maxIndices) { this.isDirect = true; buffer = BufferUtils.newShortBuffer(maxIndices); buffer.flip(); bufferHandle = Gdx.gl20.glGenBuffer(); usage = GL20.GL_STATIC_DRAW; }