code
stringlengths
1
2.01M
repo_name
stringlengths
3
62
path
stringlengths
1
267
language
stringclasses
231 values
license
stringclasses
13 values
size
int64
1
2.01M
package org.anddev.andengine.opengl.util; import static org.anddev.andengine.opengl.util.GLHelper.BYTES_PER_FLOAT; import java.lang.ref.SoftReference; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; /** * Convenient work-around for poor {@link FloatBuffer#put(float[])} performance. * This should become unnecessary in gingerbread, * @see <a href="http://code.google.com/p/android/issues/detail?id=11078">Issue 11078</a> * * @author ryanm */ public class FastFloatBuffer { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== /** * Use a {@link SoftReference} so that the array can be collected if * necessary */ private static SoftReference<int[]> sWeakIntArray = new SoftReference<int[]>(new int[0]); /** * Underlying data - give this to OpenGL */ public final ByteBuffer mByteBuffer; private final FloatBuffer mFloatBuffer; private final IntBuffer mIntBuffer; // =========================================================== // Constructors // =========================================================== /** * Constructs a new direct native-ordered buffer */ public FastFloatBuffer(final int pCapacity) { this.mByteBuffer = ByteBuffer.allocateDirect((pCapacity * BYTES_PER_FLOAT)).order(ByteOrder.nativeOrder()); this.mFloatBuffer = this.mByteBuffer.asFloatBuffer(); this.mIntBuffer = this.mByteBuffer.asIntBuffer(); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== /** * See {@link FloatBuffer#flip()} */ public void flip() { this.mByteBuffer.flip(); this.mFloatBuffer.flip(); this.mIntBuffer.flip(); } /** * See {@link FloatBuffer#put(float)} */ public void put(final float f) { final ByteBuffer byteBuffer = this.mByteBuffer; final IntBuffer intBuffer = this.mIntBuffer; byteBuffer.position(byteBuffer.position() + BYTES_PER_FLOAT); this.mFloatBuffer.put(f); intBuffer.position(intBuffer.position() + 1); } /** * It'MAGIC_CONSTANT like {@link FloatBuffer#put(float[])}, but about 10 times faster */ public void put(final float[] data) { final int length = data.length; int[] ia = sWeakIntArray.get(); if(ia == null || ia.length < length) { ia = new int[length]; sWeakIntArray = new SoftReference<int[]>(ia); } for(int i = 0; i < length; i++) { ia[i] = Float.floatToRawIntBits(data[i]); } final ByteBuffer byteBuffer = this.mByteBuffer; byteBuffer.position(byteBuffer.position() + BYTES_PER_FLOAT * length); final FloatBuffer floatBuffer = this.mFloatBuffer; floatBuffer.position(floatBuffer.position() + length); this.mIntBuffer.put(ia, 0, length); } /** * For use with pre-converted data. This is 50x faster than * {@link #put(float[])}, and 500x faster than * {@link FloatBuffer#put(float[])}, so if you've got float[] data that * won't change, {@link #convert(float...)} it to an int[] once and use this * method to put it in the buffer * * @param data floats that have been converted with {@link Float#floatToIntBits(float)} */ public void put(final int[] data) { final ByteBuffer byteBuffer = this.mByteBuffer; byteBuffer.position(byteBuffer.position() + BYTES_PER_FLOAT * data.length); final FloatBuffer floatBuffer = this.mFloatBuffer; floatBuffer.position(floatBuffer.position() + data.length); this.mIntBuffer.put(data, 0, data.length); } /** * Converts float data to a format that can be quickly added to the buffer * with {@link #put(int[])} * * @param data * @return the int-formatted data */ public static int[] convert(final float ... data) { final int length = data.length; final int[] id = new int[length]; for(int i = 0; i < length; i++) { id[i] = Float.floatToRawIntBits(data[i]); } return id; } /** * See {@link FloatBuffer#put(FloatBuffer)} */ public void put(final FastFloatBuffer b) { final ByteBuffer byteBuffer = this.mByteBuffer; byteBuffer.put(b.mByteBuffer); this.mFloatBuffer.position(byteBuffer.position() >> 2); this.mIntBuffer.position(byteBuffer.position() >> 2); } /** * @return See {@link FloatBuffer#capacity()} */ public int capacity() { return this.mFloatBuffer.capacity(); } /** * @return See {@link FloatBuffer#position()} */ public int position() { return this.mFloatBuffer.position(); } /** * See {@link FloatBuffer#position(int)} */ public void position(final int p) { this.mByteBuffer.position(p * BYTES_PER_FLOAT); this.mFloatBuffer.position(p); this.mIntBuffer.position(p); } /** * @return See {@link FloatBuffer#slice()} */ public FloatBuffer slice() { return this.mFloatBuffer.slice(); } /** * @return See {@link FloatBuffer#remaining()} */ public int remaining() { return this.mFloatBuffer.remaining(); } /** * @return See {@link FloatBuffer#limit()} */ public int limit() { return this.mFloatBuffer.limit(); } /** * See {@link FloatBuffer#clear()} */ public void clear() { this.mByteBuffer.clear(); this.mFloatBuffer.clear(); this.mIntBuffer.clear(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/util/FastFloatBuffer.java
Java
lgpl
6,062
package org.anddev.andengine.opengl.vertex; import org.anddev.andengine.opengl.util.FastFloatBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 13:07:25 - 13.03.2010 */ public class RectangleVertexBuffer extends VertexBuffer { // =========================================================== // Constants // =========================================================== public static final int VERTICES_PER_RECTANGLE = 4; private static final int FLOAT_TO_RAW_INT_BITS_ZERO = Float.floatToRawIntBits(0); // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public RectangleVertexBuffer(final int pDrawType, final boolean pManaged) { super(2 * VERTICES_PER_RECTANGLE, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void update(final float pWidth, final float pHeight) { final int x = FLOAT_TO_RAW_INT_BITS_ZERO; final int y = FLOAT_TO_RAW_INT_BITS_ZERO; final int x2 = Float.floatToRawIntBits(pWidth); final int y2 = Float.floatToRawIntBits(pHeight); final int[] bufferData = this.mBufferData; bufferData[0] = x; bufferData[1] = y; bufferData[2] = x; bufferData[3] = y2; bufferData[4] = x2; bufferData[5] = y; bufferData[6] = x2; bufferData[7] = y2; final FastFloatBuffer buffer = this.getFloatBuffer(); buffer.position(0); buffer.put(bufferData); buffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/vertex/RectangleVertexBuffer.java
Java
lgpl
2,349
package org.anddev.andengine.opengl.vertex; import org.anddev.andengine.opengl.buffer.BufferObject; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:16:18 - 09.03.2010 */ public abstract class VertexBuffer extends BufferObject { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public VertexBuffer(final int pCapacity, final int pDrawType, final boolean pManaged) { super(pCapacity, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/vertex/VertexBuffer.java
Java
lgpl
1,516
package org.anddev.andengine.opengl.vertex; import org.anddev.andengine.opengl.util.FastFloatBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 13:07:25 - 13.03.2010 */ public class LineVertexBuffer extends VertexBuffer { // =========================================================== // Constants // =========================================================== public static final int VERTICES_PER_LINE = 2; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public LineVertexBuffer(final int pDrawType, final boolean pManaged) { super(2 * VERTICES_PER_LINE, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void update(final float pX1, final float pY1, final float pX2, final float pY2) { final int[] bufferData = this.mBufferData; bufferData[0] = Float.floatToRawIntBits(pX1); bufferData[1] = Float.floatToRawIntBits(pY1); bufferData[2] = Float.floatToRawIntBits(pX2); bufferData[3] = Float.floatToRawIntBits(pY2); final FastFloatBuffer buffer = this.mFloatBuffer; buffer.position(0); buffer.put(bufferData); buffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/vertex/LineVertexBuffer.java
Java
lgpl
2,089
package org.anddev.andengine.opengl.vertex; import org.anddev.andengine.opengl.font.Font; import org.anddev.andengine.opengl.font.Letter; import org.anddev.andengine.opengl.util.FastFloatBuffer; import org.anddev.andengine.util.HorizontalAlign; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:05:08 - 07.04.2010 */ public class TextVertexBuffer extends VertexBuffer { // =========================================================== // Constants // =========================================================== public static final int VERTICES_PER_CHARACTER = 6; // =========================================================== // Fields // =========================================================== private final HorizontalAlign mHorizontalAlign; // =========================================================== // Constructors // =========================================================== public TextVertexBuffer(final int pCharacterCount, final HorizontalAlign pHorizontalAlign, final int pDrawType, final boolean pManaged) { super(2 * VERTICES_PER_CHARACTER * pCharacterCount, pDrawType, pManaged); this.mHorizontalAlign = pHorizontalAlign; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void update(final Font font, final int pMaximumLineWidth, final int[] pWidths, final String[] pLines) { final int[] bufferData = this.mBufferData; int i = 0; final int lineHeight = font.getLineHeight(); final int lineCount = pLines.length; for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) { final String line = pLines[lineIndex]; int lineX; switch(this.mHorizontalAlign) { case RIGHT: lineX = pMaximumLineWidth - pWidths[lineIndex]; break; case CENTER: lineX = (pMaximumLineWidth - pWidths[lineIndex]) >> 1; break; case LEFT: default: lineX = 0; } final int lineY = lineIndex * (font.getLineHeight() + font.getLineGap()); final int lineYBits = Float.floatToRawIntBits(lineY); final int lineLength = line.length(); for (int letterIndex = 0; letterIndex < lineLength; letterIndex++) { final Letter letter = font.getLetter(line.charAt(letterIndex)); final int lineY2 = lineY + lineHeight; final int lineX2 = lineX + letter.mWidth; final int lineXBits = Float.floatToRawIntBits(lineX); final int lineX2Bits = Float.floatToRawIntBits(lineX2); final int lineY2Bits = Float.floatToRawIntBits(lineY2); bufferData[i++] = lineXBits; bufferData[i++] = lineYBits; bufferData[i++] = lineXBits; bufferData[i++] = lineY2Bits; bufferData[i++] = lineX2Bits; bufferData[i++] = lineY2Bits; bufferData[i++] = lineX2Bits; bufferData[i++] = lineY2Bits; bufferData[i++] = lineX2Bits; bufferData[i++] = lineYBits; bufferData[i++] = lineXBits; bufferData[i++] = lineYBits; lineX += letter.mAdvance; } } final FastFloatBuffer vertexFloatBuffer = this.mFloatBuffer; vertexFloatBuffer.position(0); vertexFloatBuffer.put(bufferData); vertexFloatBuffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/vertex/TextVertexBuffer.java
Java
lgpl
3,858
package org.anddev.andengine.opengl.vertex; import org.anddev.andengine.opengl.util.FastFloatBuffer; import org.anddev.andengine.util.Transformation; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:53:48 - 14.06.2011 */ public class SpriteBatchVertexBuffer extends VertexBuffer { // =========================================================== // Constants // =========================================================== public static final int VERTICES_PER_RECTANGLE = 6; private static final float[] VERTICES_TMP = new float[8]; private static final Transformation TRANSFORATION_TMP = new Transformation(); // =========================================================== // Fields // =========================================================== protected int mIndex; // =========================================================== // Constructors // =========================================================== public SpriteBatchVertexBuffer(final int pCapacity, final int pDrawType, final boolean pManaged) { super(pCapacity * 2 * VERTICES_PER_RECTANGLE, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== public int getIndex() { return this.mIndex; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public void setIndex(final int pIndex) { this.mIndex = pIndex; } /** * @param pX * @param pY * @param pWidth * @param pHeight * @param pRotation around the center (pWidth * 0.5f, pHeight * 0.5f) */ public void add(final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation) { final float widthHalf = pWidth * 0.5f; final float heightHalf = pHeight * 0.5f; TRANSFORATION_TMP.setToIdentity(); TRANSFORATION_TMP.postTranslate(-widthHalf, -heightHalf); TRANSFORATION_TMP.postRotate(pRotation); TRANSFORATION_TMP.postTranslate(widthHalf, heightHalf); TRANSFORATION_TMP.postTranslate(pX, pY); this.add(pWidth, pHeight, TRANSFORATION_TMP); } /** * @param pX * @param pY * @param pWidth * @param pHeight * @param pRotation around the center (pWidth * 0.5f, pHeight * 0.5f) * @param pScaleX around the center (pWidth * 0.5f, pHeight * 0.5f) * @param pScaleY around the center (pWidth * 0.5f, pHeight * 0.5f) */ public void add(final float pX, final float pY, final float pWidth, final float pHeight, final float pScaleX, final float pScaleY) { final float widthHalf = pWidth * 0.5f; final float heightHalf = pHeight * 0.5f; TRANSFORATION_TMP.setToIdentity(); TRANSFORATION_TMP.postTranslate(-widthHalf, -heightHalf); TRANSFORATION_TMP.postScale(pScaleX, pScaleY); TRANSFORATION_TMP.postTranslate(widthHalf, heightHalf); TRANSFORATION_TMP.postTranslate(pX, pY); this.add(pWidth, pHeight, TRANSFORATION_TMP); } /** * @param pX * @param pY * @param pWidth * @param pHeight * @param pRotation around the center (pWidth * 0.5f, pHeight * 0.5f) * @param pScaleX around the center (pWidth * 0.5f, pHeight * 0.5f) * @param pScaleY around the center (pWidth * 0.5f, pHeight * 0.5f) */ public void add(final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation, final float pScaleX, final float pScaleY) { final float widthHalf = pWidth * 0.5f; final float heightHalf = pHeight * 0.5f; TRANSFORATION_TMP.setToIdentity(); TRANSFORATION_TMP.postTranslate(-widthHalf, -heightHalf); TRANSFORATION_TMP.postScale(pScaleX, pScaleY); TRANSFORATION_TMP.postRotate(pRotation); TRANSFORATION_TMP.postTranslate(widthHalf, heightHalf); TRANSFORATION_TMP.postTranslate(pX, pY); this.add(pWidth, pHeight, TRANSFORATION_TMP); } /** * * @param pX * @param pY * @param pWidth * @param pHeight * @param pTransformation */ public void add(final float pWidth, final float pHeight, final Transformation pTransformation) { VERTICES_TMP[0] = 0; VERTICES_TMP[1] = 0; VERTICES_TMP[2] = 0; VERTICES_TMP[3] = pHeight; VERTICES_TMP[4] = pWidth; VERTICES_TMP[5] = 0; VERTICES_TMP[6] = pWidth; VERTICES_TMP[7] = pHeight; pTransformation.transform(VERTICES_TMP); this.addInner(VERTICES_TMP[0], VERTICES_TMP[1], VERTICES_TMP[2], VERTICES_TMP[3], VERTICES_TMP[4], VERTICES_TMP[5], VERTICES_TMP[6], VERTICES_TMP[7]); } public void add(final float pX, final float pY, final float pWidth, final float pHeight) { this.addInner(pX, pY, pX + pWidth, pY + pHeight); } /** * 1-+ * |X| * +-2 */ public void addInner(final float pX1, final float pY1, final float pX2, final float pY2) { final int x1 = Float.floatToRawIntBits(pX1); final int y1 = Float.floatToRawIntBits(pY1); final int x2 = Float.floatToRawIntBits(pX2); final int y2 = Float.floatToRawIntBits(pY2); final int[] bufferData = this.mBufferData; int index = this.mIndex; bufferData[index++] = x1; bufferData[index++] = y1; bufferData[index++] = x1; bufferData[index++] = y2; bufferData[index++] = x2; bufferData[index++] = y1; bufferData[index++] = x2; bufferData[index++] = y1; bufferData[index++] = x1; bufferData[index++] = y2; bufferData[index++] = x2; bufferData[index++] = y2; this.mIndex = index; } /** * 1-3 * |X| * 2-4 */ public void addInner(final float pX1, final float pY1, final float pX2, final float pY2, final float pX3, final float pY3, final float pX4, final float pY4) { final int x1 = Float.floatToRawIntBits(pX1); final int y1 = Float.floatToRawIntBits(pY1); final int x2 = Float.floatToRawIntBits(pX2); final int y2 = Float.floatToRawIntBits(pY2); final int x3 = Float.floatToRawIntBits(pX3); final int y3 = Float.floatToRawIntBits(pY3); final int x4 = Float.floatToRawIntBits(pX4); final int y4 = Float.floatToRawIntBits(pY4); final int[] bufferData = this.mBufferData; int index = this.mIndex; bufferData[index++] = x1; bufferData[index++] = y1; bufferData[index++] = x2; bufferData[index++] = y2; bufferData[index++] = x3; bufferData[index++] = y3; bufferData[index++] = x3; bufferData[index++] = y3; bufferData[index++] = x2; bufferData[index++] = y2; bufferData[index++] = x4; bufferData[index++] = y4; this.mIndex = index; } public void submit() { final FastFloatBuffer buffer = this.mFloatBuffer; buffer.position(0); buffer.put(this.mBufferData); buffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/vertex/SpriteBatchVertexBuffer.java
Java
lgpl
7,200
package org.anddev.andengine.opengl.font; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:29:21 - 03.04.2010 */ class Size { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private float mWidth; private float mHeight; // =========================================================== // Constructors // =========================================================== public Size() { this(0, 0); } public Size(final float pWidth, final float pHeight) { this.setWidth(pWidth); this.setHeight(pHeight); } // =========================================================== // Getter & Setter // =========================================================== public void setWidth(final float width) { this.mWidth = width; } public float getWidth() { return this.mWidth; } public void setHeight(final float height) { this.mHeight = height; } public float getHeight() { return this.mHeight; } public void set(final int pWidth, final int pHeight) { this.setWidth(pWidth); this.setHeight(pHeight); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/Size.java
Java
lgpl
1,862
package org.anddev.andengine.opengl.font; import org.anddev.andengine.opengl.texture.ITexture; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Typeface; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:39:33 - 03.04.2010 */ public class StrokeFont extends Font { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Paint mStrokePaint; private final boolean mStrokeOnly; // =========================================================== // Constructors // =========================================================== public StrokeFont(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor) { this(pTexture, pTypeface, pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor, false); } public StrokeFont(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor, final boolean pStrokeOnly) { super(pTexture, pTypeface, pSize, pAntiAlias, pColor); this.mStrokePaint = new Paint(); this.mStrokePaint.setTypeface(pTypeface); this.mStrokePaint.setStyle(Style.STROKE); this.mStrokePaint.setStrokeWidth(pStrokeWidth); this.mStrokePaint.setColor(pStrokeColor); this.mStrokePaint.setTextSize(pSize); this.mStrokePaint.setAntiAlias(pAntiAlias); this.mStrokeOnly = pStrokeOnly; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected void drawCharacterString(final String pCharacterAsString) { if(!this.mStrokeOnly) { super.drawCharacterString(pCharacterAsString); } this.mCanvas.drawText(pCharacterAsString, LETTER_LEFT_OFFSET, -this.mFontMetrics.ascent, this.mStrokePaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/StrokeFont.java
Java
lgpl
2,729
package org.anddev.andengine.opengl.font; import org.anddev.andengine.opengl.texture.ITexture; import android.content.Context; import android.graphics.Typeface; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 17:17:28 - 16.06.2010 */ public class FontFactory { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private static String sAssetBasePath = ""; // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== /** * @param pAssetBasePath must end with '<code>/</code>' or have <code>.length() == 0</code>. */ public static void setAssetBasePath(final String pAssetBasePath) { if(pAssetBasePath.endsWith("/") || pAssetBasePath.length() == 0) { FontFactory.sAssetBasePath = pAssetBasePath; } else { throw new IllegalStateException("pAssetBasePath must end with '/' or be lenght zero."); } } public static void reset() { FontFactory.setAssetBasePath(""); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static Font create(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor) { return new Font(pTexture, pTypeface, pSize, pAntiAlias, pColor); } public static StrokeFont createStroke(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor) { return new StrokeFont(pTexture, pTypeface, pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor); } public static StrokeFont createStroke(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor, final boolean pStrokeOnly) { return new StrokeFont(pTexture, pTypeface, pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor, pStrokeOnly); } public static Font createFromAsset(final ITexture pTexture, final Context pContext, final String pAssetPath, final float pSize, final boolean pAntiAlias, final int pColor) { return new Font(pTexture, Typeface.createFromAsset(pContext.getAssets(), FontFactory.sAssetBasePath + pAssetPath), pSize, pAntiAlias, pColor); } public static StrokeFont createStrokeFromAsset(final ITexture pTexture, final Context pContext, final String pAssetPath, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor) { return new StrokeFont(pTexture, Typeface.createFromAsset(pContext.getAssets(), FontFactory.sAssetBasePath + pAssetPath), pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor); } public static StrokeFont createStrokeFromAsset(final ITexture pTexture, final Context pContext, final String pAssetPath, final float pSize, final boolean pAntiAlias, final int pColor, final float pStrokeWidth, final int pStrokeColor, final boolean pStrokeOnly) { return new StrokeFont(pTexture, Typeface.createFromAsset(pContext.getAssets(), FontFactory.sAssetBasePath + pAssetPath), pSize, pAntiAlias, pColor, pStrokeWidth, pStrokeColor, pStrokeOnly); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/FontFactory.java
Java
lgpl
4,045
/** * */ package org.anddev.andengine.opengl.font; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:30:22 - 03.04.2010 */ public class Letter { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== public final int mAdvance; public final int mWidth; public final int mHeight; public final float mTextureX; public final float mTextureY; public final float mTextureWidth; public final float mTextureHeight; public final char mCharacter; // =========================================================== // Constructors // =========================================================== Letter(final char pCharacter, final int pAdvance, final int pWidth, final int pHeight, final float pTextureX, final float pTextureY, final float pTextureWidth, final float pTextureHeight) { this.mCharacter = pCharacter; this.mAdvance = pAdvance; this.mWidth = pWidth; this.mHeight = pHeight; this.mTextureX = pTextureX; this.mTextureY = pTextureY; this.mTextureWidth = pTextureWidth; this.mTextureHeight = pTextureHeight; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + this.mCharacter; return result; } @Override public boolean equals(final Object obj) { if(this == obj) { return true; } if(obj == null) { return false; } if(this.getClass() != obj.getClass()) { return false; } final Letter other = (Letter) obj; if(this.mCharacter != other.mCharacter) { return false; } return true; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/Letter.java
Java
lgpl
2,492
package org.anddev.andengine.opengl.font; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.ITexture; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Paint.Style; import android.graphics.Rect; import android.graphics.Typeface; import android.opengl.GLUtils; import android.util.FloatMath; import android.util.SparseArray; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:39:33 - 03.04.2010 */ public class Font { // =========================================================== // Constants // =========================================================== protected static final float LETTER_LEFT_OFFSET = 0; protected static final int LETTER_EXTRA_WIDTH = 10; protected final static int PADDING = 1; // =========================================================== // Fields // =========================================================== private final ITexture mTexture; private final float mTextureWidth; private final float mTextureHeight; private int mCurrentTextureX = 0; private int mCurrentTextureY = 0; private final SparseArray<Letter> mManagedCharacterToLetterMap = new SparseArray<Letter>(); private final ArrayList<Letter> mLettersPendingToBeDrawnToTexture = new ArrayList<Letter>(); protected final Paint mPaint; private final Paint mBackgroundPaint; protected final FontMetrics mFontMetrics; private final int mLineHeight; private final int mLineGap; private final Size mCreateLetterTemporarySize = new Size(); private final Rect mGetLetterBitmapTemporaryRect = new Rect(); private final Rect mGetStringWidthTemporaryRect = new Rect(); private final Rect mGetLetterBoundsTemporaryRect = new Rect(); private final float[] mTemporaryTextWidthFetchers = new float[1]; protected final Canvas mCanvas = new Canvas(); // =========================================================== // Constructors // =========================================================== public Font(final ITexture pTexture, final Typeface pTypeface, final float pSize, final boolean pAntiAlias, final int pColor) { this.mTexture = pTexture; this.mTextureWidth = pTexture.getWidth(); this.mTextureHeight = pTexture.getHeight(); this.mPaint = new Paint(); this.mPaint.setTypeface(pTypeface); this.mPaint.setColor(pColor); this.mPaint.setTextSize(pSize); this.mPaint.setAntiAlias(pAntiAlias); this.mBackgroundPaint = new Paint(); this.mBackgroundPaint.setColor(Color.TRANSPARENT); this.mBackgroundPaint.setStyle(Style.FILL); this.mFontMetrics = this.mPaint.getFontMetrics(); this.mLineHeight = (int) FloatMath.ceil(Math.abs(this.mFontMetrics.ascent) + Math.abs(this.mFontMetrics.descent)) + (PADDING * 2); this.mLineGap = (int) (FloatMath.ceil(this.mFontMetrics.leading)); } // =========================================================== // Getter & Setter // =========================================================== public int getLineGap() { return this.mLineGap; } public int getLineHeight() { return this.mLineHeight; } public ITexture getTexture() { return this.mTexture; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void reload() { final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture; final SparseArray<Letter> managedCharacterToLetterMap = this.mManagedCharacterToLetterMap; /* Make all letters redraw to the texture. */ for(int i = managedCharacterToLetterMap.size() - 1; i >= 0; i--) { lettersPendingToBeDrawnToTexture.add(managedCharacterToLetterMap.valueAt(i)); } } private int getLetterAdvance(final char pCharacter) { this.mPaint.getTextWidths(String.valueOf(pCharacter), this.mTemporaryTextWidthFetchers); return (int) (FloatMath.ceil(this.mTemporaryTextWidthFetchers[0])); } private Bitmap getLetterBitmap(final char pCharacter) { final Rect getLetterBitmapTemporaryRect = this.mGetLetterBitmapTemporaryRect; final String characterAsString = String.valueOf(pCharacter); this.mPaint.getTextBounds(characterAsString, 0, 1, getLetterBitmapTemporaryRect); getLetterBitmapTemporaryRect.right += PADDING * 2; final int lineHeight = this.getLineHeight(); final Bitmap bitmap = Bitmap.createBitmap(getLetterBitmapTemporaryRect.width() == 0 ? 1 + (2 * PADDING) : getLetterBitmapTemporaryRect.width() + LETTER_EXTRA_WIDTH, lineHeight, Bitmap.Config.ARGB_8888); this.mCanvas.setBitmap(bitmap); /* Make background transparent. */ this.mCanvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), this.mBackgroundPaint); /* Actually draw the character. */ this.drawCharacterString(characterAsString); return bitmap; } protected void drawCharacterString(final String pCharacterAsString) { this.mCanvas.drawText(pCharacterAsString, LETTER_LEFT_OFFSET + PADDING, -this.mFontMetrics.ascent + PADDING, this.mPaint); } public int getStringWidth(final String pText) { this.mPaint.getTextBounds(pText, 0, pText.length(), this.mGetStringWidthTemporaryRect); return this.mGetStringWidthTemporaryRect.width(); } private void getLetterBounds(final char pCharacter, final Size pSize) { this.mPaint.getTextBounds(String.valueOf(pCharacter), 0, 1, this.mGetLetterBoundsTemporaryRect); pSize.set(this.mGetLetterBoundsTemporaryRect.width() + LETTER_EXTRA_WIDTH + (2 * PADDING), this.getLineHeight()); } public void prepareLetters(final char ... pCharacters) { for(final char character : pCharacters) { this.getLetter(character); } } public synchronized Letter getLetter(final char pCharacter) { final SparseArray<Letter> managedCharacterToLetterMap = this.mManagedCharacterToLetterMap; Letter letter = managedCharacterToLetterMap.get(pCharacter); if (letter == null) { letter = this.createLetter(pCharacter); this.mLettersPendingToBeDrawnToTexture.add(letter); managedCharacterToLetterMap.put(pCharacter, letter); } return letter; } private Letter createLetter(final char pCharacter) { final float textureWidth = this.mTextureWidth; final float textureHeight = this.mTextureHeight; final Size createLetterTemporarySize = this.mCreateLetterTemporarySize; this.getLetterBounds(pCharacter, createLetterTemporarySize); final float letterWidth = createLetterTemporarySize.getWidth(); final float letterHeight = createLetterTemporarySize.getHeight(); if (this.mCurrentTextureX + letterWidth >= textureWidth) { this.mCurrentTextureX = 0; this.mCurrentTextureY += this.getLineGap() + this.getLineHeight(); } final float letterTextureX = this.mCurrentTextureX / textureWidth; final float letterTextureY = this.mCurrentTextureY / textureHeight; final float letterTextureWidth = letterWidth / textureWidth; final float letterTextureHeight = letterHeight / textureHeight; final Letter letter = new Letter(pCharacter, this.getLetterAdvance(pCharacter), (int)letterWidth, (int)letterHeight, letterTextureX, letterTextureY, letterTextureWidth, letterTextureHeight); this.mCurrentTextureX += letterWidth; return letter; } public synchronized void update(final GL10 pGL) { final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture; if(lettersPendingToBeDrawnToTexture.size() > 0) { this.mTexture.bind(pGL); final float textureWidth = this.mTextureWidth; final float textureHeight = this.mTextureHeight; for(int i = lettersPendingToBeDrawnToTexture.size() - 1; i >= 0; i--) { final Letter letter = lettersPendingToBeDrawnToTexture.get(i); final Bitmap bitmap = this.getLetterBitmap(letter.mCharacter); // TODO What about premultiplyalpha of the textureOptions? GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, (int)(letter.mTextureX * textureWidth), (int)(letter.mTextureY * textureHeight), bitmap); bitmap.recycle(); } lettersPendingToBeDrawnToTexture.clear(); System.gc(); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/Font.java
Java
lgpl
8,764
package org.anddev.andengine.opengl.font; import org.anddev.andengine.util.Library; import android.util.SparseArray; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:52:26 - 20.08.2010 */ public class FontLibrary extends Library<Font> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public FontLibrary() { super(); } public FontLibrary(final int pInitialCapacity) { super(pInitialCapacity); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== void loadFonts(final FontManager pFontManager) { final SparseArray<Font> items = this.mItems; for(int i = items.size() - 1; i >= 0; i--) { final Font font = items.valueAt(i); if(font != null) { pFontManager.loadFont(font); } } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/FontLibrary.java
Java
lgpl
1,778
package org.anddev.andengine.opengl.font; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL10; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 17:48:46 - 08.03.2010 */ public class FontManager { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final ArrayList<Font> mFontsManaged = new ArrayList<Font>(); // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void clear() { this.mFontsManaged.clear(); } public synchronized void loadFont(final Font pFont) { if(pFont == null) { throw new IllegalArgumentException("pFont must not be null!"); } this.mFontsManaged.add(pFont); } public synchronized void loadFonts(final FontLibrary pFontLibrary) { pFontLibrary.loadFonts(this); } public void loadFonts(final Font ... pFonts) { for(int i = pFonts.length - 1; i >= 0; i--) { this.loadFont(pFonts[i]); } } public synchronized void updateFonts(final GL10 pGL) { final ArrayList<Font> fonts = this.mFontsManaged; final int fontCount = fonts.size(); if(fontCount > 0){ for(int i = fontCount - 1; i >= 0; i--){ fonts.get(i).update(pGL); } } } public synchronized void reloadFonts() { final ArrayList<Font> managedFonts = this.mFontsManaged; for(int i = managedFonts.size() - 1; i >= 0; i--) { managedFonts.get(i).reload(); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/font/FontManager.java
Java
lgpl
2,425
package org.anddev.andengine.opengl.texture; import javax.microedition.khronos.opengles.GL10; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 13:00:09 - 05.04.2010 */ public class TextureOptions { // =========================================================== // Constants // =========================================================== public static final TextureOptions NEAREST = new TextureOptions(GL10.GL_NEAREST, GL10.GL_NEAREST, GL10.GL_CLAMP_TO_EDGE, GL10.GL_CLAMP_TO_EDGE, false); public static final TextureOptions BILINEAR = new TextureOptions(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_CLAMP_TO_EDGE, GL10.GL_CLAMP_TO_EDGE, false); public static final TextureOptions REPEATING_NEAREST = new TextureOptions(GL10.GL_NEAREST, GL10.GL_NEAREST, GL10.GL_REPEAT, GL10.GL_REPEAT, false); public static final TextureOptions REPEATING_BILINEAR = new TextureOptions(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_REPEAT, GL10.GL_REPEAT, false); public static final TextureOptions NEAREST_PREMULTIPLYALPHA = new TextureOptions(GL10.GL_NEAREST, GL10.GL_NEAREST, GL10.GL_CLAMP_TO_EDGE, GL10.GL_CLAMP_TO_EDGE, true); public static final TextureOptions BILINEAR_PREMULTIPLYALPHA = new TextureOptions(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_CLAMP_TO_EDGE, GL10.GL_CLAMP_TO_EDGE, true); public static final TextureOptions REPEATING_NEAREST_PREMULTIPLYALPHA = new TextureOptions(GL10.GL_NEAREST, GL10.GL_NEAREST, GL10.GL_REPEAT, GL10.GL_REPEAT, true); public static final TextureOptions REPEATING_BILINEAR_PREMULTIPLYALPHA = new TextureOptions(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_REPEAT, GL10.GL_REPEAT, true); public static final TextureOptions DEFAULT = TextureOptions.NEAREST_PREMULTIPLYALPHA; // =========================================================== // Fields // =========================================================== public final int mMagFilter; public final int mMinFilter; public final float mWrapT; public final float mWrapS; public final boolean mPreMultipyAlpha; // =========================================================== // Constructors // =========================================================== public TextureOptions(final int pMinFilter, final int pMagFilter, final int pWrapT, final int pWrapS, final boolean pPreMultiplyAlpha) { this.mMinFilter = pMinFilter; this.mMagFilter = pMagFilter; this.mWrapT = pWrapT; this.mWrapS = pWrapS; this.mPreMultipyAlpha = pPreMultiplyAlpha; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public void apply(final GL10 pGL) { pGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, this.mMinFilter); pGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, this.mMagFilter); pGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, this.mWrapS); pGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, this.mWrapT); pGL.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/TextureOptions.java
Java
lgpl
3,650
package org.anddev.andengine.opengl.texture.compressed.pvr; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.Texture; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.util.ArrayUtils; import org.anddev.andengine.util.ByteBufferOutputStream; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.MathUtils; import org.anddev.andengine.util.StreamUtils; import org.anddev.andengine.util.constants.DataConstants; /** * [16:32:42] Ricardo Quesada: "quick tip for PVR + NPOT + RGBA4444 textures: Don't forget to pack the bytes: glPixelStorei(GL_UNPACK_ALIGNMENT,1);" * * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 16:18:10 - 13.07.2011 * @see https://github.com/cocos2d/cocos2d-iphone/blob/develop/cocos2d/CCTexturePVR.m */ public abstract class PVRTexture extends Texture { // =========================================================== // Constants // =========================================================== public static final int FLAG_MIPMAP = (1<<8); // has mip map levels public static final int FLAG_TWIDDLE = (1<<9); // is twiddled public static final int FLAG_BUMPMAP = (1<<10); // has normals encoded for a bump map public static final int FLAG_TILING = (1<<11); // is bordered for tiled pvr public static final int FLAG_CUBEMAP = (1<<12); // is a cubemap/skybox public static final int FLAG_FALSEMIPCOL = (1<<13); // are there false colored MIP levels public static final int FLAG_VOLUME = (1<<14); // is this a volume texture public static final int FLAG_ALPHA = (1<<15); // v2.1 is there transparency info in the texture public static final int FLAG_VERTICALFLIP = (1<<16); // v2.1 is the texture vertically flipped // =========================================================== // Fields // =========================================================== private final PVRTextureHeader mPVRTextureHeader; // =========================================================== // Constructors // =========================================================== public PVRTexture(final PVRTextureFormat pPVRTextureFormat) throws IllegalArgumentException, IOException { this(pPVRTextureFormat, TextureOptions.DEFAULT, null); } public PVRTexture(final PVRTextureFormat pPVRTextureFormat, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { this(pPVRTextureFormat, TextureOptions.DEFAULT, pTextureStateListener); } public PVRTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions) throws IllegalArgumentException, IOException { this(pPVRTextureFormat, pTextureOptions, null); } public PVRTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { super(pPVRTextureFormat.getPixelFormat(), pTextureOptions, pTextureStateListener); InputStream inputStream = null; try { inputStream = this.getInputStream(); this.mPVRTextureHeader = new PVRTextureHeader(StreamUtils.streamToBytes(inputStream, PVRTextureHeader.SIZE)); } finally { StreamUtils.close(inputStream); } if(!MathUtils.isPowerOfTwo(this.getWidth()) || !MathUtils.isPowerOfTwo(this.getHeight())) { // TODO GLHelper.EXTENSIONS_NON_POWER_OF_TWO throw new IllegalArgumentException("mWidth and mHeight must be a power of 2!"); } if(this.mPVRTextureHeader.getPVRTextureFormat().getPixelFormat() != pPVRTextureFormat.getPixelFormat()) { throw new IllegalArgumentException("Other PVRTextureFormat: '" + this.mPVRTextureHeader.getPVRTextureFormat().getPixelFormat() + "' found than expected: '" + pPVRTextureFormat.getPixelFormat() + "'."); } if(this.mPVRTextureHeader.getPVRTextureFormat().isCompressed()) { // TODO && ! GLHELPER_EXTENSION_PVRTC] ) { throw new IllegalArgumentException("Invalid PVRTextureFormat: '" + this.mPVRTextureHeader.getPVRTextureFormat() + "'."); } this.mUpdateOnHardwareNeeded = true; } // =========================================================== // Getter & Setter // =========================================================== @Override public int getWidth() { return this.mPVRTextureHeader.getWidth(); } @Override public int getHeight() { return this.mPVRTextureHeader.getHeight(); } public PVRTextureHeader getPVRTextureHeader() { return this.mPVRTextureHeader; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract InputStream onGetInputStream() throws IOException; protected InputStream getInputStream() throws IOException { return this.onGetInputStream(); } @Override protected void generateHardwareTextureID(final GL10 pGL) { // // TODO // if(this.mMipMapCount > 0) { pGL.glPixelStorei(GL10.GL_UNPACK_ALIGNMENT, 1); // } super.generateHardwareTextureID(pGL); } @Override protected void writeTextureToHardware(final GL10 pGL) throws IOException { final ByteBuffer pvrDataBuffer = this.getPVRDataBuffer(); int width = this.getWidth(); int height = this.getHeight(); final int dataLength = this.mPVRTextureHeader.getDataLength(); final int glFormat = this.mPixelFormat.getGLFormat(); final int glType = this.mPixelFormat.getGLType(); final int bytesPerPixel = this.mPVRTextureHeader.getBitsPerPixel() / DataConstants.BITS_PER_BYTE; /* Calculate the data size for each texture level and respect the minimum number of blocks. */ int mipmapLevel = 0; int currentPixelDataOffset = 0; while (currentPixelDataOffset < dataLength) { final int currentPixelDataSize = width * height * bytesPerPixel; if (mipmapLevel > 0 && (width != height || MathUtils.nextPowerOfTwo(width) != width)) { Debug.w(String.format("Mipmap level '%u' is not squared. Width: '%u', height: '%u'. Texture won't render correctly.", mipmapLevel, width, height)); } pvrDataBuffer.position(PVRTextureHeader.SIZE + currentPixelDataOffset); pvrDataBuffer.limit(PVRTextureHeader.SIZE + currentPixelDataOffset + currentPixelDataSize); ByteBuffer pixelBuffer = pvrDataBuffer.slice(); pGL.glTexImage2D(GL10.GL_TEXTURE_2D, mipmapLevel, glFormat, width, height, 0, glFormat, glType, pixelBuffer); currentPixelDataOffset += currentPixelDataSize; /* Prepare next mipmap level. */ width = Math.max(width >> 1, 1); height = Math.max(height >> 1, 1); mipmapLevel++; } } // =========================================================== // Methods // =========================================================== protected ByteBuffer getPVRDataBuffer() throws IOException { final InputStream inputStream = this.getInputStream(); try { final ByteBufferOutputStream os = new ByteBufferOutputStream(DataConstants.BYTES_PER_KILOBYTE, DataConstants.BYTES_PER_MEGABYTE / 2); StreamUtils.copy(inputStream, os); return os.toByteBuffer(); } finally { StreamUtils.close(inputStream); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class PVRTextureHeader { // =========================================================== // Constants // =========================================================== public static final byte[] MAGIC_IDENTIFIER = { (byte)'P', (byte)'V', (byte)'R', (byte)'!' }; public static final int SIZE = 13 * DataConstants.BYTES_PER_INT; private static final int FORMAT_FLAG_MASK = 0x0FF; // =========================================================== // Fields // =========================================================== private final ByteBuffer mDataByteBuffer; private final PVRTextureFormat mPVRTextureFormat; // =========================================================== // Constructors // =========================================================== public PVRTextureHeader(final byte[] pData) { this.mDataByteBuffer = ByteBuffer.wrap(pData); this.mDataByteBuffer.rewind(); this.mDataByteBuffer.order(ByteOrder.LITTLE_ENDIAN); /* Check magic bytes. */ if(!ArrayUtils.equals(pData, 11 * DataConstants.BYTES_PER_INT, PVRTextureHeader.MAGIC_IDENTIFIER, 0, PVRTextureHeader.MAGIC_IDENTIFIER.length)) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mPVRTextureFormat = PVRTextureFormat.fromID(this.getFlags() & PVRTextureHeader.FORMAT_FLAG_MASK); } // =========================================================== // Getter & Setter // =========================================================== public PVRTextureFormat getPVRTextureFormat() { return this.mPVRTextureFormat; } public int headerLength() { return this.mDataByteBuffer.getInt(0 * DataConstants.BYTES_PER_INT); // TODO Constants } public int getHeight() { return this.mDataByteBuffer.getInt(1 * DataConstants.BYTES_PER_INT); } public int getWidth() { return this.mDataByteBuffer.getInt(2 * DataConstants.BYTES_PER_INT); } public int getNumMipmaps() { return this.mDataByteBuffer.getInt(3 * DataConstants.BYTES_PER_INT); } public int getFlags() { return this.mDataByteBuffer.getInt(4 * DataConstants.BYTES_PER_INT); } public int getDataLength() { return this.mDataByteBuffer.getInt(5 * DataConstants.BYTES_PER_INT); } public int getBitsPerPixel() { return this.mDataByteBuffer.getInt(6 * DataConstants.BYTES_PER_INT); } public int getBitmaskRed() { return this.mDataByteBuffer.getInt(7 * DataConstants.BYTES_PER_INT); } public int getBitmaskGreen() { return this.mDataByteBuffer.getInt(8 * DataConstants.BYTES_PER_INT); } public int getBitmaskBlue() { return this.mDataByteBuffer.getInt(9 * DataConstants.BYTES_PER_INT); } public int getBitmaskAlpha() { return this.mDataByteBuffer.getInt(10 * DataConstants.BYTES_PER_INT); } public boolean hasAlpha() { return this.getBitmaskAlpha() != 0; } public int getPVRTag() { return this.mDataByteBuffer.getInt(11 * DataConstants.BYTES_PER_INT); } public int numSurfs() { return this.mDataByteBuffer.getInt(12 * DataConstants.BYTES_PER_INT); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } public static enum PVRTextureFormat { // =========================================================== // Elements // =========================================================== RGBA_4444(0x10, false, PixelFormat.RGBA_4444), RGBA_5551(0x11, false, PixelFormat.RGBA_5551), RGBA_8888(0x12, false, PixelFormat.RGBA_8888), RGB_565(0x13, false, PixelFormat.RGB_565), // RGB_555( 0x14, ...), // RGB_888( 0x15, ...), I_8(0x16, false, PixelFormat.I_8), AI_88(0x17, false, PixelFormat.AI_88), // PVRTC_2(0x18, GL10.GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, true, TextureFormat.???), // PVRTC_4(0x19, GL10.GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, true, TextureFormat.???), // BGRA_8888(0x1A, GL10.GL_RGBA, TextureFormat.???), A_8(0x1B, false, PixelFormat.A_8); // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mID; private final boolean mCompressed; private final PixelFormat mPixelFormat; // =========================================================== // Constructors // =========================================================== private PVRTextureFormat(final int pID, final boolean pCompressed, final PixelFormat pPixelFormat) { this.mID = pID; this.mCompressed = pCompressed; this.mPixelFormat = pPixelFormat; } public static PVRTextureFormat fromID(final int pID) { final PVRTextureFormat[] pvrTextureFormats = PVRTextureFormat.values(); final int pvrTextureFormatCount = pvrTextureFormats.length; for(int i = 0; i < pvrTextureFormatCount; i++) { final PVRTextureFormat pvrTextureFormat = pvrTextureFormats[i]; if(pvrTextureFormat.mID == pID) { return pvrTextureFormat; } } throw new IllegalArgumentException("Unexpected " + PVRTextureFormat.class.getSimpleName() + "-ID: '" + pID + "'."); } public static PVRTextureFormat fromPixelFormat(final PixelFormat pPixelFormat) throws IllegalArgumentException { switch(pPixelFormat) { case RGBA_8888: return PVRTextureFormat.RGBA_8888; case RGBA_4444: return PVRTextureFormat.RGBA_4444; case RGB_565: return PVRTextureFormat.RGB_565; default: throw new IllegalArgumentException("Unsupported " + PixelFormat.class.getName() + ": '" + pPixelFormat + "'."); } } // =========================================================== // Getter & Setter // =========================================================== public int getID() { return this.mID; } public boolean isCompressed() { return this.mCompressed; } public PixelFormat getPixelFormat() { return this.mPixelFormat; } // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/compressed/pvr/PVRTexture.java
Java
lgpl
14,714
package org.anddev.andengine.opengl.texture.compressed.pvr; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.util.ArrayUtils; import org.anddev.andengine.util.StreamUtils; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:17:23 - 27.07.2011 */ public abstract class PVRCCZTexture extends PVRTexture { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private CCZHeader mCCZHeader; // =========================================================== // Constructors // =========================================================== public PVRCCZTexture(final PVRTextureFormat pPVRTextureFormat) throws IllegalArgumentException, IOException { super(pPVRTextureFormat); } public PVRCCZTexture(final PVRTextureFormat pPVRTextureFormat, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureStateListener); } public PVRCCZTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureOptions); } public PVRCCZTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureOptions, pTextureStateListener); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected final InputStream getInputStream() throws IOException { final InputStream inputStream = this.onGetInputStream(); this.mCCZHeader = new CCZHeader(StreamUtils.streamToBytes(inputStream, CCZHeader.SIZE)); return this.mCCZHeader.getCCZCompressionFormat().wrap(inputStream); } @Override protected ByteBuffer getPVRDataBuffer() throws IOException { final InputStream inputStream = this.getInputStream(); try { final byte[] data = new byte[this.mCCZHeader.getUncompressedSize()]; StreamUtils.copy(inputStream, data); return ByteBuffer.wrap(data); } finally { StreamUtils.close(inputStream); } } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class CCZHeader { // =========================================================== // Constants // =========================================================== public static final byte[] MAGIC_IDENTIFIER = { (byte)'C', (byte)'C', (byte)'Z', (byte)'!' }; public static final int SIZE = 16; // =========================================================== // Fields // =========================================================== private final ByteBuffer mDataByteBuffer; private final CCZCompressionFormat mCCZCompressionFormat; // =========================================================== // Constructors // =========================================================== public CCZHeader(final byte[] pData) { this.mDataByteBuffer = ByteBuffer.wrap(pData); this.mDataByteBuffer.rewind(); this.mDataByteBuffer.order(ByteOrder.BIG_ENDIAN); /* Check magic bytes. */ if(!ArrayUtils.equals(pData, 0, CCZHeader.MAGIC_IDENTIFIER, 0, CCZHeader.MAGIC_IDENTIFIER.length)) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } // TODO Check the version? this.mCCZCompressionFormat = CCZCompressionFormat.fromID(this.getCCZCompressionFormatID()); } // =========================================================== // Getter & Setter // =========================================================== private short getCCZCompressionFormatID() { return this.mDataByteBuffer.getShort(4); } public CCZCompressionFormat getCCZCompressionFormat() { return this.mCCZCompressionFormat; } public short getVersion() { return this.mDataByteBuffer.getShort(6); } public int getUserdata() { return this.mDataByteBuffer.getInt(8); } public int getUncompressedSize() { return this.mDataByteBuffer.getInt(12); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } public static enum CCZCompressionFormat { // =========================================================== // Elements // =========================================================== ZLIB((short)0), BZIP2((short)1), GZIP((short)2), NONE((short)3); // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final short mID; // =========================================================== // Constructors // =========================================================== private CCZCompressionFormat(final short pID) { this.mID = pID; } public InputStream wrap(final InputStream pInputStream) throws IOException { switch(this) { case GZIP: return new GZIPInputStream(pInputStream); case ZLIB: return new InflaterInputStream(pInputStream, new Inflater()); case NONE: case BZIP2: default: throw new IllegalArgumentException("Unexpected " + CCZCompressionFormat.class.getSimpleName() + ": '" + this + "'."); } } public static CCZCompressionFormat fromID(final short pID) { final CCZCompressionFormat[] cczCompressionFormats = CCZCompressionFormat.values(); final int cczCompressionFormatCount = cczCompressionFormats.length; for(int i = 0; i < cczCompressionFormatCount; i++) { final CCZCompressionFormat cczCompressionFormat = cczCompressionFormats[i]; if(cczCompressionFormat.mID == pID) { return cczCompressionFormat; } } throw new IllegalArgumentException("Unexpected " + CCZCompressionFormat.class.getSimpleName() + "-ID: '" + pID + "'."); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/compressed/pvr/PVRCCZTexture.java
Java
lgpl
8,179
package org.anddev.andengine.opengl.texture.compressed.pvr; import java.io.IOException; import java.util.zip.GZIPInputStream; import org.anddev.andengine.opengl.texture.TextureOptions; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:31:23 - 15.07.2011 */ public abstract class PVRGZTexture extends PVRTexture { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public PVRGZTexture(final PVRTextureFormat pPVRTextureFormat) throws IllegalArgumentException, IOException { super(pPVRTextureFormat); } public PVRGZTexture(final PVRTextureFormat pPVRTextureFormat, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureStateListener); } public PVRGZTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureOptions); } public PVRGZTexture(final PVRTextureFormat pPVRTextureFormat, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException, IOException { super(pPVRTextureFormat, pTextureOptions, pTextureStateListener); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected GZIPInputStream getInputStream() throws IOException { return new GZIPInputStream(this.onGetInputStream()); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/compressed/pvr/PVRGZTexture.java
Java
lgpl
2,445
package org.anddev.andengine.opengl.texture.compressed.etc1; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.Texture; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.util.StreamUtils; import android.opengl.ETC1; import android.opengl.ETC1Util; /** * TODO if(!SystemUtils.isAndroidVersionOrHigher(Build.VERSION_CODES.FROYO)) --> Meaningful Exception! * * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:32:01 - 13.07.2011 */ public abstract class ETC1Texture extends Texture { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private ETC1TextureHeader mETC1TextureHeader; // =========================================================== // Constructors // =========================================================== public ETC1Texture() throws IOException { this(TextureOptions.DEFAULT, null); } public ETC1Texture(final ITextureStateListener pTextureStateListener) throws IOException { this(TextureOptions.DEFAULT, pTextureStateListener); } public ETC1Texture(final TextureOptions pTextureOptions) throws IOException { this(pTextureOptions, null); } public ETC1Texture(final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IOException { super(PixelFormat.RGB_565, pTextureOptions, pTextureStateListener); InputStream inputStream = null; try { inputStream = this.getInputStream(); this.mETC1TextureHeader = new ETC1TextureHeader(StreamUtils.streamToBytes(inputStream, ETC1.ETC_PKM_HEADER_SIZE)); } finally { StreamUtils.close(inputStream); } } // =========================================================== // Getter & Setter // =========================================================== @Override public int getWidth() { return this.mETC1TextureHeader.getWidth(); } @Override public int getHeight() { return this.mETC1TextureHeader.getHeight(); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract InputStream getInputStream() throws IOException; @Override protected void writeTextureToHardware(final GL10 pGL) throws IOException { final InputStream inputStream = this.getInputStream(); ETC1Util.loadTexture(GL10.GL_TEXTURE_2D, 0, 0, this.mPixelFormat.getGLFormat(), this.mPixelFormat.getGLType(), inputStream); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class ETC1TextureHeader { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final ByteBuffer mDataByteBuffer; private final int mWidth; private final int mHeight; // =========================================================== // Constructors // =========================================================== public ETC1TextureHeader(final byte[] pData) { if(pData.length != ETC1.ETC_PKM_HEADER_SIZE) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mDataByteBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE).order(ByteOrder.nativeOrder()); this.mDataByteBuffer.put(pData, 0, ETC1.ETC_PKM_HEADER_SIZE); this.mDataByteBuffer.position(0); if (!ETC1.isValid(this.mDataByteBuffer)) { throw new IllegalArgumentException("Invalid " + this.getClass().getSimpleName() + "!"); } this.mWidth = ETC1.getWidth(this.mDataByteBuffer); this.mHeight = ETC1.getHeight(this.mDataByteBuffer); } // =========================================================== // Getter & Setter // =========================================================== public int getWidth() { return this.mWidth; } public int getHeight() { return this.mHeight; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/compressed/etc1/ETC1Texture.java
Java
lgpl
5,309
package org.anddev.andengine.opengl.texture.buffer; import org.anddev.andengine.opengl.buffer.BufferObject; import org.anddev.andengine.opengl.font.Font; import org.anddev.andengine.opengl.font.Letter; import org.anddev.andengine.opengl.util.FastFloatBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:05:56 - 03.04.2010 */ public class TextTextureBuffer extends BufferObject { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TextTextureBuffer(final int pCapacity, final int pDrawType, final boolean pManaged) { super(pCapacity, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void update(final Font pFont, final String[] pLines) { final FastFloatBuffer textureFloatBuffer = this.getFloatBuffer(); textureFloatBuffer.position(0); final Font font = pFont; final String[] lines = pLines; final int lineCount = lines.length; for (int i = 0; i < lineCount; i++) { final String line = lines[i]; final int lineLength = line.length(); for (int j = 0; j < lineLength; j++) { final Letter letter = font.getLetter(line.charAt(j)); final float letterTextureX = letter.mTextureX; final float letterTextureY = letter.mTextureY; final float letterTextureX2 = letterTextureX + letter.mTextureWidth; final float letterTextureY2 = letterTextureY + letter.mTextureHeight; textureFloatBuffer.put(letterTextureX); textureFloatBuffer.put(letterTextureY); textureFloatBuffer.put(letterTextureX); textureFloatBuffer.put(letterTextureY2); textureFloatBuffer.put(letterTextureX2); textureFloatBuffer.put(letterTextureY2); textureFloatBuffer.put(letterTextureX2); textureFloatBuffer.put(letterTextureY2); textureFloatBuffer.put(letterTextureX2); textureFloatBuffer.put(letterTextureY); textureFloatBuffer.put(letterTextureX); textureFloatBuffer.put(letterTextureY); } } textureFloatBuffer.position(0); this.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/buffer/TextTextureBuffer.java
Java
lgpl
3,093
package org.anddev.andengine.opengl.texture; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.util.Debug; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 17:48:46 - 08.03.2010 */ public class TextureManager { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final HashSet<ITexture> mTexturesManaged = new HashSet<ITexture>(); private final ArrayList<ITexture> mTexturesLoaded = new ArrayList<ITexture>(); private final ArrayList<ITexture> mTexturesToBeLoaded = new ArrayList<ITexture>(); private final ArrayList<ITexture> mTexturesToBeUnloaded = new ArrayList<ITexture>(); // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== protected synchronized void clear() { this.mTexturesToBeLoaded.clear(); this.mTexturesLoaded.clear(); this.mTexturesManaged.clear(); } /** * @param pTexture the {@link ITexture} to be loaded before the very next frame is drawn (Or prevent it from being unloaded then). * @return <code>true</code> when the {@link ITexture} was previously not managed by this {@link TextureManager}, <code>false</code> if it was already managed. */ public synchronized boolean loadTexture(final ITexture pTexture) { if(this.mTexturesManaged.contains(pTexture)) { /* Just make sure it doesn't get deleted. */ this.mTexturesToBeUnloaded.remove(pTexture); return false; } else { this.mTexturesManaged.add(pTexture); this.mTexturesToBeLoaded.add(pTexture); return true; } } /** * @param pTexture the {@link ITexture} to be unloaded before the very next frame is drawn (Or prevent it from being loaded then). * @return <code>true</code> when the {@link ITexture} was already managed by this {@link TextureManager}, <code>false</code> if it was not managed. */ public synchronized boolean unloadTexture(final ITexture pTexture) { if(this.mTexturesManaged.contains(pTexture)) { /* If the Texture is loaded, unload it. * If the Texture is about to be loaded, stop it from being loaded. */ if(this.mTexturesLoaded.contains(pTexture)){ this.mTexturesToBeUnloaded.add(pTexture); } else if(this.mTexturesToBeLoaded.remove(pTexture)){ this.mTexturesManaged.remove(pTexture); } return true; } else { return false; } } public void loadTextures(final ITexture ... pTextures) { for(int i = pTextures.length - 1; i >= 0; i--) { this.loadTexture(pTextures[i]); } } public void unloadTextures(final ITexture ... pTextures) { for(int i = pTextures.length - 1; i >= 0; i--) { this.unloadTexture(pTextures[i]); } } public synchronized void reloadTextures() { final HashSet<ITexture> managedTextures = this.mTexturesManaged; for(final ITexture texture : managedTextures) { // TODO Can the use of the iterator be avoided somehow? texture.setLoadedToHardware(false); } this.mTexturesToBeLoaded.addAll(this.mTexturesLoaded); // TODO Check if addAll uses iterator internally! this.mTexturesLoaded.clear(); this.mTexturesManaged.removeAll(this.mTexturesToBeUnloaded); // TODO Check if removeAll uses iterator internally! this.mTexturesToBeUnloaded.clear(); } public synchronized void updateTextures(final GL10 pGL) { final HashSet<ITexture> texturesManaged = this.mTexturesManaged; final ArrayList<ITexture> texturesLoaded = this.mTexturesLoaded; final ArrayList<ITexture> texturesToBeLoaded = this.mTexturesToBeLoaded; final ArrayList<ITexture> texturesToBeUnloaded = this.mTexturesToBeUnloaded; /* First reload Textures that need to be updated. */ final int textursLoadedCount = texturesLoaded.size(); if(textursLoadedCount > 0){ for(int i = textursLoadedCount - 1; i >= 0; i--){ final ITexture textureToBeReloaded = texturesLoaded.get(i); if(textureToBeReloaded.isUpdateOnHardwareNeeded()){ try { textureToBeReloaded.reloadToHardware(pGL); } catch(IOException e) { Debug.e(e); } } } } /* Then load pending Textures. */ final int texturesToBeLoadedCount = texturesToBeLoaded.size(); if(texturesToBeLoadedCount > 0){ for(int i = texturesToBeLoadedCount - 1; i >= 0; i--){ final ITexture textureToBeLoaded = texturesToBeLoaded.remove(i); if(!textureToBeLoaded.isLoadedToHardware()){ try { textureToBeLoaded.loadToHardware(pGL); } catch(IOException e) { Debug.e(e); } } texturesLoaded.add(textureToBeLoaded); } } /* Then unload pending Textures. */ final int texturesToBeUnloadedCount = texturesToBeUnloaded.size(); if(texturesToBeUnloadedCount > 0){ for(int i = texturesToBeUnloadedCount - 1; i >= 0; i--){ final ITexture textureToBeUnloaded = texturesToBeUnloaded.remove(i); if(textureToBeUnloaded.isLoadedToHardware()){ textureToBeUnloaded.unloadFromHardware(pGL); } texturesLoaded.remove(textureToBeUnloaded); texturesManaged.remove(textureToBeUnloaded); } } /* Finally invoke the GC if anything has changed. */ if(texturesToBeLoadedCount > 0 || texturesToBeUnloadedCount > 0){ System.gc(); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/TextureManager.java
Java
lgpl
6,294
package org.anddev.andengine.opengl.texture.region.buffer; import org.anddev.andengine.opengl.buffer.BufferObject; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.BaseTextureRegion; import org.anddev.andengine.opengl.util.FastFloatBuffer; import org.anddev.andengine.opengl.vertex.SpriteBatchVertexBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:32:14 - 14.06.2011 */ public class SpriteBatchTextureRegionBuffer extends BufferObject { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected int mIndex; // =========================================================== // Constructors // =========================================================== public SpriteBatchTextureRegionBuffer(final int pCapacity, final int pDrawType, final boolean pManaged) { super(pCapacity * 2 * SpriteBatchVertexBuffer.VERTICES_PER_RECTANGLE, pDrawType, pManaged); } // =========================================================== // Getter & Setter // =========================================================== public int getIndex() { return this.mIndex; } public void setIndex(final int pIndex) { this.mIndex = pIndex; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public void add(final BaseTextureRegion pTextureRegion) { final ITexture texture = pTextureRegion.getTexture(); if(texture == null) { // TODO Check really needed? return; } final int x1 = Float.floatToRawIntBits(pTextureRegion.getTextureCoordinateX1()); final int y1 = Float.floatToRawIntBits(pTextureRegion.getTextureCoordinateY1()); final int x2 = Float.floatToRawIntBits(pTextureRegion.getTextureCoordinateX2()); final int y2 = Float.floatToRawIntBits(pTextureRegion.getTextureCoordinateY2()); final int[] bufferData = this.mBufferData; int index = this.mIndex; bufferData[index++] = x1; bufferData[index++] = y1; bufferData[index++] = x1; bufferData[index++] = y2; bufferData[index++] = x2; bufferData[index++] = y1; bufferData[index++] = x2; bufferData[index++] = y1; bufferData[index++] = x1; bufferData[index++] = y2; bufferData[index++] = x2; bufferData[index++] = y2; this.mIndex = index; } public void submit() { final FastFloatBuffer buffer = this.mFloatBuffer; buffer.position(0); buffer.put(this.mBufferData); buffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/buffer/SpriteBatchTextureRegionBuffer.java
Java
lgpl
3,215
package org.anddev.andengine.opengl.texture.region.buffer; import static org.anddev.andengine.opengl.vertex.RectangleVertexBuffer.VERTICES_PER_RECTANGLE; import org.anddev.andengine.opengl.buffer.BufferObject; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.BaseTextureRegion; import org.anddev.andengine.opengl.util.FastFloatBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:05:50 - 09.03.2010 */ public class TextureRegionBuffer extends BufferObject { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final BaseTextureRegion mTextureRegion; private boolean mFlippedVertical; private boolean mFlippedHorizontal; // =========================================================== // Constructors // =========================================================== public TextureRegionBuffer(final BaseTextureRegion pBaseTextureRegion, final int pDrawType, final boolean pManaged) { super(2 * VERTICES_PER_RECTANGLE, pDrawType, pManaged); this.mTextureRegion = pBaseTextureRegion; } // =========================================================== // Getter & Setter // =========================================================== public BaseTextureRegion getTextureRegion() { return this.mTextureRegion; } public boolean isFlippedHorizontal() { return this.mFlippedHorizontal; } public void setFlippedHorizontal(final boolean pFlippedHorizontal) { if(this.mFlippedHorizontal != pFlippedHorizontal) { this.mFlippedHorizontal = pFlippedHorizontal; this.update(); } } public boolean isFlippedVertical() { return this.mFlippedVertical; } public void setFlippedVertical(final boolean pFlippedVertical) { if(this.mFlippedVertical != pFlippedVertical) { this.mFlippedVertical = pFlippedVertical; this.update(); } } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public synchronized void update() { final BaseTextureRegion textureRegion = this.mTextureRegion; final int x1 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateX1()); final int y1 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateY1()); final int x2 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateX2()); final int y2 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateY2()); final int[] bufferData = this.mBufferData; if(this.mFlippedVertical) { if(this.mFlippedHorizontal) { bufferData[0] = x2; bufferData[1] = y2; bufferData[2] = x2; bufferData[3] = y1; bufferData[4] = x1; bufferData[5] = y2; bufferData[6] = x1; bufferData[7] = y1; } else { bufferData[0] = x1; bufferData[1] = y2; bufferData[2] = x1; bufferData[3] = y1; bufferData[4] = x2; bufferData[5] = y2; bufferData[6] = x2; bufferData[7] = y1; } } else { if(this.mFlippedHorizontal) { bufferData[0] = x2; bufferData[1] = y1; bufferData[2] = x2; bufferData[3] = y2; bufferData[4] = x1; bufferData[5] = y1; bufferData[6] = x1; bufferData[7] = y2; } else { bufferData[0] = x1; bufferData[1] = y1; bufferData[2] = x1; bufferData[3] = y2; bufferData[4] = x2; bufferData[5] = y1; bufferData[6] = x2; bufferData[7] = y2; } } final FastFloatBuffer buffer = this.mFloatBuffer; buffer.position(0); buffer.put(bufferData); buffer.position(0); super.setHardwareBufferNeedsUpdate(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/buffer/TextureRegionBuffer.java
Java
lgpl
4,315
package org.anddev.andengine.opengl.texture.region; import org.anddev.andengine.util.Library; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:52:26 - 20.08.2010 */ public class TextureRegionLibrary extends Library<BaseTextureRegion> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TextureRegionLibrary(final int pInitialCapacity) { super(pInitialCapacity); } // =========================================================== // Getter & Setter // =========================================================== @Override public TextureRegion get(final int pID) { return (TextureRegion) super.get(pID); } public TiledTextureRegion getTiled(final int pID) { return (TiledTextureRegion) this.mItems.get(pID); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/TextureRegionLibrary.java
Java
lgpl
1,695
package org.anddev.andengine.opengl.texture.region; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:15:14 - 09.03.2010 */ public class TextureRegionFactory { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static TextureRegion extractFromTexture(final ITexture pTexture, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight, final boolean pTextureRegionBufferManaged) { final TextureRegion textureRegion = new TextureRegion(pTexture, pTexturePositionX, pTexturePositionY, pWidth, pHeight); textureRegion.setTextureRegionBufferManaged(pTextureRegionBufferManaged); return textureRegion; } public static <T extends ITextureAtlasSource> TextureRegion createFromSource(final ITextureAtlas<T> pTextureAtlas, final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY, final boolean pCreateTextureRegionBuffersManaged) { final TextureRegion textureRegion = new TextureRegion(pTextureAtlas, pTexturePositionX, pTexturePositionY, pTextureAtlasSource.getWidth(), pTextureAtlasSource.getHeight()); pTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, textureRegion.getTexturePositionX(), textureRegion.getTexturePositionY()); textureRegion.setTextureRegionBufferManaged(pCreateTextureRegionBuffersManaged); return textureRegion; } public static <T extends ITextureAtlasSource> TiledTextureRegion createTiledFromSource(final ITextureAtlas<T> pTextureAtlas, final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows, final boolean pCreateTextureRegionBuffersManaged) { final TiledTextureRegion tiledTextureRegion = new TiledTextureRegion(pTextureAtlas, pTexturePositionX, pTexturePositionY, pTextureAtlasSource.getWidth(), pTextureAtlasSource.getHeight(), pTileColumns, pTileRows); pTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, tiledTextureRegion.getTexturePositionX(), tiledTextureRegion.getTexturePositionY()); tiledTextureRegion.setTextureRegionBufferManaged(pCreateTextureRegionBuffersManaged); return tiledTextureRegion; } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/TextureRegionFactory.java
Java
lgpl
3,427
package org.anddev.andengine.opengl.texture.region; import org.anddev.andengine.opengl.texture.ITexture; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:14:42 - 09.03.2010 */ public class TiledTextureRegion extends BaseTextureRegion { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mTileColumns; private final int mTileRows; private int mCurrentTileColumn; private int mCurrentTileRow; private final int mTileCount; // =========================================================== // Constructors // =========================================================== public TiledTextureRegion(final ITexture pTexture, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight, final int pTileColumns, final int pTileRows) { super(pTexture, pTexturePositionX, pTexturePositionY, pWidth, pHeight); this.mTileColumns = pTileColumns; this.mTileRows = pTileRows; this.mTileCount = this.mTileColumns * this.mTileRows; this.mCurrentTileColumn = 0; this.mCurrentTileRow = 0; this.initTextureBuffer(); } @Override protected void initTextureBuffer() { if(this.mTileRows != 0 && this.mTileColumns != 0) { super.initTextureBuffer(); } } // =========================================================== // Getter & Setter // =========================================================== public int getTileCount() { return this.mTileCount; } public int getTileWidth() { return super.getWidth() / this.mTileColumns; } public int getTileHeight() { return super.getHeight() / this.mTileRows; } public int getCurrentTileColumn() { return this.mCurrentTileColumn; } public int getCurrentTileRow() { return this.mCurrentTileRow; } public int getCurrentTileIndex() { return this.mCurrentTileRow * this.mTileColumns + this.mCurrentTileColumn; } public void setCurrentTileIndex(final int pTileColumn, final int pTileRow) { if(pTileColumn != this.mCurrentTileColumn || pTileRow != this.mCurrentTileRow) { this.mCurrentTileColumn = pTileColumn; this.mCurrentTileRow = pTileRow; super.updateTextureRegionBuffer(); } } public void setCurrentTileIndex(final int pTileIndex) { if(pTileIndex < this.mTileCount) { final int tileColumns = this.mTileColumns; this.setCurrentTileIndex(pTileIndex % tileColumns, pTileIndex / tileColumns); } } public int getTexturePositionOfCurrentTileX() { return super.getTexturePositionX() + this.mCurrentTileColumn * this.getTileWidth(); } public int getTexturePositionOfCurrentTileY() { return super.getTexturePositionY() + this.mCurrentTileRow * this.getTileHeight(); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public TiledTextureRegion deepCopy() { final TiledTextureRegion deepCopy = new TiledTextureRegion(this.mTexture, this.getTexturePositionX(), this.getTexturePositionY(), this.getWidth(), this.getHeight(), this.mTileColumns, this.mTileRows); deepCopy.setCurrentTileIndex(this.mCurrentTileColumn, this.mCurrentTileRow); return deepCopy; } @Override public float getTextureCoordinateX1() { return (float)this.getTexturePositionOfCurrentTileX() / this.mTexture.getWidth(); } @Override public float getTextureCoordinateY1() { return (float)this.getTexturePositionOfCurrentTileY() / this.mTexture.getHeight(); } @Override public float getTextureCoordinateX2() { return (float)(this.getTexturePositionOfCurrentTileX() + this.getTileWidth()) / this.mTexture.getWidth(); } @Override public float getTextureCoordinateY2() { return (float)(this.getTexturePositionOfCurrentTileY() + this.getTileHeight()) / this.mTexture.getHeight(); } // =========================================================== // Methods // =========================================================== public void nextTile() { final int tileIndex = (this.getCurrentTileIndex() + 1) % this.getTileCount(); this.setCurrentTileIndex(tileIndex); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/TiledTextureRegion.java
Java
lgpl
4,655
package org.anddev.andengine.opengl.texture.region; import org.anddev.andengine.opengl.texture.ITexture; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:29:59 - 08.03.2010 */ public class TextureRegion extends BaseTextureRegion { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TextureRegion(final ITexture pTexture, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexture, pTexturePositionX, pTexturePositionY, pWidth, pHeight); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public TextureRegion deepCopy() { return new TextureRegion(this.mTexture, this.mTexturePositionX, this.mTexturePositionY, this.mWidth, this.mHeight); } @Override public float getTextureCoordinateX1() { return (float) this.mTexturePositionX / this.mTexture.getWidth(); } @Override public float getTextureCoordinateY1() { return (float) this.mTexturePositionY / this.mTexture.getHeight(); } @Override public float getTextureCoordinateX2() { return (float) (this.mTexturePositionX + this.mWidth) / this.mTexture.getWidth(); } @Override public float getTextureCoordinateY2() { return (float) (this.mTexturePositionY + this.mHeight) / this.mTexture.getHeight(); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/TextureRegion.java
Java
lgpl
2,330
package org.anddev.andengine.opengl.texture.region; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import org.anddev.andengine.opengl.buffer.BufferObjectManager; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.buffer.TextureRegionBuffer; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.util.modifier.IModifier.DeepCopyNotSupportedException; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:29:59 - 08.03.2010 */ public abstract class BaseTextureRegion { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final ITexture mTexture; protected final TextureRegionBuffer mTextureRegionBuffer; protected int mWidth; protected int mHeight; protected int mTexturePositionX; protected int mTexturePositionY; // =========================================================== // Constructors // =========================================================== public BaseTextureRegion(final ITexture pTexture, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { this.mTexture = pTexture; this.mTexturePositionX = pTexturePositionX; this.mTexturePositionY = pTexturePositionY; this.mWidth = pWidth; this.mHeight = pHeight; this.mTextureRegionBuffer = new TextureRegionBuffer(this, GL11.GL_STATIC_DRAW, true); this.initTextureBuffer(); } protected void initTextureBuffer() { this.updateTextureRegionBuffer(); } protected abstract BaseTextureRegion deepCopy() throws DeepCopyNotSupportedException; // =========================================================== // Getter & Setter // =========================================================== public int getWidth() { return this.mWidth; } public int getHeight() { return this.mHeight; } public void setWidth(final int pWidth) { this.mWidth = pWidth; this.updateTextureRegionBuffer(); } public void setHeight(final int pHeight) { this.mHeight = pHeight; this.updateTextureRegionBuffer(); } public void setTexturePosition(final int pTexturePositionX, final int pTexturePositionY) { this.mTexturePositionX = pTexturePositionX; this.mTexturePositionY = pTexturePositionY; this.updateTextureRegionBuffer(); } public int getTexturePositionX() { return this.mTexturePositionX; } public int getTexturePositionY() { return this.mTexturePositionY; } public ITexture getTexture() { return this.mTexture; } public TextureRegionBuffer getTextureBuffer() { return this.mTextureRegionBuffer; } public boolean isFlippedHorizontal() { return this.mTextureRegionBuffer.isFlippedHorizontal(); } public void setFlippedHorizontal(final boolean pFlippedHorizontal) { this.mTextureRegionBuffer.setFlippedHorizontal(pFlippedHorizontal); } public boolean isFlippedVertical() { return this.mTextureRegionBuffer.isFlippedVertical(); } public void setFlippedVertical(final boolean pFlippedVertical) { this.mTextureRegionBuffer.setFlippedVertical(pFlippedVertical); } public boolean isTextureRegionBufferManaged() { return this.mTextureRegionBuffer.isManaged(); } /** * @param pVertexBufferManaged when passing <code>true</code> this {@link BaseTextureRegion} will make its {@link TextureRegionBuffer} unload itself from the active {@link BufferObjectManager}, when this {@link BaseTextureRegion} is finalized/garbage-collected.<b><u>WARNING:</u></b> When passing <code>false</code> one needs to take care of that by oneself! */ public void setTextureRegionBufferManaged(final boolean pTextureRegionBufferManaged) { this.mTextureRegionBuffer.setManaged(pTextureRegionBufferManaged); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== public abstract float getTextureCoordinateX1(); public abstract float getTextureCoordinateY1(); public abstract float getTextureCoordinateX2(); public abstract float getTextureCoordinateY2(); // =========================================================== // Methods // =========================================================== protected void updateTextureRegionBuffer() { this.mTextureRegionBuffer.update(); } public void onApply(final GL10 pGL) { this.mTexture.bind(pGL); if(GLHelper.EXTENSIONS_VERTEXBUFFEROBJECTS) { final GL11 gl11 = (GL11)pGL; this.mTextureRegionBuffer.selectOnHardware(gl11); GLHelper.texCoordZeroPointer(gl11); } else { GLHelper.texCoordPointer(pGL, this.mTextureRegionBuffer.getFloatBuffer()); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/region/BaseTextureRegion.java
Java
lgpl
5,258
package org.anddev.andengine.opengl.texture.source; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 13:55:12 - 12.07.2011 */ public abstract class BaseTextureAtlasSource implements ITextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected int mTexturePositionX; protected int mTexturePositionY; // =========================================================== // Constructors // =========================================================== public BaseTextureAtlasSource(final int pTexturePositionX, final int pTexturePositionY) { this.mTexturePositionX = pTexturePositionX; this.mTexturePositionY = pTexturePositionY; } // =========================================================== // Getter & Setter // =========================================================== @Override public int getTexturePositionX() { return this.mTexturePositionX; } @Override public int getTexturePositionY() { return this.mTexturePositionY; } @Override public void setTexturePositionX(final int pTexturePositionX) { this.mTexturePositionX = pTexturePositionX; } @Override public void setTexturePositionY(final int pTexturePositionY) { this.mTexturePositionY = pTexturePositionY; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public String toString() { return this.getClass().getSimpleName() + "( " + this.getWidth() + "x" + this.getHeight() + " @ "+ this.mTexturePositionX + "/" + this.mTexturePositionY + " )"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/source/BaseTextureAtlasSource.java
Java
lgpl
2,261
package org.anddev.andengine.opengl.texture.source; import org.anddev.andengine.util.modifier.IModifier.DeepCopyNotSupportedException; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:46:56 - 12.07.2011 */ public interface ITextureAtlasSource { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public int getTexturePositionX(); public int getTexturePositionY(); public void setTexturePositionX(final int pTexturePositionX); public void setTexturePositionY(final int pTexturePositionY); public int getWidth(); public int getHeight(); public ITextureAtlasSource deepCopy() throws DeepCopyNotSupportedException; }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/source/ITextureAtlasSource.java
Java
lgpl
944
package org.anddev.andengine.opengl.texture.atlas; import java.util.ArrayList; import org.anddev.andengine.opengl.texture.Texture; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.util.MathUtils; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:22:55 - 14.07.2011 */ public abstract class TextureAtlas<T extends ITextureAtlasSource> extends Texture implements ITextureAtlas<T> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final int mWidth; protected final int mHeight; protected final ArrayList<T> mTextureAtlasSources = new ArrayList<T>(); // =========================================================== // Constructors // =========================================================== public TextureAtlas(final int pWidth, final int pHeight, final PixelFormat pPixelFormat, final TextureOptions pTextureOptions, final ITextureAtlasStateListener<T> pTextureAtlasStateListener) { super(pPixelFormat, pTextureOptions, pTextureAtlasStateListener); if(!MathUtils.isPowerOfTwo(pWidth) || !MathUtils.isPowerOfTwo(pHeight)) { // TODO GLHelper.EXTENSIONS_NON_POWER_OF_TWO throw new IllegalArgumentException("pWidth and pHeight must be a power of 2!"); } this.mWidth = pWidth; this.mHeight = pHeight; } // =========================================================== // Getter & Setter // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @SuppressWarnings("unchecked") @Override public ITextureAtlasStateListener<T> getTextureStateListener() { return (ITextureAtlasStateListener<T>) super.getTextureStateListener(); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void addTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) throws IllegalArgumentException { this.checkTextureAtlasSourcePosition(pTextureAtlasSource, pTexturePositionX, pTexturePositionY); pTextureAtlasSource.setTexturePositionX(pTexturePositionX); pTextureAtlasSource.setTexturePositionY(pTexturePositionY); this.mTextureAtlasSources.add(pTextureAtlasSource); this.mUpdateOnHardwareNeeded = true; } @Override public void removeTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) { final ArrayList<T> textureSources = this.mTextureAtlasSources; for(int i = textureSources.size() - 1; i >= 0; i--) { final T textureSource = textureSources.get(i); if(textureSource == pTextureAtlasSource && textureSource.getTexturePositionX() == pTexturePositionX && textureSource.getTexturePositionY() == pTexturePositionY) { textureSources.remove(i); this.mUpdateOnHardwareNeeded = true; return; } } } @Override public void clearTextureAtlasSources() { this.mTextureAtlasSources.clear(); this.mUpdateOnHardwareNeeded = true; } // =========================================================== // Methods // =========================================================== private void checkTextureAtlasSourcePosition(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) throws IllegalArgumentException { if(pTexturePositionX < 0) { throw new IllegalArgumentException("Illegal negative pTexturePositionX supplied: '" + pTexturePositionX + "'"); } else if(pTexturePositionY < 0) { throw new IllegalArgumentException("Illegal negative pTexturePositionY supplied: '" + pTexturePositionY + "'"); } else if(pTexturePositionX + pTextureAtlasSource.getWidth() > this.getWidth() || pTexturePositionY + pTextureAtlasSource.getHeight() > this.getHeight()) { throw new IllegalArgumentException("Supplied pTextureAtlasSource must not exceed bounds of Texture."); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/TextureAtlas.java
Java
lgpl
4,614
package org.anddev.andengine.opengl.texture.atlas; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.util.Debug; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:24:29 - 14.07.2011 */ public interface ITextureAtlas<T extends ITextureAtlasSource> extends ITexture { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public void addTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) throws IllegalArgumentException; public void removeTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY); public void clearTextureAtlasSources(); @Override public ITextureAtlasStateListener<T> getTextureStateListener(); // =========================================================== // Inner and Anonymous Classes // =========================================================== public static interface ITextureAtlasStateListener<T extends ITextureAtlasSource> extends ITextureStateListener { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public void onTextureAtlasSourceLoadExeption(final ITextureAtlas<T> pTextureAtlas, final T pTextureAtlasSource, final Throwable pThrowable); // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class TextureAtlasStateAdapter<T extends ITextureAtlasSource> implements ITextureAtlasStateListener<T> { @Override public void onLoadedToHardware(final ITexture pTexture) { } @Override public void onTextureAtlasSourceLoadExeption(final ITextureAtlas<T> pTextureAtlas, final T pTextureAtlasSource, final Throwable pThrowable) { } @Override public void onUnloadedFromHardware(final ITexture pTexture) { } } public static class DebugTextureAtlasStateListener<T extends ITextureAtlasSource> implements ITextureAtlasStateListener<T> { @Override public void onLoadedToHardware(final ITexture pTexture) { Debug.d("Texture loaded: " + pTexture.toString()); } @Override public void onTextureAtlasSourceLoadExeption(final ITextureAtlas<T> pTextureAtlas, final T pTextureAtlasSource, final Throwable pThrowable) { Debug.e("Exception loading TextureAtlasSource. TextureAtlas: " + pTextureAtlas.toString() + " TextureAtlasSource: " + pTextureAtlasSource.toString(), pThrowable); } @Override public void onUnloadedFromHardware(final ITexture pTexture) { Debug.d("Texture unloaded: " + pTexture.toString()); } } } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/ITextureAtlas.java
Java
lgpl
3,253
package org.anddev.andengine.opengl.texture.atlas.buildable; import java.io.IOException; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas; import org.anddev.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas; import org.anddev.andengine.opengl.texture.atlas.buildable.builder.ITextureBuilder; import org.anddev.andengine.opengl.texture.atlas.buildable.builder.ITextureBuilder.TextureAtlasSourcePackingException; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.util.Callback; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 21:26:38 - 12.08.2010 */ public class BuildableTextureAtlas<T extends ITextureAtlasSource, A extends ITextureAtlas<T>> implements ITextureAtlas<T> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final A mTextureAtlas; private final ArrayList<TextureAtlasSourceWithWithLocationCallback<T>> mTextureAtlasSourcesToPlace = new ArrayList<TextureAtlasSourceWithWithLocationCallback<T>>(); // =========================================================== // Constructors // =========================================================== public BuildableTextureAtlas(final A pTextureAtlas) { this.mTextureAtlas = pTextureAtlas; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mTextureAtlas.getWidth(); } @Override public int getHeight() { return this.mTextureAtlas.getHeight(); } @Override public int getHardwareTextureID() { return this.mTextureAtlas.getHardwareTextureID(); } @Override public boolean isLoadedToHardware() { return this.mTextureAtlas.isLoadedToHardware(); } @Override public void setLoadedToHardware(final boolean pLoadedToHardware) { this.mTextureAtlas.setLoadedToHardware(pLoadedToHardware); } @Override public boolean isUpdateOnHardwareNeeded() { return this.mTextureAtlas.isUpdateOnHardwareNeeded(); } @Override public void setUpdateOnHardwareNeeded(final boolean pUpdateOnHardwareNeeded) { this.mTextureAtlas.setUpdateOnHardwareNeeded(pUpdateOnHardwareNeeded); } @Override public void loadToHardware(final GL10 pGL) throws IOException { this.mTextureAtlas.loadToHardware(pGL); } @Override public void unloadFromHardware(final GL10 pGL) { this.mTextureAtlas.unloadFromHardware(pGL); } @Override public void reloadToHardware(final GL10 pGL) throws IOException { this.mTextureAtlas.reloadToHardware(pGL); } @Override public void bind(final GL10 pGL) { this.mTextureAtlas.bind(pGL); } @Override public TextureOptions getTextureOptions() { return this.mTextureAtlas.getTextureOptions(); } /** * Most likely this is not the method you'd want to be using, as the {@link ITextureAtlasSource} won't get packed through this. * @deprecated Use {@link BuildableTextureAtlas#addTextureAtlasSource(ITextureAtlasSource)} instead. */ @Deprecated @Override public void addTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) { this.mTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, pTexturePositionX, pTexturePositionY); } @Override public void removeTextureAtlasSource(final T pTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) { this.mTextureAtlas.removeTextureAtlasSource(pTextureAtlasSource, pTexturePositionX, pTexturePositionY); } @Override public void clearTextureAtlasSources() { this.mTextureAtlas.clearTextureAtlasSources(); this.mTextureAtlasSourcesToPlace.clear(); } @Override public boolean hasTextureStateListener() { return this.mTextureAtlas.hasTextureStateListener(); } @Override public ITextureAtlasStateListener<T> getTextureStateListener() { return this.mTextureAtlas.getTextureStateListener(); } // =========================================================== // Methods // =========================================================== /** * When all {@link ITextureAtlasSource}MAGIC_CONSTANT are added you have to call {@link BuildableBitmapTextureAtlas#build(ITextureBuilder)}. * @param pTextureAtlasSource to be added. * @param pTextureRegion */ public void addTextureAtlasSource(final T pTextureAtlasSource, final Callback<T> pCallback) { this.mTextureAtlasSourcesToPlace.add(new TextureAtlasSourceWithWithLocationCallback<T>(pTextureAtlasSource, pCallback)); } /** * Removes a {@link ITextureAtlasSource} before {@link BuildableBitmapTextureAtlas#build(ITextureBuilder)} is called. * @param pBitmapTextureAtlasSource to be removed. */ public void removeTextureAtlasSource(final ITextureAtlasSource pTextureAtlasSource) { final ArrayList<TextureAtlasSourceWithWithLocationCallback<T>> textureSources = this.mTextureAtlasSourcesToPlace; for(int i = textureSources.size() - 1; i >= 0; i--) { final TextureAtlasSourceWithWithLocationCallback<T> textureSource = textureSources.get(i); if(textureSource.mTextureAtlasSource == pTextureAtlasSource) { textureSources.remove(i); this.mTextureAtlas.setUpdateOnHardwareNeeded(true); return; } } } /** * May draw over already added {@link ITextureAtlasSource}MAGIC_CONSTANT. * * @param pTextureAtlasSourcePackingAlgorithm the {@link ITextureBuilder} to use for packing the {@link ITextureAtlasSource} in this {@link BuildableBitmapTextureAtlas}. * @throws TextureAtlasSourcePackingException i.e. when the {@link ITextureAtlasSource}MAGIC_CONSTANT didn't fit into this {@link BuildableBitmapTextureAtlas}. */ public void build(final ITextureBuilder<T, A> pTextureAtlasSourcePackingAlgorithm) throws TextureAtlasSourcePackingException { pTextureAtlasSourcePackingAlgorithm.pack(this.mTextureAtlas, this.mTextureAtlasSourcesToPlace); this.mTextureAtlasSourcesToPlace.clear(); this.mTextureAtlas.setUpdateOnHardwareNeeded(true); } // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class TextureAtlasSourceWithWithLocationCallback<T extends ITextureAtlasSource> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final T mTextureAtlasSource; private final Callback<T> mCallback; // =========================================================== // Constructors // =========================================================== public TextureAtlasSourceWithWithLocationCallback(final T pTextureAtlasSource, final Callback<T> pCallback) { this.mTextureAtlasSource = pTextureAtlasSource; this.mCallback = pCallback; } // =========================================================== // Getter & Setter // =========================================================== public Callback<T> getCallback() { return this.mCallback; } public T getTextureAtlasSource() { return this.mTextureAtlasSource; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/buildable/BuildableTextureAtlas.java
Java
lgpl
8,519
package org.anddev.andengine.opengl.texture.atlas.buildable; import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.texture.region.TiledTextureRegion; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.util.Callback; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 16:42:08 - 12.07.2011 */ public class BuildableTextureAtlasTextureRegionFactory { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Methods using BuildableBitmapTexture // =========================================================== // =========================================================== // Methods // =========================================================== public static <T extends ITextureAtlasSource, A extends ITextureAtlas<T>> TextureRegion createFromSource(final BuildableTextureAtlas<T, A> pBuildableTextureAtlas, final T pTextureAtlasSource, final boolean pTextureRegionBufferManaged) { final TextureRegion textureRegion = new TextureRegion(pBuildableTextureAtlas, 0, 0, pTextureAtlasSource.getWidth(), pTextureAtlasSource.getHeight()); pBuildableTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, new Callback<T>() { @Override public void onCallback(final T pCallbackValue) { textureRegion.setTexturePosition(pCallbackValue.getTexturePositionX(), pCallbackValue.getTexturePositionY()); } }); textureRegion.setTextureRegionBufferManaged(pTextureRegionBufferManaged); return textureRegion; } public static <T extends ITextureAtlasSource, A extends ITextureAtlas<T>> TiledTextureRegion createTiledFromSource(final BuildableTextureAtlas<T, A> pBuildableTextureAtlas, final T pTextureAtlasSource, final int pTileColumns, final int pTileRows, final boolean pTextureRegionBufferManaged) { final TiledTextureRegion tiledTextureRegion = new TiledTextureRegion(pBuildableTextureAtlas, 0, 0, pTextureAtlasSource.getWidth(), pTextureAtlasSource.getHeight(), pTileColumns, pTileRows); pBuildableTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, new Callback<T>() { @Override public void onCallback(final T pCallbackValue) { tiledTextureRegion.setTexturePosition(pCallbackValue.getTexturePositionX(), pCallbackValue.getTexturePositionY()); } }); tiledTextureRegion.setTextureRegionBufferManaged(pTextureRegionBufferManaged); return tiledTextureRegion; } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/buildable/BuildableTextureAtlasTextureRegionFactory.java
Java
lgpl
3,656
package org.anddev.andengine.opengl.texture.atlas.buildable.builder; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas; import org.anddev.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlas.TextureAtlasSourceWithWithLocationCallback; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @author Jim Scott (BlackPawn) * @since 16:03:01 - 12.08.2010 * @see http://www.blackpawn.com/texts/lightmaps/default.html */ public class BlackPawnTextureBuilder<T extends ITextureAtlasSource, A extends ITextureAtlas<T>> implements ITextureBuilder<T, A> { // =========================================================== // Constants // =========================================================== private static final Comparator<TextureAtlasSourceWithWithLocationCallback<?>> TEXTURESOURCE_COMPARATOR = new Comparator<TextureAtlasSourceWithWithLocationCallback<?>>() { @Override public int compare(final TextureAtlasSourceWithWithLocationCallback<?> pTextureAtlasSourceWithWithLocationCallbackA, final TextureAtlasSourceWithWithLocationCallback<?> pTextureAtlasSourceWithWithLocationCallbackB) { final int deltaWidth = pTextureAtlasSourceWithWithLocationCallbackB.getTextureAtlasSource().getWidth() - pTextureAtlasSourceWithWithLocationCallbackA.getTextureAtlasSource().getWidth(); if(deltaWidth != 0) { return deltaWidth; } else { return pTextureAtlasSourceWithWithLocationCallbackB.getTextureAtlasSource().getHeight() - pTextureAtlasSourceWithWithLocationCallbackA.getTextureAtlasSource().getHeight(); } } }; // =========================================================== // Fields // =========================================================== private final int mTextureAtlasSourceSpacing; // =========================================================== // Constructors // =========================================================== public BlackPawnTextureBuilder(final int pTextureAtlasSourceSpacing) { this.mTextureAtlasSourceSpacing = pTextureAtlasSourceSpacing; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void pack(final A pTextureAtlas, final ArrayList<TextureAtlasSourceWithWithLocationCallback<T>> pTextureAtlasSourcesWithLocationCallback) throws TextureAtlasSourcePackingException { Collections.sort(pTextureAtlasSourcesWithLocationCallback, TEXTURESOURCE_COMPARATOR); final Node root = new Node(new Rect(0, 0, pTextureAtlas.getWidth(), pTextureAtlas.getHeight())); final int textureSourceCount = pTextureAtlasSourcesWithLocationCallback.size(); for(int i = 0; i < textureSourceCount; i++) { final TextureAtlasSourceWithWithLocationCallback<T> textureSourceWithLocationCallback = pTextureAtlasSourcesWithLocationCallback.get(i); final T textureSource = textureSourceWithLocationCallback.getTextureAtlasSource(); final Node inserted = root.insert(textureSource, pTextureAtlas.getWidth(), pTextureAtlas.getHeight(), this.mTextureAtlasSourceSpacing); if(inserted == null) { throw new TextureAtlasSourcePackingException("Could not pack: " + textureSource.toString()); } pTextureAtlas.addTextureAtlasSource(textureSource, inserted.mRect.mLeft, inserted.mRect.mTop); textureSourceWithLocationCallback.getCallback().onCallback(textureSource); } } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== protected static class Rect { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mLeft; private final int mTop; private final int mWidth; private final int mHeight; // =========================================================== // Constructors // =========================================================== public Rect(final int pLeft, final int pTop, final int pWidth, final int pHeight) { this.mLeft = pLeft; this.mTop = pTop; this.mWidth = pWidth; this.mHeight = pHeight; } // =========================================================== // Getter & Setter // =========================================================== public int getWidth() { return this.mWidth; } public int getHeight() { return this.mHeight; } public int getLeft() { return this.mLeft; } public int getTop() { return this.mTop; } public int getRight() { return this.mLeft + this.mWidth; } public int getBottom() { return this.mTop + this.mHeight; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public String toString() { return "@: " + this.mLeft + "/" + this.mTop + " * " + this.mWidth + "x" + this.mHeight; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } protected static class Node { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private Node mChildA; private Node mChildB; private final Rect mRect; private ITextureAtlasSource mTextureAtlasSource; // =========================================================== // Constructors // =========================================================== public Node(final int pLeft, final int pTop, final int pWidth, final int pHeight) { this(new Rect(pLeft, pTop, pWidth, pHeight)); } public Node(final Rect pRect) { this.mRect = pRect; } // =========================================================== // Getter & Setter // =========================================================== public Rect getRect() { return this.mRect; } public Node getChildA() { return this.mChildA; } public Node getChildB() { return this.mChildB; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public Node insert(final ITextureAtlasSource pTextureAtlasSource, final int pTextureWidth, final int pTextureHeight, final int pTextureSpacing) throws IllegalArgumentException { if(this.mChildA != null && this.mChildB != null) { final Node newNode = this.mChildA.insert(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing); if(newNode != null){ return newNode; } else { return this.mChildB.insert(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing); } } else { if(this.mTextureAtlasSource != null) { return null; } final int textureSourceWidth = pTextureAtlasSource.getWidth(); final int textureSourceHeight = pTextureAtlasSource.getHeight(); final int rectWidth = this.mRect.getWidth(); final int rectHeight = this.mRect.getHeight(); if(textureSourceWidth > rectWidth || textureSourceHeight > rectHeight) { return null; } final int textureSourceWidthWithSpacing = textureSourceWidth + pTextureSpacing; final int textureSourceHeightWithSpacing = textureSourceHeight + pTextureSpacing; final int rectLeft = this.mRect.getLeft(); final int rectTop = this.mRect.getTop(); final boolean fitToBottomWithoutSpacing = textureSourceHeight == rectHeight && rectTop + textureSourceHeight == pTextureHeight; final boolean fitToRightWithoutSpacing = textureSourceWidth == rectWidth && rectLeft + textureSourceWidth == pTextureWidth; if(textureSourceWidthWithSpacing == rectWidth){ if(textureSourceHeightWithSpacing == rectHeight) { /* Normal case with padding. */ this.mTextureAtlasSource = pTextureAtlasSource; return this; } else if(fitToBottomWithoutSpacing) { /* Bottom edge of the BitmapTexture. */ this.mTextureAtlasSource = pTextureAtlasSource; return this; } } if(fitToRightWithoutSpacing) { /* Right edge of the BitmapTexture. */ if(textureSourceHeightWithSpacing == rectHeight) { this.mTextureAtlasSource = pTextureAtlasSource; return this; } else if(fitToBottomWithoutSpacing) { /* Bottom edge of the BitmapTexture. */ this.mTextureAtlasSource = pTextureAtlasSource; return this; } else if(textureSourceHeightWithSpacing > rectHeight) { return null; } else { return this.createChildren(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing, rectWidth - textureSourceWidth, rectHeight - textureSourceHeightWithSpacing); } } if(fitToBottomWithoutSpacing) { if(textureSourceWidthWithSpacing == rectWidth) { this.mTextureAtlasSource = pTextureAtlasSource; return this; } else if(textureSourceWidthWithSpacing > rectWidth) { return null; } else { return this.createChildren(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing, rectWidth - textureSourceWidthWithSpacing, rectHeight - textureSourceHeight); } } else if(textureSourceWidthWithSpacing > rectWidth || textureSourceHeightWithSpacing > rectHeight) { return null; } else { return this.createChildren(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing, rectWidth - textureSourceWidthWithSpacing, rectHeight - textureSourceHeightWithSpacing); } } } private Node createChildren(final ITextureAtlasSource pTextureAtlasSource, final int pTextureWidth, final int pTextureHeight, final int pTextureSpacing, final int pDeltaWidth, final int pDeltaHeight) { final Rect rect = this.mRect; if(pDeltaWidth >= pDeltaHeight) { /* Split using a vertical axis. */ this.mChildA = new Node( rect.getLeft(), rect.getTop(), pTextureAtlasSource.getWidth() + pTextureSpacing, rect.getHeight() ); this.mChildB = new Node( rect.getLeft() + (pTextureAtlasSource.getWidth() + pTextureSpacing), rect.getTop(), rect.getWidth() - (pTextureAtlasSource.getWidth() + pTextureSpacing), rect.getHeight() ); } else { /* Split using a horizontal axis. */ this.mChildA = new Node( rect.getLeft(), rect.getTop(), rect.getWidth(), pTextureAtlasSource.getHeight() + pTextureSpacing ); this.mChildB = new Node( rect.getLeft(), rect.getTop() + (pTextureAtlasSource.getHeight() + pTextureSpacing), rect.getWidth(), rect.getHeight() - (pTextureAtlasSource.getHeight() + pTextureSpacing) ); } return this.mChildA.insert(pTextureAtlasSource, pTextureWidth, pTextureHeight, pTextureSpacing); } // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/buildable/builder/BlackPawnTextureBuilder.java
Java
lgpl
12,414
package org.anddev.andengine.opengl.texture.atlas.buildable.builder; import java.util.ArrayList; import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas; import org.anddev.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlas.TextureAtlasSourceWithWithLocationCallback; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:59:14 - 12.08.2010 */ public interface ITextureBuilder<T extends ITextureAtlasSource, A extends ITextureAtlas<T>> { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public void pack(final A pTextureAtlas, final ArrayList<TextureAtlasSourceWithWithLocationCallback<T>> pTextureAtlasSourcesWithLocationCallback) throws TextureAtlasSourcePackingException; // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class TextureAtlasSourcePackingException extends Exception { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = 4700734424214372671L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TextureAtlasSourcePackingException(final String pMessage) { super(pMessage); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/buildable/builder/ITextureBuilder.java
Java
lgpl
2,602
package org.anddev.andengine.opengl.texture.atlas.bitmap; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.atlas.TextureAtlas; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture.BitmapTextureFormat; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.util.Debug; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.opengl.GLUtils; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:55:02 - 08.03.2010 */ public class BitmapTextureAtlas extends TextureAtlas<IBitmapTextureAtlasSource> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final BitmapTextureFormat mBitmapTextureFormat; // =========================================================== // Constructors // =========================================================== /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). */ public BitmapTextureAtlas(final int pWidth, final int pHeight) { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat) { this(pWidth, pHeight, pBitmapTextureFormat, TextureOptions.DEFAULT, null); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureAtlasStateListener) { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, TextureOptions.DEFAULT, pTextureAtlasStateListener); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureAtlasStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureAtlasStateListener) { this(pWidth, pHeight, pBitmapTextureFormat, TextureOptions.DEFAULT, pTextureAtlasStateListener); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureOptions the (quality) settings of the BitmapTexture. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final TextureOptions pTextureOptions) throws IllegalArgumentException { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions, null); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureOptions the (quality) settings of the BitmapTexture. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions) throws IllegalArgumentException { this(pWidth, pHeight, pBitmapTextureFormat, pTextureOptions, null); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureOptions the (quality) settings of the BitmapTexture. * @param pTextureAtlasStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final TextureOptions pTextureOptions, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureAtlasStateListener) throws IllegalArgumentException { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions, pTextureAtlasStateListener); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureOptions the (quality) settings of the BitmapTexture. * @param pTextureAtlasStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureAtlasStateListener) throws IllegalArgumentException { super(pWidth, pHeight, pBitmapTextureFormat.getPixelFormat(), pTextureOptions, pTextureAtlasStateListener); this.mBitmapTextureFormat = pBitmapTextureFormat; } // =========================================================== // Getter & Setter // =========================================================== public BitmapTextureFormat getBitmapTextureFormat() { return this.mBitmapTextureFormat; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== @Override protected void writeTextureToHardware(final GL10 pGL) { final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig(); final int glFormat = this.mPixelFormat.getGLFormat(); final int glType = this.mPixelFormat.getGLType(); final boolean preMultipyAlpha = this.mTextureOptions.mPreMultipyAlpha; final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources; final int textureSourceCount = textureSources.size(); for(int j = 0; j < textureSourceCount; j++) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(j); if(bitmapTextureAtlasSource != null) { final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig); try { if(bitmap == null) { throw new IllegalArgumentException(bitmapTextureAtlasSource.getClass().getSimpleName() + ": " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap."); } if(preMultipyAlpha) { GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTexturePositionX(), bitmapTextureAtlasSource.getTexturePositionY(), bitmap, glFormat, glType); } else { GLHelper.glTexSubImage2D(pGL, GL10.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTexturePositionX(), bitmapTextureAtlasSource.getTexturePositionY(), bitmap, this.mPixelFormat); } bitmap.recycle(); } catch (final IllegalArgumentException iae) { // TODO Load some static checkerboard or so to visualize that loading the texture has failed. //private Buffer createImage(final int width, final int height) { // final int stride = 3 * width; // final ByteBuffer image = ByteBuffer.allocateDirect(height * stride) // .order(ByteOrder.nativeOrder()); // // // Fill with a pretty "munching squares" pattern: // for (int t = 0; t < height; t++) { // final byte red = (byte) (255 - 2 * t); // final byte green = (byte) (2 * t); // final byte blue = 0; // for (int x = 0; x < width; x++) { // final int y = x ^ t; // image.position(stride * y + x * 3); // image.put(red); // image.put(green); // image.put(blue); // } // } // image.position(0); // return image; //} Debug.e("Error loading: " + bitmapTextureAtlasSource.toString(), iae); if(this.getTextureStateListener() != null) { this.getTextureStateListener().onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, iae); } else { throw iae; } } } } } @Override protected void bindTextureOnHardware(final GL10 pGL) { super.bindTextureOnHardware(pGL); final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat(); final int glFormat = pixelFormat.getGLFormat(); final int glType = pixelFormat.getGLType(); pGL.glTexImage2D(GL10.GL_TEXTURE_2D, 0, glFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/BitmapTextureAtlas.java
Java
lgpl
10,792
package org.anddev.andengine.opengl.texture.atlas.bitmap; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture.BitmapTextureFormat; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.util.MathUtils; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 09:38:51 - 03.05.2010 */ public class BitmapTextureAtlasFactory { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat pBitmapTextureFormat, final TextureRegion pTextureRegion) { return BitmapTextureAtlasFactory.createForTextureRegionSize(pBitmapTextureFormat, pTextureRegion, TextureOptions.DEFAULT); } public static BitmapTextureAtlas createForTextureRegionSize(final BitmapTextureFormat pBitmapTextureFormat, final TextureRegion pTextureRegion, final TextureOptions pTextureOptions) { final int textureRegionWidth = pTextureRegion.getWidth(); final int textureRegionHeight = pTextureRegion.getHeight(); return new BitmapTextureAtlas(MathUtils.nextPowerOfTwo(textureRegionWidth), MathUtils.nextPowerOfTwo(textureRegionHeight), pBitmapTextureFormat, pTextureOptions); } public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat pBitmapTextureFormat, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource) { return BitmapTextureAtlasFactory.createForTextureAtlasSourceSize(pBitmapTextureFormat, pBitmapTextureAtlasSource, TextureOptions.DEFAULT); } public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat pBitmapTextureFormat, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final TextureOptions pTextureOptions) { final int textureSourceWidth = pBitmapTextureAtlasSource.getWidth(); final int textureSourceHeight = pBitmapTextureAtlasSource.getHeight(); return new BitmapTextureAtlas(MathUtils.nextPowerOfTwo(textureSourceWidth), MathUtils.nextPowerOfTwo(textureSourceHeight), pBitmapTextureFormat, pTextureOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/BitmapTextureAtlasFactory.java
Java
lgpl
3,289
package org.anddev.andengine.opengl.texture.atlas.bitmap; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlas; import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture.BitmapTextureFormat; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:51:46 - 12.07.2011 */ public class BuildableBitmapTextureAtlas extends BuildableTextureAtlas<IBitmapTextureAtlasSource, BitmapTextureAtlas> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight) { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat) { this(pWidth, pHeight, pBitmapTextureFormat, TextureOptions.DEFAULT, null); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureStateListener) { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, TextureOptions.DEFAULT, pTextureStateListener); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureStateListener) { this(pWidth, pHeight, pBitmapTextureFormat, TextureOptions.DEFAULT, pTextureStateListener); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureOptions the (quality) settings of the BitmapTexture. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final TextureOptions pTextureOptions) throws IllegalArgumentException { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions, null); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureOptions the (quality) settings of the BitmapTexture. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions) throws IllegalArgumentException { this(pWidth, pHeight, pBitmapTextureFormat, pTextureOptions, null); } /** * Uses {@link BitmapTextureFormat#RGBA_8888}. * * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pTextureOptions the (quality) settings of the BitmapTexture. * @param pTextureStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final TextureOptions pTextureOptions, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureStateListener) throws IllegalArgumentException { this(pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions, pTextureStateListener); } /** * @param pWidth must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pHeight must be a power of 2 (i.e. 32, 64, 128, 256, 512, 1024). * @param pBitmapTextureFormat use {@link BitmapTextureFormat#RGBA_8888} for {@link BitmapTextureAtlas}MAGIC_CONSTANT with transparency and {@link BitmapTextureFormat#RGB_565} for {@link BitmapTextureAtlas}MAGIC_CONSTANT without transparency. * @param pTextureOptions the (quality) settings of the BitmapTexture. * @param pTextureStateListener to be informed when this {@link BitmapTextureAtlas} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public BuildableBitmapTextureAtlas(final int pWidth, final int pHeight, final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions, final ITextureAtlasStateListener<IBitmapTextureAtlasSource> pTextureStateListener) throws IllegalArgumentException { super(new BitmapTextureAtlas(pWidth, pHeight, pBitmapTextureFormat, pTextureOptions, pTextureStateListener)); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/BuildableBitmapTextureAtlas.java
Java
lgpl
7,372
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import org.anddev.andengine.util.Debug; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Picture; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:52:58 - 21.05.2011 */ public abstract class PictureBitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final Picture mPicture; protected final int mWidth; protected final int mHeight; // =========================================================== // Constructors // =========================================================== public PictureBitmapTextureAtlasSource(final Picture pPicture) { this(pPicture, 0, 0); } public PictureBitmapTextureAtlasSource(final Picture pPicture, final int pTexturePositionX, final int pTexturePositionY) { this(pPicture, pTexturePositionX, pTexturePositionY, pPicture.getWidth(), pPicture.getHeight()); } public PictureBitmapTextureAtlasSource(final Picture pPicture, final int pTexturePositionX, final int pTexturePositionY, final float pScale) { this(pPicture, pTexturePositionX, pTexturePositionY, Math.round(pPicture.getWidth() * pScale), Math.round(pPicture.getHeight() * pScale)); } public PictureBitmapTextureAtlasSource(final Picture pPicture, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexturePositionX, pTexturePositionY); this.mPicture = pPicture; this.mWidth = pWidth; this.mHeight = pHeight; } @Override public abstract PictureBitmapTextureAtlasSource deepCopy(); // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { final Picture picture = this.mPicture; if(picture == null) { Debug.e("Failed loading Bitmap in PictureBitmapTextureAtlasSource."); return null; } final Bitmap bitmap = Bitmap.createBitmap(this.mWidth, this.mHeight, pBitmapConfig); final Canvas canvas = new Canvas(bitmap); final float scaleX = (float)this.mWidth / this.mPicture.getWidth(); final float scaleY = (float)this.mHeight / this.mPicture.getHeight(); canvas.scale(scaleX, scaleY, 0, 0); picture.draw(canvas); return bitmap; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/PictureBitmapTextureAtlasSource.java
Java
lgpl
3,504
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:08:52 - 09.03.2010 */ public interface IBitmapTextureAtlasSource extends ITextureAtlasSource { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== @Override public IBitmapTextureAtlasSource deepCopy(); public Bitmap onLoadBitmap(final Config pBitmapConfig); }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/IBitmapTextureAtlasSource.java
Java
lgpl
838
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.StreamUtils; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; /** * * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 16:39:22 - 10.08.2010 */ public class FileBitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mWidth; private final int mHeight; private final File mFile; // =========================================================== // Constructors // =========================================================== public FileBitmapTextureAtlasSource(final File pFile) { this(pFile, 0, 0); } public FileBitmapTextureAtlasSource(final File pFile, final int pTexturePositionX, final int pTexturePositionY) { super(pTexturePositionX, pTexturePositionY); this.mFile = pFile; final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inJustDecodeBounds = true; InputStream in = null; try { in = new FileInputStream(pFile); BitmapFactory.decodeStream(in, null, decodeOptions); } catch (final IOException e) { Debug.e("Failed loading Bitmap in FileBitmapTextureAtlasSource. File: " + pFile, e); } finally { StreamUtils.close(in); } this.mWidth = decodeOptions.outWidth; this.mHeight = decodeOptions.outHeight; } FileBitmapTextureAtlasSource(final File pFile, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexturePositionX, pTexturePositionY); this.mFile = pFile; this.mWidth = pWidth; this.mHeight = pHeight; } @Override public FileBitmapTextureAtlasSource deepCopy() { return new FileBitmapTextureAtlasSource(this.mFile, this.mTexturePositionX, this.mTexturePositionY, this.mWidth, this.mHeight); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inPreferredConfig = pBitmapConfig; InputStream in = null; try { in = new FileInputStream(this.mFile); return BitmapFactory.decodeStream(in, null, decodeOptions); } catch (final IOException e) { Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ". File: " + this.mFile, e); return null; } finally { StreamUtils.close(in); } } @Override public String toString() { return this.getClass().getSimpleName() + "(" + this.mFile + ")"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/FileBitmapTextureAtlasSource.java
Java
lgpl
3,949
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:07:23 - 09.03.2010 */ public class ResourceBitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mWidth; private final int mHeight; private final int mDrawableResourceID; private final Context mContext; // =========================================================== // Constructors // =========================================================== public ResourceBitmapTextureAtlasSource(final Context pContext, final int pDrawableResourceID) { this(pContext, pDrawableResourceID, 0, 0); } public ResourceBitmapTextureAtlasSource(final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY) { super(pTexturePositionX, pTexturePositionY); this.mContext = pContext; this.mDrawableResourceID = pDrawableResourceID; final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inJustDecodeBounds = true; // decodeOptions.inScaled = false; // TODO Check how this behaves with drawable-""/nodpi/ldpi/mdpi/hdpi folders BitmapFactory.decodeResource(pContext.getResources(), pDrawableResourceID, decodeOptions); this.mWidth = decodeOptions.outWidth; this.mHeight = decodeOptions.outHeight; } protected ResourceBitmapTextureAtlasSource(final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexturePositionX, pTexturePositionY); this.mContext = pContext; this.mDrawableResourceID = pDrawableResourceID; this.mWidth = pWidth; this.mHeight = pHeight; } @Override public ResourceBitmapTextureAtlasSource deepCopy() { return new ResourceBitmapTextureAtlasSource(this.mContext, this.mDrawableResourceID, this.mTexturePositionX, this.mTexturePositionY, this.mWidth, this.mHeight); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inPreferredConfig = pBitmapConfig; // decodeOptions.inScaled = false; // TODO Check how this behaves with drawable-""/nodpi/ldpi/mdpi/hdpi folders return BitmapFactory.decodeResource(this.mContext.getResources(), this.mDrawableResourceID, decodeOptions); } @Override public String toString() { return this.getClass().getSimpleName() + "(" + this.mDrawableResourceID + ")"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/ResourceBitmapTextureAtlasSource.java
Java
lgpl
3,932
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import java.io.IOException; import java.io.InputStream; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.StreamUtils; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:07:52 - 09.03.2010 */ public class AssetBitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mWidth; private final int mHeight; private final String mAssetPath; private final Context mContext; // =========================================================== // Constructors // =========================================================== public AssetBitmapTextureAtlasSource(final Context pContext, final String pAssetPath) { this(pContext, pAssetPath, 0, 0); } public AssetBitmapTextureAtlasSource(final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY) { super(pTexturePositionX, pTexturePositionY); this.mContext = pContext; this.mAssetPath = pAssetPath; final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inJustDecodeBounds = true; InputStream in = null; try { in = pContext.getAssets().open(pAssetPath); BitmapFactory.decodeStream(in, null, decodeOptions); } catch (final IOException e) { Debug.e("Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: " + pAssetPath, e); } finally { StreamUtils.close(in); } this.mWidth = decodeOptions.outWidth; this.mHeight = decodeOptions.outHeight; } AssetBitmapTextureAtlasSource(final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexturePositionX, pTexturePositionY); this.mContext = pContext; this.mAssetPath = pAssetPath; this.mWidth = pWidth; this.mHeight = pHeight; } @Override public AssetBitmapTextureAtlasSource deepCopy() { return new AssetBitmapTextureAtlasSource(this.mContext, this.mAssetPath, this.mTexturePositionX, this.mTexturePositionY, this.mWidth, this.mHeight); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { InputStream in = null; try { final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inPreferredConfig = pBitmapConfig; in = this.mContext.getAssets().open(this.mAssetPath); return BitmapFactory.decodeStream(in, null, decodeOptions); } catch (final IOException e) { Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ". AssetPath: " + this.mAssetPath, e); return null; } finally { StreamUtils.close(in); } } @Override public String toString() { return this.getClass().getSimpleName() + "(" + this.mAssetPath + ")"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/AssetBitmapTextureAtlasSource.java
Java
lgpl
4,229
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 20:20:36 - 08.08.2010 */ public class EmptyBitmapTextureAtlasSource extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mWidth; private final int mHeight; // =========================================================== // Constructors // =========================================================== public EmptyBitmapTextureAtlasSource(final int pWidth, final int pHeight) { this(0, 0, pWidth, pHeight); } public EmptyBitmapTextureAtlasSource(final int pTexturePositionX, final int pTexturePositionY, final int pWidth, final int pHeight) { super(pTexturePositionX, pTexturePositionY); this.mWidth = pWidth; this.mHeight = pHeight; } @Override public EmptyBitmapTextureAtlasSource deepCopy() { return new EmptyBitmapTextureAtlasSource(this.mTexturePositionX, this.mTexturePositionY, this.mWidth, this.mHeight); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { return Bitmap.createBitmap(this.mWidth, this.mHeight, pBitmapConfig); } @Override public String toString() { return this.getClass().getSimpleName() + "(" + this.mWidth + " x " + this.mHeight + ")"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/EmptyBitmapTextureAtlasSource.java
Java
lgpl
2,578
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import java.io.File; import org.anddev.andengine.util.FileUtils; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:03:19 - 30.05.2011 */ public class ExternalStorageFileBitmapTextureAtlasSource extends FileBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public ExternalStorageFileBitmapTextureAtlasSource(final Context pContext, final String pFilePath) { super(new File(FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath))); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/ExternalStorageFileBitmapTextureAtlasSource.java
Java
lgpl
1,678
package org.anddev.andengine.opengl.texture.atlas.bitmap.source; import java.io.File; import org.anddev.andengine.util.FileUtils; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:01:19 - 30.05.2011 */ public class InternalStorageFileBitmapTextureAtlasSource extends FileBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public InternalStorageFileBitmapTextureAtlasSource(final Context pContext, final String pFilePath) { super(new File(FileUtils.getAbsolutePathOnInternalStorage(pContext, pFilePath))); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/InternalStorageFileBitmapTextureAtlasSource.java
Java
lgpl
1,678
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.Paint.Style; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:08:00 - 05.11.2010 */ public class FillBitmapTextureAtlasSourceDecorator extends BaseShapeBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final int mFillColor; // =========================================================== // Constructors // =========================================================== public FillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFillColor) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pFillColor, null); } public FillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFillColor, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions); this.mFillColor = pFillColor; this.mPaint.setStyle(Style.FILL); this.mPaint.setColor(pFillColor); } @Override public FillBitmapTextureAtlasSourceDecorator deepCopy() { return new FillBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mFillColor, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/FillBitmapTextureAtlasSourceDecorator.java
Java
lgpl
2,797
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.LinearGradient; import android.graphics.Paint.Style; import android.graphics.Shader.TileMode; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:21:24 - 05.11.2010 */ public class LinearGradientFillBitmapTextureAtlasSourceDecorator extends BaseShapeBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final LinearGradientDirection mLinearGradientDirection; protected final int[] mColors; protected final float[] mPositions; // =========================================================== // Constructors // =========================================================== public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFromColor, final int pToColor, final LinearGradientDirection pLinearGradientDirection) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pFromColor, pToColor, pLinearGradientDirection, null); } public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFromColor, final int pToColor, final LinearGradientDirection pLinearGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, new int[] { pFromColor, pToColor }, null, pLinearGradientDirection, pTextureAtlasSourceDecoratorOptions); } public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final LinearGradientDirection pLinearGradientDirection) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColors, pPositions, pLinearGradientDirection, null); } public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final LinearGradientDirection pLinearGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions); this.mColors = pColors; this.mPositions = pPositions; this.mLinearGradientDirection = pLinearGradientDirection; this.mPaint.setStyle(Style.FILL); final int right = pBitmapTextureAtlasSource.getWidth() - 1; final int bottom = pBitmapTextureAtlasSource.getHeight() - 1; final float fromX = pLinearGradientDirection.getFromX(right); final float fromY = pLinearGradientDirection.getFromY(bottom); final float toX = pLinearGradientDirection.getToX(right); final float toY = pLinearGradientDirection.getToY(bottom); this.mPaint.setShader(new LinearGradient(fromX, fromY, toX, toY, pColors, pPositions, TileMode.CLAMP)); } @Override public LinearGradientFillBitmapTextureAtlasSourceDecorator deepCopy() { return new LinearGradientFillBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mColors, this.mPositions, this.mLinearGradientDirection, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== public static enum LinearGradientDirection { // =========================================================== // Elements // =========================================================== LEFT_TO_RIGHT(1, 0, 0, 0), RIGHT_TO_LEFT(0, 0, 1, 0), BOTTOM_TO_TOP(0, 1, 0, 0), TOP_TO_BOTTOM(0, 0, 0, 1), TOPLEFT_TO_BOTTOMRIGHT(0, 0, 1, 1), BOTTOMRIGHT_TO_TOPLEFT(1, 1, 0, 0), TOPRIGHT_TO_BOTTOMLEFT(1, 0, 0, 1), BOTTOMLEFT_TO_TOPRIGHT(0, 1, 1, 0); // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mFromX; private final int mFromY; private final int mToX; private final int mToY; // =========================================================== // Constructors // =========================================================== private LinearGradientDirection(final int pFromX, final int pFromY, final int pToX, final int pToY) { this.mFromX = pFromX; this.mFromY = pFromY; this.mToX = pToX; this.mToY = pToY; } // =========================================================== // Getter & Setter // =========================================================== final int getFromX(int pRight) { return this.mFromX * pRight; } final int getFromY(int pBottom) { return this.mFromY * pBottom; } final int getToX(int pRight) { return this.mToX * pRight; } final int getToY(int pBottom) { return this.mToY * pBottom; } // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/LinearGradientFillBitmapTextureAtlasSourceDecorator.java
Java
lgpl
7,057
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.Paint.Style; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:07:55 - 05.11.2010 */ public class OutlineBitmapTextureAtlasSourceDecorator extends BaseShapeBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final int mOutlineColor; // =========================================================== // Constructors // =========================================================== public OutlineBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pOutlineColor) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pOutlineColor, null); } public OutlineBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pOutlineColor, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions); this.mOutlineColor = pOutlineColor; this.mPaint.setStyle(Style.STROKE); this.mPaint.setColor(pOutlineColor); } @Override public OutlineBitmapTextureAtlasSourceDecorator deepCopy() { return new OutlineBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mOutlineColor, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/OutlineBitmapTextureAtlasSourceDecorator.java
Java
lgpl
2,838
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.source.BaseTextureAtlasSource; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Bitmap.Config; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 16:43:29 - 06.08.2010 */ public abstract class BaseBitmapTextureAtlasSourceDecorator extends BaseTextureAtlasSource implements IBitmapTextureAtlasSource { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final IBitmapTextureAtlasSource mBitmapTextureAtlasSource; protected TextureAtlasSourceDecoratorOptions mTextureAtlasSourceDecoratorOptions; protected Paint mPaint = new Paint(); // =========================================================== // Constructors // =========================================================== public BaseBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource) { this(pBitmapTextureAtlasSource, new TextureAtlasSourceDecoratorOptions()); } public BaseBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource.getTexturePositionX(), pBitmapTextureAtlasSource.getTexturePositionY()); this.mBitmapTextureAtlasSource = pBitmapTextureAtlasSource; this.mTextureAtlasSourceDecoratorOptions = (pTextureAtlasSourceDecoratorOptions == null) ? new TextureAtlasSourceDecoratorOptions() : pTextureAtlasSourceDecoratorOptions; this.mPaint.setAntiAlias(this.mTextureAtlasSourceDecoratorOptions.getAntiAliasing()); } @Override public abstract BaseBitmapTextureAtlasSourceDecorator deepCopy(); // =========================================================== // Getter & Setter // =========================================================== public Paint getPaint() { return this.mPaint; } public void setPaint(final Paint pPaint) { this.mPaint = pPaint; } public TextureAtlasSourceDecoratorOptions getTextureAtlasSourceDecoratorOptions() { return this.mTextureAtlasSourceDecoratorOptions; } public void setTextureAtlasSourceDecoratorOptions(final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { this.mTextureAtlasSourceDecoratorOptions = pTextureAtlasSourceDecoratorOptions; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract void onDecorateBitmap(final Canvas pCanvas); @Override public int getWidth() { return this.mBitmapTextureAtlasSource.getWidth(); } @Override public int getHeight() { return this.mBitmapTextureAtlasSource.getHeight(); } @Override public Bitmap onLoadBitmap(final Config pBitmapConfig) { final Bitmap bitmap = this.ensureLoadedBitmapIsMutable(this.mBitmapTextureAtlasSource.onLoadBitmap(pBitmapConfig)); final Canvas canvas = new Canvas(bitmap); this.onDecorateBitmap(canvas); return bitmap; } // =========================================================== // Methods // =========================================================== private Bitmap ensureLoadedBitmapIsMutable(final Bitmap pBitmap) { if(pBitmap.isMutable()) { return pBitmap; } else { final Bitmap mutableBitmap = pBitmap.copy(pBitmap.getConfig(), true); pBitmap.recycle(); return mutableBitmap; } } // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class TextureAtlasSourceDecoratorOptions { // =========================================================== // Constants // =========================================================== public static final TextureAtlasSourceDecoratorOptions DEFAULT = new TextureAtlasSourceDecoratorOptions(); // =========================================================== // Fields // =========================================================== private float mInsetLeft = 0.25f; private float mInsetRight = 0.25f; private float mInsetTop = 0.25f; private float mInsetBottom = 0.25f; private boolean mAntiAliasing; // =========================================================== // Constructors // =========================================================== protected TextureAtlasSourceDecoratorOptions deepCopy() { final TextureAtlasSourceDecoratorOptions textureSourceDecoratorOptions = new TextureAtlasSourceDecoratorOptions(); textureSourceDecoratorOptions.setInsets(this.mInsetLeft, this.mInsetTop, this.mInsetRight, this.mInsetBottom); textureSourceDecoratorOptions.setAntiAliasing(this.mAntiAliasing); return textureSourceDecoratorOptions; } // =========================================================== // Getter & Setter // =========================================================== public boolean getAntiAliasing() { return this.mAntiAliasing; } public float getInsetLeft() { return this.mInsetLeft; } public float getInsetRight() { return this.mInsetRight; } public float getInsetTop() { return this.mInsetTop; } public float getInsetBottom() { return this.mInsetBottom; } public TextureAtlasSourceDecoratorOptions setAntiAliasing(final boolean pAntiAliasing) { this.mAntiAliasing = pAntiAliasing; return this; } public TextureAtlasSourceDecoratorOptions setInsetLeft(final float pInsetLeft) { this.mInsetLeft = pInsetLeft; return this; } public TextureAtlasSourceDecoratorOptions setInsetRight(final float pInsetRight) { this.mInsetRight = pInsetRight; return this; } public TextureAtlasSourceDecoratorOptions setInsetTop(final float pInsetTop) { this.mInsetTop = pInsetTop; return this; } public TextureAtlasSourceDecoratorOptions setInsetBottom(final float pInsetBottom) { this.mInsetBottom = pInsetBottom; return this; } public TextureAtlasSourceDecoratorOptions setInsets(final float pInsets) { return this.setInsets(pInsets, pInsets, pInsets, pInsets); } public TextureAtlasSourceDecoratorOptions setInsets(final float pInsetLeft, final float pInsetTop, final float pInsetRight, final float pInsetBottom) { this.mInsetLeft = pInsetLeft; this.mInsetTop = pInsetTop; this.mInsetRight = pInsetRight; this.mInsetBottom = pInsetBottom; return this; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/BaseBitmapTextureAtlasSourceDecorator.java
Java
lgpl
7,578
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import org.anddev.andengine.util.ArrayUtils; import android.graphics.Paint.Style; import android.graphics.RadialGradient; import android.graphics.Shader.TileMode; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:21:24 - 05.11.2010 */ public class RadialGradientFillBitmapTextureAtlasSourceDecorator extends BaseShapeBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== private static final float[] POSITIONS_DEFAULT = new float[] { 0.0f, 1.0f }; // =========================================================== // Fields // =========================================================== protected final RadialGradientDirection mRadialGradientDirection; protected final int[] mColors; protected final float[] mPositions; // =========================================================== // Constructors // =========================================================== public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFromColor, final int pToColor, final RadialGradientDirection pRadialGradientDirection) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pFromColor, pToColor, pRadialGradientDirection, null); } public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pFromColor, final int pToColor, final RadialGradientDirection pRadialGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, new int[] { pFromColor, pToColor }, POSITIONS_DEFAULT, pRadialGradientDirection, pTextureAtlasSourceDecoratorOptions); } public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final RadialGradientDirection pRadialGradientDirection) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColors, pPositions, pRadialGradientDirection, null); } public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final RadialGradientDirection pRadialGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions); this.mColors = pColors; this.mPositions = pPositions; this.mRadialGradientDirection = pRadialGradientDirection; this.mPaint.setStyle(Style.FILL); final int width = pBitmapTextureAtlasSource.getWidth(); final int height = pBitmapTextureAtlasSource.getHeight(); final float centerX = width * 0.5f; final float centerY = height * 0.5f; final float radius = Math.max(centerX, centerY); switch(pRadialGradientDirection) { case INSIDE_OUT: this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP)); break; case OUTSIDE_IN: ArrayUtils.reverse(pColors); this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP)); break; } } @Override public RadialGradientFillBitmapTextureAtlasSourceDecorator deepCopy() { return new RadialGradientFillBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mColors, this.mPositions, this.mRadialGradientDirection, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== public static enum RadialGradientDirection { // =========================================================== // Elements // =========================================================== INSIDE_OUT, OUTSIDE_IN; // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/RadialGradientFillBitmapTextureAtlasSourceDecorator.java
Java
lgpl
6,461
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:50:09 - 04.01.2011 */ public class RoundedRectangleBitmapTextureAtlasSourceDecoratorShape implements IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== private static final float CORNER_RADIUS_DEFAULT = 1; // =========================================================== // Fields // =========================================================== private final RectF mRectF = new RectF(); private final float mCornerRadiusX; private final float mCornerRadiusY; private static RoundedRectangleBitmapTextureAtlasSourceDecoratorShape sDefaultInstance; // =========================================================== // Constructors // =========================================================== public RoundedRectangleBitmapTextureAtlasSourceDecoratorShape() { this(CORNER_RADIUS_DEFAULT, CORNER_RADIUS_DEFAULT); } public RoundedRectangleBitmapTextureAtlasSourceDecoratorShape(final float pCornerRadiusX, final float pCornerRadiusY) { this.mCornerRadiusX = pCornerRadiusX; this.mCornerRadiusY = pCornerRadiusY; } public static RoundedRectangleBitmapTextureAtlasSourceDecoratorShape getDefaultInstance() { if(sDefaultInstance == null) { sDefaultInstance = new RoundedRectangleBitmapTextureAtlasSourceDecoratorShape(); } return sDefaultInstance; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions) { final float left = pDecoratorOptions.getInsetLeft(); final float top = pDecoratorOptions.getInsetTop(); final float right = pCanvas.getWidth() - pDecoratorOptions.getInsetRight(); final float bottom = pCanvas.getHeight() - pDecoratorOptions.getInsetBottom(); this.mRectF.set(left, top, right, bottom); pCanvas.drawRoundRect(this.mRectF, this.mCornerRadiusX, this.mCornerRadiusY, pPaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/RoundedRectangleBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
3,094
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:53:13 - 04.01.2011 */ public class CircleBitmapTextureAtlasSourceDecoratorShape implements IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private static CircleBitmapTextureAtlasSourceDecoratorShape sDefaultInstance; // =========================================================== // Constructors // =========================================================== public CircleBitmapTextureAtlasSourceDecoratorShape() { } public static CircleBitmapTextureAtlasSourceDecoratorShape getDefaultInstance() { if(sDefaultInstance == null) { sDefaultInstance = new CircleBitmapTextureAtlasSourceDecoratorShape(); } return sDefaultInstance; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions) { final float width = pCanvas.getWidth() - pDecoratorOptions.getInsetLeft() - pDecoratorOptions.getInsetRight(); final float height = pCanvas.getHeight() - pDecoratorOptions.getInsetTop() - pDecoratorOptions.getInsetBottom(); final float centerX = (pCanvas.getWidth() + pDecoratorOptions.getInsetLeft() - pDecoratorOptions.getInsetRight()) * 0.5f; final float centerY = (pCanvas.getHeight() + pDecoratorOptions.getInsetTop() - pDecoratorOptions.getInsetBottom()) * 0.5f; final float radius = Math.min(width * 0.5f, height * 0.5f); pCanvas.drawCircle(centerX, centerY, radius, pPaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/CircleBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
2,769
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:52:55 - 04.01.2011 */ public class EllipseBitmapTextureAtlasSourceDecoratorShape implements IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private static EllipseBitmapTextureAtlasSourceDecoratorShape sDefaultInstance; private final RectF mRectF = new RectF(); // =========================================================== // Constructors // =========================================================== public EllipseBitmapTextureAtlasSourceDecoratorShape() { } public static EllipseBitmapTextureAtlasSourceDecoratorShape getDefaultInstance() { if(sDefaultInstance == null) { sDefaultInstance = new EllipseBitmapTextureAtlasSourceDecoratorShape(); } return sDefaultInstance; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions) { final float left = pDecoratorOptions.getInsetLeft(); final float top = pDecoratorOptions.getInsetTop(); final float right = pCanvas.getWidth() - pDecoratorOptions.getInsetRight(); final float bottom = pCanvas.getHeight() - pDecoratorOptions.getInsetBottom(); this.mRectF.set(left, top, right, bottom); pCanvas.drawOval(this.mRectF, pPaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/EllipseBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
2,609
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:47:40 - 04.01.2011 */ public interface IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== // =========================================================== // Methods // =========================================================== public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions); }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/IBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
920
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:52:55 - 04.01.2011 */ public class ArcBitmapTextureAtlasSourceDecoratorShape implements IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== private static final float STARTANGLE_DEFAULT = 0; private static final float SWEEPANGLE_DEFAULT = 360; private static final boolean USECENTER_DEFAULT = true; // =========================================================== // Fields // =========================================================== private static ArcBitmapTextureAtlasSourceDecoratorShape sDefaultInstance; private final RectF mRectF = new RectF(); private final float mStartAngle; private final float mSweepAngle; private final boolean mUseCenter; // =========================================================== // Constructors // =========================================================== public ArcBitmapTextureAtlasSourceDecoratorShape() { this(STARTANGLE_DEFAULT, SWEEPANGLE_DEFAULT, USECENTER_DEFAULT); } public ArcBitmapTextureAtlasSourceDecoratorShape(final float pStartAngle, final float pSweepAngle, final boolean pUseCenter) { this.mStartAngle = pStartAngle; this.mSweepAngle = pSweepAngle; this.mUseCenter = pUseCenter; } @Deprecated public static ArcBitmapTextureAtlasSourceDecoratorShape getDefaultInstance() { if(sDefaultInstance == null) { sDefaultInstance = new ArcBitmapTextureAtlasSourceDecoratorShape(); } return sDefaultInstance; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions) { final float left = pDecoratorOptions.getInsetLeft(); final float top = pDecoratorOptions.getInsetTop(); final float right = pCanvas.getWidth() - pDecoratorOptions.getInsetRight(); final float bottom = pCanvas.getHeight() - pDecoratorOptions.getInsetBottom(); this.mRectF.set(left, top, right, bottom); pCanvas.drawArc(this.mRectF, this.mStartAngle, this.mSweepAngle, this.mUseCenter, pPaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/ArcBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
3,232
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.BaseBitmapTextureAtlasSourceDecorator.TextureAtlasSourceDecoratorOptions; import android.graphics.Canvas; import android.graphics.Paint; public class RectangleBitmapTextureAtlasSourceDecoratorShape implements IBitmapTextureAtlasSourceDecoratorShape { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private static RectangleBitmapTextureAtlasSourceDecoratorShape sDefaultInstance; // =========================================================== // Constructors // =========================================================== public RectangleBitmapTextureAtlasSourceDecoratorShape() { } public static RectangleBitmapTextureAtlasSourceDecoratorShape getDefaultInstance() { if(sDefaultInstance == null) { sDefaultInstance = new RectangleBitmapTextureAtlasSourceDecoratorShape(); } return sDefaultInstance; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onDecorateBitmap(final Canvas pCanvas, final Paint pPaint, final TextureAtlasSourceDecoratorOptions pDecoratorOptions) { final float left = pDecoratorOptions.getInsetLeft(); final float top = pDecoratorOptions.getInsetTop(); final float right = pCanvas.getWidth() - pDecoratorOptions.getInsetRight(); final float bottom = pCanvas.getHeight() - pDecoratorOptions.getInsetBottom(); pCanvas.drawRect(left, top, right, bottom, pPaint); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== };
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/shape/RectangleBitmapTextureAtlasSourceDecoratorShape.java
Java
lgpl
2,371
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.Color; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 22:16:41 - 06.08.2010 */ public class ColorKeyBitmapTextureAtlasSourceDecorator extends ColorSwapBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public ColorKeyBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, Color.TRANSPARENT); } public ColorKeyBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, Color.TRANSPARENT, pTextureAtlasSourceDecoratorOptions); } public ColorKeyBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pTolerance) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, Color.TRANSPARENT, pTolerance); } public ColorKeyBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pTolerance, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, pTolerance, Color.TRANSPARENT, pTextureAtlasSourceDecoratorOptions); } @Override public ColorKeyBitmapTextureAtlasSourceDecorator deepCopy() { return new ColorKeyBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mColorKeyColor, this.mTolerance, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/ColorKeyBitmapTextureAtlasSourceDecorator.java
Java
lgpl
3,619
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.AvoidXfermode; import android.graphics.AvoidXfermode.Mode; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:41:39 - 07.06.2011 */ public class ColorSwapBitmapTextureAtlasSourceDecorator extends BaseShapeBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== private static final int TOLERANCE_DEFAULT = 0; // =========================================================== // Fields // =========================================================== protected final int mColorKeyColor; protected final int mTolerance; protected final int mColorSwapColor; // =========================================================== // Constructors // =========================================================== public ColorSwapBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pColorSwapColor) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, TOLERANCE_DEFAULT, pColorSwapColor, null); } public ColorSwapBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pColorSwapColor, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, TOLERANCE_DEFAULT, pColorSwapColor, pTextureAtlasSourceDecoratorOptions); } public ColorSwapBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pTolerance, final int pColorSwapColor) { this(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pColorKeyColor, pTolerance, pColorSwapColor, null); } public ColorSwapBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int pColorKeyColor, final int pTolerance, final int pColorSwapColor, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions); this.mColorKeyColor = pColorKeyColor; this.mTolerance = pTolerance; this.mColorSwapColor = pColorSwapColor; this.mPaint.setXfermode(new AvoidXfermode(pColorKeyColor, pTolerance, Mode.TARGET)); this.mPaint.setColor(pColorSwapColor); } @Override public ColorSwapBitmapTextureAtlasSourceDecorator deepCopy() { return new ColorSwapBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mColorKeyColor, this.mTolerance, this.mColorSwapColor, this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/ColorSwapBitmapTextureAtlasSourceDecorator.java
Java
lgpl
4,215
package org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.IBitmapTextureAtlasSourceDecoratorShape; import android.graphics.Canvas; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 20:09:41 - 05.11.2010 */ public abstract class BaseShapeBitmapTextureAtlasSourceDecorator extends BaseBitmapTextureAtlasSourceDecorator { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final IBitmapTextureAtlasSourceDecoratorShape mBitmapTextureAtlasSourceDecoratorShape; // =========================================================== // Constructors // =========================================================== public BaseShapeBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) { super(pBitmapTextureAtlasSource, pTextureAtlasSourceDecoratorOptions); this.mBitmapTextureAtlasSourceDecoratorShape = pBitmapTextureAtlasSourceDecoratorShape; } @Override public abstract BaseShapeBitmapTextureAtlasSourceDecorator deepCopy(); // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected void onDecorateBitmap(final Canvas pCanvas){ this.mBitmapTextureAtlasSourceDecoratorShape.onDecorateBitmap(pCanvas, this.mPaint, (this.mTextureAtlasSourceDecoratorOptions == null) ? TextureAtlasSourceDecoratorOptions.DEFAULT : this.mTextureAtlasSourceDecoratorOptions); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/BaseShapeBitmapTextureAtlasSourceDecorator.java
Java
lgpl
2,596
package org.anddev.andengine.opengl.texture.atlas.bitmap; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.ResourceBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlasTextureRegionFactory; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.texture.region.TextureRegionFactory; import org.anddev.andengine.opengl.texture.region.TiledTextureRegion; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:15:14 - 09.03.2010 */ public class BitmapTextureAtlasTextureRegionFactory { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private static String sAssetBasePath = ""; private static boolean sCreateTextureRegionBuffersManaged; // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== /** * @param pAssetBasePath must end with '<code>/</code>' or have <code>.length() == 0</code>. */ public static void setAssetBasePath(final String pAssetBasePath) { if(pAssetBasePath.endsWith("/") || pAssetBasePath.length() == 0) { BitmapTextureAtlasTextureRegionFactory.sAssetBasePath = pAssetBasePath; } else { throw new IllegalArgumentException("pAssetBasePath must end with '/' or be lenght zero."); } } public static void setCreateTextureRegionBuffersManaged(final boolean pCreateTextureRegionBuffersManaged) { BitmapTextureAtlasTextureRegionFactory.sCreateTextureRegionBuffersManaged = pCreateTextureRegionBuffersManaged; } public static void reset() { BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(""); BitmapTextureAtlasTextureRegionFactory.setCreateTextureRegionBuffersManaged(false); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Methods using BitmapTexture // =========================================================== public static TextureRegion createFromAsset(final BitmapTextureAtlas pBitmapTextureAtlas, final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(pContext, BitmapTextureAtlasTextureRegionFactory.sAssetBasePath + pAssetPath); return BitmapTextureAtlasTextureRegionFactory.createFromSource(pBitmapTextureAtlas, bitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY); } public static TiledTextureRegion createTiledFromAsset(final BitmapTextureAtlas pBitmapTextureAtlas, final Context pContext, final String pAssetPath, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(pContext, BitmapTextureAtlasTextureRegionFactory.sAssetBasePath + pAssetPath); return BitmapTextureAtlasTextureRegionFactory.createTiledFromSource(pBitmapTextureAtlas, bitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY, pTileColumns, pTileRows); } public static TextureRegion createFromResource(final BitmapTextureAtlas pBitmapTextureAtlas, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new ResourceBitmapTextureAtlasSource(pContext, pDrawableResourceID); return BitmapTextureAtlasTextureRegionFactory.createFromSource(pBitmapTextureAtlas, bitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY); } public static TiledTextureRegion createTiledFromResource(final BitmapTextureAtlas pBitmapTextureAtlas, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new ResourceBitmapTextureAtlasSource(pContext, pDrawableResourceID); return BitmapTextureAtlasTextureRegionFactory.createTiledFromSource(pBitmapTextureAtlas, bitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY, pTileColumns, pTileRows); } public static TextureRegion createFromSource(final BitmapTextureAtlas pBitmapTextureAtlas, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY) { return TextureRegionFactory.createFromSource(pBitmapTextureAtlas, pBitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY, sCreateTextureRegionBuffersManaged); } public static TiledTextureRegion createTiledFromSource(final BitmapTextureAtlas pBitmapTextureAtlas, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows) { return TextureRegionFactory.createTiledFromSource(pBitmapTextureAtlas, pBitmapTextureAtlasSource, pTexturePositionX, pTexturePositionY, pTileColumns, pTileRows, sCreateTextureRegionBuffersManaged); } // =========================================================== // Methods using BuildableTexture // =========================================================== public static TextureRegion createFromAsset(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final Context pContext, final String pAssetPath) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(pContext, BitmapTextureAtlasTextureRegionFactory.sAssetBasePath + pAssetPath); return BitmapTextureAtlasTextureRegionFactory.createFromSource(pBuildableBitmapTextureAtlas, bitmapTextureAtlasSource); } public static TiledTextureRegion createTiledFromAsset(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final Context pContext, final String pAssetPath, final int pTileColumns, final int pTileRows) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(pContext, BitmapTextureAtlasTextureRegionFactory.sAssetBasePath + pAssetPath); return BitmapTextureAtlasTextureRegionFactory.createTiledFromSource(pBuildableBitmapTextureAtlas, bitmapTextureAtlasSource, pTileColumns, pTileRows); } public static TextureRegion createFromResource(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final Context pContext, final int pDrawableResourceID) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new ResourceBitmapTextureAtlasSource(pContext, pDrawableResourceID); return BitmapTextureAtlasTextureRegionFactory.createFromSource(pBuildableBitmapTextureAtlas, bitmapTextureAtlasSource); } public static TiledTextureRegion createTiledFromResource(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final Context pContext, final int pDrawableResourceID, final int pTileColumns, final int pTileRows) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = new ResourceBitmapTextureAtlasSource(pContext, pDrawableResourceID); return BitmapTextureAtlasTextureRegionFactory.createTiledFromSource(pBuildableBitmapTextureAtlas, bitmapTextureAtlasSource, pTileColumns, pTileRows); } public static TextureRegion createFromSource(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource) { return BuildableTextureAtlasTextureRegionFactory.createFromSource(pBuildableBitmapTextureAtlas, pBitmapTextureAtlasSource, sCreateTextureRegionBuffersManaged); } public static TiledTextureRegion createTiledFromSource(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final int pTileColumns, final int pTileRows) { return BuildableTextureAtlasTextureRegionFactory.createTiledFromSource(pBuildableBitmapTextureAtlas, pBitmapTextureAtlasSource, pTileColumns, pTileRows, sCreateTextureRegionBuffersManaged); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/atlas/bitmap/BitmapTextureAtlasTextureRegionFactory.java
Java
lgpl
9,097
package org.anddev.andengine.opengl.texture; import java.io.IOException; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.util.Debug; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:01:03 - 11.07.2011 */ public interface ITexture { // =========================================================== // Constants // =========================================================== // =========================================================== // Methods // =========================================================== public int getWidth(); public int getHeight(); public int getHardwareTextureID(); public boolean isLoadedToHardware(); public void setLoadedToHardware(final boolean pLoadedToHardware); public boolean isUpdateOnHardwareNeeded(); public void setUpdateOnHardwareNeeded(final boolean pUpdateOnHardwareNeeded); public void loadToHardware(final GL10 pGL) throws IOException; public void unloadFromHardware(final GL10 pGL); public void reloadToHardware(final GL10 pGL) throws IOException; public void bind(final GL10 pGL); public TextureOptions getTextureOptions(); public boolean hasTextureStateListener(); public ITextureStateListener getTextureStateListener(); // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== public static interface ITextureStateListener { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public void onLoadedToHardware(final ITexture pTexture); public void onUnloadedFromHardware(final ITexture pTexture); // =========================================================== // Inner and Anonymous Classes // =========================================================== public static class TextureStateAdapter<T extends ITextureAtlasSource> implements ITextureStateListener { @Override public void onLoadedToHardware(final ITexture pTexture) { } @Override public void onUnloadedFromHardware(final ITexture pTexture) { } } public static class DebugTextureStateListener<T extends ITextureAtlasSource> implements ITextureStateListener { @Override public void onLoadedToHardware(final ITexture pTexture) { Debug.d("Texture loaded: " + pTexture.toString()); } @Override public void onUnloadedFromHardware(final ITexture pTexture) { Debug.d("Texture unloaded: " + pTexture.toString()); } } } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/ITexture.java
Java
lgpl
2,911
package org.anddev.andengine.opengl.texture; import java.io.IOException; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.source.ITextureAtlasSource; import org.anddev.andengine.opengl.util.GLHelper; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 14:55:02 - 08.03.2010 */ public abstract class Texture implements ITexture { // =========================================================== // Constants // =========================================================== private static final int[] HARDWARETEXTUREID_FETCHER = new int[1]; // =========================================================== // Fields // =========================================================== protected final PixelFormat mPixelFormat; protected final TextureOptions mTextureOptions; protected int mHardwareTextureID = -1; protected boolean mLoadedToHardware; protected boolean mUpdateOnHardwareNeeded = false; protected final ITextureStateListener mTextureStateListener; // =========================================================== // Constructors // =========================================================== /** * @param pPixelFormat * @param pTextureOptions the (quality) settings of the Texture. * @param pTextureStateListener to be informed when this {@link Texture} is loaded, unloaded or a {@link ITextureAtlasSource} failed to load. */ public Texture(final PixelFormat pPixelFormat, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IllegalArgumentException { this.mPixelFormat = pPixelFormat; this.mTextureOptions = pTextureOptions; this.mTextureStateListener = pTextureStateListener; } // =========================================================== // Getter & Setter // =========================================================== @Override public int getHardwareTextureID() { return this.mHardwareTextureID; } @Override public boolean isLoadedToHardware() { return this.mLoadedToHardware; } @Override public void setLoadedToHardware(final boolean pLoadedToHardware) { this.mLoadedToHardware = pLoadedToHardware; } @Override public boolean isUpdateOnHardwareNeeded() { return this.mUpdateOnHardwareNeeded; } @Override public void setUpdateOnHardwareNeeded(final boolean pUpdateOnHardwareNeeded) { this.mUpdateOnHardwareNeeded = pUpdateOnHardwareNeeded; } public PixelFormat getPixelFormat() { return this.mPixelFormat; } @Override public TextureOptions getTextureOptions() { return this.mTextureOptions; } @Override public ITextureStateListener getTextureStateListener() { return this.mTextureStateListener; } @Override public boolean hasTextureStateListener() { return this.mTextureStateListener != null; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract void writeTextureToHardware(final GL10 pGL) throws IOException; @Override public void loadToHardware(final GL10 pGL) throws IOException { GLHelper.enableTextures(pGL); this.generateHardwareTextureID(pGL); this.bindTextureOnHardware(pGL); this.applyTextureOptions(pGL); this.writeTextureToHardware(pGL); this.mUpdateOnHardwareNeeded = false; this.mLoadedToHardware = true; if(this.mTextureStateListener != null) { this.mTextureStateListener.onLoadedToHardware(this); } } @Override public void unloadFromHardware(final GL10 pGL) { GLHelper.enableTextures(pGL); this.deleteTextureOnHardware(pGL); this.mHardwareTextureID = -1; this.mLoadedToHardware = false; if(this.mTextureStateListener != null) { this.mTextureStateListener.onUnloadedFromHardware(this); } } @Override public void reloadToHardware(final GL10 pGL) throws IOException { this.unloadFromHardware(pGL); this.loadToHardware(pGL); } @Override public void bind(final GL10 pGL) { GLHelper.bindTexture(pGL, this.mHardwareTextureID); } // =========================================================== // Methods // =========================================================== protected void applyTextureOptions(final GL10 pGL) { this.mTextureOptions.apply(pGL); } protected void bindTextureOnHardware(final GL10 pGL) { GLHelper.forceBindTexture(pGL, this.mHardwareTextureID); } protected void deleteTextureOnHardware(final GL10 pGL) { GLHelper.deleteTexture(pGL, this.mHardwareTextureID); } protected void generateHardwareTextureID(final GL10 pGL) { pGL.glGenTextures(1, Texture.HARDWARETEXTUREID_FETCHER, 0); this.mHardwareTextureID = Texture.HARDWARETEXTUREID_FETCHER[0]; } // =========================================================== // Inner and Anonymous Classes // =========================================================== public enum PixelFormat { // =========================================================== // Elements // =========================================================== UNDEFINED(-1, -1, -1), RGBA_4444(GL10.GL_RGBA, GL10.GL_UNSIGNED_SHORT_4_4_4_4, 16), RGBA_5551(GL10.GL_RGBA, GL10.GL_UNSIGNED_SHORT_5_5_5_1, 16), RGBA_8888(GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, 32), RGB_565(GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, 16), A_8(GL10.GL_ALPHA, GL10.GL_UNSIGNED_BYTE, 8), I_8(GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE, 8), AI_88(GL10.GL_LUMINANCE_ALPHA, GL10.GL_UNSIGNED_BYTE, 16); // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mGLFormat; private final int mGLType; private final int mBitsPerPixel; // =========================================================== // Constructors // =========================================================== private PixelFormat(final int pGLFormat, final int pGLType, final int pBitsPerPixel) { this.mGLFormat = pGLFormat; this.mGLType = pGLType; this.mBitsPerPixel = pBitsPerPixel; } // =========================================================== // Getter & Setter // =========================================================== public int getGLFormat() { return this.mGLFormat; } public int getGLType() { return this.mGLType; } public int getBitsPerPixel() { return this.mBitsPerPixel; } // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/Texture.java
Java
lgpl
7,296
package org.anddev.andengine.opengl.texture.bitmap; import java.io.IOException; import java.io.InputStream; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.Texture; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.util.MathUtils; import org.anddev.andengine.util.StreamUtils; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.opengl.GLUtils; /** * (c) Zynga 2011 * * @author Nicolas Gramlich <ngramlich@zynga.com> * @since 16:16:25 - 30.07.2011 */ public abstract class BitmapTexture extends Texture { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mWidth; private final int mHeight; private final BitmapTextureFormat mBitmapTextureFormat; // =========================================================== // Constructors // =========================================================== public BitmapTexture() throws IOException { this(BitmapTextureFormat.RGBA_8888, TextureOptions.DEFAULT, null); } public BitmapTexture(final BitmapTextureFormat pBitmapTextureFormat) throws IOException { this(pBitmapTextureFormat, TextureOptions.DEFAULT, null); } public BitmapTexture(final TextureOptions pTextureOptions) throws IOException { this(BitmapTextureFormat.RGBA_8888, pTextureOptions, null); } public BitmapTexture(final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions) throws IOException { this(pBitmapTextureFormat, pTextureOptions, null); } public BitmapTexture(final BitmapTextureFormat pBitmapTextureFormat, final TextureOptions pTextureOptions, final ITextureStateListener pTextureStateListener) throws IOException { super(pBitmapTextureFormat.getPixelFormat(), pTextureOptions, pTextureStateListener); this.mBitmapTextureFormat = pBitmapTextureFormat; final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inJustDecodeBounds = true; final InputStream in = null; try { BitmapFactory.decodeStream(this.onGetInputStream(), null, decodeOptions); } finally { StreamUtils.close(in); } this.mWidth = decodeOptions.outWidth; this.mHeight = decodeOptions.outHeight; if(!MathUtils.isPowerOfTwo(this.mWidth) || !MathUtils.isPowerOfTwo(this.mHeight)) { // TODO GLHelper.EXTENSIONS_NON_POWER_OF_TWO throw new IllegalArgumentException("pWidth and pHeight must be a power of 2!"); } } // =========================================================== // Getter & Setter // =========================================================== @Override public int getWidth() { return this.mWidth; } @Override public int getHeight() { return this.mHeight; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract InputStream onGetInputStream() throws IOException; @Override protected void writeTextureToHardware(final GL10 pGL) throws IOException { final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig(); final boolean preMultipyAlpha = this.mTextureOptions.mPreMultipyAlpha; final BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); decodeOptions.inPreferredConfig = bitmapConfig; final Bitmap bitmap = BitmapFactory.decodeStream(this.onGetInputStream(), null, decodeOptions); if(preMultipyAlpha) { GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); } else { GLHelper.glTexImage2D(pGL, GL10.GL_TEXTURE_2D, 0, bitmap, 0, this.mPixelFormat); } bitmap.recycle(); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== public static enum BitmapTextureFormat { // =========================================================== // Elements // =========================================================== RGBA_8888(Config.ARGB_8888, PixelFormat.RGBA_8888), RGB_565(Config.RGB_565, PixelFormat.RGB_565), RGBA_4444(Config.ARGB_4444, PixelFormat.RGBA_4444), // TODO A_8(Config.ALPHA_8, PixelFormat.A_8); // TODO // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Config mBitmapConfig; private final PixelFormat mPixelFormat; // =========================================================== // Constructors // =========================================================== private BitmapTextureFormat(final Config pBitmapConfig, final PixelFormat pPixelFormat) { this.mBitmapConfig = pBitmapConfig; this.mPixelFormat = pPixelFormat; } public static BitmapTextureFormat fromPixelFormat(final PixelFormat pPixelFormat) { switch(pPixelFormat) { case RGBA_8888: return RGBA_8888; case RGBA_4444: return RGBA_4444; case RGB_565: return RGB_565; case A_8: return A_8; default: throw new IllegalArgumentException("Unsupported " + PixelFormat.class.getName() + ": '" + pPixelFormat + "'."); } } // =========================================================== // Getter & Setter // =========================================================== public Config getBitmapConfig() { return this.mBitmapConfig; } public PixelFormat getPixelFormat() { return this.mPixelFormat; } // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== } }
zzy421-andengine
src/org/anddev/andengine/opengl/texture/bitmap/BitmapTexture.java
Java
lgpl
6,542
package org.anddev.andengine.collision; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_X; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_Y; import org.anddev.andengine.entity.primitive.Line; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:27:22 - 17.07.2010 */ public class LineCollisionChecker extends ShapeCollisionChecker { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static boolean checkLineCollision(final float pX1, final float pY1, final float pX2, final float pY2, final float pX3, final float pY3, final float pX4, final float pY4) { return ((BaseCollisionChecker.relativeCCW(pX1, pY1, pX2, pY2, pX3, pY3) * BaseCollisionChecker.relativeCCW(pX1, pY1, pX2, pY2, pX4, pY4) <= 0) && (BaseCollisionChecker.relativeCCW(pX3, pY3, pX4, pY4, pX1, pY1) * BaseCollisionChecker.relativeCCW(pX3, pY3, pX4, pY4, pX2, pY2) <= 0)); } public static void fillVertices(final Line pLine, final float[] pVertices) { pVertices[0 + VERTEX_INDEX_X] = 0; pVertices[0 + VERTEX_INDEX_Y] = 0; pVertices[2 + VERTEX_INDEX_X] = pLine.getX2() - pLine.getX1(); pVertices[2 + VERTEX_INDEX_Y] = pLine.getY2() - pLine.getY1(); pLine.getLocalToSceneTransformation().transform(pVertices); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/collision/LineCollisionChecker.java
Java
lgpl
2,371
package org.anddev.andengine.collision; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_X; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_Y; import org.anddev.andengine.engine.camera.Camera; import org.anddev.andengine.entity.primitive.Line; import org.anddev.andengine.entity.shape.RectangularShape; import org.anddev.andengine.util.MathUtils; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:50:19 - 11.03.2010 */ public class RectangularShapeCollisionChecker extends ShapeCollisionChecker { // =========================================================== // Constants // =========================================================== private static final int RECTANGULARSHAPE_VERTEX_COUNT = 4; private static final int LINE_VERTEX_COUNT = 2; private static final float[] VERTICES_CONTAINS_TMP = new float[2 * RECTANGULARSHAPE_VERTEX_COUNT]; private static final float[] VERTICES_COLLISION_TMP_A = new float[2 * RECTANGULARSHAPE_VERTEX_COUNT]; private static final float[] VERTICES_COLLISION_TMP_B = new float[2 * RECTANGULARSHAPE_VERTEX_COUNT]; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static boolean checkContains(final RectangularShape pRectangularShape, final float pX, final float pY) { RectangularShapeCollisionChecker.fillVertices(pRectangularShape, VERTICES_CONTAINS_TMP); return ShapeCollisionChecker.checkContains(VERTICES_CONTAINS_TMP, 2 * RECTANGULARSHAPE_VERTEX_COUNT, pX, pY); } public static boolean isVisible(final Camera pCamera, final RectangularShape pRectangularShape) { RectangularShapeCollisionChecker.fillVertices(pCamera, VERTICES_COLLISION_TMP_A); RectangularShapeCollisionChecker.fillVertices(pRectangularShape, VERTICES_COLLISION_TMP_B); return ShapeCollisionChecker.checkCollision(2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_A, 2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_B); } public static boolean isVisible(final Camera pCamera, final Line pLine) { RectangularShapeCollisionChecker.fillVertices(pCamera, VERTICES_COLLISION_TMP_A); LineCollisionChecker.fillVertices(pLine, VERTICES_COLLISION_TMP_B); return ShapeCollisionChecker.checkCollision(2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_A, 2 * LINE_VERTEX_COUNT, VERTICES_COLLISION_TMP_B); } public static boolean checkCollision(final RectangularShape pRectangularShapeA, final RectangularShape pRectangularShapeB) { RectangularShapeCollisionChecker.fillVertices(pRectangularShapeA, VERTICES_COLLISION_TMP_A); RectangularShapeCollisionChecker.fillVertices(pRectangularShapeB, VERTICES_COLLISION_TMP_B); return ShapeCollisionChecker.checkCollision(2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_A, 2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_B); } public static boolean checkCollision(final RectangularShape pRectangularShape, final Line pLine) { RectangularShapeCollisionChecker.fillVertices(pRectangularShape, VERTICES_COLLISION_TMP_A); LineCollisionChecker.fillVertices(pLine, VERTICES_COLLISION_TMP_B); return ShapeCollisionChecker.checkCollision(2 * RECTANGULARSHAPE_VERTEX_COUNT, VERTICES_COLLISION_TMP_A, 2 * LINE_VERTEX_COUNT, VERTICES_COLLISION_TMP_B); } public static void fillVertices(final RectangularShape pRectangularShape, final float[] pVertices) { final float left = 0; final float top = 0; final float right = pRectangularShape.getWidth(); final float bottom = pRectangularShape.getHeight(); pVertices[0 + VERTEX_INDEX_X] = left; pVertices[0 + VERTEX_INDEX_Y] = top; pVertices[2 + VERTEX_INDEX_X] = right; pVertices[2 + VERTEX_INDEX_Y] = top; pVertices[4 + VERTEX_INDEX_X] = right; pVertices[4 + VERTEX_INDEX_Y] = bottom; pVertices[6 + VERTEX_INDEX_X] = left; pVertices[6 + VERTEX_INDEX_Y] = bottom; pRectangularShape.getLocalToSceneTransformation().transform(pVertices); } private static void fillVertices(final Camera pCamera, final float[] pVertices) { pVertices[0 + VERTEX_INDEX_X] = pCamera.getMinX(); pVertices[0 + VERTEX_INDEX_Y] = pCamera.getMinY(); pVertices[2 + VERTEX_INDEX_X] = pCamera.getMaxX(); pVertices[2 + VERTEX_INDEX_Y] = pCamera.getMinY(); pVertices[4 + VERTEX_INDEX_X] = pCamera.getMaxX(); pVertices[4 + VERTEX_INDEX_Y] = pCamera.getMaxY(); pVertices[6 + VERTEX_INDEX_X] = pCamera.getMinX(); pVertices[6 + VERTEX_INDEX_Y] = pCamera.getMaxY(); MathUtils.rotateAroundCenter(pVertices, pCamera.getRotation(), pCamera.getCenterX(), pCamera.getCenterY()); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/collision/RectangularShapeCollisionChecker.java
Java
lgpl
5,561
package org.anddev.andengine.collision; import org.anddev.andengine.util.constants.Constants; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:50:19 - 11.03.2010 */ public class ShapeCollisionChecker extends BaseCollisionChecker { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static boolean checkCollision(final int pVerticesALength, final float[] pVerticesA, final int pVerticesBLength, final float[] pVerticesB) { /* Check all the lines of A ... */ for(int a = pVerticesALength - 4; a >= 0; a -= 2) { /* ... against all lines in B. */ if(ShapeCollisionChecker.checkCollisionSub(a, a + 2, pVerticesA, pVerticesB, pVerticesBLength)){ return true; } } /* Also check the 'around the corner of the array' line of A against all lines in B. */ if(ShapeCollisionChecker.checkCollisionSub(pVerticesALength - 2, 0, pVerticesA, pVerticesB, pVerticesBLength)){ return true; } else { /* At last check if one polygon 'contains' the other one by checking * if one vertex of the one vertices is contained by all of the other vertices. */ if(ShapeCollisionChecker.checkContains(pVerticesA, pVerticesALength, pVerticesB[Constants.VERTEX_INDEX_X], pVerticesB[Constants.VERTEX_INDEX_Y])) { return true; } else if(ShapeCollisionChecker.checkContains(pVerticesB, pVerticesBLength, pVerticesA[Constants.VERTEX_INDEX_X], pVerticesA[Constants.VERTEX_INDEX_Y])) { return true; } else { return false; } } } /** * Checks line specified by pVerticesA[pVertexIndexA1] and pVerticesA[pVertexIndexA2] against all lines in pVerticesB. */ private static boolean checkCollisionSub(final int pVertexIndexA1, final int pVertexIndexA2, final float[] pVerticesA, final float[] pVerticesB, final int pVerticesBLength) { /* Check against all the lines of B. */ final float vertexA1X = pVerticesA[pVertexIndexA1 + Constants.VERTEX_INDEX_X]; final float vertexA1Y = pVerticesA[pVertexIndexA1 + Constants.VERTEX_INDEX_Y]; final float vertexA2X = pVerticesA[pVertexIndexA2 + Constants.VERTEX_INDEX_X]; final float vertexA2Y = pVerticesA[pVertexIndexA2 + Constants.VERTEX_INDEX_Y]; for(int b = pVerticesBLength - 4; b >= 0; b -= 2) { if(LineCollisionChecker.checkLineCollision(vertexA1X, vertexA1Y, vertexA2X, vertexA2Y, pVerticesB[b + Constants.VERTEX_INDEX_X], pVerticesB[b + Constants.VERTEX_INDEX_Y], pVerticesB[b + 2 + Constants.VERTEX_INDEX_X], pVerticesB[b + 2 + Constants.VERTEX_INDEX_Y])){ return true; } } /* Also check the 'around the corner of the array' line of B. */ if(LineCollisionChecker.checkLineCollision(vertexA1X, vertexA1Y, vertexA2X, vertexA2Y, pVerticesB[pVerticesBLength - 2], pVerticesB[pVerticesBLength - 1], pVerticesB[Constants.VERTEX_INDEX_X], pVerticesB[Constants.VERTEX_INDEX_Y])){ return true; } return false; } public static boolean checkContains(final float[] pVertices, final int pVerticesLength, final float pX, final float pY) { int edgeResultSum = 0; for(int i = pVerticesLength - 4; i >= 0; i -= 2) { final int edgeResult = BaseCollisionChecker.relativeCCW(pVertices[i], pVertices[i + 1], pVertices[i + 2], pVertices[i + 3], pX, pY); if(edgeResult == 0) { return true; } else { edgeResultSum += edgeResult; } } /* Also check the 'around the corner of the array' line. */ final int edgeResult = BaseCollisionChecker.relativeCCW(pVertices[pVerticesLength - 2], pVertices[pVerticesLength - 1], pVertices[Constants.VERTEX_INDEX_X], pVertices[Constants.VERTEX_INDEX_Y], pX, pY); if(edgeResult == 0){ return true; } else { edgeResultSum += edgeResult; } final int vertexCount = pVerticesLength / 2; /* Point is not on the edge, so check if the edge is on the same side(left or right) of all edges. */ return edgeResultSum == vertexCount || edgeResultSum == -vertexCount ; } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/collision/ShapeCollisionChecker.java
Java
lgpl
5,008
package org.anddev.andengine.collision; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:50:19 - 11.03.2010 */ public class BaseCollisionChecker { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static boolean checkAxisAlignedRectangleCollision(final float pLeftA, final float pTopA, final float pRightA, final float pBottomA, final float pLeftB, final float pTopB, final float pRightB, final float pBottomB) { return (pLeftA < pRightB && pLeftB < pRightA && pTopA < pBottomB && pTopB < pBottomA); } /** * Returns an indicator of where the specified point (PX,&nbsp;PY) lies with * respect to the line segment from (X1,&nbsp;Y1) to (X2,&nbsp;Y2). The * return value can be either 1, -1, or 0 and indicates in which direction * the specified line must pivot around its first endpoint, (X1,&nbsp;Y1), * in order to point at the specified point (PX,&nbsp;PY). * <p> * A return value of 1 indicates that the line segment must turn in the * direction that takes the positive X axis towards the negative Y axis. In * the default coordinate system used by Java 2D, this direction is * counterclockwise. * <p> * A return value of -1 indicates that the line segment must turn in the * direction that takes the positive X axis towards the positive Y axis. In * the default coordinate system, this direction is clockwise. * <p> * A return value of 0 indicates that the point lies exactly on the line * segment. Note that an indicator value of 0 is rare and not useful for * determining colinearity because of floating point rounding issues. * <p> * If the point is colinear with the line segment, but not between the * endpoints, then the value will be -1 if the point lies * "beyond (X1,&nbsp;Y1)" or 1 if the point lies "beyond (X2,&nbsp;Y2)". * * @param pX1 * ,&nbsp;Y1 the coordinates of the beginning of the specified * line segment * @param pX2 * ,&nbsp;Y2 the coordinates of the end of the specified line * segment * @param pPX * ,&nbsp;PY the coordinates of the specified point to be * compared with the specified line segment * @return an integer that indicates the position of the third specified * coordinates with respect to the line segment formed by the first * two specified coordinates. */ public static int relativeCCW(final float pX1, final float pY1, float pX2, float pY2, float pPX, float pPY) { pX2 -= pX1; pY2 -= pY1; pPX -= pX1; pPY -= pY1; float ccw = pPX * pY2 - pPY * pX2; if (ccw == 0.0f) { // The point is colinear, classify based on which side of // the segment the point falls on. We can calculate a // relative value using the projection of PX,PY onto the // segment - a negative value indicates the point projects // outside of the segment in the direction of the particular // endpoint used as the origin for the projection. ccw = pPX * pX2 + pPY * pY2; if (ccw > 0.0f) { // Reverse the projection to be relative to the original X2,Y2 // X2 and Y2 are simply negated. // PX and PY need to have (X2 - X1) or (Y2 - Y1) subtracted // from them (based on the original values) // Since we really want to get a positive answer when the // point is "beyond (X2,Y2)", then we want to calculate // the inverse anyway - thus we leave X2 & Y2 negated. pPX -= pX2; pPY -= pY2; ccw = pPX * pX2 + pPY * pY2; if (ccw < 0.0f) { ccw = 0.0f; } } } return (ccw < 0.0f) ? -1 : ((ccw > 0.0f) ? 1 : 0); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/collision/BaseCollisionChecker.java
Java
lgpl
4,759
package org.anddev.andengine.entity.sprite.batch; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import org.anddev.andengine.engine.camera.Camera; import org.anddev.andengine.entity.Entity; import org.anddev.andengine.entity.shape.Shape; import org.anddev.andengine.entity.sprite.BaseSprite; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.BaseTextureRegion; import org.anddev.andengine.opengl.texture.region.buffer.SpriteBatchTextureRegionBuffer; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.opengl.vertex.SpriteBatchVertexBuffer; /** * TODO Texture could be semi-changeable, being resetting to null in end(...) * TODO Make use of pGL.glColorPointer(size, type, stride, pointer) which should allow individual color tinting. * * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:45:48 - 14.06.2011 */ public class SpriteBatch extends Entity { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final ITexture mTexture; protected final int mCapacity; protected int mIndex; private int mVertices; private int mSourceBlendFunction; private int mDestinationBlendFunction; private final SpriteBatchVertexBuffer mSpriteBatchVertexBuffer; private final SpriteBatchTextureRegionBuffer mSpriteBatchTextureRegionBuffer; // =========================================================== // Constructors // =========================================================== public SpriteBatch(final ITexture pTexture, final int pCapacity) { this(pTexture, pCapacity, new SpriteBatchVertexBuffer(pCapacity, GL11.GL_STATIC_DRAW, true), new SpriteBatchTextureRegionBuffer(pCapacity, GL11.GL_STATIC_DRAW, true)); } public SpriteBatch(final ITexture pTexture, final int pCapacity, final SpriteBatchVertexBuffer pSpriteBatchVertexBuffer, final SpriteBatchTextureRegionBuffer pSpriteBatchTextureRegionBuffer) { this.mTexture = pTexture; this.mCapacity = pCapacity; this.mSpriteBatchVertexBuffer = pSpriteBatchVertexBuffer; this.mSpriteBatchTextureRegionBuffer = pSpriteBatchTextureRegionBuffer; this.initBlendFunction(); } // =========================================================== // Getter & Setter // =========================================================== public void setBlendFunction(final int pSourceBlendFunction, final int pDestinationBlendFunction) { this.mSourceBlendFunction = pSourceBlendFunction; this.mDestinationBlendFunction = pDestinationBlendFunction; } public int getIndex() { return this.mIndex; } public void setIndex(final int pIndex) { this.assertCapacity(pIndex); this.mIndex = pIndex; final int vertexIndex = pIndex * 2 * SpriteBatchVertexBuffer.VERTICES_PER_RECTANGLE; this.mSpriteBatchVertexBuffer.setIndex(vertexIndex); this.mSpriteBatchTextureRegionBuffer.setIndex(vertexIndex); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected void doDraw(final GL10 pGL, final Camera pCamera) { this.onInitDraw(pGL); this.begin(pGL); this.onApplyVertices(pGL); this.onApplyTextureRegion(pGL); this.drawVertices(pGL, pCamera); this.end(pGL); } @Override public void reset() { super.reset(); this.initBlendFunction(); } @Override protected void finalize() throws Throwable { super.finalize(); if(this.mSpriteBatchVertexBuffer.isManaged()) { this.mSpriteBatchVertexBuffer.unloadFromActiveBufferObjectManager(); } if(this.mSpriteBatchTextureRegionBuffer.isManaged()) { this.mSpriteBatchTextureRegionBuffer.unloadFromActiveBufferObjectManager(); } } // =========================================================== // Methods // =========================================================== protected void begin(@SuppressWarnings("unused") final GL10 pGL) { // GLHelper.disableDepthMask(pGL); } protected void end(@SuppressWarnings("unused") final GL10 pGL) { // GLHelper.enableDepthMask(pGL); } /** * @see {@link SpriteBatchVertexBuffer#add(float, float, float, float)}. */ public void draw(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight) { this.assertCapacity(); this.assertTexture(pTextureRegion); this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } public void drawWithoutChecks(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight) { this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } /** * @see {@link SpriteBatchVertexBuffer#add(float, float, float, float, float)}. */ public void draw(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation) { this.assertCapacity(); this.assertTexture(pTextureRegion); this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pRotation); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } public void drawWithoutChecks(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation) { this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pRotation); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } /** * @see {@link SpriteBatchVertexBuffer#add(float, float, float, float, float, float)}. */ public void draw(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pScaleX, final float pScaleY) { this.assertCapacity(); this.assertTexture(pTextureRegion); this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pScaleX, pScaleY); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } public void drawWithoutChecks(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pScaleX, final float pScaleY) { this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pScaleX, pScaleY); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } /** * @see {@link SpriteBatchVertexBuffer#add(float, float, float, float, float, float, float)}. */ public void draw(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation, final float pScaleX, final float pScaleY) { this.assertCapacity(); this.assertTexture(pTextureRegion); this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pRotation, pScaleX, pScaleY); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } public void drawWithoutChecks(final BaseTextureRegion pTextureRegion, final float pX, final float pY, final float pWidth, final float pHeight, final float pRotation, final float pScaleX, final float pScaleY) { this.mSpriteBatchVertexBuffer.add(pX, pY, pWidth, pHeight, pRotation, pScaleX, pScaleY); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } /** * {@link SpriteBatchVertexBuffer#add(float, float, float, float, float, float, float, float)}. */ public void draw(final BaseTextureRegion pTextureRegion, final float pX1, final float pY1, final float pX2, final float pY2, final float pX3, final float pY3, final float pX4, final float pY4) { this.assertCapacity(); this.assertTexture(pTextureRegion); this.mSpriteBatchVertexBuffer.addInner(pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } public void drawWithoutChecks(final BaseTextureRegion pTextureRegion, final float pX1, final float pY1, final float pX2, final float pY2, final float pX3, final float pY3, final float pX4, final float pY4) { this.mSpriteBatchVertexBuffer.addInner(pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4); this.mSpriteBatchTextureRegionBuffer.add(pTextureRegion); this.mIndex++; } /** * {@link SpriteBatchVertexBuffer#add(BaseSprite)}. */ public void draw(final BaseSprite pBaseSprite) { if(pBaseSprite.isVisible()) { this.assertCapacity(); final BaseTextureRegion textureRegion = pBaseSprite.getTextureRegion(); this.assertTexture(textureRegion); if(pBaseSprite.getRotation() == 0 && !pBaseSprite.isScaled()) { this.mSpriteBatchVertexBuffer.add(pBaseSprite.getX(), pBaseSprite.getY(), pBaseSprite.getWidth(), pBaseSprite.getHeight()); } else { this.mSpriteBatchVertexBuffer.add(pBaseSprite.getWidth(), pBaseSprite.getHeight(), pBaseSprite.getLocalToParentTransformation()); } this.mSpriteBatchTextureRegionBuffer.add(textureRegion); this.mIndex++; } } public void drawWithoutChecks(final BaseSprite pBaseSprite) { if(pBaseSprite.isVisible()) { final BaseTextureRegion textureRegion = pBaseSprite.getTextureRegion(); if(pBaseSprite.getRotation() == 0 && !pBaseSprite.isScaled()) { this.mSpriteBatchVertexBuffer.add(pBaseSprite.getX(), pBaseSprite.getY(), pBaseSprite.getWidth(), pBaseSprite.getHeight()); } else { this.mSpriteBatchVertexBuffer.add(pBaseSprite.getWidth(), pBaseSprite.getHeight(), pBaseSprite.getLocalToParentTransformation()); } this.mSpriteBatchTextureRegionBuffer.add(textureRegion); this.mIndex++; } } public void submit() { this.onSubmit(); } private void onSubmit() { this.mVertices = this.mIndex * SpriteBatchVertexBuffer.VERTICES_PER_RECTANGLE; this.mSpriteBatchVertexBuffer.submit(); this.mSpriteBatchTextureRegionBuffer.submit(); this.mIndex = 0; this.mSpriteBatchVertexBuffer.setIndex(0); this.mSpriteBatchTextureRegionBuffer.setIndex(0); } private void initBlendFunction() { if(this.mTexture.getTextureOptions().mPreMultipyAlpha) { this.setBlendFunction(Shape.BLENDFUNCTION_SOURCE_PREMULTIPLYALPHA_DEFAULT, Shape.BLENDFUNCTION_DESTINATION_PREMULTIPLYALPHA_DEFAULT); } } protected void onInitDraw(final GL10 pGL) { GLHelper.setColor(pGL, this.mRed, this.mGreen, this.mBlue, this.mAlpha); GLHelper.enableVertexArray(pGL); GLHelper.blendFunction(pGL, this.mSourceBlendFunction, this.mDestinationBlendFunction); GLHelper.enableTextures(pGL); GLHelper.enableTexCoordArray(pGL); } protected void onApplyVertices(final GL10 pGL) { if(GLHelper.EXTENSIONS_VERTEXBUFFEROBJECTS) { final GL11 gl11 = (GL11)pGL; this.mSpriteBatchVertexBuffer.selectOnHardware(gl11); GLHelper.vertexZeroPointer(gl11); } else { GLHelper.vertexPointer(pGL, this.mSpriteBatchVertexBuffer.getFloatBuffer()); } } private void onApplyTextureRegion(final GL10 pGL) { if(GLHelper.EXTENSIONS_VERTEXBUFFEROBJECTS) { final GL11 gl11 = (GL11)pGL; this.mSpriteBatchTextureRegionBuffer.selectOnHardware(gl11); this.mTexture.bind(pGL); GLHelper.texCoordZeroPointer(gl11); } else { this.mTexture.bind(pGL); GLHelper.texCoordPointer(pGL, this.mSpriteBatchTextureRegionBuffer.getFloatBuffer()); } } private void drawVertices(final GL10 pGL, @SuppressWarnings("unused") final Camera pCamera) { pGL.glDrawArrays(GL10.GL_TRIANGLES, 0, this.mVertices); } private void assertCapacity(final int pIndex) { if(pIndex >= this.mCapacity) { throw new IllegalStateException("This supplied pIndex: '" + pIndex + "' is exceeding the capacity: '" + this.mCapacity + "' of this SpriteBatch!"); } } private void assertCapacity() { if(this.mIndex == this.mCapacity) { throw new IllegalStateException("This SpriteBatch has already reached its capacity (" + this.mCapacity + ") !"); } } protected void assertTexture(final BaseTextureRegion pTextureRegion) { if(pTextureRegion.getTexture() != this.mTexture) { throw new IllegalArgumentException("The supplied Texture does match the Texture of this SpriteBatch!"); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/batch/SpriteBatch.java
Java
lgpl
12,899
package org.anddev.andengine.entity.sprite.batch; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.buffer.SpriteBatchTextureRegionBuffer; import org.anddev.andengine.opengl.vertex.SpriteBatchVertexBuffer; /** * (c) Zynga 2011 * * @author Nicolas Gramlich <ngramlich@zynga.com> * @since 21:48:21 - 27.07.2011 */ public abstract class DynamicSpriteBatch extends SpriteBatch { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public DynamicSpriteBatch(final ITexture pTexture, final int pCapacity) { super(pTexture, pCapacity); } public DynamicSpriteBatch(final ITexture pTexture, final int pCapacity, final SpriteBatchVertexBuffer pSpriteBatchVertexBuffer, final SpriteBatchTextureRegionBuffer pSpriteBatchTextureRegionBuffer) { super(pTexture, pCapacity, pSpriteBatchVertexBuffer, pSpriteBatchTextureRegionBuffer); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== /** * @return <code>true</code> to submit, if you made any changes, or <code>false</code> otherwise. */ protected abstract boolean onUpdateSpriteBatch(); @Override protected void begin(final GL10 pGL) { super.begin(pGL); if(this.onUpdateSpriteBatch()) { this.submit(); } } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/batch/DynamicSpriteBatch.java
Java
lgpl
2,235
package org.anddev.andengine.entity.sprite.batch; import java.util.ArrayList; import org.anddev.andengine.entity.IEntity; import org.anddev.andengine.entity.sprite.BaseSprite; import org.anddev.andengine.opengl.texture.ITexture; import org.anddev.andengine.opengl.texture.region.buffer.SpriteBatchTextureRegionBuffer; import org.anddev.andengine.opengl.vertex.SpriteBatchVertexBuffer; import org.anddev.andengine.util.SmartList; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 12:10:35 - 15.06.2011 */ public class SpriteGroup extends DynamicSpriteBatch { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public SpriteGroup(final ITexture pTexture, final int pCapacity) { super(pTexture, pCapacity); /* Make children not be drawn automatically, as we handle the drawing ourself. */ this.setChildrenVisible(false); } public SpriteGroup(final ITexture pTexture, final int pCapacity, final SpriteBatchVertexBuffer pSpriteBatchVertexBuffer, final SpriteBatchTextureRegionBuffer pSpriteBatchTextureRegionBuffer) { super(pTexture, pCapacity, pSpriteBatchVertexBuffer, pSpriteBatchTextureRegionBuffer); /* Make children not be drawn automatically, as we handle the drawing ourself. */ this.setChildrenVisible(false); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== /** * Instead use {@link SpriteGroup#attachChild(BaseSprite)}. */ @Override @Deprecated public void attachChild(final IEntity pEntity) throws IllegalArgumentException { if(pEntity instanceof BaseSprite) { this.attachChild((BaseSprite)pEntity); } else { throw new IllegalArgumentException("A SpriteGroup can only handle children of type BaseSprite or subclasses of BaseSprite, like Sprite, TiledSprite or AnimatedSprite."); } } public void attachChild(final BaseSprite pBaseSprite) { this.assertCapacity(); this.assertTexture(pBaseSprite.getTextureRegion()); super.attachChild(pBaseSprite); } public void attachChildren(final ArrayList<? extends BaseSprite> pBaseSprites) { final int baseSpriteCount = pBaseSprites.size(); for(int i = 0; i < baseSpriteCount; i++) { this.attachChild(pBaseSprites.get(i)); } } @Override protected boolean onUpdateSpriteBatch() { final SmartList<IEntity> children = this.mChildren; if(children == null) { return false; } else { final int childCount = children.size(); for(int i = 0; i < childCount; i++) { super.drawWithoutChecks((BaseSprite)children.get(i)); } return true; } } // =========================================================== // Methods // =========================================================== private void assertCapacity() { if(this.getChildCount() >= this.mCapacity) { throw new IllegalStateException("This SpriteGroup has already reached its capacity (" + this.mCapacity + ") !"); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/batch/SpriteGroup.java
Java
lgpl
3,784
package org.anddev.andengine.entity.sprite; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.engine.camera.Camera; import org.anddev.andengine.entity.primitive.BaseRectangle; import org.anddev.andengine.opengl.texture.region.BaseTextureRegion; import org.anddev.andengine.opengl.texture.region.buffer.TextureRegionBuffer; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:38:53 - 08.03.2010 */ public abstract class BaseSprite extends BaseRectangle { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== protected final BaseTextureRegion mTextureRegion; // =========================================================== // Constructors // =========================================================== public BaseSprite(final float pX, final float pY, final float pWidth, final float pHeight, final BaseTextureRegion pTextureRegion) { super(pX, pY, pWidth, pHeight); this.mTextureRegion = pTextureRegion; this.initBlendFunction(); } public BaseSprite(final float pX, final float pY, final float pWidth, final float pHeight, final BaseTextureRegion pTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pWidth, pHeight, pRectangleVertexBuffer); this.mTextureRegion = pTextureRegion; this.initBlendFunction(); } // =========================================================== // Getter & Setter // =========================================================== public BaseTextureRegion getTextureRegion() { return this.mTextureRegion; } public void setFlippedHorizontal(final boolean pFlippedHorizontal) { this.mTextureRegion.setFlippedHorizontal(pFlippedHorizontal); } public void setFlippedVertical(final boolean pFlippedVertical) { this.mTextureRegion.setFlippedVertical(pFlippedVertical); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void reset() { super.reset(); this.initBlendFunction(); } @Override protected void onInitDraw(final GL10 pGL) { super.onInitDraw(pGL); GLHelper.enableTextures(pGL); GLHelper.enableTexCoordArray(pGL); } @Override protected void doDraw(final GL10 pGL, final Camera pCamera) { this.mTextureRegion.onApply(pGL); super.doDraw(pGL, pCamera); } @Override protected void finalize() throws Throwable { super.finalize(); final TextureRegionBuffer textureRegionBuffer = this.mTextureRegion.getTextureBuffer(); if(textureRegionBuffer.isManaged()) { textureRegionBuffer.unloadFromActiveBufferObjectManager(); } } // =========================================================== // Methods // =========================================================== private void initBlendFunction() { if(this.mTextureRegion.getTexture().getTextureOptions().mPreMultipyAlpha) { this.setBlendFunction(BLENDFUNCTION_SOURCE_PREMULTIPLYALPHA_DEFAULT, BLENDFUNCTION_DESTINATION_PREMULTIPLYALPHA_DEFAULT); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/BaseSprite.java
Java
lgpl
3,682
package org.anddev.andengine.entity.sprite; import java.util.Arrays; import org.anddev.andengine.opengl.texture.region.TiledTextureRegion; import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer; import org.anddev.andengine.util.MathUtils; import org.anddev.andengine.util.constants.TimeConstants; public class AnimatedSprite extends TiledSprite implements TimeConstants { // =========================================================== // Constants // =========================================================== private static final int LOOP_CONTINUOUS = -1; // =========================================================== // Fields // =========================================================== private boolean mAnimationRunning; private long mAnimationProgress; private long mAnimationDuration; private long[] mFrameEndsInNanoseconds; private int mFirstTileIndex; private int mInitialLoopCount; private int mLoopCount; private IAnimationListener mAnimationListener; private int mFrameCount; private int[] mFrames; // =========================================================== // Constructors // =========================================================== public AnimatedSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion) { super(pX, pY, pTiledTextureRegion); } public AnimatedSprite(final float pX, final float pY, final float pTileWidth, final float pTileHeight, final TiledTextureRegion pTiledTextureRegion) { super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion); } public AnimatedSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pTiledTextureRegion, pRectangleVertexBuffer); } public AnimatedSprite(final float pX, final float pY, final float pTileWidth, final float pTileHeight, final TiledTextureRegion pTiledTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion, pRectangleVertexBuffer); } // =========================================================== // Getter & Setter // =========================================================== public boolean isAnimationRunning() { return this.mAnimationRunning; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected void onManagedUpdate(final float pSecondsElapsed) { super.onManagedUpdate(pSecondsElapsed); if(this.mAnimationRunning) { final long nanoSecondsElapsed = (long) (pSecondsElapsed * TimeConstants.NANOSECONDSPERSECOND); this.mAnimationProgress += nanoSecondsElapsed; if(this.mAnimationProgress > this.mAnimationDuration) { this.mAnimationProgress %= this.mAnimationDuration; if(this.mInitialLoopCount != AnimatedSprite.LOOP_CONTINUOUS) { this.mLoopCount--; } } if(this.mInitialLoopCount == AnimatedSprite.LOOP_CONTINUOUS || this.mLoopCount >= 0) { final int currentFrameIndex = this.calculateCurrentFrameIndex(); if(this.mFrames == null) { this.setCurrentTileIndex(this.mFirstTileIndex + currentFrameIndex); } else { this.setCurrentTileIndex(this.mFrames[currentFrameIndex]); } } else { this.mAnimationRunning = false; if(this.mAnimationListener != null) { this.mAnimationListener.onAnimationEnd(this); } } } } // =========================================================== // Methods // =========================================================== public void stopAnimation() { this.mAnimationRunning = false; } public void stopAnimation(final int pTileIndex) { this.mAnimationRunning = false; this.setCurrentTileIndex(pTileIndex); } private int calculateCurrentFrameIndex() { final long animationProgress = this.mAnimationProgress; final long[] frameEnds = this.mFrameEndsInNanoseconds; final int frameCount = this.mFrameCount; for(int i = 0; i < frameCount; i++) { if(frameEnds[i] > animationProgress) { return i; } } return frameCount - 1; } public AnimatedSprite animate(final long pFrameDurationEach) { return this.animate(pFrameDurationEach, true); } public AnimatedSprite animate(final long pFrameDurationEach, final boolean pLoop) { return this.animate(pFrameDurationEach, (pLoop) ? AnimatedSprite.LOOP_CONTINUOUS : 0, null); } public AnimatedSprite animate(final long pFrameDurationEach, final int pLoopCount) { return this.animate(pFrameDurationEach, pLoopCount, null); } public AnimatedSprite animate(final long pFrameDurationEach, final boolean pLoop, final IAnimationListener pAnimationListener) { return this.animate(pFrameDurationEach, (pLoop) ? AnimatedSprite.LOOP_CONTINUOUS : 0, pAnimationListener); } public AnimatedSprite animate(final long pFrameDurationEach, final int pLoopCount, final IAnimationListener pAnimationListener) { final long[] frameDurations = new long[this.getTextureRegion().getTileCount()]; Arrays.fill(frameDurations, pFrameDurationEach); return this.animate(frameDurations, pLoopCount, pAnimationListener); } public AnimatedSprite animate(final long[] pFrameDurations) { return this.animate(pFrameDurations, true); } public AnimatedSprite animate(final long[] pFrameDurations, final boolean pLoop) { return this.animate(pFrameDurations, (pLoop) ? AnimatedSprite.LOOP_CONTINUOUS : 0, null); } public AnimatedSprite animate(final long[] pFrameDurations, final int pLoopCount) { return this.animate(pFrameDurations, pLoopCount, null); } public AnimatedSprite animate(final long[] pFrameDurations, final boolean pLoop, final IAnimationListener pAnimationListener) { return this.animate(pFrameDurations, (pLoop) ? AnimatedSprite.LOOP_CONTINUOUS : 0, pAnimationListener); } public AnimatedSprite animate(final long[] pFrameDurations, final int pLoopCount, final IAnimationListener pAnimationListener) { return this.animate(pFrameDurations, 0, this.getTextureRegion().getTileCount() - 1, pLoopCount, pAnimationListener); } public AnimatedSprite animate(final long[] pFrameDurations, final int pFirstTileIndex, final int pLastTileIndex, final boolean pLoop) { return this.animate(pFrameDurations, pFirstTileIndex, pLastTileIndex, (pLoop) ? AnimatedSprite.LOOP_CONTINUOUS : 0, null); } public AnimatedSprite animate(final long[] pFrameDurations, final int pFirstTileIndex, final int pLastTileIndex, final int pLoopCount) { return this.animate(pFrameDurations, pFirstTileIndex, pLastTileIndex, pLoopCount, null); } public AnimatedSprite animate(final long[] pFrameDurations, final int[] pFrames, final int pLoopCount) { return this.animate(pFrameDurations, pFrames, pLoopCount, null); } /** * Animate specifics frames * * @param pFrameDurations must have the same length as pFrames. * @param pFrames indices of the frames to animate. * @param pLoopCount * @param pAnimationListener */ public AnimatedSprite animate(final long[] pFrameDurations, final int[] pFrames, final int pLoopCount, final IAnimationListener pAnimationListener) { final int frameCount = pFrames.length; if(pFrameDurations.length != frameCount) { throw new IllegalArgumentException("pFrameDurations must have the same length as pFrames."); } return this.init(pFrameDurations, frameCount, pFrames, 0, pLoopCount, pAnimationListener); } /** * @param pFrameDurations * must have the same length as pFirstTileIndex to * pLastTileIndex. * @param pFirstTileIndex * @param pLastTileIndex * @param pLoopCount * @param pAnimationListener */ public AnimatedSprite animate(final long[] pFrameDurations, final int pFirstTileIndex, final int pLastTileIndex, final int pLoopCount, final IAnimationListener pAnimationListener) { if(pLastTileIndex - pFirstTileIndex < 1) { throw new IllegalArgumentException("An animation needs at least two tiles to animate between."); } final int frameCount = (pLastTileIndex - pFirstTileIndex) + 1; if(pFrameDurations.length != frameCount) { throw new IllegalArgumentException("pFrameDurations must have the same length as pFirstTileIndex to pLastTileIndex."); } return this.init(pFrameDurations, frameCount, null, pFirstTileIndex, pLoopCount, pAnimationListener); } private AnimatedSprite init(final long[] pFrameDurations, final int frameCount, final int[] pFrames, final int pFirstTileIndex, final int pLoopCount, final IAnimationListener pAnimationListener) { this.mFrameCount = frameCount; this.mAnimationListener = pAnimationListener; this.mInitialLoopCount = pLoopCount; this.mLoopCount = pLoopCount; this.mFrames = pFrames; this.mFirstTileIndex = pFirstTileIndex; if(this.mFrameEndsInNanoseconds == null || this.mFrameCount > this.mFrameEndsInNanoseconds.length) { this.mFrameEndsInNanoseconds = new long[this.mFrameCount]; } final long[] frameEndsInNanoseconds = this.mFrameEndsInNanoseconds; MathUtils.arraySumInto(pFrameDurations, frameEndsInNanoseconds, TimeConstants.NANOSECONDSPERMILLISECOND); final long lastFrameEnd = frameEndsInNanoseconds[this.mFrameCount - 1]; this.mAnimationDuration = lastFrameEnd; this.mAnimationProgress = 0; this.mAnimationRunning = true; return this; } // =========================================================== // Inner and Anonymous Classes // =========================================================== public static interface IAnimationListener { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== public void onAnimationEnd(final AnimatedSprite pAnimatedSprite); } }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/AnimatedSprite.java
Java
lgpl
10,173
package org.anddev.andengine.entity.sprite; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:22:38 - 09.03.2010 */ public class Sprite extends BaseSprite { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public Sprite(final float pX, final float pY, final TextureRegion pTextureRegion) { super(pX, pY, pTextureRegion.getWidth(), pTextureRegion.getHeight(), pTextureRegion); } public Sprite(final float pX, final float pY, final float pWidth, final float pHeight, final TextureRegion pTextureRegion) { super(pX, pY, pWidth, pHeight, pTextureRegion); } public Sprite(final float pX, final float pY, final TextureRegion pTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pTextureRegion.getWidth(), pTextureRegion.getHeight(), pTextureRegion, pRectangleVertexBuffer); } public Sprite(final float pX, final float pY, final float pWidth, final float pHeight, final TextureRegion pTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pWidth, pHeight, pTextureRegion, pRectangleVertexBuffer); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public TextureRegion getTextureRegion() { return (TextureRegion)this.mTextureRegion; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/Sprite.java
Java
lgpl
2,426
package org.anddev.andengine.entity.sprite; import org.anddev.andengine.opengl.texture.region.TiledTextureRegion; import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:30:13 - 09.03.2010 */ public class TiledSprite extends BaseSprite { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TiledSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion) { super(pX, pY, pTiledTextureRegion.getTileWidth(), pTiledTextureRegion.getTileHeight(), pTiledTextureRegion); } public TiledSprite(final float pX, final float pY, final float pTileWidth, final float pTileHeight, final TiledTextureRegion pTiledTextureRegion) { super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion); } public TiledSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pTiledTextureRegion.getTileWidth(), pTiledTextureRegion.getTileHeight(), pTiledTextureRegion, pRectangleVertexBuffer); } public TiledSprite(final float pX, final float pY, final float pTileWidth, final float pTileHeight, final TiledTextureRegion pTiledTextureRegion, final RectangleVertexBuffer pRectangleVertexBuffer) { super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion, pRectangleVertexBuffer); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public TiledTextureRegion getTextureRegion() { return (TiledTextureRegion)super.getTextureRegion(); } // =========================================================== // Methods // =========================================================== public int getCurrentTileIndex() { return this.getTextureRegion().getCurrentTileIndex(); } public void setCurrentTileIndex(final int pTileIndex) { this.getTextureRegion().setCurrentTileIndex(pTileIndex); } public void setCurrentTileIndex(final int pTileColumn, final int pTileRow) { this.getTextureRegion().setCurrentTileIndex(pTileColumn, pTileRow); } public void nextTile() { this.getTextureRegion().nextTile(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/sprite/TiledSprite.java
Java
lgpl
3,051
package org.anddev.andengine.entity.layer.tiled.tmx; import java.util.ArrayList; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:20:49 - 29.07.2010 */ public class TMXObjectGroup implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final String mName; private final int mWidth; private final int mHeight; private final ArrayList<TMXObject> mTMXObjects = new ArrayList<TMXObject>(); private final TMXProperties<TMXObjectGroupProperty> mTMXObjectGroupProperties = new TMXProperties<TMXObjectGroupProperty>(); // =========================================================== // Constructors // =========================================================== public TMXObjectGroup(final Attributes pAttributes) { this.mName = pAttributes.getValue("", TAG_OBJECTGROUP_ATTRIBUTE_NAME); this.mWidth = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_OBJECTGROUP_ATTRIBUTE_WIDTH); this.mHeight = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_OBJECTGROUP_ATTRIBUTE_HEIGHT); } // =========================================================== // Getter & Setter // =========================================================== public String getName() { return this.mName; } public int getWidth() { return this.mWidth; } public int getHeight() { return this.mHeight; } void addTMXObject(final TMXObject pTMXObject) { this.mTMXObjects.add(pTMXObject); } public ArrayList<TMXObject> getTMXObjects() { return this.mTMXObjects ; } public void addTMXObjectGroupProperty(final TMXObjectGroupProperty pTMXObjectGroupProperty) { this.mTMXObjectGroupProperties.add(pTMXObjectGroupProperty); } public TMXProperties<TMXObjectGroupProperty> getTMXObjectGroupProperties() { return this.mTMXObjectGroupProperties; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXObjectGroup.java
Java
lgpl
2,776
package org.anddev.andengine.entity.layer.tiled.tmx; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException; import org.anddev.andengine.opengl.texture.TextureManager; import org.anddev.andengine.opengl.texture.TextureOptions; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:10:45 - 20.07.2010 */ public class TMXLoader { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Context mContext; private final TextureManager mTextureManager; private final TextureOptions mTextureOptions; private final ITMXTilePropertiesListener mTMXTilePropertyListener; // =========================================================== // Constructors // =========================================================== public TMXLoader(final Context pContext, final TextureManager pTextureManager) { this(pContext, pTextureManager, TextureOptions.DEFAULT); } public TMXLoader(final Context pContext, final TextureManager pTextureManager, final TextureOptions pTextureOptions) { this(pContext, pTextureManager, pTextureOptions, null); } public TMXLoader(final Context pContext, final TextureManager pTextureManager, final ITMXTilePropertiesListener pTMXTilePropertyListener) { this(pContext, pTextureManager, TextureOptions.DEFAULT, pTMXTilePropertyListener); } public TMXLoader(final Context pContext, final TextureManager pTextureManager, final TextureOptions pTextureOptions, final ITMXTilePropertiesListener pTMXTilePropertyListener) { this.mContext = pContext; this.mTextureManager = pTextureManager; this.mTextureOptions = pTextureOptions; this.mTMXTilePropertyListener = pTMXTilePropertyListener; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public TMXTiledMap loadFromAsset(final Context pContext, final String pAssetPath) throws TMXLoadException { try { return this.load(pContext.getAssets().open(pAssetPath)); } catch (final IOException e) { throw new TMXLoadException("Could not load TMXTiledMap from asset: " + pAssetPath, e); } } public TMXTiledMap load(final InputStream pInputStream) throws TMXLoadException { try{ final SAXParserFactory spf = SAXParserFactory.newInstance(); final SAXParser sp = spf.newSAXParser(); final XMLReader xr = sp.getXMLReader(); final TMXParser tmxParser = new TMXParser(this.mContext, this.mTextureManager, this.mTextureOptions, this.mTMXTilePropertyListener); xr.setContentHandler(tmxParser); xr.parse(new InputSource(new BufferedInputStream(pInputStream))); return tmxParser.getTMXTiledMap(); } catch (final SAXException e) { throw new TMXLoadException(e); } catch (final ParserConfigurationException pe) { /* Doesn't happen. */ return null; } catch (final IOException e) { throw new TMXLoadException(e); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== public interface ITMXTilePropertiesListener { // =========================================================== // Final Fields // =========================================================== // =========================================================== // Methods // =========================================================== public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties); } }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXLoader.java
Java
lgpl
4,644
package org.anddev.andengine.entity.layer.tiled.tmx; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXParseException; import org.anddev.andengine.opengl.texture.TextureManager; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:37:32 - 08.08.2010 */ public class TSXParser extends DefaultHandler implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Context mContext; private final TextureManager mTextureManager; private final TextureOptions mTextureOptions; private TMXTileSet mTMXTileSet; private int mLastTileSetTileID; @SuppressWarnings("unused") private boolean mInTileset; @SuppressWarnings("unused") private boolean mInImage; @SuppressWarnings("unused") private boolean mInTile; @SuppressWarnings("unused") private boolean mInProperties; @SuppressWarnings("unused") private boolean mInProperty; private final int mFirstGlobalTileID; // =========================================================== // Constructors // =========================================================== public TSXParser(final Context pContext, final TextureManager pTextureManager, final TextureOptions pTextureOptions, final int pFirstGlobalTileID) { this.mContext = pContext; this.mTextureManager = pTextureManager; this.mTextureOptions = pTextureOptions; this.mFirstGlobalTileID = pFirstGlobalTileID; } // =========================================================== // Getter & Setter // =========================================================== TMXTileSet getTMXTileSet() { return this.mTMXTileSet; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException { if(pLocalName.equals(TAG_TILESET)){ this.mInTileset = true; this.mTMXTileSet = new TMXTileSet(this.mFirstGlobalTileID, pAttributes, this.mTextureOptions); } else if(pLocalName.equals(TAG_IMAGE)){ this.mInImage = true; this.mTMXTileSet.setImageSource(this.mContext, this.mTextureManager, pAttributes); } else if(pLocalName.equals(TAG_TILE)) { this.mInTile = true; this.mLastTileSetTileID = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TILE_ATTRIBUTE_ID); } else if(pLocalName.equals(TAG_PROPERTIES)) { this.mInProperties = true; } else if(pLocalName.equals(TAG_PROPERTY)) { this.mInProperty = true; this.mTMXTileSet.addTMXTileProperty(this.mLastTileSetTileID, new TMXTileProperty(pAttributes)); } else { throw new TMXParseException("Unexpected start tag: '" + pLocalName + "'."); } } @Override public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException { if(pLocalName.equals(TAG_TILESET)){ this.mInTileset = false; } else if(pLocalName.equals(TAG_IMAGE)){ this.mInImage = false; } else if(pLocalName.equals(TAG_TILE)) { this.mInTile = false; } else if(pLocalName.equals(TAG_PROPERTIES)) { this.mInProperties = false; } else if(pLocalName.equals(TAG_PROPERTY)) { this.mInProperty = false; } else { throw new TMXParseException("Unexpected end tag: '" + pLocalName + "'."); } } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TSXParser.java
Java
lgpl
4,369
package org.anddev.andengine.entity.layer.tiled.tmx; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:21:01 - 29.07.2010 */ public class TMXObject implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final String mName; private final String mType; private final int mX; private final int mY; private final int mWidth; private final int mHeight; private final TMXProperties<TMXObjectProperty> mTMXObjectProperties = new TMXProperties<TMXObjectProperty>(); // =========================================================== // Constructors // =========================================================== public TMXObject(final Attributes pAttributes) { this.mName = pAttributes.getValue("", TAG_OBJECT_ATTRIBUTE_NAME); this.mType = pAttributes.getValue("", TAG_OBJECT_ATTRIBUTE_TYPE); this.mX = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_OBJECT_ATTRIBUTE_X); this.mY = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_OBJECT_ATTRIBUTE_Y); this.mWidth = SAXUtils.getIntAttribute(pAttributes, TAG_OBJECT_ATTRIBUTE_WIDTH, 0); this.mHeight = SAXUtils.getIntAttribute(pAttributes, TAG_OBJECT_ATTRIBUTE_HEIGHT, 0); } // =========================================================== // Getter & Setter // =========================================================== public String getName() { return this.mName; } public String getType() { return this.mType; } public int getX() { return this.mX; } public int getY() { return this.mY; } public int getWidth() { return this.mWidth; } public int getHeight() { return this.mHeight; } public void addTMXObjectProperty(final TMXObjectProperty pTMXObjectProperty) { this.mTMXObjectProperties.add(pTMXObjectProperty); } public TMXProperties<TMXObjectProperty> getTMXObjectProperties() { return this.mTMXObjectProperties; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXObject.java
Java
lgpl
2,865
package org.anddev.andengine.entity.layer.tiled.tmx; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL11; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; import android.util.SparseArray; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:38:11 - 20.07.2010 */ public class TMXTiledMap implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final String mOrientation; private final int mTileColumns; private final int mTilesRows; private final int mTileWidth; private final int mTileHeight; private final ArrayList<TMXTileSet> mTMXTileSets = new ArrayList<TMXTileSet>(); private final ArrayList<TMXLayer> mTMXLayers = new ArrayList<TMXLayer>(); private final ArrayList<TMXObjectGroup> mTMXObjectGroups = new ArrayList<TMXObjectGroup>(); private final RectangleVertexBuffer mSharedVertexBuffer; private final SparseArray<TextureRegion> mGlobalTileIDToTextureRegionCache = new SparseArray<TextureRegion>(); private final SparseArray<TMXProperties<TMXTileProperty>> mGlobalTileIDToTMXTilePropertiesCache = new SparseArray<TMXProperties<TMXTileProperty>>(); private final TMXProperties<TMXTiledMapProperty> mTMXTiledMapProperties = new TMXProperties<TMXTiledMapProperty>(); // =========================================================== // Constructors // =========================================================== TMXTiledMap(final Attributes pAttributes) { this.mOrientation = pAttributes.getValue("", TAG_MAP_ATTRIBUTE_ORIENTATION); if(!this.mOrientation.equals(TAG_MAP_ATTRIBUTE_ORIENTATION_VALUE_ORTHOGONAL)) { throw new IllegalArgumentException(TAG_MAP_ATTRIBUTE_ORIENTATION + ": '" + this.mOrientation + "' is not supported."); } this.mTileColumns = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_MAP_ATTRIBUTE_WIDTH); this.mTilesRows = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_MAP_ATTRIBUTE_HEIGHT); this.mTileWidth = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_MAP_ATTRIBUTE_TILEWIDTH); this.mTileHeight = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_MAP_ATTRIBUTE_TILEHEIGHT); this.mSharedVertexBuffer = new RectangleVertexBuffer(GL11.GL_STATIC_DRAW, true); this.mSharedVertexBuffer.update(this.mTileWidth, this.mTileHeight); } // =========================================================== // Getter & Setter // =========================================================== public final String getOrientation() { return this.mOrientation; } /** * @deprecated Instead use {@link TMXTiledMap#getTileColumns()} * {@link TMXTiledMap#getTileWidth()}. * @return */ @Deprecated public final int getWidth() { return this.mTileColumns; } public final int getTileColumns() { return this.mTileColumns; } /** * @deprecated Instead use {@link TMXTiledMap#getTileRows()} * {@link TMXTiledMap#getTileHeight()}. * @return */ @Deprecated public final int getHeight() { return this.mTilesRows; } public final int getTileRows() { return this.mTilesRows; } public final int getTileWidth() { return this.mTileWidth; } public final int getTileHeight() { return this.mTileHeight; } public RectangleVertexBuffer getSharedVertexBuffer() { return this.mSharedVertexBuffer; } void addTMXTileSet(final TMXTileSet pTMXTileSet) { this.mTMXTileSets.add(pTMXTileSet); } public ArrayList<TMXTileSet> getTMXTileSets() { return this.mTMXTileSets; } void addTMXLayer(final TMXLayer pTMXLayer) { this.mTMXLayers.add(pTMXLayer); } public ArrayList<TMXLayer> getTMXLayers() { return this.mTMXLayers; } void addTMXObjectGroup(final TMXObjectGroup pTMXObjectGroup) { this.mTMXObjectGroups.add(pTMXObjectGroup); } public ArrayList<TMXObjectGroup> getTMXObjectGroups() { return this.mTMXObjectGroups; } public TMXProperties<TMXTileProperty> getTMXTilePropertiesByGlobalTileID(final int pGlobalTileID) { return this.mGlobalTileIDToTMXTilePropertiesCache.get(pGlobalTileID); } public void addTMXTiledMapProperty(final TMXTiledMapProperty pTMXTiledMapProperty) { this.mTMXTiledMapProperties.add(pTMXTiledMapProperty); } public TMXProperties<TMXTiledMapProperty> getTMXTiledMapProperties() { return this.mTMXTiledMapProperties; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected void finalize() throws Throwable { if(this.mSharedVertexBuffer.isManaged()) { this.mSharedVertexBuffer.unloadFromActiveBufferObjectManager(); } } // =========================================================== // Methods // =========================================================== public TMXProperties<TMXTileProperty> getTMXTileProperties(final int pGlobalTileID) { final SparseArray<TMXProperties<TMXTileProperty>> globalTileIDToTMXTilePropertiesCache = this.mGlobalTileIDToTMXTilePropertiesCache; final TMXProperties<TMXTileProperty> cachedTMXTileProperties = globalTileIDToTMXTilePropertiesCache.get(pGlobalTileID); if(cachedTMXTileProperties != null) { return cachedTMXTileProperties; } else { final ArrayList<TMXTileSet> tmxTileSets = this.mTMXTileSets; for(int i = tmxTileSets.size() - 1; i >= 0; i--) { final TMXTileSet tmxTileSet = tmxTileSets.get(i); if(pGlobalTileID >= tmxTileSet.getFirstGlobalTileID()) { return tmxTileSet.getTMXTilePropertiesFromGlobalTileID(pGlobalTileID); } } throw new IllegalArgumentException("No TMXTileProperties found for pGlobalTileID=" + pGlobalTileID); } } public TextureRegion getTextureRegionFromGlobalTileID(final int pGlobalTileID) { final SparseArray<TextureRegion> globalTileIDToTextureRegionCache = this.mGlobalTileIDToTextureRegionCache; final TextureRegion cachedTextureRegion = globalTileIDToTextureRegionCache.get(pGlobalTileID); if(cachedTextureRegion != null) { return cachedTextureRegion; } else { final ArrayList<TMXTileSet> tmxTileSets = this.mTMXTileSets; for(int i = tmxTileSets.size() - 1; i >= 0; i--) { final TMXTileSet tmxTileSet = tmxTileSets.get(i); if(pGlobalTileID >= tmxTileSet.getFirstGlobalTileID()) { final TextureRegion textureRegion = tmxTileSet.getTextureRegionFromGlobalTileID(pGlobalTileID); /* Add to cache for the all future pGlobalTileIDs with the same value. */ globalTileIDToTextureRegionCache.put(pGlobalTileID, textureRegion); return textureRegion; } } throw new IllegalArgumentException("No TextureRegion found for pGlobalTileID=" + pGlobalTileID); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXTiledMap.java
Java
lgpl
7,460
package org.anddev.andengine.entity.layer.tiled.tmx; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXParseException; import org.anddev.andengine.opengl.texture.TextureManager; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasFactory; import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.ColorKeyBitmapTextureAtlasSourceDecorator; import org.anddev.andengine.opengl.texture.atlas.bitmap.source.decorator.shape.RectangleBitmapTextureAtlasSourceDecoratorShape; import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture.BitmapTextureFormat; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; import android.content.Context; import android.graphics.Color; import android.util.SparseArray; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:03:24 - 20.07.2010 */ public class TMXTileSet implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final int mFirstGlobalTileID; private final String mName; private final int mTileWidth; private final int mTileHeight; private String mImageSource; private BitmapTextureAtlas mBitmapTextureAtlas; private final TextureOptions mTextureOptions; private int mTilesHorizontal; @SuppressWarnings("unused") private int mTilesVertical; private final int mSpacing; private final int mMargin; private final SparseArray<TMXProperties<TMXTileProperty>> mTMXTileProperties = new SparseArray<TMXProperties<TMXTileProperty>>(); // =========================================================== // Constructors // =========================================================== TMXTileSet(final Attributes pAttributes, final TextureOptions pTextureOptions) { this(SAXUtils.getIntAttribute(pAttributes, TAG_TILESET_ATTRIBUTE_FIRSTGID, 1), pAttributes, pTextureOptions); } TMXTileSet(final int pFirstGlobalTileID, final Attributes pAttributes, final TextureOptions pTextureOptions) { this.mFirstGlobalTileID = pFirstGlobalTileID; this.mName = pAttributes.getValue("", TAG_TILESET_ATTRIBUTE_NAME); this.mTileWidth = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TILESET_ATTRIBUTE_TILEWIDTH); this.mTileHeight = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TILESET_ATTRIBUTE_TILEHEIGHT); this.mSpacing = SAXUtils.getIntAttribute(pAttributes, TAG_TILESET_ATTRIBUTE_SPACING, 0); this.mMargin = SAXUtils.getIntAttribute(pAttributes, TAG_TILESET_ATTRIBUTE_MARGIN, 0); this.mTextureOptions = pTextureOptions; } // =========================================================== // Getter & Setter // =========================================================== public final int getFirstGlobalTileID() { return this.mFirstGlobalTileID; } public final String getName() { return this.mName; } public final int getTileWidth() { return this.mTileWidth; } public final int getTileHeight() { return this.mTileHeight; } public BitmapTextureAtlas getBitmapTextureAtlas() { return this.mBitmapTextureAtlas; } public void setImageSource(final Context pContext, final TextureManager pTextureManager, final Attributes pAttributes) throws TMXParseException { this.mImageSource = pAttributes.getValue("", TAG_IMAGE_ATTRIBUTE_SOURCE); final AssetBitmapTextureAtlasSource assetBitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(pContext, this.mImageSource); this.mTilesHorizontal = TMXTileSet.determineCount(assetBitmapTextureAtlasSource.getWidth(), this.mTileWidth, this.mMargin, this.mSpacing); this.mTilesVertical = TMXTileSet.determineCount(assetBitmapTextureAtlasSource.getHeight(), this.mTileHeight, this.mMargin, this.mSpacing); this.mBitmapTextureAtlas = BitmapTextureAtlasFactory.createForTextureAtlasSourceSize(BitmapTextureFormat.RGBA_8888, assetBitmapTextureAtlasSource, this.mTextureOptions); // TODO Make TextureFormat variable final String transparentColor = SAXUtils.getAttribute(pAttributes, TAG_IMAGE_ATTRIBUTE_TRANS, null); if(transparentColor == null) { BitmapTextureAtlasTextureRegionFactory.createFromSource(this.mBitmapTextureAtlas, assetBitmapTextureAtlasSource, 0, 0); } else { try{ final int color = Color.parseColor((transparentColor.charAt(0) == '#') ? transparentColor : "#" + transparentColor); BitmapTextureAtlasTextureRegionFactory.createFromSource(this.mBitmapTextureAtlas, new ColorKeyBitmapTextureAtlasSourceDecorator(assetBitmapTextureAtlasSource, RectangleBitmapTextureAtlasSourceDecoratorShape.getDefaultInstance(), color), 0, 0); } catch (final IllegalArgumentException e) { throw new TMXParseException("Illegal value: '" + transparentColor + "' for attribute 'trans' supplied!", e); } } pTextureManager.loadTexture(this.mBitmapTextureAtlas); } public String getImageSource() { return this.mImageSource; } public SparseArray<TMXProperties<TMXTileProperty>> getTMXTileProperties() { return this.mTMXTileProperties; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public TMXProperties<TMXTileProperty> getTMXTilePropertiesFromGlobalTileID(final int pGlobalTileID) { final int localTileID = pGlobalTileID - this.mFirstGlobalTileID; return this.mTMXTileProperties.get(localTileID); } public void addTMXTileProperty(final int pLocalTileID, final TMXTileProperty pTMXTileProperty) { final TMXProperties<TMXTileProperty> existingProperties = this.mTMXTileProperties.get(pLocalTileID); if(existingProperties != null) { existingProperties.add(pTMXTileProperty); } else { final TMXProperties<TMXTileProperty> newProperties = new TMXProperties<TMXTileProperty>(); newProperties.add(pTMXTileProperty); this.mTMXTileProperties.put(pLocalTileID, newProperties); } } public TextureRegion getTextureRegionFromGlobalTileID(final int pGlobalTileID) { final int localTileID = pGlobalTileID - this.mFirstGlobalTileID; final int tileColumn = localTileID % this.mTilesHorizontal; final int tileRow = localTileID / this.mTilesHorizontal; final int texturePositionX = this.mMargin + (this.mSpacing + this.mTileWidth) * tileColumn; final int texturePositionY = this.mMargin + (this.mSpacing + this.mTileHeight) * tileRow; return new TextureRegion(this.mBitmapTextureAtlas, texturePositionX, texturePositionY, this.mTileWidth, this.mTileHeight); } private static int determineCount(final int pTotalExtent, final int pTileExtent, final int pMargin, final int pSpacing) { int count = 0; int remainingExtent = pTotalExtent; remainingExtent -= pMargin * 2; while(remainingExtent > 0) { remainingExtent -= pTileExtent; remainingExtent -= pSpacing; count++; } return count; } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXTileSet.java
Java
lgpl
7,980
package org.anddev.andengine.entity.layer.tiled.tmx; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TSXLoadException; import org.anddev.andengine.opengl.texture.TextureManager; import org.anddev.andengine.opengl.texture.TextureOptions; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 17:18:37 - 08.08.2010 */ public class TSXLoader { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Context mContext; private final TextureManager mTextureManager; private final TextureOptions mTextureOptions; // =========================================================== // Constructors // =========================================================== public TSXLoader(final Context pContext, final TextureManager pTextureManager, final TextureOptions pTextureOptions) { this.mContext = pContext; this.mTextureManager = pTextureManager; this.mTextureOptions = pTextureOptions; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public TMXTileSet loadFromAsset(final Context pContext, final int pFirstGlobalTileID, final String pAssetPath) throws TSXLoadException { try { return this.load(pFirstGlobalTileID, pContext.getAssets().open(pAssetPath)); } catch (final IOException e) { throw new TSXLoadException("Could not load TMXTileSet from asset: " + pAssetPath, e); } } private TMXTileSet load(final int pFirstGlobalTileID, final InputStream pInputStream) throws TSXLoadException { try{ final SAXParserFactory spf = SAXParserFactory.newInstance(); final SAXParser sp = spf.newSAXParser(); final XMLReader xr = sp.getXMLReader(); final TSXParser tsxParser = new TSXParser(this.mContext, this.mTextureManager, this.mTextureOptions, pFirstGlobalTileID); xr.setContentHandler(tsxParser); xr.parse(new InputSource(new BufferedInputStream(pInputStream))); return tsxParser.getTMXTileSet(); } catch (final SAXException e) { throw new TSXLoadException(e); } catch (final ParserConfigurationException pe) { /* Doesn't happen. */ return null; } catch (final IOException e) { throw new TSXLoadException(e); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TSXLoader.java
Java
lgpl
3,411
package org.anddev.andengine.entity.layer.tiled.tmx.util.exception; import org.xml.sax.SAXException; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 00:10:02 - 28.07.2010 */ public class TMXParseException extends SAXException { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = 2213964295487921492L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXParseException() { super(); } public TMXParseException(final String pDetailMessage) { super(pDetailMessage); } public TMXParseException(final Exception pException) { super(pException); } public TMXParseException(final String pMessage, final Exception pException) { super(pMessage, pException); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/exception/TMXParseException.java
Java
lgpl
1,788
package org.anddev.andengine.entity.layer.tiled.tmx.util.exception; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 00:10:02 - 28.07.2010 */ public class TMXLoadException extends TMXException { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = -8295358631698809883L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXLoadException() { super(); } public TMXLoadException(final String pDetailMessage, final Throwable pThrowable) { super(pDetailMessage, pThrowable); } public TMXLoadException(final String pDetailMessage) { super(pDetailMessage); } public TMXLoadException(final Throwable pThrowable) { super(pThrowable); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/exception/TMXLoadException.java
Java
lgpl
1,760
package org.anddev.andengine.entity.layer.tiled.tmx.util.exception; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:37:53 - 08.08.2010 */ public class TSXLoadException extends TMXException { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = 10055223972707703L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TSXLoadException() { super(); } public TSXLoadException(final String pDetailMessage, final Throwable pThrowable) { super(pDetailMessage, pThrowable); } public TSXLoadException(final String pDetailMessage) { super(pDetailMessage); } public TSXLoadException(final Throwable pThrowable) { super(pThrowable); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/exception/TSXLoadException.java
Java
lgpl
1,757
package org.anddev.andengine.entity.layer.tiled.tmx.util.exception; import org.xml.sax.SAXException; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:37:46 - 08.08.2010 */ public class TSXParseException extends SAXException { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = -7598783248970268198L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TSXParseException() { super(); } public TSXParseException(final String pDetailMessage) { super(pDetailMessage); } public TSXParseException(final Exception pException) { super(pException); } public TSXParseException(final String pMessage, final Exception pException) { super(pMessage, pException); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/exception/TSXParseException.java
Java
lgpl
1,789
package org.anddev.andengine.entity.layer.tiled.tmx.util.exception; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 17:20:25 - 08.08.2010 */ public abstract class TMXException extends Exception { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = 337819550394833109L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXException() { super(); } public TMXException(final String pDetailMessage, final Throwable pThrowable) { super(pDetailMessage, pThrowable); } public TMXException(final String pDetailMessage) { super(pDetailMessage); } public TMXException(final Throwable pThrowable) { super(pThrowable); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/exception/TMXException.java
Java
lgpl
1,744
package org.anddev.andengine.entity.layer.tiled.tmx.util.constants; /** * See: <a href="http://sourceforge.net/apps/mediawiki/tiled/index.php?title=TMX_Map_Format">TMX Map Format</a>. * * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:20:22 - 20.07.2010 */ public interface TMXConstants { // =========================================================== // Final Fields // =========================================================== public static final int BYTES_PER_GLOBALTILEID = 4; public static final String TAG_MAP = "map"; public static final String TAG_MAP_ATTRIBUTE_ORIENTATION = "orientation"; public static final String TAG_MAP_ATTRIBUTE_ORIENTATION_VALUE_ORTHOGONAL = "orthogonal"; public static final String TAG_MAP_ATTRIBUTE_ORIENTATION_VALUE_ISOMETRIC = "isometric"; public static final String TAG_MAP_ATTRIBUTE_WIDTH = "width"; public static final String TAG_MAP_ATTRIBUTE_HEIGHT = "height"; public static final String TAG_MAP_ATTRIBUTE_TILEWIDTH = "tilewidth"; public static final String TAG_MAP_ATTRIBUTE_TILEHEIGHT = "tileheight"; public static final String TAG_TILESET = "tileset"; public static final String TAG_TILESET_ATTRIBUTE_FIRSTGID = "firstgid"; public static final String TAG_TILESET_ATTRIBUTE_SOURCE = "source"; public static final String TAG_TILESET_ATTRIBUTE_NAME = "name"; public static final String TAG_TILESET_ATTRIBUTE_TILEWIDTH = "tilewidth"; public static final String TAG_TILESET_ATTRIBUTE_TILEHEIGHT = "tileheight"; public static final String TAG_TILESET_ATTRIBUTE_SPACING = "spacing"; public static final String TAG_TILESET_ATTRIBUTE_MARGIN = "margin"; public static final String TAG_IMAGE = "image"; public static final String TAG_IMAGE_ATTRIBUTE_SOURCE = "source"; public static final String TAG_IMAGE_ATTRIBUTE_TRANS = "trans"; public static final String TAG_TILE = "tile"; public static final String TAG_TILE_ATTRIBUTE_ID = "id"; public static final String TAG_TILE_ATTRIBUTE_GID = "gid"; public static final String TAG_PROPERTIES = "properties"; public static final String TAG_PROPERTY = "property"; public static final String TAG_PROPERTY_ATTRIBUTE_NAME = "name"; public static final String TAG_PROPERTY_ATTRIBUTE_VALUE = "value"; public static final String TAG_LAYER = "layer"; public static final String TAG_LAYER_ATTRIBUTE_NAME = "name"; public static final String TAG_LAYER_ATTRIBUTE_WIDTH = "width"; public static final String TAG_LAYER_ATTRIBUTE_HEIGHT = "height"; public static final String TAG_LAYER_ATTRIBUTE_VISIBLE = "visible"; public static final int TAG_LAYER_ATTRIBUTE_VISIBLE_VALUE_DEFAULT = 1; public static final String TAG_LAYER_ATTRIBUTE_OPACITY = "opacity"; public static final float TAG_LAYER_ATTRIBUTE_OPACITY_VALUE_DEFAULT = 1.0f; public static final String TAG_DATA = "data"; public static final String TAG_DATA_ATTRIBUTE_ENCODING = "encoding"; public static final String TAG_DATA_ATTRIBUTE_ENCODING_VALUE_BASE64 = "base64"; public static final String TAG_DATA_ATTRIBUTE_COMPRESSION = "compression"; public static final String TAG_DATA_ATTRIBUTE_COMPRESSION_VALUE_GZIP = "gzip"; public static final String TAG_DATA_ATTRIBUTE_COMPRESSION_VALUE_ZLIB = "zlib"; public static final String TAG_OBJECTGROUP = "objectgroup"; public static final String TAG_OBJECTGROUP_ATTRIBUTE_NAME = "name"; public static final String TAG_OBJECTGROUP_ATTRIBUTE_WIDTH = "width"; public static final String TAG_OBJECTGROUP_ATTRIBUTE_HEIGHT = "height"; public static final String TAG_OBJECT = "object"; public static final String TAG_OBJECT_ATTRIBUTE_NAME = "name"; public static final String TAG_OBJECT_ATTRIBUTE_TYPE = "type"; public static final String TAG_OBJECT_ATTRIBUTE_X = "x"; public static final String TAG_OBJECT_ATTRIBUTE_Y = "y"; public static final String TAG_OBJECT_ATTRIBUTE_WIDTH = "width"; public static final String TAG_OBJECT_ATTRIBUTE_HEIGHT = "height"; // =========================================================== // Methods // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/util/constants/TMXConstants.java
Java
lgpl
4,166
package org.anddev.andengine.entity.layer.tiled.tmx; import org.anddev.andengine.opengl.texture.region.TextureRegion; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:39:48 - 05.08.2010 */ public class TMXTile { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== int mGlobalTileID; private final int mTileRow; private final int mTileColumn; private final int mTileWidth; private final int mTileHeight; TextureRegion mTextureRegion; // =========================================================== // Constructors // =========================================================== public TMXTile(final int pGlobalTileID, final int pTileColumn, final int pTileRow, final int pTileWidth, final int pTileHeight, final TextureRegion pTextureRegion) { this.mGlobalTileID = pGlobalTileID; this.mTileRow = pTileRow; this.mTileColumn = pTileColumn; this.mTileWidth = pTileWidth; this.mTileHeight = pTileHeight; this.mTextureRegion = pTextureRegion; } // =========================================================== // Getter & Setter // =========================================================== public int getGlobalTileID() { return this.mGlobalTileID; } public int getTileRow() { return this.mTileRow; } public int getTileColumn() { return this.mTileColumn; } public int getTileX() { return this.mTileColumn * this.mTileWidth; } public int getTileY() { return this.mTileRow * this.mTileHeight; } public int getTileWidth() { return this.mTileWidth; } public int getTileHeight() { return this.mTileHeight; } public TextureRegion getTextureRegion() { return this.mTextureRegion; } /** * Note this will also set the {@link TextureRegion} with the associated pGlobalTileID of the {@link TMXTiledMap}. * @param pTMXTiledMap * @param pGlobalTileID */ public void setGlobalTileID(final TMXTiledMap pTMXTiledMap, final int pGlobalTileID) { this.mGlobalTileID = pGlobalTileID; this.mTextureRegion = pTMXTiledMap.getTextureRegionFromGlobalTileID(pGlobalTileID); } /** * You'd probably want to call {@link TMXTile#setGlobalTileID(TMXTiledMap, int)} instead. * @param pTextureRegion */ public void setTextureRegion(final TextureRegion pTextureRegion) { this.mTextureRegion = pTextureRegion; } public TMXProperties<TMXTileProperty> getTMXTileProperties(final TMXTiledMap pTMXTiledMap) { return pTMXTiledMap.getTMXTileProperties(this.mGlobalTileID); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXTile.java
Java
lgpl
3,300
package org.anddev.andengine.entity.layer.tiled.tmx; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:48:41 - 12.10.2010 */ public class TMXTiledMapProperty extends TMXProperty { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXTiledMapProperty(final Attributes pAttributes) { super(pAttributes); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXTiledMapProperty.java
Java
lgpl
1,449
package org.anddev.andengine.entity.layer.tiled.tmx; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_X; import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_Y; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import org.anddev.andengine.collision.RectangularShapeCollisionChecker; import org.anddev.andengine.engine.camera.Camera; import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader.ITMXTilePropertiesListener; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.entity.shape.RectangularShape; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.util.GLHelper; import org.anddev.andengine.util.Base64; import org.anddev.andengine.util.Base64InputStream; import org.anddev.andengine.util.MathUtils; import org.anddev.andengine.util.SAXUtils; import org.anddev.andengine.util.StreamUtils; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 20:27:31 - 20.07.2010 */ public class TMXLayer extends RectangularShape implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final TMXTiledMap mTMXTiledMap; private final String mName; private final int mTileColumns; private final int mTileRows; private final TMXTile[][] mTMXTiles; private int mTilesAdded; private final int mGlobalTileIDsExpected; private final float[] mCullingVertices = new float[2 * 4]; private final TMXProperties<TMXLayerProperty> mTMXLayerProperties = new TMXProperties<TMXLayerProperty>(); // =========================================================== // Constructors // =========================================================== public TMXLayer(final TMXTiledMap pTMXTiledMap, final Attributes pAttributes) { super(0, 0, 0, 0, null); this.mTMXTiledMap = pTMXTiledMap; this.mName = pAttributes.getValue("", TAG_LAYER_ATTRIBUTE_NAME); this.mTileColumns = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_LAYER_ATTRIBUTE_WIDTH); this.mTileRows = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_LAYER_ATTRIBUTE_HEIGHT); this.mTMXTiles = new TMXTile[this.mTileRows][this.mTileColumns]; super.mWidth = pTMXTiledMap.getTileWidth() * this.mTileColumns; final float width = super.mWidth; super.mBaseWidth = width; super.mHeight = pTMXTiledMap.getTileHeight() * this.mTileRows; final float height = super.mHeight; super.mBaseHeight = height; this.mRotationCenterX = width * 0.5f; this.mRotationCenterY = height * 0.5f; this.mScaleCenterX = this.mRotationCenterX; this.mScaleCenterY = this.mRotationCenterY; this.mGlobalTileIDsExpected = this.mTileColumns * this.mTileRows; this.setVisible(SAXUtils.getIntAttribute(pAttributes, TAG_LAYER_ATTRIBUTE_VISIBLE, TAG_LAYER_ATTRIBUTE_VISIBLE_VALUE_DEFAULT) == 1); this.setAlpha(SAXUtils.getFloatAttribute(pAttributes, TAG_LAYER_ATTRIBUTE_OPACITY, TAG_LAYER_ATTRIBUTE_OPACITY_VALUE_DEFAULT)); } // =========================================================== // Getter & Setter // =========================================================== public String getName() { return this.mName; } public int getTileColumns() { return this.mTileColumns; } public int getTileRows() { return this.mTileRows; } public TMXTile[][] getTMXTiles() { return this.mTMXTiles; } public TMXTile getTMXTile(final int pTileColumn, final int pTileRow) throws ArrayIndexOutOfBoundsException { return this.mTMXTiles[pTileRow][pTileColumn]; } /** * @param pX in SceneCoordinates. * @param pY in SceneCoordinates. * @return the {@link TMXTile} located at <code>pX/pY</code>. */ public TMXTile getTMXTileAt(final float pX, final float pY) { final float[] localCoords = this.convertSceneToLocalCoordinates(pX, pY); final TMXTiledMap tmxTiledMap = this.mTMXTiledMap; final int tileColumn = (int)(localCoords[VERTEX_INDEX_X] / tmxTiledMap.getTileWidth()); if(tileColumn < 0 || tileColumn > this.mTileColumns - 1) { return null; } final int tileRow = (int)(localCoords[VERTEX_INDEX_Y] / tmxTiledMap.getTileWidth()); if(tileRow < 0 || tileRow > this.mTileRows - 1) { return null; } return this.mTMXTiles[tileRow][tileColumn]; } public void addTMXLayerProperty(final TMXLayerProperty pTMXLayerProperty) { this.mTMXLayerProperties.add(pTMXLayerProperty); } public TMXProperties<TMXLayerProperty> getTMXLayerProperties() { return this.mTMXLayerProperties; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override @Deprecated public void setRotation(final float pRotation) { } @Override protected void onUpdateVertexBuffer() { /* Nothing. */ } @Override protected void onInitDraw(final GL10 pGL) { super.onInitDraw(pGL); GLHelper.enableTextures(pGL); GLHelper.enableTexCoordArray(pGL); } @Override protected void onApplyVertices(final GL10 pGL) { if(GLHelper.EXTENSIONS_VERTEXBUFFEROBJECTS) { final GL11 gl11 = (GL11)pGL; this.mTMXTiledMap.getSharedVertexBuffer().selectOnHardware(gl11); GLHelper.vertexZeroPointer(gl11); } else { GLHelper.vertexPointer(pGL, this.mTMXTiledMap.getSharedVertexBuffer().getFloatBuffer()); } } @Override protected void drawVertices(final GL10 pGL, final Camera pCamera) { final TMXTile[][] tmxTiles = this.mTMXTiles; final int tileColumns = this.mTileColumns; final int tileRows = this.mTileRows; final int tileWidth = this.mTMXTiledMap.getTileWidth(); final int tileHeight = this.mTMXTiledMap.getTileHeight(); final float scaledTileWidth = tileWidth * this.mScaleX; final float scaledTileHeight = tileHeight * this.mScaleY; final float[] cullingVertices = this.mCullingVertices; RectangularShapeCollisionChecker.fillVertices(this, cullingVertices); final float layerMinX = cullingVertices[VERTEX_INDEX_X]; final float layerMinY = cullingVertices[VERTEX_INDEX_Y]; final float cameraMinX = pCamera.getMinX(); final float cameraMinY = pCamera.getMinY(); final float cameraWidth = pCamera.getWidth(); final float cameraHeight = pCamera.getHeight(); /* Determine the area that is visible in the camera. */ final float firstColumnRaw = (cameraMinX - layerMinX) / scaledTileWidth; final int firstColumn = MathUtils.bringToBounds(0, tileColumns - 1, (int)Math.floor(firstColumnRaw)); final int lastColumn = MathUtils.bringToBounds(0, tileColumns - 1, (int)Math.ceil(firstColumnRaw + cameraWidth / scaledTileWidth)); final float firstRowRaw = (cameraMinY - layerMinY) / scaledTileHeight; final int firstRow = MathUtils.bringToBounds(0, tileRows - 1, (int)Math.floor(firstRowRaw)); final int lastRow = MathUtils.bringToBounds(0, tileRows - 1, (int)Math.floor(firstRowRaw + cameraHeight / scaledTileHeight)); final int visibleTilesTotalWidth = (lastColumn - firstColumn + 1) * tileWidth; pGL.glTranslatef(firstColumn * tileWidth, firstRow * tileHeight, 0); for(int row = firstRow; row <= lastRow; row++) { final TMXTile[] tmxTileRow = tmxTiles[row]; for(int column = firstColumn; column <= lastColumn; column++) { final TextureRegion textureRegion = tmxTileRow[column].mTextureRegion; if(textureRegion != null) { textureRegion.onApply(pGL); pGL.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); } pGL.glTranslatef(tileWidth, 0, 0); } /* Translate one row downwards and the back left to the first column. * Just like the 'Carriage Return' + 'New Line' (\r\n) on a typewriter. */ pGL.glTranslatef(-visibleTilesTotalWidth, tileHeight, 0); } pGL.glLoadIdentity(); } @Override protected void onManagedUpdate(final float pSecondsElapsed) { /* Nothing. */ } // =========================================================== // Methods // =========================================================== void initializeTMXTileFromXML(final Attributes pAttributes, final ITMXTilePropertiesListener pTMXTilePropertyListener) { this.addTileByGlobalTileID(SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TILE_ATTRIBUTE_GID), pTMXTilePropertyListener); } void initializeTMXTilesFromDataString(final String pDataString, final String pDataEncoding, final String pDataCompression, final ITMXTilePropertiesListener pTMXTilePropertyListener) throws IOException, IllegalArgumentException { DataInputStream dataIn = null; try{ InputStream in = new ByteArrayInputStream(pDataString.getBytes("UTF-8")); /* Wrap decoding Streams if necessary. */ if(pDataEncoding != null && pDataEncoding.equals(TAG_DATA_ATTRIBUTE_ENCODING_VALUE_BASE64)) { in = new Base64InputStream(in, Base64.DEFAULT); } if(pDataCompression != null){ if(pDataCompression.equals(TAG_DATA_ATTRIBUTE_COMPRESSION_VALUE_GZIP)) { in = new GZIPInputStream(in); } else { throw new IllegalArgumentException("Supplied compression '" + pDataCompression + "' is not supported yet."); } } dataIn = new DataInputStream(in); while(this.mTilesAdded < this.mGlobalTileIDsExpected) { final int globalTileID = this.readGlobalTileID(dataIn); this.addTileByGlobalTileID(globalTileID, pTMXTilePropertyListener); } } finally { StreamUtils.close(dataIn); } } private void addTileByGlobalTileID(final int pGlobalTileID, final ITMXTilePropertiesListener pTMXTilePropertyListener) { final TMXTiledMap tmxTiledMap = this.mTMXTiledMap; final int tilesHorizontal = this.mTileColumns; final int column = this.mTilesAdded % tilesHorizontal; final int row = this.mTilesAdded / tilesHorizontal; final TMXTile[][] tmxTiles = this.mTMXTiles; final TextureRegion tmxTileTextureRegion; if(pGlobalTileID == 0) { tmxTileTextureRegion = null; } else { tmxTileTextureRegion = tmxTiledMap.getTextureRegionFromGlobalTileID(pGlobalTileID); } final TMXTile tmxTile = new TMXTile(pGlobalTileID, column, row, this.mTMXTiledMap.getTileWidth(), this.mTMXTiledMap.getTileHeight(), tmxTileTextureRegion); tmxTiles[row][column] = tmxTile; if(pGlobalTileID != 0) { /* Notify the ITMXTilePropertiesListener if it exists. */ if(pTMXTilePropertyListener != null) { final TMXProperties<TMXTileProperty> tmxTileProperties = tmxTiledMap.getTMXTileProperties(pGlobalTileID); if(tmxTileProperties != null) { pTMXTilePropertyListener.onTMXTileWithPropertiesCreated(tmxTiledMap, this, tmxTile, tmxTileProperties); } } } this.mTilesAdded++; } private int readGlobalTileID(final DataInputStream pDataIn) throws IOException { final int lowestByte = pDataIn.read(); final int secondLowestByte = pDataIn.read(); final int secondHighestByte = pDataIn.read(); final int highestByte = pDataIn.read(); if(lowestByte < 0 || secondLowestByte < 0 || secondHighestByte < 0 || highestByte < 0) { throw new IllegalArgumentException("Couldn't read global Tile ID."); } return lowestByte | secondLowestByte << 8 |secondHighestByte << 16 | highestByte << 24; } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXLayer.java
Java
lgpl
12,010
package org.anddev.andengine.entity.layer.tiled.tmx; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:14:06 - 27.07.2010 */ public class TMXProperty implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final String mName; private final String mValue; // =========================================================== // Constructors // =========================================================== public TMXProperty(final Attributes pAttributes) { this.mName = pAttributes.getValue("", TAG_PROPERTY_ATTRIBUTE_NAME); this.mValue = pAttributes.getValue("", TAG_PROPERTY_ATTRIBUTE_VALUE); } // =========================================================== // Getter & Setter // =========================================================== public String getName() { return this.mName; } public String getValue() { return this.mValue; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public String toString() { return this.mName + "='" + this.mValue + "'"; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXProperty.java
Java
lgpl
1,912
package org.anddev.andengine.entity.layer.tiled.tmx; import java.io.IOException; import java.util.ArrayList; import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader.ITMXTilePropertiesListener; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXParseException; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TSXLoadException; import org.anddev.andengine.opengl.texture.TextureManager; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.SAXUtils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.content.Context; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:11:29 - 20.07.2010 */ public class TMXParser extends DefaultHandler implements TMXConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Context mContext; private final TextureManager mTextureManager; private final ITMXTilePropertiesListener mTMXTilePropertyListener; private final TextureOptions mTextureOptions; private TMXTiledMap mTMXTiledMap; private int mLastTileSetTileID; private final StringBuilder mStringBuilder = new StringBuilder(); private String mDataEncoding; private String mDataCompression; private boolean mInMap; private boolean mInTileset; @SuppressWarnings("unused") private boolean mInImage; private boolean mInTile; private boolean mInProperties; @SuppressWarnings("unused") private boolean mInProperty; private boolean mInLayer; private boolean mInData; private boolean mInObjectGroup; private boolean mInObject; // =========================================================== // Constructors // =========================================================== public TMXParser(final Context pContext, final TextureManager pTextureManager, final TextureOptions pTextureOptions, final ITMXTilePropertiesListener pTMXTilePropertyListener) { this.mContext = pContext; this.mTextureManager = pTextureManager; this.mTextureOptions = pTextureOptions; this.mTMXTilePropertyListener = pTMXTilePropertyListener; } // =========================================================== // Getter & Setter // =========================================================== TMXTiledMap getTMXTiledMap() { return this.mTMXTiledMap; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException { if(pLocalName.equals(TAG_MAP)){ this.mInMap = true; this.mTMXTiledMap = new TMXTiledMap(pAttributes); } else if(pLocalName.equals(TAG_TILESET)){ this.mInTileset = true; final TMXTileSet tmxTileSet; final String tsxTileSetSource = pAttributes.getValue("", TAG_TILESET_ATTRIBUTE_SOURCE); if(tsxTileSetSource == null) { tmxTileSet = new TMXTileSet(pAttributes, this.mTextureOptions); } else { try { final int firstGlobalTileID = SAXUtils.getIntAttribute(pAttributes, TAG_TILESET_ATTRIBUTE_FIRSTGID, 1); tmxTileSet = new TSXLoader(this.mContext, this.mTextureManager, this.mTextureOptions).loadFromAsset(this.mContext, firstGlobalTileID, tsxTileSetSource); } catch (final TSXLoadException e) { throw new TMXParseException("Failed to load TMXTileSet from source: " + tsxTileSetSource, e); } } this.mTMXTiledMap.addTMXTileSet(tmxTileSet); } else if(pLocalName.equals(TAG_IMAGE)){ this.mInImage = true; final ArrayList<TMXTileSet> tmxTileSets = this.mTMXTiledMap.getTMXTileSets(); tmxTileSets.get(tmxTileSets.size() - 1).setImageSource(this.mContext, this.mTextureManager, pAttributes); } else if(pLocalName.equals(TAG_TILE)) { this.mInTile = true; if(this.mInTileset) { this.mLastTileSetTileID = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TILE_ATTRIBUTE_ID); } else if(this.mInData) { final ArrayList<TMXLayer> tmxLayers = this.mTMXTiledMap.getTMXLayers(); tmxLayers.get(tmxLayers.size() - 1).initializeTMXTileFromXML(pAttributes, this.mTMXTilePropertyListener); } } else if(pLocalName.equals(TAG_PROPERTIES)) { this.mInProperties = true; } else if(this.mInProperties && pLocalName.equals(TAG_PROPERTY)) { this.mInProperty = true; if(this.mInTile) { final ArrayList<TMXTileSet> tmxTileSets = this.mTMXTiledMap.getTMXTileSets(); final TMXTileSet lastTMXTileSet = tmxTileSets.get(tmxTileSets.size() - 1); lastTMXTileSet.addTMXTileProperty(this.mLastTileSetTileID, new TMXTileProperty(pAttributes)); } else if(this.mInLayer) { final ArrayList<TMXLayer> tmxLayers = this.mTMXTiledMap.getTMXLayers(); final TMXLayer lastTMXLayer = tmxLayers.get(tmxLayers.size() - 1); lastTMXLayer.addTMXLayerProperty(new TMXLayerProperty(pAttributes)); } else if(this.mInObject) { final ArrayList<TMXObjectGroup> tmxObjectGroups = this.mTMXTiledMap.getTMXObjectGroups(); final TMXObjectGroup lastTMXObjectGroup = tmxObjectGroups.get(tmxObjectGroups.size() - 1); final ArrayList<TMXObject> tmxObjects = lastTMXObjectGroup.getTMXObjects(); final TMXObject lastTMXObject = tmxObjects.get(tmxObjects.size() - 1); lastTMXObject.addTMXObjectProperty(new TMXObjectProperty(pAttributes)); } else if(this.mInObjectGroup) { final ArrayList<TMXObjectGroup> tmxObjectGroups = this.mTMXTiledMap.getTMXObjectGroups(); final TMXObjectGroup lastTMXObjectGroup = tmxObjectGroups.get(tmxObjectGroups.size() - 1); lastTMXObjectGroup.addTMXObjectGroupProperty(new TMXObjectGroupProperty(pAttributes)); } else if(this.mInMap) { this.mTMXTiledMap.addTMXTiledMapProperty(new TMXTiledMapProperty(pAttributes)); } } else if(pLocalName.equals(TAG_LAYER)){ this.mInLayer = true; this.mTMXTiledMap.addTMXLayer(new TMXLayer(this.mTMXTiledMap, pAttributes)); } else if(pLocalName.equals(TAG_DATA)){ this.mInData = true; this.mDataEncoding = pAttributes.getValue("", TAG_DATA_ATTRIBUTE_ENCODING); this.mDataCompression = pAttributes.getValue("", TAG_DATA_ATTRIBUTE_COMPRESSION); } else if(pLocalName.equals(TAG_OBJECTGROUP)){ this.mInObjectGroup = true; this.mTMXTiledMap.addTMXObjectGroup(new TMXObjectGroup(pAttributes)); } else if(pLocalName.equals(TAG_OBJECT)){ this.mInObject = true; final ArrayList<TMXObjectGroup> tmxObjectGroups = this.mTMXTiledMap.getTMXObjectGroups(); tmxObjectGroups.get(tmxObjectGroups.size() - 1).addTMXObject(new TMXObject(pAttributes)); } else { throw new TMXParseException("Unexpected start tag: '" + pLocalName + "'."); } } @Override public void characters(final char[] pCharacters, final int pStart, final int pLength) throws SAXException { this.mStringBuilder.append(pCharacters, pStart, pLength); } @Override public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException { if(pLocalName.equals(TAG_MAP)){ this.mInMap = false; } else if(pLocalName.equals(TAG_TILESET)){ this.mInTileset = false; } else if(pLocalName.equals(TAG_IMAGE)){ this.mInImage = false; } else if(pLocalName.equals(TAG_TILE)) { this.mInTile = false; } else if(pLocalName.equals(TAG_PROPERTIES)) { this.mInProperties = false; } else if(pLocalName.equals(TAG_PROPERTY)) { this.mInProperty = false; } else if(pLocalName.equals(TAG_LAYER)){ this.mInLayer = false; } else if(pLocalName.equals(TAG_DATA)){ final boolean binarySaved = this.mDataCompression != null && this.mDataEncoding != null; if(binarySaved) { final ArrayList<TMXLayer> tmxLayers = this.mTMXTiledMap.getTMXLayers(); try { tmxLayers.get(tmxLayers.size() - 1).initializeTMXTilesFromDataString(this.mStringBuilder.toString().trim(), this.mDataEncoding, this.mDataCompression, this.mTMXTilePropertyListener); } catch (final IOException e) { Debug.e(e); } this.mDataCompression = null; this.mDataEncoding = null; } this.mInData = false; } else if(pLocalName.equals(TAG_OBJECTGROUP)){ this.mInObjectGroup = false; } else if(pLocalName.equals(TAG_OBJECT)){ this.mInObject = false; } else { throw new TMXParseException("Unexpected end tag: '" + pLocalName + "'."); } /* Reset the StringBuilder. */ this.mStringBuilder.setLength(0); } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXParser.java
Java
lgpl
9,337
package org.anddev.andengine.entity.layer.tiled.tmx; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:48:01 - 12.10.2010 */ public class TMXObjectGroupProperty extends TMXProperty { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXObjectGroupProperty(final Attributes pAttributes) { super(pAttributes); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXObjectGroupProperty.java
Java
lgpl
1,455
package org.anddev.andengine.entity.layer.tiled.tmx; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 18:48:46 - 12.10.2010 */ public class TMXLayerProperty extends TMXProperty { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXLayerProperty(final Attributes pAttributes) { super(pAttributes); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXLayerProperty.java
Java
lgpl
1,443
package org.anddev.andengine.entity.layer.tiled.tmx; import java.util.ArrayList; import org.anddev.andengine.entity.layer.tiled.tmx.util.constants.TMXConstants; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 10:14:06 - 27.07.2010 */ public class TMXProperties<T extends TMXProperty> extends ArrayList<T> implements TMXConstants { // =========================================================== // Constants // =========================================================== private static final long serialVersionUID = 8912773556975105201L; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== public boolean containsTMXProperty(final String pName, final String pValue) { for(int i = this.size() - 1; i >= 0; i--) { final T tmxProperty = this.get(i); if(tmxProperty.getName().equals(pName) && tmxProperty.getValue().equals(pValue)) { return true; } } return false; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXProperties.java
Java
lgpl
1,857
package org.anddev.andengine.entity.layer.tiled.tmx; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:19:44 - 29.07.2010 */ public class TMXObjectProperty extends TMXProperty { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXObjectProperty(final Attributes pAttributes) { super(pAttributes); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXObjectProperty.java
Java
lgpl
1,445
package org.anddev.andengine.entity.layer.tiled.tmx; import org.xml.sax.Attributes; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:20:09 - 29.07.2010 */ public class TMXTileProperty extends TMXProperty { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public TMXTileProperty(final Attributes pAttributes) { super(pAttributes); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/layer/tiled/tmx/TMXTileProperty.java
Java
lgpl
1,441
package org.anddev.andengine.entity.util; import org.anddev.andengine.engine.handler.IUpdateHandler; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.constants.TimeConstants; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 19:52:31 - 09.03.2010 */ public class FrameCountCrasher implements IUpdateHandler, TimeConstants { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private int mFramesLeft; private final float[] mFrameLengths; // =========================================================== // Constructors // =========================================================== public FrameCountCrasher(final int pFrameCount) { this.mFramesLeft = pFrameCount; this.mFrameLengths = new float[pFrameCount]; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onUpdate(final float pSecondsElapsed) { this.mFramesLeft--; final float[] frameLengths = this.mFrameLengths; if(this.mFramesLeft >= 0) { frameLengths[this.mFramesLeft] = pSecondsElapsed; } else { for(int i = frameLengths.length - 1; i >= 0; i--) { Debug.d("Elapsed: " + frameLengths[i]); } throw new RuntimeException(); } } @Override public void reset() { } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/util/FrameCountCrasher.java
Java
lgpl
2,144
package org.anddev.andengine.entity.util; import org.anddev.andengine.engine.handler.IUpdateHandler; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:00:55 - 22.06.2010 */ public class FrameCounter implements IUpdateHandler { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private int mFrames; // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== public int getFrames() { return this.mFrames; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onUpdate(final float pSecondsElapsed) { this.mFrames++; } @Override public void reset() { this.mFrames = 0; } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
zzy421-andengine
src/org/anddev/andengine/entity/util/FrameCounter.java
Java
lgpl
1,611