Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added createTiledFromAssetDirectory() method to BitmapTextureAtlasTextureRegionFactory #99

Merged
merged 2 commits into from

2 participants

@nazgee

This method allows to load mutliple assets from a given directory to a
single ITiledTextureRegion.

Instead of loading multiple assets one by one it is sometimes convinient to do sth like this:

ITiledTextureRegion TEXS_SUNSHINE = BitmapTextureAtlasTextureRegionFactory.createTiledFromAssetDirectory(
                    atlasSunshine, c.getAssets(), "shine");

And have dozen of sunshine files from gfx/shine directory loaded as tiles of ITiledTextureRegion in just one go.

...as/bitmap/BitmapTextureAtlasTextureRegionFactory.java
((9 lines not shown))
+ * @param pAssetManager
+ * @param pAssetsSubdirectory to load all files from "gfx/flowers" put "flowers" here (assuming, that you've used setAssetBasePath("gfx/") on this factory)
+ * @return
+ */
+ public static ITiledTextureRegion createTiledFromAssetDirectory(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final AssetManager pAssetManager, final String pAssetsSubdirectory) {
+ ITextureRegion textures[];
+ String[] files;
+
+ try {
+ files = pAssetManager.list(getAssetBasePath() + pAssetsSubdirectory);
+ textures = new ITextureRegion[files.length];
+ } catch (IOException ioex) {
+ throw new RuntimeException("Listing assets directory failed! " + getAssetBasePath() + pAssetsSubdirectory + " does not exist?", ioex);
+ }
+
+ int i = 0;
@nicolasgramlich Owner

Can we convert this to a index based for loop?

@nazgee
nazgee added a note

will do... though after entering Java world from C/C++ universe, this construction seems sooooo hot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/org/andengine/audio/music/MusicFactory.java
@@ -45,6 +45,10 @@ public static void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public static String getAssetBasePath() {
+ return sAssetBasePath;
@nicolasgramlich Owner

Can you add the 'scope' to these variables? =)

I mean "sAssetBasePath" -> "MusicFactory.sAssetBasePath"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...as/bitmap/BitmapTextureAtlasTextureRegionFactory.java
((4 lines not shown))
+ /**
+ * Loads all files from a given assets directory (in alphabetical order) as
+ * consecutive tiles of ITiledTextureRegion which it returns
+ *
+ * @param pBuildableBitmapTextureAtlas
+ * @param pAssetManager
+ * @param pAssetsSubdirectory to load all files from "gfx/flowers" put "flowers" here (assuming, that you've used setAssetBasePath("gfx/") on this factory)
+ * @return
+ */
+ public static ITiledTextureRegion createTiledFromAssetDirectory(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final AssetManager pAssetManager, final String pAssetsSubdirectory) {
+ ITextureRegion textures[];
+ String[] files;
+
+ try {
+ files = pAssetManager.list(getAssetBasePath() + pAssetsSubdirectory);
+ textures = new ITextureRegion[files.length];
@nicolasgramlich Owner

Can you move this below the try-catch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...as/bitmap/BitmapTextureAtlasTextureRegionFactory.java
((6 lines not shown))
+ * consecutive tiles of ITiledTextureRegion which it returns
+ *
+ * @param pBuildableBitmapTextureAtlas
+ * @param pAssetManager
+ * @param pAssetsSubdirectory to load all files from "gfx/flowers" put "flowers" here (assuming, that you've used setAssetBasePath("gfx/") on this factory)
+ * @return
+ */
+ public static ITiledTextureRegion createTiledFromAssetDirectory(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final AssetManager pAssetManager, final String pAssetsSubdirectory) {
+ ITextureRegion textures[];
+ String[] files;
+
+ try {
+ files = pAssetManager.list(getAssetBasePath() + pAssetsSubdirectory);
+ textures = new ITextureRegion[files.length];
+ } catch (IOException ioex) {
+ throw new RuntimeException("Listing assets directory failed! " + getAssetBasePath() + pAssetsSubdirectory + " does not exist?", ioex);
@nicolasgramlich Owner

Can you throw an AndEngineRuntimeException here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
nazgee added some commits
@nazgee nazgee Added getAssetBasePath() method to all factories
This method was added to all factories that had setAssetBasePath()
method declared.
dcdeb80
@nazgee nazgee Added createTiledFromAssetDirectory() method to BitmapTextureAtlasTex…
…tureRegionFactory

This method allows to load mutliple assets from a given directory to a
single ITiledTextureRegion.
ce7742b
@nazgee

^ fixed all your remarks :)

@nicolasgramlich nicolasgramlich merged commit e799e9e into nicolasgramlich:GLES2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2012
  1. @nazgee

    Added getAssetBasePath() method to all factories

    nazgee authored
    This method was added to all factories that had setAssetBasePath()
    method declared.
  2. @nazgee

    Added createTiledFromAssetDirectory() method to BitmapTextureAtlasTex…

    nazgee authored
    …tureRegionFactory
    
    This method allows to load mutliple assets from a given directory to a
    single ITiledTextureRegion.
This page is out of date. Refresh to see the latest.
View
4 src/org/andengine/audio/music/MusicFactory.java
@@ -45,6 +45,10 @@ public static void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public static String getAssetBasePath() {
+ return MusicFactory.sAssetBasePath;
+ }
+
public static void onCreate() {
MusicFactory.setAssetBasePath("");
}
View
4 src/org/andengine/audio/sound/SoundFactory.java
@@ -44,6 +44,10 @@ public static void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public static String getAssetBasePath() {
+ return SoundFactory.sAssetBasePath;
+ }
+
public static void onCreate() {
SoundFactory.setAssetBasePath("");
}
View
4 src/org/andengine/opengl/font/FontFactory.java
@@ -50,6 +50,10 @@ public static void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public static String getAssetBasePath() {
+ return FontFactory.sAssetBasePath;
+ }
+
public static void onCreate() {
FontFactory.setAssetBasePath("");
}
View
36 src/org/andengine/opengl/texture/atlas/bitmap/BitmapTextureAtlasTextureRegionFactory.java
@@ -1,13 +1,17 @@
package org.andengine.opengl.texture.atlas.bitmap;
+import java.io.IOException;
+
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.bitmap.source.ResourceBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
+import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
+import org.andengine.util.exception.AndEngineRuntimeException;
import android.content.Context;
import android.content.res.AssetManager;
@@ -51,6 +55,10 @@ public static void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public static String getAssetBasePath() {
+ return BitmapTextureAtlasTextureRegionFactory.sAssetBasePath;
+ }
+
public static void reset() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("");
}
@@ -143,6 +151,34 @@ public static TiledTextureRegion createTiledFromAsset(final BuildableBitmapTextu
return BitmapTextureAtlasTextureRegionFactory.createTiledFromSource(pBuildableBitmapTextureAtlas, bitmapTextureAtlasSource, pTileColumns, pTileRows);
}
+ /**
+ * Loads all files from a given assets directory (in alphabetical order) as
+ * consecutive tiles of ITiledTextureRegion which it returns
+ *
+ * @param pBuildableBitmapTextureAtlas
+ * @param pAssetManager
+ * @param pAssetsSubdirectory to load all files from "gfx/flowers" put "flowers" here (assuming, that you've used setAssetBasePath("gfx/") on this factory)
+ * @return
+ */
+ public static ITiledTextureRegion createTiledFromAssetDirectory(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final AssetManager pAssetManager, final String pAssetsSubdirectory) {
+ ITextureRegion textures[];
+ String[] files;
+
+ try {
+ files = pAssetManager.list(getAssetBasePath() + pAssetsSubdirectory);
+ } catch (IOException ioex) {
+ throw new AndEngineRuntimeException("Listing assets directory failed! " + getAssetBasePath() + pAssetsSubdirectory + " does not exist?", ioex);
+ }
+ textures = new ITextureRegion[files.length];
+
+ for (int i = 0; i < files.length; i++) {
+ String file = pAssetsSubdirectory + "/" + files[i];
+ textures[i] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(pBuildableBitmapTextureAtlas, pAssetManager, file);
+ }
+
+ return new TiledTextureRegion(pBuildableBitmapTextureAtlas, textures);
+ }
+
public static ITextureRegion createFromResource(final BuildableBitmapTextureAtlas pBuildableBitmapTextureAtlas, final Context pContext, final int pDrawableResourceID) {
return BitmapTextureAtlasTextureRegionFactory.createFromResource(pBuildableBitmapTextureAtlas, pContext.getResources(), pDrawableResourceID);
View
4 src/org/andengine/util/level/LevelLoader.java
@@ -76,6 +76,10 @@ public void setAssetBasePath(final String pAssetBasePath) {
}
}
+ public String getAssetBasePath() {
+ return mAssetBasePath;
+ }
+
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
Something went wrong with that request. Please try again.