Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added AtlasSprite support when loading the animations and the sprites

  • Loading branch information...
commit 48aa4eebde48fe2d1d305a9c389f8bdfb4d6d7d0 1 parent 5e632ce
Ariel Coppes acoppes authored
26 commons-gdx-resources/src/main/java/com/gemserk/commons/gdx/resources/LibgdxResourceBuilder.java
View
@@ -2,7 +2,6 @@
import java.util.List;
-import org.omg.CORBA.INTERNAL;
import org.w3c.dom.Document;
import com.badlogic.gdx.Gdx;
@@ -15,6 +14,7 @@
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.gemserk.animation4j.FrameAnimationImpl;
@@ -123,17 +123,22 @@ public Sprite load() {
public void spriteAtlas(final String id, final String textureAtlasId, final String regionId) {
resourceManager.addVolatile(id, new DataLoader<Sprite>() {
- private AtlasRegion region = null;
+ private Sprite sprite = null;
@Override
public Sprite load() {
TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
- if (region == null) {
- region = textureAtlas.findRegion(regionId);
- if (region == null)
+
+ if (sprite == null) {
+ sprite = textureAtlas.createSprite(regionId);
+ if (sprite == null)
throw new RuntimeException("Failed to create Sprite resource " + id + " from region " + regionId + " from texture atlas " + textureAtlasId);
}
- return new Sprite(region);
+
+ if (sprite instanceof AtlasSprite)
+ return new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
+ else
+ return new Sprite(sprite);
}
});
}
@@ -157,7 +162,12 @@ public Animation load() {
Sprite[] frames = new Sprite[sprites.size()];
for (int i = 0; i < frames.length; i++) {
- frames[i] = new Sprite(sprites.get(i));
+ Sprite sprite = sprites.get(i);
+ if (sprite instanceof AtlasSprite)
+ frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
+ else
+ frames[i] = new Sprite(sprite);
+
}
int framesCount = frames.length;
@@ -351,7 +361,7 @@ public AnimationResourceBuilder animation2(String textureId) {
public static XmlDocumentResourceBuilder xmlDocument(String file) {
return xmlDocument(internal(file));
}
-
+
public static XmlDocumentResourceBuilder xmlDocument(FileHandle file) {
return new XmlDocumentResourceBuilder().file(file);
}
19 commons-gdx-resources/src/main/java/com/gemserk/commons/gdx/resources/SpriteResourceBuilder.java
View
@@ -3,7 +3,7 @@
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
-import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite;
import com.gemserk.commons.gdx.graphics.SpriteUtils;
import com.gemserk.resources.ResourceManager;
@@ -21,7 +21,7 @@
private String textureAtlasId;
private String regionId;
- private AtlasRegion region;
+ private Sprite spriteRegion = null;
private boolean flip = false;
private boolean flop = false;
@@ -121,21 +121,28 @@ public Sprite build() {
return sprite;
} else if (textureAtlasId != null) {
- if (region == null) {
+ if (spriteRegion == null) {
TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
try {
- region = textureAtlas.findRegion(regionId, regionIndex);
+ // region = textureAtlas.findRegion(regionId, regionIndex);
+ spriteRegion = textureAtlas.createSprite(regionId, regionIndex);
} catch (Exception e) {
throw new RuntimeException("Failed to load AtlasRegion " + regionId + " with index " + regionIndex + " from TextureAtlas " + textureAtlasId, e);
}
- if (region == null)
+ if (spriteRegion == null)
throw new RuntimeException("AtlasRegion " + regionId + " with index " + regionIndex + " from TextureAtlas " + textureAtlasId + " not found");
}
// note that whis resource will not be updated if the resource of the texture atlas changed...
- Sprite sprite = new Sprite(region);
+ Sprite sprite = null;
+
+ if (sprite instanceof AtlasSprite)
+ sprite = new AtlasSprite(((AtlasSprite) spriteRegion).getAtlasRegion());
+ else
+ sprite = new Sprite(spriteRegion);
+
sprite.flip(flop, flip);
sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
SpriteUtils.center(sprite, cx, cy);
Please sign in to comment.
Something went wrong with that request. Please try again.