Skip to content

Instantly share code, notes, and snippets.

@nipundavid
Created October 6, 2013 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nipundavid/6850218 to your computer and use it in GitHub Desktop.
Save nipundavid/6850218 to your computer and use it in GitHub Desktop.
package com.example.samplegame;
import org.andengine.engine.options.EngineOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import android.content.Context;
import android.util.Log;
public class ResourceManager {
// ResourceManager Singleton instance
private static ResourceManager INSTANCE;
/* The variables listed should be kept public, allowing us easy access
to them when creating new Sprites, Text objects and to play sound files */
public ITextureRegion mTextureRegionBackGround_1;
public ITextureRegion mTextureRegionBackGround_2;
public ITextureRegion mTextureRegionBackGround_3;
public ITextureRegion mTextureRegionBackGround_4;
ResourceManager(){
// The constructor is of no use to us
}
public synchronized static ResourceManager getInstance(){
if(INSTANCE == null){
INSTANCE = new ResourceManager();
}
return INSTANCE;
}
/* Each scene within a game should have a loadTextures method as well
* as an accompanying unloadTextures method. This way, we can display
* a loading image during scene swapping, unload the first scene's textures
* then load the next scenes textures.
*/
public synchronized void loadGameTextures(EngineOptions pEngine, Context pContext, org.andengine.engine.Engine mEngine){
// Set our game assets folder in "assets/gfx/game/"
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_1 = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 800, 480);
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_2 = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 800, 480);
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_3 = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 800, 480);
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_4 = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 800, 480);
mTextureRegionBackGround_1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlasBackGround_1, pContext, "bg_1.png");
mTextureRegionBackGround_2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlasBackGround_2, pContext, "bg_2.png");
mTextureRegionBackGround_3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlasBackGround_3, pContext, "bg_3.png");
mTextureRegionBackGround_4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlasBackGround_4, pContext, "bg_4.png");
try {
mBitmapTextureAtlasBackGround_1.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
mBitmapTextureAtlasBackGround_2.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
mBitmapTextureAtlasBackGround_3.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
mBitmapTextureAtlasBackGround_4.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
mBitmapTextureAtlasBackGround_1.load();
mBitmapTextureAtlasBackGround_2.load();
mBitmapTextureAtlasBackGround_3.load();
mBitmapTextureAtlasBackGround_4.load();
} catch (TextureAtlasBuilderException e) {
Log.e("Exceptions", ""+e);
}
}
/* All textures should have a method call for unloading once
* they're no longer needed; ie. a level transition. */
public synchronized void unloadGameTextures(){
// call unload to remove the corresponding texture atlas from memory
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_1 = (BuildableBitmapTextureAtlas) mTextureRegionBackGround_1.getTexture();
mBitmapTextureAtlasBackGround_1.unload();
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_2 = (BuildableBitmapTextureAtlas) mTextureRegionBackGround_2.getTexture();
mBitmapTextureAtlasBackGround_2.unload();
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_3 = (BuildableBitmapTextureAtlas) mTextureRegionBackGround_3.getTexture();
mBitmapTextureAtlasBackGround_3.unload();
BuildableBitmapTextureAtlas mBitmapTextureAtlasBackGround_4 = (BuildableBitmapTextureAtlas) mTextureRegionBackGround_4.getTexture();
mBitmapTextureAtlasBackGround_4.unload();
}
/* As with textures, we can create methods to load sound/music objects
* for different scene's within our games.
*/
public synchronized void loadSounds(EngineOptions pEngine, Context pContext, org.andengine.engine.Engine mEngine){
}
/* In some cases, we may only load one set of sounds throughout
* our entire game's life-cycle. If that's the case, we may not
* need to include an unloadSounds() method. Of course, this all
* depends on how much variance we have in terms of sound
*/
public synchronized void unloadSounds(){
}
/* Lastly, we've got the loadFonts method which, once again,
* tends to only need to be loaded once as Font's are generally
* used across an entire game, from menu to shop to game-play.
*/
public synchronized void loadFonts(EngineOptions pEngine, Context con, org.andengine.engine.Engine mEngine){
}
/* If an unloadFonts() method is necessary, we can provide one
*/
public synchronized void unloadFonts(){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment