Skip to content

Commit

Permalink
Added AnimationPackParser/Loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Gramlich committed May 7, 2012
1 parent b2c25bf commit b73f4ac
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/org/andengine/util/animationpack/AnimationPack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.andengine.util.animationpack;

import org.andengine.util.texturepack.TexturePackLibrary;


/**
* (c) Zynga 2012
*
* @author Nicolas Gramlich <ngramlich@zynga.com>
* @since 16:58:15 - 03.05.2012
*/
public class AnimationPack {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private final TexturePackLibrary mTexturePackLibrary;
private final AnimationPackTiledTextureRegionLibrary mAnimationPackTiledTextureRegionLibrary;

// ===========================================================
// Constructors
// ===========================================================

public AnimationPack(final TexturePackLibrary pTexturePackLibrary, final AnimationPackTiledTextureRegionLibrary pAnimationPackTiledTextureRegionLibrary) {
this.mTexturePackLibrary = pTexturePackLibrary;
this.mAnimationPackTiledTextureRegionLibrary = pAnimationPackTiledTextureRegionLibrary;
}

// ===========================================================
// Getter & Setter
// ===========================================================

public TexturePackLibrary getTexturePackLibrary() {
return this.mTexturePackLibrary;
}

public AnimationPackTiledTextureRegionLibrary getAnimationPackAnimationDataLibrary() {
return this.mAnimationPackTiledTextureRegionLibrary;
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
94 changes: 94 additions & 0 deletions src/org/andengine/util/animationpack/AnimationPackLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.andengine.util.animationpack;

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.animationpack.exception.AnimationPackParseException;
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 AnimationPackLoader {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private final AssetManager mAssetManager;
private final TextureManager mTextureManager;

// ===========================================================
// Constructors
// ===========================================================

public AnimationPackLoader(final AssetManager pAssetManager, final TextureManager pTextureManager) {
this.mAssetManager = pAssetManager;
this.mTextureManager = pTextureManager;
}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public AnimationPack loadFromAsset(final String pAssetPath, final String pAssetBasePath) throws AnimationPackParseException {
try {
return this.load(this.mAssetManager.open(pAssetPath), pAssetBasePath);
} catch (final IOException e) {
throw new AnimationPackParseException("Could not load " + this.getClass().getSimpleName() + " data from asset: " + pAssetPath, e);
}
}

public AnimationPack load(final InputStream pInputStream, final String pAssetBasePath) throws AnimationPackParseException {
try{
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();

final XMLReader xr = sp.getXMLReader();
final AnimationPackParser animationPackParser = new AnimationPackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
xr.setContentHandler(animationPackParser);

xr.parse(new InputSource(new BufferedInputStream(pInputStream)));

return animationPackParser.getAnimationPack();
} catch (final SAXException e) {
throw new AnimationPackParseException(e);
} catch (final ParserConfigurationException pe) {
/* Doesn't happen. */
return null;
} catch (final IOException e) {
throw new AnimationPackParseException(e);
} finally {
StreamUtils.close(pInputStream);
}
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
164 changes: 164 additions & 0 deletions src/org/andengine/util/animationpack/AnimationPackParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package org.andengine.util.animationpack;

import java.util.ArrayList;

import org.andengine.entity.sprite.IAnimationData;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.util.SAXUtils;
import org.andengine.util.adt.list.LongArrayList;
import org.andengine.util.animationpack.exception.AnimationPackParseException;
import org.andengine.util.texturepack.TexturePack;
import org.andengine.util.texturepack.TexturePackLibrary;
import org.andengine.util.texturepack.TexturePackLoader;
import org.andengine.util.texturepack.TexturePackTextureRegion;
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 AnimationPackParser extends DefaultHandler {
// ===========================================================
// Constants
// ===========================================================

private static final String TAG_ANIMATIONPACK = "animationpack";
private static final String TAG_ANIMATIONPACK_ATTRIBUTE_VERSION = "version";

private static final String TAG_TEXTUREPACKS = "texturepacks";

private static final String TAG_TEXTUREPACK = "texturepack";
private static final String TAG_TEXTUREPACK_ATTRIBUTE_FILENAME = "filename";

private static final String TAG_ANIMATIONS = "animations";

private static final String TAG_ANIMATION = "animation";
private static final String TAG_ANIMATION_ATTRIBUTE_NAME = "name";
private static final String TAG_ANIMATION_ATTRIBUTE_LOOPCOUNT = "loopcount";

private static final String TAG_ANIMATIONFRAME = "animationframe";
private static final String TAG_ANIMATIONFRAME_ATTRIBUTE_DURATION = "duration";
private static final String TAG_ANIMATIONFRAME_ATTRIBUTE_TEXTUREREGION = "textureregion";

// ===========================================================
// Fields
// ===========================================================

private final AssetManager mAssetManager;
private final String mAssetBasePath;
private final TextureManager mTextureManager;

private AnimationPack mAnimationPack;
private AnimationPackTiledTextureRegionLibrary mAnimationPackTiledTextureRegionLibrary;
private TexturePackLibrary mTexturePackLibrary;
private TexturePackLoader mTexturePackLoader;

private String mCurrentAnimationName;
private int mCurrentAnimationLoopCount = IAnimationData.LOOP_CONTINUOUS;
private final LongArrayList mCurrentAnimationFrameDurations = new LongArrayList();
private final ArrayList<TexturePackTextureRegion> mCurrentAnimationFrameTexturePackTextureRegions = new ArrayList<TexturePackTextureRegion>();

// ===========================================================
// Constructors
// ===========================================================

public AnimationPackParser(final AssetManager pAssetManager, final String pAssetBasePath, final TextureManager pTextureManager) {
this.mAssetManager = pAssetManager;
this.mAssetBasePath = pAssetBasePath;
this.mTextureManager = pTextureManager;
}

// ===========================================================
// Getter & Setter
// ===========================================================

public AnimationPack getAnimationPack() {
return this.mAnimationPack;
}

// ===========================================================
// 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(AnimationPackParser.TAG_ANIMATIONPACK)) {
final int version = SAXUtils.getIntAttributeOrThrow(pAttributes, AnimationPackParser.TAG_ANIMATIONPACK_ATTRIBUTE_VERSION);
if(version != 1) {
throw new AnimationPackParseException("Unexpected version: '" + version + "'.");
}

this.mTexturePackLoader = new TexturePackLoader(this.mAssetManager, this.mTextureManager);
this.mTexturePackLibrary = new TexturePackLibrary();
this.mAnimationPackTiledTextureRegionLibrary = new AnimationPackTiledTextureRegionLibrary();
this.mAnimationPack = new AnimationPack(this.mTexturePackLibrary, this.mAnimationPackTiledTextureRegionLibrary);
} else if(pLocalName.equals(AnimationPackParser.TAG_TEXTUREPACKS)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_TEXTUREPACK)) {
final String texturePackName = SAXUtils.getAttributeOrThrow(pAttributes, AnimationPackParser.TAG_TEXTUREPACK_ATTRIBUTE_FILENAME);
final String texturePackPath = this.mAssetBasePath + texturePackName;

final TexturePack texturePack = this.mTexturePackLoader.loadFromAsset(texturePackPath, this.mAssetBasePath);
this.mTexturePackLibrary.put(texturePackName, texturePack);
texturePack.loadTexture();
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATIONS)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATION)) {
this.mCurrentAnimationName = SAXUtils.getAttributeOrThrow(pAttributes, AnimationPackParser.TAG_ANIMATION_ATTRIBUTE_NAME);
this.mCurrentAnimationLoopCount = SAXUtils.getIntAttribute(pAttributes, AnimationPackParser.TAG_ANIMATION_ATTRIBUTE_LOOPCOUNT, IAnimationData.LOOP_CONTINUOUS);
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATIONFRAME)) {
final int duration = SAXUtils.getIntAttributeOrThrow(pAttributes, AnimationPackParser.TAG_ANIMATIONFRAME_ATTRIBUTE_DURATION);
this.mCurrentAnimationFrameDurations.add(duration);

final String textureRegionName = SAXUtils.getAttributeOrThrow(pAttributes, AnimationPackParser.TAG_ANIMATIONFRAME_ATTRIBUTE_TEXTUREREGION);
final TexturePackTextureRegion texturePackTextureRegion = this.mTexturePackLibrary.getTexturePackTextureRegion(textureRegionName);
this.mCurrentAnimationFrameTexturePackTextureRegions.add(texturePackTextureRegion);
} else {
throw new AnimationPackParseException("Unexpected tag: '" + pLocalName + "'.");
}
}

@Override
public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException {
if(pLocalName.equals(AnimationPackParser.TAG_ANIMATIONPACK)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_TEXTUREPACKS)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_TEXTUREPACK)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATIONS)) {
/* Nothing. */
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATION)) {
final int currentAnimationFrameFrameCount = this.mCurrentAnimationFrameDurations.size();
final long[] frameDurations = this.mCurrentAnimationFrameDurations.toArray();
final TexturePackTextureRegion[] textureRegions = new TexturePackTextureRegion[currentAnimationFrameFrameCount];
this.mCurrentAnimationFrameTexturePackTextureRegions.toArray(textureRegions);

final AnimationPackTiledTextureRegion animationPackTiledTextureRegion = new AnimationPackTiledTextureRegion(this.mCurrentAnimationName, frameDurations, this.mCurrentAnimationLoopCount, textureRegions[0].getTexture(), textureRegions);
this.mAnimationPackTiledTextureRegionLibrary.put(animationPackTiledTextureRegion);

this.mCurrentAnimationName = null;
this.mCurrentAnimationLoopCount = IAnimationData.LOOP_CONTINUOUS;
this.mCurrentAnimationFrameDurations.clear();
this.mCurrentAnimationFrameTexturePackTextureRegions.clear();
} else if(pLocalName.equals(AnimationPackParser.TAG_ANIMATIONFRAME)) {
/* Nothing. */
} else {
throw new AnimationPackParseException("Unexpected end tag: '" + pLocalName + "'.");
}
}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.andengine.util.animationpack;

import org.andengine.entity.sprite.AnimationData;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;

/**
* (c) Zynga 2012
*
* @author Nicolas Gramlich <ngramlich@zynga.com>
* @since 17:17:17 - 03.05.2012
*/
public class AnimationPackTiledTextureRegion extends TiledTextureRegion {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private final String mAnimationName;
private final AnimationData mAnimationData;

// ===========================================================
// Constructors
// ===========================================================

public AnimationPackTiledTextureRegion(final String pAnimationName, final long[] pFrameDurations, final int pLoopCount, final ITexture pTexture, final ITextureRegion ... pTextureRegions) {
super(pTexture, pTextureRegions);

this.mAnimationName = pAnimationName;
final int frameCount = pFrameDurations.length;

final int[] frames= new int[frameCount];
for(int i = 0; i < frameCount; i++) {
frames[i] = i;
}

this.mAnimationData = new AnimationData(pFrameDurations, frames, pLoopCount);
}

// ===========================================================
// Getter & Setter
// ===========================================================

public String getAnimationName() {
return this.mAnimationName;
}

public AnimationData getAnimationData() {
return this.mAnimationData;
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Loading

0 comments on commit b73f4ac

Please sign in to comment.