OSDN Git Service

[added] Ability to disable vsync with LWJGL.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 28 Mar 2011 07:33:32 +0000 (07:33 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 28 Mar 2011 07:33:32 +0000 (07:33 +0000)
[fixed] Non-looping music (OpenAL).
[fixed] WAV file reading (OpenAL).
[fixed] Default ant target.
[fixed] Moved setEnforcePotImages to Texture, so it can actually be disabled.

backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglApplication.java
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglGraphics.java
backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java
backends/gdx-openal/src/com/badlogic/gdx/backends/openal/Wav.java
build.xml
gdx/src/com/badlogic/gdx/graphics/Texture.java

index 715f56a..32d0f3f 100644 (file)
@@ -128,7 +128,7 @@ public class LwjglApplication implements Application {
                        listener.render();\r
                        audio.update();\r
                        Display.update();\r
-                       Display.sync(60);\r
+                       if (graphics.vsync) Display.sync(60);\r
                }\r
 \r
                listener.pause();\r
index cbaef2d..90c85b9 100644 (file)
@@ -158,7 +158,7 @@ public class LwjglCanvas implements Application {
                                listener.render();\r
                                audio.update();\r
                                Display.update();\r
-                               Display.sync(60);\r
+                               if (graphics.vsync) Display.sync(60);\r
                        }\r
                };\r
 \r
