Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged AndEngineTexturePackerExtension into GLES2.

  • Loading branch information...
commit 75c216ea4c6ed219134cb1fd642fea3667717bbc 1 parent 5506b9a
Nicolas Gramlich authored
63 src/org/andengine/util/texturepack/TexturePack.java
View
@@ -0,0 +1,63 @@
+package org.andengine.util.texturepack;
+
+import org.andengine.opengl.texture.ITexture;
+
+/**
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 23:23:47 - 30.07.2011
+ */
+public class TexturePack {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final ITexture mTexture;
+ private final TexturePackTextureRegionLibrary mTexturePackTextureRegionLibrary;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePack(final ITexture pTexture, final TexturePackTextureRegionLibrary pTexturePackTextureRegionLibrary) {
+ this.mTexture = pTexture;
+ this.mTexturePackTextureRegionLibrary = pTexturePackTextureRegionLibrary;
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ public ITexture getTexture() {
+ return this.mTexture;
+ }
+
+ public TexturePackTextureRegionLibrary getTexturePackTextureRegionLibrary() {
+ return this.mTexturePackTextureRegionLibrary;
+ }
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ public void loadTexture() {
+ this.mTexture.load();
+ }
+
+ public void unloadTexture() {
+ this.mTexture.unload();
+ }
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
51 src/org/andengine/util/texturepack/TexturePackLibrary.java
View
@@ -0,0 +1,51 @@
+package org.andengine.util.texturepack;
+
+import java.util.HashMap;
+
+/**
+ * (c) Zynga 2012
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 17:29:37 - 03.05.2012
+ */
+public class TexturePackLibrary {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final HashMap<String, TexturePack> mTexturePackMapping = new HashMap<String, TexturePack>();
+ private final HashMap<String, TexturePackTextureRegion> mTexturePackTextureRegionSourceMapping = new HashMap<String, TexturePackTextureRegion>();
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ public void put(final String pID, final TexturePack pTexturePack) {
+ this.mTexturePackMapping.put(pID, pTexturePack);
+ this.mTexturePackTextureRegionSourceMapping.putAll(pTexturePack.getTexturePackTextureRegionLibrary().getSourceMapping());
+ }
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ public TexturePackTextureRegion getTexturePackTextureRegion(final String pTexturePackTextureRegionSource) {
+ return this.mTexturePackTextureRegionSourceMapping.get(pTexturePackTextureRegionSource);
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
94 src/org/andengine/util/texturepack/TexturePackLoader.java
View
@@ -0,0 +1,94 @@
+package org.andengine.util.texturepack;
+
+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.andengine.opengl.texture.TextureManager;
+import org.andengine.util.StreamUtils;
+import org.andengine.util.texturepack.exception.TexturePackParseException;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
+import android.content.res.AssetManager;
+
+/**
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 17:05:15 - 29.07.2011
+ */
+public class TexturePackLoader {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final AssetManager mAssetManager;
+ private final TextureManager mTextureManager;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePackLoader(final AssetManager pAssetManager, final TextureManager pTextureManager) {
+ this.mAssetManager = pAssetManager;
+ this.mTextureManager = pTextureManager;
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ public TexturePack loadFromAsset(final String pAssetPath, final String pAssetBasePath) throws TexturePackParseException {
+ try {
+ return this.load(this.mAssetManager.open(pAssetPath), pAssetBasePath);
+ } catch (final IOException e) {
+ throw new TexturePackParseException("Could not load " + this.getClass().getSimpleName() + " data from asset: " + pAssetPath, e);
+ }
+ }
+
+ public TexturePack load(final InputStream pInputStream, final String pAssetBasePath) throws TexturePackParseException {
+ try{
+ final SAXParserFactory spf = SAXParserFactory.newInstance();
+ final SAXParser sp = spf.newSAXParser();
+
+ final XMLReader xr = sp.getXMLReader();
+ final TexturePackParser texturePackParser = new TexturePackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
+ xr.setContentHandler(texturePackParser);
+
+ xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
+
+ return texturePackParser.getTexturePack();
+ } catch (final SAXException e) {
+ throw new TexturePackParseException(e);
+ } catch (final ParserConfigurationException pe) {
+ /* Doesn't happen. */
+ return null;
+ } catch (final IOException e) {
+ throw new TexturePackParseException(e);
+ } finally {
+ StreamUtils.close(pInputStream);
+ }
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
294 src/org/andengine/util/texturepack/TexturePackParser.java
View
@@ -0,0 +1,294 @@
+package org.andengine.util.texturepack;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.microedition.khronos.opengles.GL10;
+
+import org.andengine.opengl.texture.ITexture;
+import org.andengine.opengl.texture.PixelFormat;
+import org.andengine.opengl.texture.TextureManager;
+import org.andengine.opengl.texture.TextureOptions;
+import org.andengine.opengl.texture.bitmap.BitmapTexture;
+import org.andengine.opengl.texture.bitmap.BitmapTextureFormat;
+import org.andengine.opengl.texture.compressed.pvr.PVRCCZTexture;
+import org.andengine.opengl.texture.compressed.pvr.PVRGZTexture;
+import org.andengine.opengl.texture.compressed.pvr.PVRTexture;
+import org.andengine.opengl.texture.compressed.pvr.PVRTexture.PVRTextureFormat;
+import org.andengine.opengl.texture.compressed.pvr.pixelbufferstrategy.SmartPVRTexturePixelBufferStrategy;
+import org.andengine.util.SAXUtils;
+import org.andengine.util.adt.DataConstants;
+import org.andengine.util.adt.io.in.IInputStreamOpener;
+import org.andengine.util.texturepack.exception.TexturePackParseException;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+import android.content.res.AssetManager;
+
+/**
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 17:19:26 - 29.07.2011
+ */
+public class TexturePackParser extends DefaultHandler {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ private static final String TAG_TEXTURE = "texture";
+ private static final String TAG_TEXTURE_ATTRIBUTE_VERSION = "version";
+ private static final String TAG_TEXTURE_ATTRIBUTE_FILE = "file";
+// private static final String TAG_TEXTURE_ATTRIBUTE_WIDTH = "width";
+// private static final String TAG_TEXTURE_ATTRIBUTE_HEIGHT = "height";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER = "minfilter";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST = "nearest";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR = "linear";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR_MIPMAP_LINEAR = "linear_mipmap_linear";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR_MIPMAP_NEAREST = "linear_mipmap_nearest";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST_MIPMAP_LINEAR = "nearest_mipmap_linear";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST_MIPMAP_NEAREST = "nearest_mipmap_nearest";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MAGFILTER = "magfilter";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MAGFILTER_VALUE_NEAREST = "nearest";
+ private static final String TAG_TEXTURE_ATTRIBUTE_MAGFILTER_VALUE_LINEAR = "linear";
+ private static final String TAG_TEXTURE_ATTRIBUTE_WRAPT = "wrapt";
+ private static final String TAG_TEXTURE_ATTRIBUTE_WRAPS = "wraps";
+ private static final String TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_CLAMP = "clamp";
+ private static final String TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_CLAMP_TO_EDGE = "clamp_to_edge";
+ private static final String TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_REPEAT = "repeat";
+ private static final String TAG_TEXTURE_ATTRIBUTE_PREMULTIPLYALPHA = "premultiplyalpha";
+ private static final String TAG_TEXTURE_ATTRIBUTE_TYPE = "type";
+ private static final String TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRCCZ = "pvrccz";
+ private static final String TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRGZ = "pvrgz";
+ private static final String TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVR = "pvr";
+ private static final String TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_BITMAP = "bitmap";
+ private static final String TAG_TEXTURE_ATTRIBUTE_PIXELFORMAT = "pixelformat";
+
+ private static final String TAG_TEXTUREREGION = "textureregion";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_ID = "id";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_X = "x";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_Y = "y";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_WIDTH = "width";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_HEIGHT = "height";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_ROTATED = "rotated";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_TRIMMED = "trimmed";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_SOURCE = "src";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_X = "srcx";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_Y = "srcy";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_WIDTH = "srcwidth";
+ private static final String TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_HEIGHT = "srcheight";
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final AssetManager mAssetManager;
+ private final String mAssetBasePath;
+ private final TextureManager mTextureManager;
+
+ private TexturePack mTexturePack;
+ private TexturePackTextureRegionLibrary mTextureRegionLibrary;
+ private ITexture mTexture;
+ private int mVersion;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePackParser(final AssetManager pAssetManager, final String pAssetBasePath, final TextureManager pTextureManager) {
+ this.mAssetManager = pAssetManager;
+ this.mAssetBasePath = pAssetBasePath;
+ this.mTextureManager = pTextureManager;
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ public TexturePack getTexturePack() {
+ return this.mTexturePack;
+ }
+
+ // ===========================================================
+ // 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(TexturePackParser.TAG_TEXTURE)) {
+ this.mVersion = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_VERSION);
+ this.mTexture = this.parseTexture(pAttributes);
+ this.mTextureRegionLibrary = new TexturePackTextureRegionLibrary(10);
+
+ this.mTexturePack = new TexturePack(this.mTexture, this.mTextureRegionLibrary);
+ } else if(pLocalName.equals(TexturePackParser.TAG_TEXTUREREGION)) {
+ final int id = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_ID);
+ final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_X);
+ final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_Y);
+ final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_WIDTH);
+ final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_HEIGHT);
+
+ final String source = SAXUtils.getAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE);
+
+ // TODO Not sure how trimming could be transparently supported...
+ final boolean trimmed = SAXUtils.getBooleanAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_TRIMMED);
+ final boolean rotated = SAXUtils.getBooleanAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_ROTATED);
+ final int sourceX = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_X);
+ final int sourceY = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_Y);
+ final int sourceWidth = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_WIDTH);
+ final int sourceHeight = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_HEIGHT);
+
+ this.mTextureRegionLibrary.put(new TexturePackTextureRegion(this.mTexture, x, y, width, height, id, source, rotated, trimmed, sourceX, sourceY, sourceWidth, sourceHeight));
+ } else {
+ throw new TexturePackParseException("Unexpected tag: '" + pLocalName + "'.");
+ }
+ }
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ protected InputStream onGetInputStream(final String pFilename) throws IOException {
+ return this.mAssetManager.open(this.mAssetBasePath + pFilename);
+ }
+
+ private ITexture parseTexture(final Attributes pAttributes) throws TexturePackParseException {
+ final String file = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_FILE);
+
+ if(this.mTextureManager.hasMappedTexture(file)) {
+ return this.mTextureManager.getMappedTexture(file);
+ }
+
+ final String type = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE);
+ final PixelFormat pixelFormat = TexturePackParser.parsePixelFormat(pAttributes);
+
+ final TextureOptions textureOptions = this.parseTextureOptions(pAttributes);
+
+ final ITexture texture;
+ if(type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_BITMAP)) {
+ try {
+ texture = new BitmapTexture(this.mTextureManager, new IInputStreamOpener() {
+ @Override
+ public InputStream open() throws IOException {
+ return TexturePackParser.this.onGetInputStream(file);
+ }
+ }, BitmapTextureFormat.fromPixelFormat(pixelFormat), textureOptions);
+ } catch (final IOException e) {
+ throw new TexturePackParseException(e);
+ }
+ } else if(type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVR)) {
+ try {
+ texture = new PVRTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
+ @Override
+ protected InputStream onGetInputStream() throws IOException {
+ return TexturePackParser.this.onGetInputStream(file);
+ }
+ };
+ } catch (final IOException e) {
+ throw new TexturePackParseException(e);
+ }
+ } else if(type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRGZ)) {
+ try {
+ texture = new PVRGZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
+ @Override
+ protected InputStream onGetInputStream() throws IOException {
+ return TexturePackParser.this.onGetInputStream(file);
+ }
+ };
+ } catch (final IOException e) {
+ throw new TexturePackParseException(e);
+ }
+ } else if(type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRCCZ)) {
+ try {
+ texture = new PVRCCZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
+ @Override
+ protected InputStream onGetInputStream() throws IOException {
+ return TexturePackParser.this.onGetInputStream(file);
+ }
+ };
+ } catch (final IOException e) {
+ throw new TexturePackParseException(e);
+ }
+ } else {
+ throw new TexturePackParseException(new IllegalArgumentException("Unsupported pTextureFormat: '" + type + "'."));
+ }
+
+ this.mTextureManager.addMappedTexture(file, texture);
+
+ return texture;
+ }
+
+ private static PixelFormat parsePixelFormat(final Attributes pAttributes) {
+ return PixelFormat.valueOf(SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_PIXELFORMAT));
+ }
+
+ private TextureOptions parseTextureOptions(final Attributes pAttributes) {
+ final int minFilter = TexturePackParser.parseMinFilter(pAttributes);
+ final int magFilter = TexturePackParser.parseMagFilter(pAttributes);
+ final int wrapT = this.parseWrapT(pAttributes);
+ final int wrapS = this.parseWrapS(pAttributes);
+ final boolean preMultiplyAlpha = this.parsePremultiplyalpha(pAttributes);
+
+ return new TextureOptions(minFilter, magFilter, wrapT, wrapS, preMultiplyAlpha);
+ }
+
+ private static int parseMinFilter(final Attributes pAttributes) {
+ final String minFilter = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER);
+ if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST)) {
+ return GL10.GL_NEAREST;
+ } else if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR)) {
+ return GL10.GL_LINEAR;
+ } else if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR_MIPMAP_LINEAR)) {
+ return GL10.GL_LINEAR_MIPMAP_LINEAR;
+ } else if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_LINEAR_MIPMAP_NEAREST)) {
+ return GL10.GL_LINEAR_MIPMAP_NEAREST;
+ } else if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST_MIPMAP_LINEAR)) {
+ return GL10.GL_NEAREST_MIPMAP_LINEAR;
+ } else if(minFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER_VALUE_NEAREST_MIPMAP_NEAREST)) {
+ return GL10.GL_NEAREST_MIPMAP_NEAREST;
+ } else {
+ throw new IllegalArgumentException("Unexpected " + TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MINFILTER + " attribute: '" + minFilter + "'.");
+ }
+ }
+
+ private static int parseMagFilter(final Attributes pAttributes) {
+ final String magFilter = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MAGFILTER);
+ if(magFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MAGFILTER_VALUE_NEAREST)) {
+ return GL10.GL_NEAREST;
+ } else if(magFilter.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MAGFILTER_VALUE_LINEAR)) {
+ return GL10.GL_LINEAR;
+ } else {
+ throw new IllegalArgumentException("Unexpected " + TexturePackParser.TAG_TEXTURE_ATTRIBUTE_MAGFILTER + " attribute: '" + magFilter + "'.");
+ }
+ }
+
+ private int parseWrapT(final Attributes pAttributes) {
+ return this.parseWrap(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_WRAPT);
+ }
+
+ private int parseWrapS(final Attributes pAttributes) {
+ return this.parseWrap(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_WRAPS);
+ }
+
+ private int parseWrap(final Attributes pAttributes, final String pWrapAttributeName) {
+ final String wrapAttribute = SAXUtils.getAttributeOrThrow(pAttributes, pWrapAttributeName);
+ if(this.mVersion == 1 && wrapAttribute.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_CLAMP)) {
+ return GL10.GL_CLAMP_TO_EDGE;
+ } else if(wrapAttribute.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_CLAMP_TO_EDGE)) {
+ return GL10.GL_CLAMP_TO_EDGE;
+ } else if(wrapAttribute.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_WRAP_VALUE_REPEAT)) {
+ return GL10.GL_REPEAT;
+ } else {
+ throw new IllegalArgumentException("Unexpected " + pWrapAttributeName + " attribute: '" + wrapAttribute + "'.");
+ }
+ }
+
+ private boolean parsePremultiplyalpha(final Attributes pAttributes) {
+ return SAXUtils.getBooleanAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_PREMULTIPLYALPHA);
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
89 src/org/andengine/util/texturepack/TexturePackTextureRegion.java
View
@@ -0,0 +1,89 @@
+package org.andengine.util.texturepack;
+
+import org.andengine.opengl.texture.ITexture;
+import org.andengine.opengl.texture.region.TextureRegion;
+
+/**
+ *
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 16:28:48 - 15.08.2011
+ */
+public class TexturePackTextureRegion extends TextureRegion {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final int mID;
+ private final String mSource;
+ private final boolean mTrimmed;
+ private final int mSourceX;
+ private final int mSourceY;
+ private final int mSourceWidth;
+ private final int mSourceHeight;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePackTextureRegion(final ITexture pTexture, final int pX, final int pY, final int pWidth, final int pHeight, final int pID, final String pSource, final boolean pRotated, final boolean pTrimmed, final int pSourceX, final int pSourceY, final int pSourceWidth, final int pSourceHeight) {
+ super(pTexture, pX, pY, pWidth, pHeight, pRotated);
+
+ this.mID = pID;
+ this.mSource = pSource;
+ this.mTrimmed = pTrimmed;
+ this.mSourceX = pSourceX;
+ this.mSourceY = pSourceY;
+ this.mSourceWidth = pSourceWidth;
+ this.mSourceHeight = pSourceHeight;
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ public int getID() {
+ return this.mID;
+ }
+
+ public String getSource() {
+ return this.mSource;
+ }
+
+ public boolean isTrimmed() {
+ return this.mTrimmed;
+ }
+
+ public int getSourceX() {
+ return this.mSourceX;
+ }
+
+ public int getSourceY() {
+ return this.mSourceY;
+ }
+
+ public int getSourceWidth() {
+ return this.mSourceWidth;
+ }
+
+ public int getSourceHeight() {
+ return this.mSourceHeight;
+ }
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
98 src/org/andengine/util/texturepack/TexturePackTextureRegionLibrary.java
View
@@ -0,0 +1,98 @@
+package org.andengine.util.texturepack;
+
+import java.util.HashMap;
+
+import android.util.SparseArray;
+
+/**
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 16:34:23 - 15.08.2011
+ */
+public class TexturePackTextureRegionLibrary {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final SparseArray<TexturePackTextureRegion> mIDMapping;
+ private final HashMap<String, TexturePackTextureRegion> mSourceMapping;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePackTextureRegionLibrary(final int pInitialCapacity) {
+ this.mIDMapping = new SparseArray<TexturePackTextureRegion>(pInitialCapacity);
+ this.mSourceMapping = new HashMap<String, TexturePackTextureRegion>(pInitialCapacity);
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ public SparseArray<TexturePackTextureRegion> getIDMapping() {
+ return this.mIDMapping;
+ }
+
+ public HashMap<String, TexturePackTextureRegion> getSourceMapping() {
+ return this.mSourceMapping;
+ }
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ public void put(final TexturePackTextureRegion pTexturePackTextureRegion) {
+ this.throwOnCollision(pTexturePackTextureRegion);
+
+ this.mIDMapping.put(pTexturePackTextureRegion.getID(), pTexturePackTextureRegion);
+ this.mSourceMapping.put(pTexturePackTextureRegion.getSource(), pTexturePackTextureRegion);
+ }
+
+ public void remove(final int pID) {
+ this.mIDMapping.remove(pID);
+ }
+
+ public TexturePackTextureRegion get(final int pID) {
+ return this.mIDMapping.get(pID);
+ }
+
+ public TexturePackTextureRegion get(final String pSource) {
+ return this.mSourceMapping.get(pSource);
+ }
+
+ public TexturePackTextureRegion get(final String pSource, final boolean pStripExtension) {
+ if(pStripExtension) {
+ final int indexOfExtension = pSource.lastIndexOf('.');
+ if(indexOfExtension == -1) {
+ return this.get(pSource);
+ } else {
+ final String stripped = pSource.substring(0, indexOfExtension);
+ return this.mSourceMapping.get(stripped);
+ }
+ } else {
+ return this.get(pSource);
+ }
+ }
+
+ private void throwOnCollision(final TexturePackTextureRegion pTexturePackTextureRegion) throws IllegalArgumentException {
+ if(this.mIDMapping.get(pTexturePackTextureRegion.getID()) != null) {
+ throw new IllegalArgumentException("Collision with ID: '" + pTexturePackTextureRegion.getID() + "'.");
+ } else if(this.mSourceMapping.get(pTexturePackTextureRegion.getSource()) != null) {
+ throw new IllegalArgumentException("Collision with Source: '" + pTexturePackTextureRegion.getSource() + "'.");
+ }
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
57 src/org/andengine/util/texturepack/exception/TexturePackParseException.java
View
@@ -0,0 +1,57 @@
+package org.andengine.util.texturepack.exception;
+
+import org.xml.sax.SAXException;
+
+/**
+ * (c) Zynga 2011
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 17:29:20 - 29.07.2011
+ */
+public class TexturePackParseException extends SAXException {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ private static final long serialVersionUID = 5773816582330137037L;
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ public TexturePackParseException() {
+ super();
+ }
+
+ public TexturePackParseException(final String pDetailMessage) {
+ super(pDetailMessage);
+ }
+
+ public TexturePackParseException(final Exception pException) {
+ super(pException);
+ }
+
+ public TexturePackParseException(final String pMessage, final Exception pException) {
+ super(pMessage, pException);
+ }
+
+ // ===========================================================
+ // Getter & Setter
+ // ===========================================================
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
Please sign in to comment.
Something went wrong with that request. Please try again.