OSDN Git Service

[updated] Util maps, added get with default value.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sun, 30 Oct 2011 23:31:16 +0000 (23:31 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sun, 30 Oct 2011 23:31:16 +0000 (23:31 +0000)
[updated] Interpolation, added bounce constructors.
[updated] SoundTest, use volume to start playback.
[fixed] OpenAL crash, issue 551.
[updated] Sound, reversed pan values (now -1 is left, 1 is right... wth Mario!? :p).

backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSound.java
backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java
gdx/src/com/badlogic/gdx/math/Interpolation.java
gdx/src/com/badlogic/gdx/utils/IdentityMap.java
gdx/src/com/badlogic/gdx/utils/IntMap.java
gdx/src/com/badlogic/gdx/utils/LongMap.java
tests/gdx-tests/src/com/badlogic/gdx/tests/SoundTest.java

index 1b0ad83..c92f30d 100644 (file)
@@ -96,9 +96,9 @@ final class AndroidSound implements Sound {
                float rightVolume = volume;\r
 \r
                if (pan < 0) {\r
-                       rightVolume *= (1 - Math.abs(pan));\r
-               } else if (pan > 0) {\r
                        leftVolume *= (1 - Math.abs(pan));\r
+               } else if (pan > 0) {\r
+                       rightVolume *= (1 - Math.abs(pan));\r
                }\r
 \r
                soundPool.setVolume((int)soundId, leftVolume, rightVolume);\r
index 723dfe8..8248936 100644 (file)
@@ -178,9 +178,10 @@ public class OpenALAudio implements Audio {
                for (int i = 0, n = idleSources.size; i < n; i++) {\r
                        int sourceID = idleSources.get(i);\r
                        if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) {\r
-                               long soundId = sourceToSoundId.remove(sourceID);\r
-                               soundIdToSource.remove(soundId);\r
-\r
+                               if (sourceToSoundId.containsKey(sourceID)) {\r
+                                       long soundId = sourceToSoundId.remove(sourceID);\r
+                                       soundIdToSource.remove(soundId);\r
+                               }\r
                                alSourceStop(sourceID);\r
                                alSourcei(sourceID, AL_BUFFER, 0);\r
                        }\r
@@ -191,9 +192,10 @@ public class OpenALAudio implements Audio {
                for (int i = 0, n = idleSources.size; i < n; i++) {\r
                        int sourceID = idleSources.get(i);\r
                        if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) {\r
-                               long soundId = sourceToSoundId.remove(sourceID);\r
-                               soundIdToSource.remove(soundId);\r
-\r
+                               if (sourceToSoundId.containsKey(sourceID)) {\r
+                                       long soundId = sourceToSoundId.remove(sourceID);\r
+                                       soundIdToSource.remove(soundId);\r
+                               }\r
                                alSourceStop(sourceID);\r
                        }\r
                }\r
@@ -237,7 +239,7 @@ public class OpenALAudio implements Audio {
                if (!soundIdToSource.containsKey(soundId)) return;\r
                int sourceId = soundIdToSource.get(soundId);\r
 \r
-               AL10.alSource3f(sourceId, AL10.AL_POSITION, -pan, 0, 0);\r
+               AL10.alSource3f(sourceId, AL10.AL_POSITION, pan, 0, 0);\r
        }\r
 \r
        public void dispose () {\r
index 8d2b341..4396db7 100644 (file)
@@ -88,14 +88,10 @@ public abstract class Interpolation {
        static public final Elastic elasticIn = new ElasticIn(2, 10);\r
        static public final Elastic elasticOut = new ElasticOut(2, 10);\r
 \r
-       //\r
-\r
        static public final Interpolation swing = new Swing(1.5f);\r
        static public final Interpolation swingIn = new SwingIn(2f);\r
        static public final Interpolation swingOut = new SwingOut(2f);\r
 \r
-       //\r
-\r
        static public final Interpolation bounce = new Bounce(4);\r
        static public final Interpolation bounceIn = new BounceIn(4);\r
        static public final Interpolation bounceOut = new BounceOut(4);\r
@@ -186,11 +182,11 @@ public abstract class Interpolation {
                public float apply (float a) {\r
                        if (a <= 0.5f) {\r
                                a *= 2;\r
-                               return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 3.14f / 0.157f) * 1.0955f / 2;\r
+                               return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f / 2;\r
                        }\r
                        a = 1 - a;\r
                        a *= 2;\r
-                       return 1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 3.14f / 0.157f) * 1.0955f / 2;\r
+                       return 1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 20) * 1.0955f / 2;\r
                }\r
        }\r
 \r
@@ -200,7 +196,7 @@ public abstract class Interpolation {
                }\r
 \r
                public float apply (float a) {\r
-                       return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 3.14f / 0.157f) * 1.0955f;\r
+                       return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f;\r
                }\r
        }\r
 \r
