Permalink
Browse files

Merge branch 'GLES2' of github.com:masconsult/AndEngine into GLES2

  • Loading branch information...
2 parents f2d6e82 + 09697b6 commit 9b818cd7c14cd52d9e7f8d6c5f67b6b2ad5003c7 @groupsky groupsky committed Feb 6, 2012
@@ -1,7 +1,5 @@
package org.andengine.entity;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.Comparator;
import org.andengine.engine.camera.Camera;
@@ -12,6 +10,7 @@
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.opengl.util.GLState;
import org.andengine.util.Constants;
+import org.andengine.util.adt.list.ListUtils;
import org.andengine.util.adt.list.SmartList;
import org.andengine.util.adt.transformation.Transformation;
import org.andengine.util.call.ParameterCallable;
@@ -649,7 +648,7 @@ public boolean swapChildren(final IEntity pEntityA, final IEntity pEntityB) {
@Override
public boolean swapChildren(final int pIndexA, final int pIndexB) {
try {
- Collections.swap(this.mChildren, pIndexA, pIndexB);
+ ListUtils.swap(this.mChildren, pIndexA, pIndexB);
return true;
} catch (final IndexOutOfBoundsException e) {
return false;
@@ -1064,7 +1063,7 @@ public void reset() {
}
if(this.mChildren != null) {
- final ArrayList<IEntity> entities = this.mChildren;
+ final SmartList<IEntity> entities = this.mChildren;
for(int i = entities.size() - 1; i >= 0; i--) {
entities.get(i).reset();
}
@@ -1102,7 +1101,7 @@ public void toString(final StringBuilder pStringBuilder) {
if(this.mChildren != null && this.mChildren.size() > 0) {
pStringBuilder.append(" [");
- final ArrayList<IEntity> entities = this.mChildren;
+ final SmartList<IEntity> entities = this.mChildren;
for(int i = 0; i < entities.size(); i++) {
entities.get(i).toString(pStringBuilder);
if(i < entities.size() - 1) {
@@ -1221,7 +1220,7 @@ protected void onManagedDraw(final GLState pGLState, final Camera pCamera) {
{
this.onApplyTransformations(pGLState);
- final ArrayList<IEntity> children = this.mChildren;
+ final SmartList<IEntity> children = this.mChildren;
if(children == null || !this.mChildrenVisible) {
/* Draw only self. */
this.preDraw(pGLState, pCamera);
@@ -1270,7 +1269,7 @@ protected void onManagedUpdate(final float pSecondsElapsed) {
}
if(this.mChildren != null && !this.mChildrenIgnoreUpdate) {
- final ArrayList<IEntity> entities = this.mChildren;
+ final SmartList<IEntity> entities = this.mChildren;
final int entityCount = entities.size();
for(int i = 0; i < entityCount; i++) {
entities.get(i).onUpdate(pSecondsElapsed);
@@ -3,6 +3,7 @@
import java.util.Comparator;
import java.util.List;
+import org.andengine.util.adt.list.IList;
import org.andengine.util.algorithm.sort.InsertionSorter;
/**
@@ -73,6 +74,14 @@ public void sort(final List<IEntity> pEntities, final int pStart, final int pEnd
this.sort(pEntities, pStart, pEnd, this.mZIndexComparator);
}
+ public void sort(final IList<IEntity> pEntities) {
+ this.sort(pEntities, this.mZIndexComparator);
+ }
+
+ public void sort(final IList<IEntity> pEntities, final int pStart, final int pEnd) {
+ this.sort(pEntities, pStart, pEnd, this.mZIndexComparator);
+ }
+
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
@@ -2,20 +2,22 @@
import java.util.Arrays;
+import org.andengine.util.adt.list.IList;
+
/**
* TODO This class could take some kind of AllocationStrategy object.
*
* This implementation is particular useful/efficient for enter/poll operations.
* Its {@link java.util.Queue} like behavior performs better than a plain {@link java.util.ArrayList}, since it automatically shift the contents of its internal Array only when really necessary.
- * Besides sparse allocations to increase the size of the internal Array, {@link CircularQueue} is allocation free (unlike the {@link java.util.LinkedList} family).
+ * Besides sparse allocations to increase the size of the internal Array, {@link CircularList} is allocation free (unlike the {@link java.util.LinkedList} family).
*
* (c) Zynga 2012
*
* @author Greg Haynes
* @author Nicolas Gramlich <ngramlich@zynga.com>
* @since 15:02:40 - 24.02.2012
*/
-public class CircularQueue<T> implements IQueue<T>, IList<T> {
+public class CircularList<T> implements IList<T> {
// ===========================================================
// Constants
// ===========================================================
@@ -35,11 +37,11 @@
// Constructors
// ===========================================================
- public CircularQueue() {
- this(CircularQueue.CAPACITY_INITIAL_DEFAULT);
+ public CircularList() {
+ this(CircularList.CAPACITY_INITIAL_DEFAULT);
}
- public CircularQueue(final int pInitialCapacity) {
+ public CircularList(final int pInitialCapacity) {
this.mItems = new Object[pInitialCapacity];
}
@@ -57,7 +59,7 @@ public boolean isEmpty() {
}
@Override
- public void enter(final T pItem) {
+ public void add(final T pItem) {
this.ensureCapacity();
this.mItems[this.encodeToInternalIndex(this.mSize)] = pItem;
this.mSize++;
@@ -70,6 +72,11 @@ public T get(final int pIndex) throws ArrayIndexOutOfBoundsException {
}
@Override
+ public void set(final int pIndex, final T pItem) throws IndexOutOfBoundsException {
+ this.mItems[this.encodeToInternalIndex(pIndex)] = pItem;
+ }
+
+ @Override
public int indexOf(final T pItem) {
final int size = this.size();
if(pItem == null) {
@@ -81,52 +88,21 @@ public int indexOf(final T pItem) {
} else {
for(int i = 0; i < size; i++) {
if(pItem.equals(this.get(i))) {
- this.remove(i);
return i;
}
}
}
- return CircularQueue.INDEX_INVALID;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public T peek() {
- if(this.mSize == 0) {
- return null;
- } else {
- return (T) this.mItems[this.mHead];
- }
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public T poll() {
- if(this.mSize == 0) {
- return null;
- } else {
- final T item = (T) this.mItems[this.mHead];
- this.mItems[this.mHead] = null;
- this.mHead++;
- if(this.mHead == this.mItems.length) {
- this.mHead = 0;
- }
- this.mSize--;
- if(this.mSize == 0) {
- this.mHead = 0;
- }
- return item;
- }
+ return CircularList.INDEX_INVALID;
}
@Override
- public void enter(final int pIndex, final T pItem) {
+ public void add(final int pIndex, final T pItem) {
int internalIndex = this.encodeToInternalIndex(pIndex);
this.ensureCapacity();
- final int tail = this.encodeToInternalIndex(this.mSize);
- if(internalIndex == tail) {
+ final int internalTail = this.encodeToInternalIndex(this.mSize);
+ if(internalIndex == internalTail) {
// nothing to shift, tail is free
} else if(internalIndex == this.mHead) {
this.mHead--;
@@ -138,8 +114,8 @@ public void enter(final int pIndex, final T pItem) {
internalIndex = this.mItems.length - 1;
}
} else if((internalIndex < this.mHead) || (this.mHead == 0)) {
- System.arraycopy(this.mItems, internalIndex, this.mItems, internalIndex + 1, tail - internalIndex);
- } else if(internalIndex > tail) {
+ System.arraycopy(this.mItems, internalIndex, this.mItems, internalIndex + 1, internalTail - internalIndex);
+ } else if(internalIndex > internalTail) {
System.arraycopy(this.mItems, this.mHead, this.mItems, this.mHead - 1, pIndex);
this.mHead--;
if(this.mHead == -1) {
@@ -160,26 +136,26 @@ public void enter(final int pIndex, final T pItem) {
internalIndex = this.mItems.length - 1;
}
} else {
- System.arraycopy(this.mItems, internalIndex, this.mItems, internalIndex + 1, tail - internalIndex);
+ System.arraycopy(this.mItems, internalIndex, this.mItems, internalIndex + 1, internalTail - internalIndex);
}
this.mItems[internalIndex] = pItem;
this.mSize++;
}
@Override
- public void add(final T pItem) {
- this.enter(pItem);
+ public T removeFirst() {
+ return this.remove(0);
}
-
+
@Override
- public void add(final int pIndex, final T pItem) throws ArrayIndexOutOfBoundsException {
- this.enter(pIndex, pItem);
+ public T removeLast() {
+ return this.remove(this.size() - 1);
}
@Override
public boolean remove(final T pItem) {
final int index = this.indexOf(pItem);
- if(index == CircularQueue.INDEX_INVALID) {
+ if(index == CircularList.INDEX_INVALID) {
return false;
} else {
this.remove(index);
@@ -193,20 +169,20 @@ public T remove(final int pIndex) {
final int internalIndex = this.encodeToInternalIndex(pIndex);
final T removed = (T) this.mItems[internalIndex];
- final int tail = this.encodeToInternalIndex(this.mSize - 1);
+ final int internalTail = this.encodeToInternalIndex(this.mSize - 1);
- if(internalIndex == tail) {
- this.mItems[tail] = null;
+ if(internalIndex == internalTail) {
+ this.mItems[internalTail] = null;
} else if(internalIndex == this.mHead) {
this.mItems[this.mHead] = null;
this.mHead++;
if(this.mHead == this.mItems.length) {
this.mHead = 0;
}
} else if(internalIndex < this.mHead) {
- System.arraycopy(this.mItems, internalIndex + 1, this.mItems, internalIndex, tail - internalIndex);
- this.mItems[tail] = null;
- } else if(internalIndex > tail) {
+ System.arraycopy(this.mItems, internalIndex + 1, this.mItems, internalIndex, internalTail - internalIndex);
+ this.mItems[internalTail] = null;
+ } else if(internalIndex > internalTail) {
System.arraycopy(this.mItems, this.mHead, this.mItems, this.mHead + 1, pIndex);
this.mItems[this.mHead] = null;
this.mHead++;
@@ -221,8 +197,8 @@ public T remove(final int pIndex) {
this.mHead = 0;
}
} else {
- System.arraycopy(this.mItems, internalIndex + 1, this.mItems, internalIndex, tail - internalIndex);
- this.mItems[tail] = null;
+ System.arraycopy(this.mItems, internalIndex + 1, this.mItems, internalIndex, internalTail - internalIndex);
+ this.mItems[internalTail] = null;
}
this.mSize--;
@@ -236,7 +212,18 @@ public int size() {
@Override
public void clear() {
- Arrays.fill(this.mItems, null);
+ final int tail = this.mHead + this.mSize;
+ final int capacity = this.mItems.length;
+ /* Check if items can be blacked out in one or two calls. */
+ if(tail <= capacity) {
+ Arrays.fill(this.mItems, this.mHead, tail, null);
+ } else {
+ final int headToCapacity = capacity - this.mHead;
+ /* Black out items from head to the end of the array. */
+ Arrays.fill(this.mItems, this.mHead, capacity, null);
+ /* Black out items from the beginning of the array to the tail. */
+ Arrays.fill(this.mItems, 0, this.mSize - headToCapacity, null);
+ }
this.mHead = 0;
this.mSize = 0;
}
@@ -102,7 +102,7 @@ private void ensureCapacity(final int pCapacity) {
final int currentCapacity = this.mItems.length;
if(currentCapacity < pCapacity) {
/* Increase array size. */
- final int newCapacity = (currentCapacity * 3) / 2 + 1;
+ final int newCapacity = ((currentCapacity * 3) >> 1) + 1;
final float newItems[] = new float[newCapacity];
System.arraycopy(this.mItems, 0, newItems, 0, currentCapacity);
this.mItems = newItems;
@@ -17,10 +17,13 @@
public boolean isEmpty();
public T get(final int pIndex) throws IndexOutOfBoundsException;
+ public void set(final int pIndex, final T pItem) throws IndexOutOfBoundsException;
public int indexOf(final T pItem);
public void add(final T pItem);
public void add(final int pIndex, final T pItem) throws IndexOutOfBoundsException;
public boolean remove(final T pItem);
+ public T removeFirst();
+ public T removeLast();
public T remove(final int pIndex) throws IndexOutOfBoundsException;
public int size();
public void clear();
@@ -3,11 +3,12 @@
import java.util.ArrayList;
import java.util.List;
-import org.andengine.util.adt.list.concurrent.SynchronizedQueue;
+import org.andengine.util.adt.queue.IQueue;
+import org.andengine.util.adt.queue.concurrent.SynchronizedQueue;
import org.andengine.util.math.MathUtils;
/**
- * (c) 2010 Nicolas Gramlich
+ * (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
@@ -41,7 +42,7 @@
public static final <T> IQueue<T> synchronizedQueue(final IQueue<T> pQueue) {
return new SynchronizedQueue<T>(pQueue);
}
-
+
public static final <T> T random(final List<T> pList) {
return pList.get(MathUtils.random(0, pList.size() - 1));
}
@@ -61,6 +62,18 @@
return out;
}
+ public static <T> void swap(final List<T> pItems, final int pIndexA, final int pIndexB) {
+ final T tmp = pItems.get(pIndexA);
+ pItems.set(pIndexA, pItems.get(pIndexB));
+ pItems.set(pIndexB, tmp);
+ }
+
+ public static <T> void swap(final IList<T> pItems, final int pIndexA, final int pIndexB) {
+ final T tmp = pItems.get(pIndexA);
+ pItems.set(pIndexA, pItems.get(pIndexB));
+ pItems.set(pIndexB, tmp);
+ }
+
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
Oops, something went wrong.

0 comments on commit 9b818cd

Please sign in to comment.