index 5f95d1f..dec0bd1 100644 (file)
@@ -55,7 +55,7 @@ public final class LwjglGraphics implements Graphics {
        int height;\r
        String title;\r
        Canvas canvas;\r
-       boolean enforcePotImages = true;\r
+       boolean vsync = true;\r
 \r
        LwjglGraphics (String title, int width, int height, boolean useGL2IfAvailable) {\r
                useGL2 = useGL2IfAvailable;\r
@@ -212,7 +212,7 @@ public final class LwjglGraphics implements Graphics {
                return (Toolkit.getDefaultToolkit().getScreenResolution() / 2.54f);\r
        }\r
 \r
-       public void setEnforcePotImages (boolean enforcePotImages) {\r
-               this.enforcePotImages = enforcePotImages;\r
+       public void setVsync (boolean vsync) {\r
+               this.vsync = vsync;\r
        }\r
 }\r
index d4ecf9e..687f737 100644 (file)
@@ -68,9 +68,11 @@ public abstract class OpenALMusic implements Music {
                        if (sourceID == -1) return;\r
                        alSourcei(sourceID, AL_LOOPING, AL_FALSE);\r
                        alSourcef(sourceID, AL_GAIN, volume);\r
-                       for (int i = 0; i < bufferCount; i++)\r
-                               fill(buffers.get(i));\r
-                       alSourceQueueBuffers(sourceID, buffers);\r
+                       for (int i = 0; i < bufferCount; i++) {\r
+                               int bufferID = buffers.get(i);\r
+                               if (!fill(bufferID)) break;\r
+                               alSourceQueueBuffers(sourceID, bufferID);\r
+                       }\r
                        if (alGetError() != AL_NO_ERROR) {\r
                                stop();\r
                                return;\r
@@ -131,17 +133,22 @@ public abstract class OpenALMusic implements Music {
        public void update () {\r
                if (sourceID == -1) return;\r
 \r
-               // A buffer underflow will cause the source to stop.\r
-               if (isPlaying && alGetSourcei(sourceID, AL_SOURCE_STATE) != AL_PLAYING) alSourcePlay(sourceID);\r
-\r
+               boolean end = false;\r
                int buffers = alGetSourcei(sourceID, AL_BUFFERS_PROCESSED);\r
                while (buffers-- > 0) {\r
                        int bufferID = alSourceUnqueueBuffers(sourceID);\r
                        if (bufferID == AL_INVALID_VALUE) break;\r
                        renderedSeconds += secondsPerBuffer;\r
-                       if (!fill(bufferID)) return;\r
-                       alSourceQueueBuffers(sourceID, bufferID);\r
+                       if (end) continue;\r
+                       if (fill(bufferID))\r
+                               alSourceQueueBuffers(sourceID, bufferID);\r
+                       else\r
+                               end = true;\r
                }\r
+               if (end && alGetSourcei(sourceID, AL_BUFFERS_QUEUED) == 0) stop();\r
+\r
+               // A buffer underflow will cause the source to stop.\r
+               if (isPlaying && alGetSourcei(sourceID, AL_SOURCE_STATE) != AL_PLAYING) alSourcePlay(sourceID);\r
        }\r
 \r
        private boolean fill (int bufferID) {\r
@@ -153,10 +160,8 @@ public abstract class OpenALMusic implements Music {
                                renderedSeconds = 0;\r
                                length = read(tempBytes);\r
                                if (length <= 0) return false;\r
-                       } else {\r
-                               stop();\r
+                       } else\r
                                return false;\r
-                       }\r
                }\r
                tempBuffer.put(tempBytes, 0, length).flip();\r
                alBufferData(bufferID, format, tempBuffer, sampleRate);\r
index b41681e..849c79f 100644 (file)
@@ -22,9 +22,7 @@ public class Wav {
                                setup(input.channels, input.sampleRate);\r
                        }\r
                        try {\r
-                               int read = input.read(buffer);\r
-                               System.out.println(read);\r
-                               return read;\r
+                               return input.readData(buffer);\r
                        } catch (IOException ex) {\r
                                throw new GdxRuntimeException("Error reading WAV file: " + file, ex);\r
                        }\r
@@ -48,11 +46,9 @@ public class Wav {
                        ByteArrayOutputStream output = new ByteArrayOutputStream(4096);\r
                        try {\r
                                byte[] buffer = new byte[2048];\r
-                               while (input.dataLength > 0) {\r
-                                       int length = input.read(buffer);\r
+                               while (true) {\r
+                                       int length = input.readData(buffer);\r
                                        if (length == -1) break;\r
-                                       length = Math.min(length, input.dataLength);\r
-                                       input.dataLength -= length;\r
                                        output.write(buffer, 0, length);\r
                                }\r
                        } catch (IOException ex) {\r
@@ -63,7 +59,7 @@ public class Wav {
        }\r
 \r
        static private class WavInputStream extends FilterInputStream {\r
-               int channels, sampleRate, dataLength;\r
+               int channels, sampleRate, dataRemaining;\r
 \r
                WavInputStream (FileHandle file) {\r
                        super(file.read());\r
@@ -100,7 +96,7 @@ public class Wav {
                                if (read() != 'd' || read() != 'a' || read() != 't' || read() != 'a')\r
                                        throw new GdxRuntimeException("data header not found: " + file);\r
 \r
-                               dataLength = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;\r
+                               dataRemaining = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;\r
                        } catch (Throwable ex) {\r
                                try {\r
                                        close();\r
@@ -109,5 +105,13 @@ public class Wav {
                                throw new GdxRuntimeException("Error reading WAV file: " + file, ex);\r
                        }\r
                }\r
+\r
+               public int readData (byte[] buffer) throws IOException {\r
+                       if (dataRemaining == 0) return -1;\r
+                       int length = Math.min(read(buffer), dataRemaining);\r
+                       if (length == -1) return -1;\r
+                       dataRemaining -= length;\r
+                       return length;\r
+               }\r
        }\r
 }\r
index 3efd141..181fb0e 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -2,7 +2,7 @@
 libgdx distribution ant build file. Performs the compilation of the\r
 gdx and backend modules and creates a distribution out of this mess. \r
 -->\r
-<project name="gdx" default="all" basedir=".">\r
+<project name="gdx" default="package" basedir=".">\r
        <description>libgdx distribution build file</description>\r
 \r
        <!-- property for NDK_HOME, you need to set this to\r
@@ -140,7 +140,7 @@ gdx and backend modules and creates a distribution out of this mess.
        </target>\r
        \r
        <!-- creates jars and places other files in the output dir -->\r
-       <target name="package">\r
+       <target name="package" depends="compile">\r
                <!-- for some reason excludes don't work so we have to delete the ndk directory first... -->\r
                <delete dir="gdx/bin/ndk" />\r
                <jar destfile="${stable}/gdx.jar" basedir="gdx/bin" />\r
index e629230..4d435f3 100644 (file)
@@ -54,6 +54,8 @@ import com.badlogic.gdx.utils.MathUtils;
  * \r
  */\r
 public class Texture implements Disposable {\r
+        static private boolean enforcePotImages = true;\r
+\r
        /**\r
         * Texture filter enum\r
         * \r
@@ -291,7 +293,7 @@ public class Texture implements Disposable {
                \r
                this.width = pixmap.getWidth();\r
                this.height = pixmap.getHeight();\r
-               if(Gdx.gl20 == null && (!MathUtils.isPowerOfTwo(width) || !MathUtils.isPowerOfTwo(height)))\r
+               if(enforcePotImages && Gdx.gl20 == null && (!MathUtils.isPowerOfTwo(width) || !MathUtils.isPowerOfTwo(height)))\r
                        throw new GdxRuntimeException("texture width and height must be powers of two");\r
                Gdx.gl.glBindTexture(GL10.GL_TEXTURE_2D, glHandle);\r
                Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());\r
@@ -479,7 +481,11 @@ public class Texture implements Disposable {
        public Format getFormat() {\r
                return format;\r
        }\r
-       \r
+\r
+       static public void setEnforcePotImages (boolean enforcePotImages) {\r
+               Texture.enforcePotImages = enforcePotImages;\r
+       }\r
+\r
 //     public static Texture getFrameBufferTexture () {\r
 //             ByteBuffer pixels = BufferUtils.newByteBuffer(Gdx.graphics.getWidth() * Gdx.graphics.getHeight() * 4);\r
 //             Gdx.gl.glReadPixels(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);\r