@@ -211,13 +207,17 @@ public abstract class Interpolation {
 \r
                public float apply (float a) {\r
                        a = 1 - a;\r
-                       return (1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 3.14f / 0.157f) * 1.0955f);\r
+                       return (1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f);\r
                }\r
        }\r
 \r
        //\r
 \r
        static public class Bounce extends BounceOut {\r
+               public Bounce (float[] widths, float[] heights) {\r
+                       super(widths, heights);\r
+               }\r
+\r
                public Bounce (int bounces) {\r
                        super(bounces);\r
                }\r
@@ -237,6 +237,13 @@ public abstract class Interpolation {
        static public class BounceOut extends Interpolation {\r
                final float[] widths, heights;\r
 \r
+               public BounceOut (float[] widths, float[] heights) {\r
+                       if (widths.length != heights.length)\r
+                               throw new IllegalArgumentException("Must be the same number of widths and heights.");\r
+                       this.widths = widths;\r
+                       this.heights = heights;\r
+               }\r
+\r
                public BounceOut (int bounces) {\r
                        if (bounces < 2 || bounces > 5) throw new IllegalArgumentException("bounces cannot be < 2 or > 5: " + bounces);\r
                        widths = new float[bounces];\r
@@ -297,6 +304,10 @@ public abstract class Interpolation {
        }\r
 \r
        static public class BounceIn extends BounceOut {\r
+               public BounceIn (float[] widths, float[] heights) {\r
+                       super(widths, heights);\r
+               }\r
+\r
                public BounceIn (int bounces) {\r
                        super(bounces);\r
                }\r
index f7067a5..2ee2dec 100644 (file)
@@ -269,17 +269,30 @@ public class IdentityMap<K, V> {
                        index = hash2(hashCode);\r
                        if (!key.equals(keyTable[index])) {\r
                                index = hash3(hashCode);\r
-                               if (!key.equals(keyTable[index])) return getStash(key);\r
+                               if (!key.equals(keyTable[index])) return getStash(key, null);\r
                        }\r
                }\r
                return valueTable[index];\r
        }\r
 \r
-       private V getStash (K key) {\r
+       public V get (K key, V defaultValue) {\r
+               int hashCode = System.identityHashCode(key);\r
+               int index = hashCode & mask;\r
+               if (!key.equals(keyTable[index])) {\r
+                       index = hash2(hashCode);\r
+                       if (!key.equals(keyTable[index])) {\r
+                               index = hash3(hashCode);\r
+                               if (!key.equals(keyTable[index])) return getStash(key, defaultValue);\r
+                       }\r
+               }\r
+               return valueTable[index];\r
+       }\r
+\r
+       private V getStash (K key, V defaultValue) {\r
                K[] keyTable = this.keyTable;\r
                for (int i = capacity, n = i + stashSize; i < n; i++)\r
                        if (keyTable[i] == key) return valueTable[i];\r
-               return null;\r
+               return defaultValue;\r
        }\r
 \r
        public V remove (K key) {\r
index 44de64f..84f8797 100644 (file)
@@ -285,17 +285,30 @@ public class IntMap<V> {
                        index = hash2(key);\r
                        if (keyTable[index] != key) {\r
                                index = hash3(key);\r
-                               if (keyTable[index] != key) return getStash(key);\r
+                               if (keyTable[index] != key) return getStash(key, null);\r
                        }\r
                }\r
                return valueTable[index];\r
        }\r
 \r
-       private V getStash (int key) {\r
+       public V get (int key, V defaultValue) {\r
+               if (key == 0) return zeroValue;\r
+               int index = key & mask;\r
+               if (keyTable[index] != key) {\r
+                       index = hash2(key);\r
+                       if (keyTable[index] != key) {\r
+                               index = hash3(key);\r
+                               if (keyTable[index] != key) return getStash(key, defaultValue);\r
+                       }\r
+               }\r
+               return valueTable[index];\r
+       }\r
+\r
+       private V getStash (int key, V defaultValue) {\r
                int[] keyTable = this.keyTable;\r
                for (int i = capacity, n = i + stashSize; i < n; i++)\r
                        if (keyTable[i] == key) return valueTable[i];\r
-               return null;\r
+               return defaultValue;\r
        }\r
 \r
        public V remove (int key) {\r
index fb55553..6476d9d 100644 (file)
@@ -285,17 +285,30 @@ public class LongMap<V> {
                        index = hash2(key);\r
                        if (keyTable[index] != key) {\r
                                index = hash3(key);\r
-                               if (keyTable[index] != key) return getStash(key);\r
+                               if (keyTable[index] != key) return getStash(key, null);\r
+                       }\r
+               }\r
+               return valueTable[index];\r
+       }\r
+       \r
+       public V get (long key, V defaultValue) {\r
+               if (key == 0) return zeroValue;\r
+               int index = (int)(key & mask);\r
+               if (keyTable[index] != key) {\r
+                       index = hash2(key);\r
+                       if (keyTable[index] != key) {\r
+                               index = hash3(key);\r
+                               if (keyTable[index] != key) return getStash(key, defaultValue);\r
                        }\r
                }\r
                return valueTable[index];\r
        }\r
 \r
-       private V getStash (long key) {\r
+       private V getStash (long key, V defaultValue) {\r
                long[] keyTable = this.keyTable;\r
                for (int i = capacity, n = i + stashSize; i < n; i++)\r
                        if (keyTable[i] == key) return valueTable[i];\r
-               return null;\r
+               return defaultValue;\r
        }\r
 \r
        public V remove (long key) {\r
index ec13266..f56f1a7 100644 (file)
@@ -84,7 +84,7 @@ public class SoundTest extends GdxTest {
                play.setClickListener(new ClickListener() {\r
                        @Override\r
                        public void click (Actor actor) {\r
-                               soundId = sound.play();\r
+                               soundId = sound.play(volume.getValue());\r
                                sound.setPitch(soundId, pitch.getValue());\r
                                sound.setPan(soundId, pan.getValue(), volume.getValue());\r
                        }\r