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.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.opengl.util.GLHelper;
import org.anddev.andengine.util.ArrayUtils;
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 InputStream inputStream = this.getInputStream();
try {
final byte[] data = StreamUtils.streamToBytes(inputStream);
final ByteBuffer dataByteBuffer = ByteBuffer.wrap(data);
dataByteBuffer.rewind();
dataByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
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() / 8;
/* 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;
final ByteBuffer pixelData = ByteBuffer.allocate(currentPixelDataSize).order(ByteOrder.nativeOrder());
pixelData.put(data, PVRTextureHeader.SIZE + currentPixelDataOffset, currentPixelDataSize);
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));
}
pGL.glTexImage2D(GL10.GL_TEXTURE_2D, mipmapLevel, glFormat, width, height, 0, glFormat, glType, pixelData);
GLHelper.checkGLError(pGL);
currentPixelDataOffset += currentPixelDataSize;
/* Prepare next mipmap level. */
width = Math.max(width >> 1, 1);
height = Math.max(height >> 1, 1);
mipmapLevel++;
}
} finally {
StreamUtils.close(inputStream);
}
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// 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;
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
// ===========================================================
}
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/compressed/pvr/PVRTexture.java | Java | lgpl | 14,461 |
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
// ===========================================================
// ===========================================================
// 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();
final CCZHeader cczHeader = new CCZHeader(StreamUtils.streamToBytes(inputStream, CCZHeader.SIZE));
return cczHeader.getCCZCompressionFormat().wrap(inputStream, cczHeader.getUncompressedSize());
}
// ===========================================================
// 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, final int pUncompressedSize) throws IOException {
switch(this) {
case GZIP:
return new GZIPInputStream(pInputStream, pUncompressedSize);
case ZLIB:
return new InflaterInputStream(pInputStream, new Inflater(), pUncompressedSize);
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
// ===========================================================
}
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/compressed/pvr/PVRCCZTexture.java | Java | lgpl | 7,897 |
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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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 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 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 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 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 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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/TextureManager.java | Java | lgpl | 6,229 |
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
// ===========================================================
}
| 07734dan-learnmore | 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 ITexture texture = textureRegion.getTexture();
if(texture == null) { // TODO Check really needed?
return;
}
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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/region/buffer/TextureRegionBuffer.java | Java | lgpl | 4,444 |
package org.anddev.andengine.opengl.texture.region.crop;
import javax.microedition.khronos.opengles.GL11;
import org.anddev.andengine.opengl.texture.ITexture;
import org.anddev.andengine.opengl.texture.region.BaseTextureRegion;
import org.anddev.andengine.opengl.util.GLHelper;
/**
* @author Jonathan Heek
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 13:51:50 - 05.07.2011
*/
public class TextureRegionCrop {
// ===========================================================
// Constants
// ===========================================================
private static final int LEFT_INDEX = 0;
private static final int BOTTOM_INDEX = 1;
private static final int WIDTH_INDEX = 2;
private static final int HEIGHT_INDEX = 3;
// ===========================================================
// Fields
// ===========================================================
protected final BaseTextureRegion mTextureRegion;
private boolean mFlippedHorizontal;
private boolean mFlippedVertical;
private final int[] mData = new int[4];
private boolean mDirty = true;
// ===========================================================
// Constructors
// ===========================================================
public TextureRegionCrop(final BaseTextureRegion pBaseTextureRegion) {
this.mTextureRegion = pBaseTextureRegion;
}
// ===========================================================
// Getter & Setter
// ===========================================================
public boolean isDirty() {
return this.mDirty;
}
public int[] getData() {
return this.mData;
}
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 void update() {
final BaseTextureRegion textureRegion = this.mTextureRegion;
final ITexture texture = textureRegion.getTexture();
if(texture == null) { // TODO Check really needed?
return;
}
final int[] values = this.mData;
final int textureCropLeft = textureRegion.getTextureCropLeft();
final int textureCropTop = textureRegion.getTextureCropTop();
final int textureCropWidth = textureRegion.getTextureCropWidth();
final int textureCropHeight = textureRegion.getTextureCropHeight();
// TODO support flipping !
if(this.mFlippedVertical) {
if(this.mFlippedHorizontal) {
} else {
}
} else {
if(this.mFlippedHorizontal) {
} else {
values[LEFT_INDEX] = textureCropLeft;
values[BOTTOM_INDEX] = textureCropTop + textureCropHeight;
values[WIDTH_INDEX] = textureCropWidth;
values[HEIGHT_INDEX] = -textureCropHeight;
}
}
this.mDirty = true;
}
public void selectOnHardware(final GL11 pGL11) {
if(this.mDirty) {
this.mDirty = false;
synchronized(this) {
GLHelper.textureCrop(pGL11, this);
}
}
}
public void apply(final GL11 pGL11) {
GLHelper.textureCrop(pGL11, this);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/region/crop/TextureRegionCrop.java | Java | lgpl | 3,963 |
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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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 clone() {
final TiledTextureRegion clone = new TiledTextureRegion(this.mTexture, this.getTexturePositionX(), this.getTexturePositionY(), this.getWidth(), this.getHeight(), this.mTileColumns, this.mTileRows);
clone.setCurrentTileIndex(this.mCurrentTileColumn, this.mCurrentTileRow);
return clone;
}
@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();
}
@Override
public int getTextureCropLeft() {
return this.getTexturePositionOfCurrentTileX();
}
@Override
public int getTextureCropTop() {
return this.getTexturePositionOfCurrentTileY();
}
@Override
public int getTextureCropWidth() {
return this.getTileWidth();
}
@Override
public int getTextureCropHeight() {
return this.getTileHeight();
}
// ===========================================================
// Methods
// ===========================================================
public void nextTile() {
final int tileIndex = (this.getCurrentTileIndex() + 1) % this.getTileCount();
this.setCurrentTileIndex(tileIndex);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/region/TiledTextureRegion.java | Java | lgpl | 5,026 |
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 clone() {
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();
}
@Override
public int getTextureCropLeft() {
return this.getTexturePositionX();
}
@Override
public int getTextureCropTop() {
return this.getTexturePositionY();
}
@Override
public int getTextureCropWidth() {
return this.getWidth();
}
@Override
public int getTextureCropHeight() {
return this.getHeight();
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/region/TextureRegion.java | Java | lgpl | 2,676 |
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.texture.region.crop.TextureRegionCrop;
import org.anddev.andengine.opengl.util.GLHelper;
/**
* (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;
// TODO Should TextureRegionCrop be a part of TextureRegionCrop ?
protected final TextureRegionBuffer mTextureRegionBuffer;
protected final TextureRegionCrop mTextureRegionCrop;
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.mTextureRegionCrop = new TextureRegionCrop(this);
this.initTextureBuffer();
}
protected void initTextureBuffer() {
this.updateTextureRegionBuffer();
}
// ===========================================================
// 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 TextureRegionCrop getTexureRegionCrop() {
return this.mTextureRegionCrop;
}
public boolean isFlippedHorizontal() {
return this.mTextureRegionBuffer.isFlippedHorizontal();
}
public void setFlippedHorizontal(final boolean pFlippedHorizontal) {
this.mTextureRegionBuffer.setFlippedHorizontal(pFlippedHorizontal);
this.mTextureRegionCrop.setFlippedHorizontal(pFlippedHorizontal);
}
public boolean isFlippedVertical() {
return this.mTextureRegionBuffer.isFlippedVertical();
}
public void setFlippedVertical(final boolean pFlippedVertical) {
this.mTextureRegionBuffer.setFlippedVertical(pFlippedVertical);
this.mTextureRegionCrop.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();
public abstract int getTextureCropLeft();
public abstract int getTextureCropTop();
public abstract int getTextureCropWidth();
public abstract int getTextureCropHeight();
// ===========================================================
// Methods
// ===========================================================
protected void updateTextureRegionBuffer() {
this.mTextureRegionBuffer.update();
this.mTextureRegionCrop.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());
}
}
public void onApplyCrop(final GL11 pGL11) {
this.mTexture.bind(pGL11);
this.mTextureRegionCrop.apply(pGL11);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/region/BaseTextureRegion.java | Java | lgpl | 5,905 |
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;
}
@Override
public abstract BaseTextureAtlasSource clone();
// ===========================================================
// 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
// ===========================================================
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/source/BaseTextureAtlasSource.java | Java | lgpl | 2,325 |
package org.anddev.andengine.opengl.texture.source;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 11:46:56 - 12.07.2011
*/
public interface ITextureAtlasSource extends Cloneable {
// ===========================================================
// 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 clone();
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/source/ITextureAtlasSource.java | Java | lgpl | 836 |
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
// ===========================================================
}
| 07734dan-learnmore | 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());
}
}
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
}
| 07734dan-learnmore | 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);
this.sendPlaceholderBitmapToHardware();
}
private void sendPlaceholderBitmapToHardware() {
final Bitmap textureBitmap = Bitmap.createBitmap(this.mWidth, this.mHeight, this.mBitmapTextureFormat.getBitmapConfig());
// TODO Check if there is an easier/faster method to create a white placeholder bitmap.
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, textureBitmap, 0);
textureBitmap.recycle();
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/BitmapTextureAtlas.java | Java | lgpl | 10,919 |
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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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 clone();
// ===========================================================
// 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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/PictureBitmapTextureAtlasSource.java | Java | lgpl | 3,501 |
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 clone();
public Bitmap onLoadBitmap(final Config pBitmapConfig);
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/IBitmapTextureAtlasSource.java | Java | lgpl | 835 |
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 clone() {
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
// ===========================================================
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/FileBitmapTextureAtlasSource.java | Java | lgpl | 3,946 |
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 clone() {
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
// ===========================================================
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/ResourceBitmapTextureAtlasSource.java | Java | lgpl | 3,929 |
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 clone() {
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
// ===========================================================
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/AssetBitmapTextureAtlasSource.java | Java | lgpl | 4,226 |
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 clone() {
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
// ===========================================================
} | 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/EmptyBitmapTextureAtlasSource.java | Java | lgpl | 2,575 |
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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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 clone() {
return new FillBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mFillColor, this.mTextureAtlasSourceDecoratorOptions);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/FillBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 2,794 |
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 clone() {
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
// ===========================================================
}
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/LinearGradientFillBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 7,054 |
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 clone() {
return new OutlineBitmapTextureAtlasSourceDecorator(this.mBitmapTextureAtlasSource, this.mBitmapTextureAtlasSourceDecoratorShape, this.mOutlineColor, this.mTextureAtlasSourceDecoratorOptions);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/OutlineBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 2,835 |
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 clone();
// ===========================================================
// 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
// ===========================================================
@Override
protected TextureAtlasSourceDecoratorOptions clone() {
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
// ===========================================================
}
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/BaseBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 7,585 |
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 clone() {
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
// ===========================================================
}
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/RadialGradientFillBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 6,458 |
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
// ===========================================================
} | 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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);
} | 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}; | 07734dan-learnmore | 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 clone() {
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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/ColorKeyBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 3,616 |
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 clone() {
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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/ColorSwapBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 4,212 |
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 clone();
// ===========================================================
// 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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/opengl/texture/atlas/bitmap/source/decorator/BaseShapeBitmapTextureAtlasSourceDecorator.java | Java | lgpl | 2,593 |
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
// ===========================================================
}
| 07734dan-learnmore | 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());
}
}
}
} | 07734dan-learnmore | 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
// ===========================================================
}
} | 07734dan-learnmore | 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
// ===========================================================
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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, PY) lies with
* respect to the line segment from (X1, Y1) to (X2, 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, Y1),
* in order to point at the specified point (PX, 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, Y1)" or 1 if the point lies "beyond (X2, Y2)".
*
* @param pX1
* , Y1 the coordinates of the beginning of the specified
* line segment
* @param pX2
* , Y2 the coordinates of the end of the specified line
* segment
* @param pPX
* , 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/sprite/batch/SpriteGroup.java | Java | lgpl | 3,780 |
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
// ===========================================================
}
| 07734dan-learnmore | 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);
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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);
}
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | 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
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/FrameCounter.java | Java | lgpl | 1,611 |
package org.anddev.andengine.entity.util;
import org.anddev.andengine.util.Debug;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 19:52:31 - 09.03.2010
*/
public class FPSLogger extends AverageFPSCounter {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
protected float mShortestFrame = Float.MAX_VALUE;
protected float mLongestFrame = Float.MIN_VALUE;
// ===========================================================
// Constructors
// ===========================================================
public FPSLogger() {
super();
}
public FPSLogger(final float pAverageDuration) {
super(pAverageDuration);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onHandleAverageDurationElapsed(final float pFPS) {
this.onLogFPS();
this.mLongestFrame = Float.MIN_VALUE;
this.mShortestFrame = Float.MAX_VALUE;
}
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
this.mShortestFrame = Math.min(this.mShortestFrame, pSecondsElapsed);
this.mLongestFrame = Math.max(this.mLongestFrame, pSecondsElapsed);
}
@Override
public void reset() {
super.reset();
this.mShortestFrame = Float.MAX_VALUE;
this.mLongestFrame = Float.MIN_VALUE;
}
// ===========================================================
// Methods
// ===========================================================
protected void onLogFPS() {
Debug.d(String.format("FPS: %.2f (MIN: %.0f ms | MAX: %.0f ms)",
this.mFrames / this.mSecondsElapsed,
this.mShortestFrame * MILLISECONDSPERSECOND,
this.mLongestFrame * MILLISECONDSPERSECOND));
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/FPSLogger.java | Java | lgpl | 2,421 |
package org.anddev.andengine.entity.util;
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 abstract class AverageFPSCounter extends FPSCounter implements TimeConstants {
// ===========================================================
// Constants
// ===========================================================
private static final float AVERAGE_DURATION_DEFAULT = 5;
// ===========================================================
// Fields
// ===========================================================
protected final float mAverageDuration;
// ===========================================================
// Constructors
// ===========================================================
public AverageFPSCounter() {
this(AVERAGE_DURATION_DEFAULT);
}
public AverageFPSCounter(final float pAverageDuration) {
this.mAverageDuration = pAverageDuration;
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
protected abstract void onHandleAverageDurationElapsed(final float pFPS);
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
if(this.mSecondsElapsed > this.mAverageDuration){
this.onHandleAverageDurationElapsed(this.getFPS());
this.mSecondsElapsed -= this.mAverageDuration;
this.mFrames = 0;
}
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/AverageFPSCounter.java | Java | lgpl | 2,072 |
package org.anddev.andengine.entity.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.microedition.khronos.opengles.GL10;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.util.ScreenGrabber.IScreenGrabberCallback;
import org.anddev.andengine.util.Debug;
import org.anddev.andengine.util.StreamUtils;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 15:11:50 - 15.03.2010
*/
public class ScreenCapture extends Entity implements IScreenGrabberCallback {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private String mFilePath;
private final ScreenGrabber mScreenGrabber = new ScreenGrabber();
private IScreenCaptureCallback mScreenCaptureCallback;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
this.mScreenGrabber.onManagedDraw(pGL, pCamera);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
/* Nothing */
}
@Override
public void reset() {
/* Nothing */
}
@Override
public void onScreenGrabbed(final Bitmap pBitmap) {
try {
ScreenCapture.saveCapture(pBitmap, this.mFilePath);
this.mScreenCaptureCallback.onScreenCaptured(this.mFilePath);
} catch (final FileNotFoundException e) {
this.mScreenCaptureCallback.onScreenCaptureFailed(this.mFilePath, e);
}
}
@Override
public void onScreenGrabFailed(final Exception pException) {
this.mScreenCaptureCallback.onScreenCaptureFailed(this.mFilePath, pException);
}
// ===========================================================
// Methods
// ===========================================================
public void capture(final int pCaptureWidth, final int pCaptureHeight, final String pFilePath, final IScreenCaptureCallback pScreenCaptureCallback) {
this.capture(0, 0, pCaptureWidth, pCaptureHeight, pFilePath, pScreenCaptureCallback);
}
public void capture(final int pCaptureX, final int pCaptureY, final int pCaptureWidth, final int pCaptureHeight, final String pFilePath, final IScreenCaptureCallback pScreencaptureCallback) {
this.mFilePath = pFilePath;
this.mScreenCaptureCallback = pScreencaptureCallback;
this.mScreenGrabber.grab(pCaptureX, pCaptureY, pCaptureWidth, pCaptureHeight, this);
}
private static void saveCapture(final Bitmap pBitmap, final String pFilePath) throws FileNotFoundException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pFilePath);
pBitmap.compress(CompressFormat.PNG, 100, fos);
} catch (final FileNotFoundException e) {
StreamUtils.flushCloseStream(fos);
Debug.e("Error saving file to: " + pFilePath, e);
throw e;
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static interface IScreenCaptureCallback {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public void onScreenCaptured(final String pFilePath);
public void onScreenCaptureFailed(final String pFilePath, final Exception pException);
}
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/ScreenCapture.java | Java | lgpl | 4,295 |
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 19:52:31 - 09.03.2010
*/
public class FPSCounter implements IUpdateHandler {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
protected float mSecondsElapsed;
protected int mFrames;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
public float getFPS() {
return this.mFrames / this.mSecondsElapsed;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void onUpdate(final float pSecondsElapsed) {
this.mFrames++;
this.mSecondsElapsed += pSecondsElapsed;
}
@Override
public void reset() {
this.mFrames = 0;
this.mSecondsElapsed = 0;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/FPSCounter.java | Java | lgpl | 1,743 |
package org.anddev.andengine.entity.util;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.Entity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 15:27:22 - 10.01.2011
*/
public class ScreenGrabber extends Entity {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private int mGrabX;
private int mGrabY;
private int mGrabWidth;
private int mGrabHeight;
private boolean mScreenGrabPending = false;
private IScreenGrabberCallback mScreenGrabCallback;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
if(this.mScreenGrabPending) {
try {
final Bitmap screenGrab = ScreenGrabber.grab(this.mGrabX, this.mGrabY, this.mGrabWidth, this.mGrabHeight, pGL);
this.mScreenGrabCallback.onScreenGrabbed(screenGrab);
} catch (final Exception e) {
this.mScreenGrabCallback.onScreenGrabFailed(e);
}
this.mScreenGrabPending = false;
}
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
/* Nothing */
}
@Override
public void reset() {
/* Nothing */
}
// ===========================================================
// Methods
// ===========================================================
public void grab(final int pGrabWidth, final int pGrabHeight, final IScreenGrabberCallback pScreenGrabCallback) {
this.grab(0, 0, pGrabWidth, pGrabHeight, pScreenGrabCallback);
}
public void grab(final int pGrabX, final int pGrabY, final int pGrabWidth, final int pGrabHeight, final IScreenGrabberCallback pScreenGrabCallback) {
this.mGrabX = pGrabX;
this.mGrabY = pGrabY;
this.mGrabWidth = pGrabWidth;
this.mGrabHeight = pGrabHeight;
this.mScreenGrabCallback = pScreenGrabCallback;
this.mScreenGrabPending = true;
}
private static Bitmap grab(final int pGrabX, final int pGrabY, final int pGrabWidth, final int pGrabHeight, final GL10 pGL) {
final int[] source = new int[pGrabWidth * (pGrabY + pGrabHeight)];
final IntBuffer sourceBuffer = IntBuffer.wrap(source);
sourceBuffer.position(0);
// TODO Check availability of OpenGL and GL10.GL_RGBA combinations that require less conversion operations.
// Note: There is (said to be) a bug with glReadPixels when 'y != 0', so we simply read starting from 'y == 0'.
pGL.glReadPixels(pGrabX, 0, pGrabWidth, pGrabY + pGrabHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, sourceBuffer);
final int[] pixels = new int[pGrabWidth * pGrabHeight];
// Convert from RGBA_8888 (Which is actually ABGR as the whole buffer seems to be inverted) --> ARGB_8888
for (int y = 0; y < pGrabHeight; y++) {
for (int x = 0; x < pGrabWidth; x++) {
final int pixel = source[x + ((pGrabY + y) * pGrabWidth)];
final int blue = (pixel & 0x00FF0000) >> 16;
final int red = (pixel & 0x000000FF) << 16;
final int greenAlpha = pixel & 0xFF00FF00;
pixels[x + ((pGrabHeight - y - 1) * pGrabWidth)] = greenAlpha | red | blue;
}
}
return Bitmap.createBitmap(pixels, pGrabWidth, pGrabHeight, Config.ARGB_8888);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static interface IScreenGrabberCallback {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public void onScreenGrabbed(final Bitmap pBitmap);
public void onScreenGrabFailed(final Exception pException);
}
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/util/ScreenGrabber.java | Java | lgpl | 4,674 |
package org.anddev.andengine.entity.scene;
import javax.microedition.khronos.opengles.GL10;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.input.touch.TouchEvent;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 15:35:53 - 29.03.2010
*/
public class CameraScene extends Scene {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
protected Camera mCamera;
// ===========================================================
// Constructors
// ===========================================================
/**
* {@link CameraScene#setCamera(Camera)} needs to be called manually. Otherwise nothing will be drawn.
*/
public CameraScene() {
this(null);
}
public CameraScene(final Camera pCamera) {
this.mCamera = pCamera;
}
// ===========================================================
// Getter & Setter
// ===========================================================
public Camera getCamera() {
return this.mCamera;
}
public void setCamera(final Camera pCamera) {
this.mCamera = pCamera;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public boolean onSceneTouchEvent(final TouchEvent pSceneTouchEvent) {
if(this.mCamera == null) {
return false;
} else {
this.mCamera.convertSceneToCameraSceneTouchEvent(pSceneTouchEvent);
final boolean handled = super.onSceneTouchEvent(pSceneTouchEvent);
if(handled) {
return true;
} else {
this.mCamera.convertCameraSceneToSceneTouchEvent(pSceneTouchEvent);
return false;
}
}
}
@Override
protected boolean onChildSceneTouchEvent(final TouchEvent pSceneTouchEvent) {
final boolean childIsCameraScene = this.mChildScene instanceof CameraScene;
if(childIsCameraScene) {
this.mCamera.convertCameraSceneToSceneTouchEvent(pSceneTouchEvent);
final boolean result = super.onChildSceneTouchEvent(pSceneTouchEvent);
this.mCamera.convertSceneToCameraSceneTouchEvent(pSceneTouchEvent);
return result;
} else {
return super.onChildSceneTouchEvent(pSceneTouchEvent);
}
}
@Override
protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
if(this.mCamera != null) {
pGL.glMatrixMode(GL10.GL_PROJECTION);
this.mCamera.onApplyCameraSceneMatrix(pGL);
{
pGL.glMatrixMode(GL10.GL_MODELVIEW);
pGL.glPushMatrix();
pGL.glLoadIdentity();
super.onManagedDraw(pGL, pCamera);
pGL.glPopMatrix();
}
pGL.glMatrixMode(GL10.GL_PROJECTION);
}
}
// ===========================================================
// Methods
// ===========================================================
public void centerShapeInCamera(final Shape pShape) {
final Camera camera = this.mCamera;
pShape.setPosition((camera.getWidth() - pShape.getWidth()) * 0.5f, (camera.getHeight() - pShape.getHeight()) * 0.5f);
}
public void centerShapeInCameraHorizontally(final Shape pShape) {
pShape.setPosition((this.mCamera.getWidth() - pShape.getWidth()) * 0.5f, pShape.getY());
}
public void centerShapeInCameraVertically(final Shape pShape) {
pShape.setPosition(pShape.getX(), (this.mCamera.getHeight() - pShape.getHeight()) * 0.5f);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/CameraScene.java | Java | lgpl | 3,872 |
package org.anddev.andengine.entity.scene;
import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.runnable.RunnableHandler;
import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.scene.Scene.ITouchArea.ITouchAreaMatcher;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.IBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.util.GLHelper;
import org.anddev.andengine.util.IMatcher;
import org.anddev.andengine.util.SmartList;
import org.anddev.andengine.util.constants.Constants;
import android.util.SparseArray;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 12:47:39 - 08.03.2010
*/
public class Scene extends Entity {
// ===========================================================
// Constants
// ===========================================================
private static final int TOUCHAREAS_CAPACITY_DEFAULT = 4;
// ===========================================================
// Fields
// ===========================================================
private float mSecondsElapsedTotal;
protected Scene mParentScene;
protected Scene mChildScene;
private boolean mChildSceneModalDraw;
private boolean mChildSceneModalUpdate;
private boolean mChildSceneModalTouch;
protected SmartList<ITouchArea> mTouchAreas = new SmartList<ITouchArea>(TOUCHAREAS_CAPACITY_DEFAULT);
private final RunnableHandler mRunnableHandler = new RunnableHandler();
private IOnSceneTouchListener mOnSceneTouchListener;
private IOnAreaTouchListener mOnAreaTouchListener;
private IBackground mBackground = new ColorBackground(0, 0, 0); // Black
private boolean mBackgroundEnabled = true;
private boolean mOnAreaTouchTraversalBackToFront = true;
private boolean mTouchAreaBindingEnabled = false;
private final SparseArray<ITouchArea> mTouchAreaBindings = new SparseArray<ITouchArea>();
private boolean mOnSceneTouchListenerBindingEnabled = false;
private final SparseArray<IOnSceneTouchListener> mOnSceneTouchListenerBindings = new SparseArray<IOnSceneTouchListener>();
// ===========================================================
// Constructors
// ===========================================================
public Scene() {
}
@Deprecated
public Scene(final int pChildCount) {
for(int i = 0; i < pChildCount; i++) {
this.attachChild(new Entity());
}
}
// ===========================================================
// Getter & Setter
// ===========================================================
public float getSecondsElapsedTotal() {
return this.mSecondsElapsedTotal;
}
public IBackground getBackground() {
return this.mBackground;
}
public void setBackground(final IBackground pBackground) {
this.mBackground = pBackground;
}
public boolean isBackgroundEnabled() {
return this.mBackgroundEnabled;
}
public void setBackgroundEnabled(final boolean pEnabled) {
this.mBackgroundEnabled = pEnabled;
}
public void setOnSceneTouchListener(final IOnSceneTouchListener pOnSceneTouchListener) {
this.mOnSceneTouchListener = pOnSceneTouchListener;
}
public IOnSceneTouchListener getOnSceneTouchListener() {
return this.mOnSceneTouchListener;
}
public boolean hasOnSceneTouchListener() {
return this.mOnSceneTouchListener != null;
}
public void setOnAreaTouchListener(final IOnAreaTouchListener pOnAreaTouchListener) {
this.mOnAreaTouchListener = pOnAreaTouchListener;
}
public IOnAreaTouchListener getOnAreaTouchListener() {
return this.mOnAreaTouchListener;
}
public boolean hasOnAreaTouchListener() {
return this.mOnAreaTouchListener != null;
}
private void setParentScene(final Scene pParentScene) {
this.mParentScene = pParentScene;
}
public boolean hasChildScene() {
return this.mChildScene != null;
}
public Scene getChildScene() {
return this.mChildScene;
}
public void setChildSceneModal(final Scene pChildScene) {
this.setChildScene(pChildScene, true, true, true);
}
public void setChildScene(final Scene pChildScene) {
this.setChildScene(pChildScene, false, false, false);
}
public void setChildScene(final Scene pChildScene, final boolean pModalDraw, final boolean pModalUpdate, final boolean pModalTouch) {
pChildScene.setParentScene(this);
this.mChildScene = pChildScene;
this.mChildSceneModalDraw = pModalDraw;
this.mChildSceneModalUpdate = pModalUpdate;
this.mChildSceneModalTouch = pModalTouch;
}
public void clearChildScene() {
this.mChildScene = null;
}
public void setOnAreaTouchTraversalBackToFront() {
this.mOnAreaTouchTraversalBackToFront = true;
}
public void setOnAreaTouchTraversalFrontToBack() {
this.mOnAreaTouchTraversalBackToFront = false;
}
public boolean isTouchAreaBindingEnabled() {
return this.mTouchAreaBindingEnabled;
}
/**
* Enable or disable the binding of TouchAreas to PointerIDs (fingers).
* When enabled: TouchAreas get bound to a PointerID (finger) when returning true in
* {@link Shape#onAreaTouched(TouchEvent, float, float)} or
* {@link IOnAreaTouchListener#onAreaTouched(TouchEvent, ITouchArea, float, float)}
* with {@link TouchEvent#ACTION_DOWN}, they will receive all subsequent {@link TouchEvent}s
* that are made with the same PointerID (finger)
* <b>even if the {@link TouchEvent} is outside of the actual {@link ITouchArea}</b>!
*
* @param pTouchAreaBindingEnabled
*/
public void setTouchAreaBindingEnabled(final boolean pTouchAreaBindingEnabled) {
if(this.mTouchAreaBindingEnabled && !pTouchAreaBindingEnabled) {
this.mTouchAreaBindings.clear();
}
this.mTouchAreaBindingEnabled = pTouchAreaBindingEnabled;
}
public boolean isOnSceneTouchListenerBindingEnabled() {
return this.mOnSceneTouchListenerBindingEnabled;
}
/**
* Enable or disable the binding of TouchAreas to PointerIDs (fingers).
* When enabled: The OnSceneTouchListener gets bound to a PointerID (finger) when returning true in
* {@link Shape#onAreaTouched(TouchEvent, float, float)} or
* {@link IOnAreaTouchListener#onAreaTouched(TouchEvent, ITouchArea, float, float)}
* with {@link TouchEvent#ACTION_DOWN}, it will receive all subsequent {@link TouchEvent}s
* that are made with the same PointerID (finger)
* <b>even if the {@link TouchEvent} is would belong to an overlaying {@link ITouchArea}</b>!
*
* @param pOnSceneTouchListenerBindingEnabled
*/
public void setOnSceneTouchListenerBindingEnabled(final boolean pOnSceneTouchListenerBindingEnabled) {
if(this.mOnSceneTouchListenerBindingEnabled && !pOnSceneTouchListenerBindingEnabled) {
this.mOnSceneTouchListenerBindings.clear();
}
this.mOnSceneTouchListenerBindingEnabled = pOnSceneTouchListenerBindingEnabled;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
final Scene childScene = this.mChildScene;
if(childScene == null || !this.mChildSceneModalDraw) {
if(this.mBackgroundEnabled) {
pCamera.onApplySceneBackgroundMatrix(pGL);
GLHelper.setModelViewIdentityMatrix(pGL);
this.mBackground.onDraw(pGL, pCamera);
}
pCamera.onApplySceneMatrix(pGL);
GLHelper.setModelViewIdentityMatrix(pGL);
super.onManagedDraw(pGL, pCamera);
}
if(childScene != null) {
childScene.onDraw(pGL, pCamera);
}
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
this.mSecondsElapsedTotal += pSecondsElapsed;
this.mRunnableHandler.onUpdate(pSecondsElapsed);
final Scene childScene = this.mChildScene;
if(childScene == null || !this.mChildSceneModalUpdate) {
this.mBackground.onUpdate(pSecondsElapsed);
super.onManagedUpdate(pSecondsElapsed);
}
if(childScene != null) {
childScene.onUpdate(pSecondsElapsed);
}
}
public boolean onSceneTouchEvent(final TouchEvent pSceneTouchEvent) {
final int action = pSceneTouchEvent.getAction();
final boolean isActionDown = pSceneTouchEvent.isActionDown();
if(!isActionDown) {
if(this.mOnSceneTouchListenerBindingEnabled) {
final IOnSceneTouchListener boundOnSceneTouchListener = this.mOnSceneTouchListenerBindings.get(pSceneTouchEvent.getPointerID());
if (boundOnSceneTouchListener != null) {
/* Check if boundTouchArea needs to be removed. */
switch(action) {
case TouchEvent.ACTION_UP:
case TouchEvent.ACTION_CANCEL:
this.mOnSceneTouchListenerBindings.remove(pSceneTouchEvent.getPointerID());
}
final Boolean handled = this.mOnSceneTouchListener.onSceneTouchEvent(this, pSceneTouchEvent);
if(handled != null && handled) {
return true;
}
}
}
if(this.mTouchAreaBindingEnabled) {
final SparseArray<ITouchArea> touchAreaBindings = this.mTouchAreaBindings;
final ITouchArea boundTouchArea = touchAreaBindings.get(pSceneTouchEvent.getPointerID());
/* In the case a ITouchArea has been bound to this PointerID,
* we'll pass this this TouchEvent to the same ITouchArea. */
if(boundTouchArea != null) {
final float sceneTouchEventX = pSceneTouchEvent.getX();
final float sceneTouchEventY = pSceneTouchEvent.getY();
/* Check if boundTouchArea needs to be removed. */
switch(action) {
case TouchEvent.ACTION_UP:
case TouchEvent.ACTION_CANCEL:
touchAreaBindings.remove(pSceneTouchEvent.getPointerID());
}
final Boolean handled = this.onAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, boundTouchArea);
if(handled != null && handled) {
return true;
}
}
}
}
final Scene childScene = this.mChildScene;
if(childScene != null) {
final boolean handledByChild = this.onChildSceneTouchEvent(pSceneTouchEvent);
if(handledByChild) {
return true;
} else if(this.mChildSceneModalTouch) {
return false;
}
}
final float sceneTouchEventX = pSceneTouchEvent.getX();
final float sceneTouchEventY = pSceneTouchEvent.getY();
final ArrayList<ITouchArea> touchAreas = this.mTouchAreas;
if(touchAreas != null) {
final int touchAreaCount = touchAreas.size();
if(touchAreaCount > 0) {
if(this.mOnAreaTouchTraversalBackToFront) { /* Back to Front. */
for(int i = 0; i < touchAreaCount; i++) {
final ITouchArea touchArea = touchAreas.get(i);
if(touchArea.contains(sceneTouchEventX, sceneTouchEventY)) {
final Boolean handled = this.onAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, touchArea);
if(handled != null && handled) {
/* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
* bind this ITouchArea to the PointerID. */
if(this.mTouchAreaBindingEnabled && isActionDown) {
this.mTouchAreaBindings.put(pSceneTouchEvent.getPointerID(), touchArea);
}
return true;
}
}
}
} else { /* Front to back. */
for(int i = touchAreaCount - 1; i >= 0; i--) {
final ITouchArea touchArea = touchAreas.get(i);
if(touchArea.contains(sceneTouchEventX, sceneTouchEventY)) {
final Boolean handled = this.onAreaTouchEvent(pSceneTouchEvent, sceneTouchEventX, sceneTouchEventY, touchArea);
if(handled != null && handled) {
/* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
* bind this ITouchArea to the PointerID. */
if(this.mTouchAreaBindingEnabled && isActionDown) {
this.mTouchAreaBindings.put(pSceneTouchEvent.getPointerID(), touchArea);
}
return true;
}
}
}
}
}
}
/* If no area was touched, the Scene itself was touched as a fallback. */
if(this.mOnSceneTouchListener != null){
final Boolean handled = this.mOnSceneTouchListener.onSceneTouchEvent(this, pSceneTouchEvent);
if(handled != null && handled) {
/* If binding of ITouchAreas is enabled and this is an ACTION_DOWN event,
* bind the active OnSceneTouchListener to the PointerID. */
if(this.mOnSceneTouchListenerBindingEnabled && isActionDown) {
this.mOnSceneTouchListenerBindings.put(pSceneTouchEvent.getPointerID(), this.mOnSceneTouchListener);
}
return true;
} else {
return false;
}
} else {
return false;
}
}
private Boolean onAreaTouchEvent(final TouchEvent pSceneTouchEvent, final float sceneTouchEventX, final float sceneTouchEventY, final ITouchArea touchArea) {
final float[] touchAreaLocalCoordinates = touchArea.convertSceneToLocalCoordinates(sceneTouchEventX, sceneTouchEventY);
final float touchAreaLocalX = touchAreaLocalCoordinates[Constants.VERTEX_INDEX_X];
final float touchAreaLocalY = touchAreaLocalCoordinates[Constants.VERTEX_INDEX_Y];
final boolean handledSelf = touchArea.onAreaTouched(pSceneTouchEvent, touchAreaLocalX, touchAreaLocalY);
if(handledSelf) {
return Boolean.TRUE;
} else if(this.mOnAreaTouchListener != null) {
return this.mOnAreaTouchListener.onAreaTouched(pSceneTouchEvent, touchArea, touchAreaLocalX, touchAreaLocalY);
} else {
return null;
}
}
protected boolean onChildSceneTouchEvent(final TouchEvent pSceneTouchEvent) {
return this.mChildScene.onSceneTouchEvent(pSceneTouchEvent);
}
@Override
public void reset() {
super.reset();
this.clearChildScene();
}
@Override
public void setParent(final IEntity pEntity) {
// super.setParent(pEntity);
}
// ===========================================================
// Methods
// ===========================================================
public void postRunnable(final Runnable pRunnable) {
this.mRunnableHandler.postRunnable(pRunnable);
}
public void registerTouchArea(final ITouchArea pTouchArea) {
this.mTouchAreas.add(pTouchArea);
}
public boolean unregisterTouchArea(final ITouchArea pTouchArea) {
return this.mTouchAreas.remove(pTouchArea);
}
public boolean unregisterTouchAreas(final ITouchAreaMatcher pTouchAreaMatcher) {
return this.mTouchAreas.removeAll(pTouchAreaMatcher);
}
public void clearTouchAreas() {
this.mTouchAreas.clear();
}
public ArrayList<ITouchArea> getTouchAreas() {
return this.mTouchAreas;
}
public void back() {
this.clearChildScene();
if(this.mParentScene != null) {
this.mParentScene.clearChildScene();
this.mParentScene = null;
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static interface ITouchArea {
// ===========================================================
// Final Fields
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public boolean contains(final float pX, final float pY);
public float[] convertSceneToLocalCoordinates(final float pX, final float pY);
public float[] convertLocalToSceneCoordinates(final float pX, final float pY);
/**
* This method only fires if this {@link ITouchArea} is registered to the {@link Scene} via {@link Scene#registerTouchArea(ITouchArea)}.
* @param pSceneTouchEvent
* @return <code>true</code> if the event was handled (that means {@link IOnAreaTouchListener} of the {@link Scene} will not be fired!), otherwise <code>false</code>.
*/
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY);
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public interface ITouchAreaMatcher extends IMatcher<ITouchArea> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
}
}
/**
* An interface for a callback to be invoked when a {@link TouchEvent} is
* dispatched to an {@link ITouchArea} area. The callback will be invoked
* before the {@link TouchEvent} is passed to the {@link ITouchArea}.
*/
public static interface IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
/**
* Called when a {@link TouchEvent} is dispatched to an {@link ITouchArea}. This allows
* listeners to get a chance to respond before the target {@link ITouchArea#onAreaTouched(TouchEvent, float, float)} is called.
*
* @param pTouchArea The {@link ITouchArea} that the {@link TouchEvent} has been dispatched to.
* @param pSceneTouchEvent The {@link TouchEvent} object containing full information about the event.
* @param pTouchAreaLocalX the x coordinate within the area touched.
* @param pTouchAreaLocalY the y coordinate within the area touched.
*
* @return <code>true</code> if this {@link IOnAreaTouchListener} has consumed the {@link TouchEvent}, <code>false</code> otherwise.
*/
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY);
}
/**
* An interface for a callback to be invoked when a {@link TouchEvent} is
* dispatched to a {@link Scene}. The callback will be invoked
* after all {@link ITouchArea}s have been checked and none consumed the {@link TouchEvent}.
*/
public static interface IOnSceneTouchListener {
// ===========================================================
// Final Fields
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
/**
* Called when a {@link TouchEvent} is dispatched to a {@link Scene}.
*
* @param pScene The {@link Scene} that the {@link TouchEvent} has been dispatched to.
* @param pSceneTouchEvent The {@link TouchEvent} object containing full information about the event.
*
* @return <code>true</code> if this {@link IOnSceneTouchListener} has consumed the {@link TouchEvent}, <code>false</code> otherwise.
*/
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent);
}
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/Scene.java | Java | lgpl | 19,459 |
package org.anddev.andengine.entity.scene.popup;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.modifier.IEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.util.HorizontalAlign;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 17:19:30 - 03.08.2010
*/
public class TextPopupScene extends PopupScene {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private final Text mText;
// ===========================================================
// Constructors
// ===========================================================
public TextPopupScene(final Camera pCamera, final Scene pParentScene, final Font pFont, final String pText, final float pDurationSeconds) {
this(pCamera, pParentScene, pFont, pText, pDurationSeconds, null, null);
}
public TextPopupScene(final Camera pCamera, final Scene pParentScene, final Font pFont, final String pText, final float pDurationSeconds, final IEntityModifier pShapeModifier) {
this(pCamera, pParentScene, pFont, pText, pDurationSeconds, pShapeModifier, null);
}
public TextPopupScene(final Camera pCamera, final Scene pParentScene, final Font pFont, final String pText, final float pDurationSeconds, final Runnable pRunnable) {
this(pCamera, pParentScene, pFont, pText, pDurationSeconds, null, pRunnable);
}
public TextPopupScene(final Camera pCamera, final Scene pParentScene, final Font pFont, final String pText, final float pDurationSeconds, final IEntityModifier pShapeModifier, final Runnable pRunnable) {
super(pCamera, pParentScene, pDurationSeconds, pRunnable);
this.mText = new Text(0, 0, pFont, pText, HorizontalAlign.CENTER);
this.centerShapeInCamera(this.mText);
if(pShapeModifier != null) {
this.mText.registerEntityModifier(pShapeModifier);
}
this.attachChild(this.mText);
}
// ===========================================================
// Getter & Setter
// ===========================================================
public Text getText() {
return this.mText;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/popup/TextPopupScene.java | Java | lgpl | 2,975 |
package org.anddev.andengine.entity.scene.popup;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.scene.CameraScene;
import org.anddev.andengine.entity.scene.Scene;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 16:36:51 - 03.08.2010
*/
public class PopupScene extends CameraScene {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
public PopupScene(final Camera pCamera, final Scene pParentScene, final float pDurationSeconds) {
this(pCamera, pParentScene, pDurationSeconds, null);
}
public PopupScene(final Camera pCamera, final Scene pParentScene, final float pDurationSeconds, final Runnable pRunnable) {
super(pCamera);
this.setBackgroundEnabled(false);
pParentScene.setChildScene(this, false, true, true);
this.registerUpdateHandler(new TimerHandler(pDurationSeconds, new ITimerCallback() {
@Override
public void onTimePassed(final TimerHandler pTimerHandler) {
PopupScene.this.unregisterUpdateHandler(pTimerHandler);
pParentScene.clearChildScene();
if(pRunnable != null) {
pRunnable.run();
}
}
}));
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/popup/PopupScene.java | Java | lgpl | 2,347 |
package org.anddev.andengine.entity.scene.menu;
import java.util.ArrayList;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.scene.CameraScene;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.menu.animator.IMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.input.touch.TouchEvent;
import android.view.MotionEvent;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 20:06:51 - 01.04.2010
*/
public class MenuScene extends CameraScene implements IOnAreaTouchListener, IOnSceneTouchListener {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private final ArrayList<IMenuItem> mMenuItems = new ArrayList<IMenuItem>();
private IOnMenuItemClickListener mOnMenuItemClickListener;
private IMenuAnimator mMenuAnimator = IMenuAnimator.DEFAULT;
private IMenuItem mSelectedMenuItem;
// ===========================================================
// Constructors
// ===========================================================
public MenuScene() {
this(null, null);
}
public MenuScene(final IOnMenuItemClickListener pOnMenuItemClickListener) {
this(null, pOnMenuItemClickListener);
}
public MenuScene(final Camera pCamera) {
this(pCamera, null);
}
public MenuScene(final Camera pCamera, final IOnMenuItemClickListener pOnMenuItemClickListener) {
super(pCamera);
this.mOnMenuItemClickListener = pOnMenuItemClickListener;
this.setOnSceneTouchListener(this);
this.setOnAreaTouchListener(this);
}
// ===========================================================
// Getter & Setter
// ===========================================================
public IOnMenuItemClickListener getOnMenuItemClickListener() {
return this.mOnMenuItemClickListener;
}
public void setOnMenuItemClickListener(final IOnMenuItemClickListener pOnMenuItemClickListener) {
this.mOnMenuItemClickListener = pOnMenuItemClickListener;
}
public int getMenuItemCount() {
return this.mMenuItems.size();
}
public void addMenuItem(final IMenuItem pMenuItem) {
this.mMenuItems.add(pMenuItem);
this.attachChild(pMenuItem);
this.registerTouchArea(pMenuItem);
}
@Override
public MenuScene getChildScene() {
return (MenuScene)super.getChildScene();
}
@Override
public void setChildScene(final Scene pChildScene, final boolean pModalDraw, final boolean pModalUpdate, final boolean pModalTouch) throws IllegalArgumentException {
if(pChildScene instanceof MenuScene) {
super.setChildScene(pChildScene, pModalDraw, pModalUpdate, pModalTouch);
} else {
throw new IllegalArgumentException("MenuScene accepts only MenuScenes as a ChildScene.");
}
}
@Override
public void clearChildScene() {
if(this.getChildScene() != null) {
this.getChildScene().reset();
super.clearChildScene();
}
}
public void setMenuAnimator(final IMenuAnimator pMenuAnimator) {
this.mMenuAnimator = pMenuAnimator;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
final IMenuItem menuItem = ((IMenuItem)pTouchArea);
switch(pSceneTouchEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if(this.mSelectedMenuItem != null && this.mSelectedMenuItem != menuItem) {
this.mSelectedMenuItem.onUnselected();
}
this.mSelectedMenuItem = menuItem;
this.mSelectedMenuItem.onSelected();
break;
case MotionEvent.ACTION_UP:
if(this.mOnMenuItemClickListener != null) {
final boolean handled = this.mOnMenuItemClickListener.onMenuItemClicked(this, menuItem, pTouchAreaLocalX, pTouchAreaLocalY);
menuItem.onUnselected();
this.mSelectedMenuItem = null;
return handled;
}
break;
case MotionEvent.ACTION_CANCEL:
menuItem.onUnselected();
this.mSelectedMenuItem = null;
break;
}
return true;
}
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
if(this.mSelectedMenuItem != null) {
this.mSelectedMenuItem.onUnselected();
this.mSelectedMenuItem = null;
}
return false;
}
@Override
public void back() {
super.back();
this.reset();
}
@Override
public void reset() {
super.reset();
final ArrayList<IMenuItem> menuItems = this.mMenuItems;
for(int i = menuItems.size() - 1; i >= 0; i--) {
menuItems.get(i).reset();
}
this.prepareAnimations();
}
// ===========================================================
// Methods
// ===========================================================
public void closeMenuScene() {
this.back();
}
public void buildAnimations() {
this.prepareAnimations();
final float cameraWidthRaw = this.mCamera.getWidthRaw();
final float cameraHeightRaw = this.mCamera.getHeightRaw();
this.mMenuAnimator.buildAnimations(this.mMenuItems, cameraWidthRaw, cameraHeightRaw);
}
public void prepareAnimations() {
final float cameraWidthRaw = this.mCamera.getWidthRaw();
final float cameraHeightRaw = this.mCamera.getHeightRaw();
this.mMenuAnimator.prepareAnimations(this.mMenuItems, cameraWidthRaw, cameraHeightRaw);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static interface IOnMenuItemClickListener {
// ===========================================================
// Final Fields
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY);
}
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/menu/MenuScene.java | Java | lgpl | 6,673 |
package org.anddev.andengine.entity.scene.menu.animator;
import java.util.ArrayList;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.util.HorizontalAlign;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 11:04:35 - 02.04.2010
*/
public class SlideMenuAnimator extends BaseMenuAnimator {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
public SlideMenuAnimator(){
super();
}
public SlideMenuAnimator(final IEaseFunction pEaseFunction) {
super(pEaseFunction);
}
public SlideMenuAnimator(final HorizontalAlign pHorizontalAlign) {
super(pHorizontalAlign);
}
public SlideMenuAnimator(final HorizontalAlign pHorizontalAlign, final IEaseFunction pEaseFunction) {
super(pHorizontalAlign, pEaseFunction);
}
public SlideMenuAnimator(final float pMenuItemSpacing) {
super(pMenuItemSpacing);
}
public SlideMenuAnimator(final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
super(pMenuItemSpacing, pEaseFunction);
}
public SlideMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing) {
super(pHorizontalAlign, pMenuItemSpacing);
}
public SlideMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
super(pHorizontalAlign, pMenuItemSpacing, pEaseFunction);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void buildAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
final IEaseFunction easeFunction = this.mEaseFunction;
final float maximumWidth = this.getMaximumWidth(pMenuItems);
final float overallHeight = this.getOverallHeight(pMenuItems);
final float baseX = (pCameraWidth - maximumWidth) * 0.5f;
final float baseY = (pCameraHeight - overallHeight) * 0.5f;
float offsetY = 0;
final int menuItemCount = pMenuItems.size();
for(int i = 0; i < menuItemCount; i++) {
final IMenuItem menuItem = pMenuItems.get(i);
final float offsetX;
switch(this.mHorizontalAlign) {
case LEFT:
offsetX = 0;
break;
case RIGHT:
offsetX = maximumWidth - menuItem.getWidthScaled();
break;
case CENTER:
default:
offsetX = (maximumWidth - menuItem.getWidthScaled()) * 0.5f;
break;
}
final MoveModifier moveModifier = new MoveModifier(DURATION, -maximumWidth, baseX + offsetX, baseY + offsetY, baseY + offsetY, easeFunction);
moveModifier.setRemoveWhenFinished(false);
menuItem.registerEntityModifier(moveModifier);
offsetY += menuItem.getHeight() + this.mMenuItemSpacing;
}
}
@Override
public void prepareAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
final float maximumWidth = this.getMaximumWidth(pMenuItems);
final float overallHeight = this.getOverallHeight(pMenuItems);
final float baseY = (pCameraHeight - overallHeight) * 0.5f;
final float menuItemSpacing = this.mMenuItemSpacing;
float offsetY = 0;
final int menuItemCount = pMenuItems.size();
for(int i = 0; i < menuItemCount; i++) {
final IMenuItem menuItem = pMenuItems.get(i);
menuItem.setPosition(-maximumWidth, baseY + offsetY);
offsetY += menuItem.getHeight() + menuItemSpacing;
}
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/menu/animator/SlideMenuAnimator.java | Java | lgpl | 4,526 |
package org.anddev.andengine.entity.scene.menu.animator;
import java.util.ArrayList;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.util.HorizontalAlign;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 11:17:32 - 02.04.2010
*/
public abstract class BaseMenuAnimator implements IMenuAnimator {
// ===========================================================
// Constants
// ===========================================================
protected static final float DURATION = 1.0f;
private static final float MENUITEMSPACING_DEFAULT = 1.0f;
private static final HorizontalAlign HORIZONTALALIGN_DEFAULT = HorizontalAlign.CENTER;
// ===========================================================
// Fields
// ===========================================================
protected final float mMenuItemSpacing;
protected final HorizontalAlign mHorizontalAlign;
protected final IEaseFunction mEaseFunction;
// ===========================================================
// Constructors
// ===========================================================
public BaseMenuAnimator() {
this(MENUITEMSPACING_DEFAULT);
}
public BaseMenuAnimator(final IEaseFunction pEaseFunction) {
this(MENUITEMSPACING_DEFAULT, pEaseFunction);
}
public BaseMenuAnimator(final float pMenuItemSpacing) {
this(HORIZONTALALIGN_DEFAULT, pMenuItemSpacing);
}
public BaseMenuAnimator(final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
this(HORIZONTALALIGN_DEFAULT, pMenuItemSpacing, pEaseFunction);
}
public BaseMenuAnimator(final HorizontalAlign pHorizontalAlign) {
this(pHorizontalAlign, MENUITEMSPACING_DEFAULT);
}
public BaseMenuAnimator(final HorizontalAlign pHorizontalAlign, final IEaseFunction pEaseFunction) {
this(pHorizontalAlign, MENUITEMSPACING_DEFAULT, pEaseFunction);
}
public BaseMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing) {
this(pHorizontalAlign, pMenuItemSpacing, IEaseFunction.DEFAULT);
}
public BaseMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
this.mHorizontalAlign = pHorizontalAlign;
this.mMenuItemSpacing = pMenuItemSpacing;
this.mEaseFunction = pEaseFunction;
}
// ===========================================================
// Getter & Setter
// ===========================================================
protected float getMaximumWidth(final ArrayList<IMenuItem> pMenuItems) {
float maximumWidth = Float.MIN_VALUE;
for(int i = pMenuItems.size() - 1; i >= 0; i--) {
final IMenuItem menuItem = pMenuItems.get(i);
maximumWidth = Math.max(maximumWidth, menuItem.getWidthScaled());
}
return maximumWidth;
}
protected float getOverallHeight(final ArrayList<IMenuItem> pMenuItems) {
float overallHeight = 0;
for(int i = pMenuItems.size() - 1; i >= 0; i--) {
final IMenuItem menuItem = pMenuItems.get(i);
overallHeight += menuItem.getHeight();
}
overallHeight += (pMenuItems.size() - 1) * this.mMenuItemSpacing;
return overallHeight;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/menu/animator/BaseMenuAnimator.java | Java | lgpl | 3,790 |
package org.anddev.andengine.entity.scene.menu.animator;
import java.util.ArrayList;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.util.HorizontalAlign;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 11:04:35 - 02.04.2010
*/
public class AlphaMenuAnimator extends BaseMenuAnimator {
// ===========================================================
// Constants
// ===========================================================
private static final float ALPHA_FROM = 0.0f;
private static final float ALPHA_TO = 1.0f;
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
public AlphaMenuAnimator(){
super();
}
public AlphaMenuAnimator(final IEaseFunction pEaseFunction) {
super(pEaseFunction);
}
public AlphaMenuAnimator(final HorizontalAlign pHorizontalAlign) {
super(pHorizontalAlign);
}
public AlphaMenuAnimator(final HorizontalAlign pHorizontalAlign, final IEaseFunction pEaseFunction) {
super(pHorizontalAlign, pEaseFunction);
}
public AlphaMenuAnimator(final float pMenuItemSpacing) {
super(pMenuItemSpacing);
}
public AlphaMenuAnimator(final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
super(pMenuItemSpacing, pEaseFunction);
}
public AlphaMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing) {
super(pHorizontalAlign, pMenuItemSpacing);
}
public AlphaMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
super(pHorizontalAlign, pMenuItemSpacing, pEaseFunction);
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void buildAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
final IEaseFunction easeFunction = this.mEaseFunction;
final int menuItemCount = pMenuItems.size();
for(int i = menuItemCount - 1; i >= 0; i--) {
final AlphaModifier alphaModifier = new AlphaModifier(DURATION, ALPHA_FROM, ALPHA_TO, easeFunction);
alphaModifier.setRemoveWhenFinished(false);
pMenuItems.get(i).registerEntityModifier(alphaModifier);
}
}
@Override
public void prepareAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
final float maximumWidth = this.getMaximumWidth(pMenuItems);
final float overallHeight = this.getOverallHeight(pMenuItems);
final float baseX = (pCameraWidth - maximumWidth) * 0.5f;
final float baseY = (pCameraHeight - overallHeight) * 0.5f;
final float menuItemSpacing = this.mMenuItemSpacing;
float offsetY = 0;
final int menuItemCount = pMenuItems.size();
for(int i = 0; i < menuItemCount; i++) {
final IMenuItem menuItem = pMenuItems.get(i);
final float offsetX;
switch(this.mHorizontalAlign) {
case LEFT:
offsetX = 0;
break;
case RIGHT:
offsetX = maximumWidth - menuItem.getWidthScaled();
break;
case CENTER:
default:
offsetX = (maximumWidth - menuItem.getWidthScaled()) * 0.5f;
break;
}
menuItem.setPosition(baseX + offsetX , baseY + offsetY);
menuItem.setAlpha(ALPHA_FROM);
offsetY += menuItem.getHeight() + menuItemSpacing;
}
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
| 07734dan-learnmore | src/org/anddev/andengine/entity/scene/menu/animator/AlphaMenuAnimator.java | Java | lgpl | 4,302 |