From f5e1dcd34690011271000e0715860eee2da605c9 Mon Sep 17 00:00:00 2001 From: "nathan.sweet" Date: Sun, 30 Oct 2011 23:31:16 +0000 Subject: [PATCH] [updated] Util maps, added get with default value. [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). --- .../gdx/backends/android/AndroidSound.java | 4 ++-- .../badlogic/gdx/backends/openal/OpenALAudio.java | 16 +++++++------ gdx/src/com/badlogic/gdx/math/Interpolation.java | 27 +++++++++++++++------- gdx/src/com/badlogic/gdx/utils/IdentityMap.java | 19 ++++++++++++--- gdx/src/com/badlogic/gdx/utils/IntMap.java | 19 ++++++++++++--- gdx/src/com/badlogic/gdx/utils/LongMap.java | 19 ++++++++++++--- .../src/com/badlogic/gdx/tests/SoundTest.java | 2 +- 7 files changed, 79 insertions(+), 27 deletions(-) diff --git a/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSound.java b/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSound.java index 1b0ad83ee..c92f30dee 100644 --- a/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSound.java +++ b/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSound.java @@ -96,9 +96,9 @@ final class AndroidSound implements Sound { float rightVolume = volume; if (pan < 0) { - rightVolume *= (1 - Math.abs(pan)); - } else if (pan > 0) { leftVolume *= (1 - Math.abs(pan)); + } else if (pan > 0) { + rightVolume *= (1 - Math.abs(pan)); } soundPool.setVolume((int)soundId, leftVolume, rightVolume); diff --git a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java index 723dfe84d..8248936e3 100644 --- a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java +++ b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java @@ -178,9 +178,10 @@ public class OpenALAudio implements Audio { for (int i = 0, n = idleSources.size; i < n; i++) { int sourceID = idleSources.get(i); if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) { - long soundId = sourceToSoundId.remove(sourceID); - soundIdToSource.remove(soundId); - + if (sourceToSoundId.containsKey(sourceID)) { + long soundId = sourceToSoundId.remove(sourceID); + soundIdToSource.remove(soundId); + } alSourceStop(sourceID); alSourcei(sourceID, AL_BUFFER, 0); } @@ -191,9 +192,10 @@ public class OpenALAudio implements Audio { for (int i = 0, n = idleSources.size; i < n; i++) { int sourceID = idleSources.get(i); if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) { - long soundId = sourceToSoundId.remove(sourceID); - soundIdToSource.remove(soundId); - + if (sourceToSoundId.containsKey(sourceID)) { + long soundId = sourceToSoundId.remove(sourceID); + soundIdToSource.remove(soundId); + } alSourceStop(sourceID); } } @@ -237,7 +239,7 @@ public class OpenALAudio implements Audio { if (!soundIdToSource.containsKey(soundId)) return; int sourceId = soundIdToSource.get(soundId); - AL10.alSource3f(sourceId, AL10.AL_POSITION, -pan, 0, 0); + AL10.alSource3f(sourceId, AL10.AL_POSITION, pan, 0, 0); } public void dispose () { diff --git a/gdx/src/com/badlogic/gdx/math/Interpolation.java b/gdx/src/com/badlogic/gdx/math/Interpolation.java index 8d2b34149..4396db766 100644 --- a/gdx/src/com/badlogic/gdx/math/Interpolation.java +++ b/gdx/src/com/badlogic/gdx/math/Interpolation.java @@ -88,14 +88,10 @@ public abstract class Interpolation { static public final Elastic elasticIn = new ElasticIn(2, 10); static public final Elastic elasticOut = new ElasticOut(2, 10); - // - static public final Interpolation swing = new Swing(1.5f); static public final Interpolation swingIn = new SwingIn(2f); static public final Interpolation swingOut = new SwingOut(2f); - // - static public final Interpolation bounce = new Bounce(4); static public final Interpolation bounceIn = new BounceIn(4); static public final Interpolation bounceOut = new BounceOut(4); @@ -186,11 +182,11 @@ public abstract class Interpolation { public float apply (float a) { if (a <= 0.5f) { a *= 2; - return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 3.14f / 0.157f) * 1.0955f / 2; + return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f / 2; } a = 1 - a; a *= 2; - return 1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 3.14f / 0.157f) * 1.0955f / 2; + return 1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 20) * 1.0955f / 2; } } @@ -200,7 +196,7 @@ public abstract class Interpolation { } public float apply (float a) { - return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 3.14f / 0.157f) * 1.0955f; + return (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f; } } @@ -211,13 +207,17 @@ public abstract class Interpolation { public float apply (float a) { a = 1 - a; - return (1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin((a) * 3.14f / 0.157f) * 1.0955f); + return (1 - (float)Math.pow(value, power * (a - 1)) * MathUtils.sin(a * 20) * 1.0955f); } } // static public class Bounce extends BounceOut { + public Bounce (float[] widths, float[] heights) { + super(widths, heights); + } + public Bounce (int bounces) { super(bounces); } @@ -237,6 +237,13 @@ public abstract class Interpolation { static public class BounceOut extends Interpolation { final float[] widths, heights; + public BounceOut (float[] widths, float[] heights) { + if (widths.length != heights.length) + throw new IllegalArgumentException("Must be the same number of widths and heights."); + this.widths = widths; + this.heights = heights; + } + public BounceOut (int bounces) { if (bounces < 2 || bounces > 5) throw new IllegalArgumentException("bounces cannot be < 2 or > 5: " + bounces); widths = new float[bounces]; @@ -297,6 +304,10 @@ public abstract class Interpolation { } static public class BounceIn extends BounceOut { + public BounceIn (float[] widths, float[] heights) { + super(widths, heights); + } + public BounceIn (int bounces) { super(bounces); } diff --git a/gdx/src/com/badlogic/gdx/utils/IdentityMap.java b/gdx/src/com/badlogic/gdx/utils/IdentityMap.java index f7067a5e7..2ee2dec2c 100644 --- a/gdx/src/com/badlogic/gdx/utils/IdentityMap.java +++ b/gdx/src/com/badlogic/gdx/utils/IdentityMap.java @@ -269,17 +269,30 @@ public class IdentityMap { index = hash2(hashCode); if (!key.equals(keyTable[index])) { index = hash3(hashCode); - if (!key.equals(keyTable[index])) return getStash(key); + if (!key.equals(keyTable[index])) return getStash(key, null); } } return valueTable[index]; } - private V getStash (K key) { + public V get (K key, V defaultValue) { + int hashCode = System.identityHashCode(key); + int index = hashCode & mask; + if (!key.equals(keyTable[index])) { + index = hash2(hashCode); + if (!key.equals(keyTable[index])) { + index = hash3(hashCode); + if (!key.equals(keyTable[index])) return getStash(key, defaultValue); + } + } + return valueTable[index]; + } + + private V getStash (K key, V defaultValue) { K[] keyTable = this.keyTable; for (int i = capacity, n = i + stashSize; i < n; i++) if (keyTable[i] == key) return valueTable[i]; - return null; + return defaultValue; } public V remove (K key) { diff --git a/gdx/src/com/badlogic/gdx/utils/IntMap.java b/gdx/src/com/badlogic/gdx/utils/IntMap.java index 44de64f95..84f879780 100644 --- a/gdx/src/com/badlogic/gdx/utils/IntMap.java +++ b/gdx/src/com/badlogic/gdx/utils/IntMap.java @@ -285,17 +285,30 @@ public class IntMap { index = hash2(key); if (keyTable[index] != key) { index = hash3(key); - if (keyTable[index] != key) return getStash(key); + if (keyTable[index] != key) return getStash(key, null); } } return valueTable[index]; } - private V getStash (int key) { + public V get (int key, V defaultValue) { + if (key == 0) return zeroValue; + int index = key & mask; + if (keyTable[index] != key) { + index = hash2(key); + if (keyTable[index] != key) { + index = hash3(key); + if (keyTable[index] != key) return getStash(key, defaultValue); + } + } + return valueTable[index]; + } + + private V getStash (int key, V defaultValue) { int[] keyTable = this.keyTable; for (int i = capacity, n = i + stashSize; i < n; i++) if (keyTable[i] == key) return valueTable[i]; - return null; + return defaultValue; } public V remove (int key) { diff --git a/gdx/src/com/badlogic/gdx/utils/LongMap.java b/gdx/src/com/badlogic/gdx/utils/LongMap.java index fb5555374..6476d9d49 100644 --- a/gdx/src/com/badlogic/gdx/utils/LongMap.java +++ b/gdx/src/com/badlogic/gdx/utils/LongMap.java @@ -285,17 +285,30 @@ public class LongMap { index = hash2(key); if (keyTable[index] != key) { index = hash3(key); - if (keyTable[index] != key) return getStash(key); + if (keyTable[index] != key) return getStash(key, null); + } + } + return valueTable[index]; + } + + public V get (long key, V defaultValue) { + if (key == 0) return zeroValue; + int index = (int)(key & mask); + if (keyTable[index] != key) { + index = hash2(key); + if (keyTable[index] != key) { + index = hash3(key); + if (keyTable[index] != key) return getStash(key, defaultValue); } } return valueTable[index]; } - private V getStash (long key) { + private V getStash (long key, V defaultValue) { long[] keyTable = this.keyTable; for (int i = capacity, n = i + stashSize; i < n; i++) if (keyTable[i] == key) return valueTable[i]; - return null; + return defaultValue; } public V remove (long key) { diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/SoundTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/SoundTest.java index ec13266c3..f56f1a7cc 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/SoundTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/SoundTest.java @@ -84,7 +84,7 @@ public class SoundTest extends GdxTest { play.setClickListener(new ClickListener() { @Override public void click (Actor actor) { - soundId = sound.play(); + soundId = sound.play(volume.getValue()); sound.setPitch(soundId, pitch.getValue()); sound.setPan(soundId, pan.getValue(), volume.getValue()); } -- 2.11.0