OSDN Git Service

Bugfix for Framebuffer on iOS: frame buffer handle is not necessarily 0 on iOS!
authorChristoph Aschwanden <contact@noblemaster.com>
Fri, 28 Sep 2012 18:00:34 +0000 (03:00 +0900)
committerChristoph Aschwanden <contact@noblemaster.com>
Fri, 28 Sep 2012 18:00:34 +0000 (03:00 +0900)
gdx/src/com/badlogic/gdx/graphics/glutils/FrameBuffer.java

index 2229ddf..7c277fb 100644 (file)
@@ -16,6 +16,8 @@
 \r
 package com.badlogic.gdx.graphics.glutils;\r
 \r
+import java.nio.ByteBuffer;\r
+import java.nio.ByteOrder;\r
 import java.nio.IntBuffer;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
@@ -24,6 +26,7 @@ import java.util.Map;
 \r
 import com.badlogic.gdx.Application;\r
 import com.badlogic.gdx.Gdx;\r
+import com.badlogic.gdx.Application.ApplicationType;\r
 import com.badlogic.gdx.graphics.GL20;\r
 import com.badlogic.gdx.graphics.Pixmap;\r
 import com.badlogic.gdx.graphics.Texture;\r
@@ -56,6 +59,11 @@ public class FrameBuffer implements Disposable {
        /** the color buffer texture **/\r
        private Texture colorTexture;\r
 \r
+       /** the default framebuffer handle, a.k.a screen. */\r
+       private static int defaultFramebufferHandle;\r
+       /** true if we have polled for the default handle already. */\r
+       private static boolean defaultFramebufferHandleInitialized = false;\r
+       \r
        /** the framebuffer handle **/\r
        private int framebufferHandle;\r
 \r
@@ -94,10 +102,24 @@ public class FrameBuffer implements Disposable {
        private void build () {\r
                if (!Gdx.graphics.isGL20Available()) throw new GdxRuntimeException("GL2 is required.");\r
 \r
+               GL20 gl = Gdx.graphics.getGL20();\r
+\r
+               // iOS uses a different framebuffer handle! (not necessarily 0)\r
+               if (!defaultFramebufferHandleInitialized) {\r
+                       defaultFramebufferHandleInitialized = true;\r
+                  if (Gdx.app.getType() == ApplicationType.iOS) {\r
+                    IntBuffer intbuf = ByteBuffer.allocateDirect(16 * Integer.SIZE / 8).order(ByteOrder.nativeOrder()).asIntBuffer();\r
+                    gl.glGetIntegerv(GL20.GL_FRAMEBUFFER_BINDING, intbuf);\r
+                    defaultFramebufferHandle = intbuf.get();\r
+                  }\r
+                  else {\r
+                    defaultFramebufferHandle = 0;\r
+                  }\r
+               }\r
+               \r
                colorTexture = new Texture(width, height, format);\r
                colorTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);\r
                colorTexture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);\r
-               GL20 gl = Gdx.graphics.getGL20();\r
 \r
                IntBuffer handle = BufferUtils.newIntBuffer(1);\r
                gl.glGenFramebuffers(1, handle);\r
@@ -127,7 +149,7 @@ public class FrameBuffer implements Disposable {
 \r
                gl.glBindRenderbuffer(GL20.GL_RENDERBUFFER, 0);\r
                gl.glBindTexture(GL20.GL_TEXTURE_2D, 0);\r
-               gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, 0);\r
+               gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, defaultFramebufferHandle);\r
 \r
                if (result != GL20.GL_FRAMEBUFFER_COMPLETE) {\r
                        colorTexture.dispose();\r
@@ -183,7 +205,7 @@ public class FrameBuffer implements Disposable {
        /** Unbinds the framebuffer, all drawing will be performed to the normal framebuffer from here on. */\r
        public void end () {\r
                Gdx.graphics.getGL20().glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());\r
-               Gdx.graphics.getGL20().glBindFramebuffer(GL20.GL_FRAMEBUFFER, 0);\r
+               Gdx.graphics.getGL20().glBindFramebuffer(GL20.GL_FRAMEBUFFER, defaultFramebufferHandle);\r
        }\r
 \r
        private void addManagedFrameBuffer (Application app, FrameBuffer frameBuffer) {\r