Android Open Source - lamp-game Resources Manager






From Project

Back to project page lamp-game.

License

The source code is released under:

GNU General Public License

If you think the Android project lamp-game listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lifestudio.manager;
//from  www .  j  a v  a2  s .c o  m
import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
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 org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;

import com.lifestudio.GameActivity;

public class ResourcesManager {

  // ---------------------------------------------
  // VARIABLES
  // ---------------------------------------------

  private static final ResourcesManager INSTANCE = new ResourcesManager();

  public Engine engine;
  public GameActivity activity;
  public BoundCamera camera;
  public VertexBufferObjectManager vbom;

  public ITextureRegion splash_region;
  public ITextureRegion black_region;
  private BuildableBitmapTextureAtlas splashTextureAtlas;

  public Font font;

  // ---------------------------------------------
  // TEXTURES & TEXTURE REGIONS
  // ---------------------------------------------
  public ITextureRegion menu_background_region;
  public ITextureRegion play_region;
  public ITextureRegion options_region;
  public ITextureRegion light_region; 
  //public ITiledTextureRegion light_region;

  private BuildableBitmapTextureAtlas menuTextureAtlas;
  
  // Game Texture
  public BuildableBitmapTextureAtlas gameTextureAtlas;
      
  // Game Texture Regions
  public ITextureRegion platform1_region;
  public ITextureRegion platform2_region;
  public ITextureRegion platform3_region;
  public ITextureRegion coin_region;
  
  // Player Texture & Region
  public ITiledTextureRegion player_region;
  
  // Level Complete Window
  public ITextureRegion complete_window_region;
  public ITiledTextureRegion complete_stars_region;
  
  // ---------------------------------------------
  // SOUND & MUSIC
  // ---------------------------------------------
  
  public Music music;
  
  
  // ---------------------------------------------
  // CLASS LOGIC
  // ---------------------------------------------

  public void loadMenuResources() {
    loadMenuGraphics();
    loadMenuAudio();
    loadMenuFonts();
    //play();
  }

  public void loadGameResources() {
    loadGameGraphics();
    loadGameFonts();
    loadGameAudio();
  }

  private void loadMenuGraphics() {
    // Creating a buildable bitmap texture atlas so we don't have to specify
    // positions of particular graphics inside texture.
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
    menuTextureAtlas = new BuildableBitmapTextureAtlas(
        activity.getTextureManager(), 2048, 1024,
        TextureOptions.BILINEAR);
    menu_background_region = BitmapTextureAtlasTextureRegionFactory
        .createFromAsset(menuTextureAtlas, activity,
            "menu_background.png");
    play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
        menuTextureAtlas, activity, "play.png");
    options_region = BitmapTextureAtlasTextureRegionFactory
        .createFromAsset(menuTextureAtlas, activity, "options.png");
    
    
    light_region = BitmapTextureAtlasTextureRegionFactory
        .createFromAsset(menuTextureAtlas, activity, "light.png");
    
    //light_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(menuTextureAtlas, activity, "light_sprite.png", 7, 7);

    try {
      this.menuTextureAtlas
          .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
              0, 1, 0));
      this.menuTextureAtlas.load();
    } catch (final TextureAtlasBuilderException e) {
      Debug.e(e);
    }

  }

  private void loadMenuFonts() {
    FontFactory.setAssetBasePath("font/");
    final ITexture mainFontTexture = new BitmapTextureAtlas(
        activity.getTextureManager(), 256, 256,
        TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    font = FontFactory.createStrokeFromAsset(activity.getFontManager(),
        mainFontTexture, activity.getAssets(), "regv2.ttf",
        50, true, Color.WHITE, 2, Color.BLACK);
    font.load();
  }

  private void loadMenuAudio() {
    MusicFactory.setAssetBasePath("mfx/");
    try {
      music = MusicFactory.createMusicFromAsset(activity.getMusicManager(), activity, "music_test.mp3");
      music.setLooping(true);
    } catch (final IOException e) {
      Debug.e(e);
    }
  }
  
  public void play() {
    music.play();
  }
  
  public Music getMusic() {
    return music;
  }

  private void loadGameGraphics() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
      gameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 4096, 1024, TextureOptions.BILINEAR);
      
      platform1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform1.png");
      platform2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform2.png");
      platform3_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform3.png");
      coin_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "coin.png");
      
      player_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "player.png", 27, 1);
      
      complete_window_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "levelCompleteWindow.png");
      complete_stars_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "star.png", 2, 1);
          
      try 
      {
        
          this.gameTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
          this.gameTextureAtlas.load();
      } 
      catch (final TextureAtlasBuilderException e)
      {
          Debug.e(e);
          Log.wtf("MyTag", "ERROR: " + e.toString());
      }
  }

  private void loadGameFonts() {

  }

  private void loadGameAudio() {

  }

  public void loadSplashScreen() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    splashTextureAtlas = new BuildableBitmapTextureAtlas(
        activity.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
    
    splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
        splashTextureAtlas, activity, "splash2.png");
    
    black_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
        splashTextureAtlas, activity, "fade_splash.png");
    
    try {
      this.splashTextureAtlas
          .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
              0, 1, 0));
      this.splashTextureAtlas.load();
    } catch (final TextureAtlasBuilderException e) {
      Debug.e(e);
    }
    
    loadMenuAudio();
    play();
  }

  public void unloadSplashScreen() {
    splashTextureAtlas.unload();
    splash_region = null;
  }

  public void unloadMenuTextures() {
    menuTextureAtlas.unload();
  }

  public void loadMenuTextures() {
    menuTextureAtlas.load();
  }
  
  public void unloadGameTextures(){
    // TODO 
  }

  /**
   * @param engine
   * @param activity
   * @param camera
   * @param vbom
   * <br>
   * <br>
   *            We use this method at beginning of game loading, to prepare
   *            Resources Manager properly, setting all needed parameters, so
   *            we can latter access them from different classes (eg. scenes)
   */
  public static void prepareManager(Engine engine, GameActivity activity,
      BoundCamera camera, VertexBufferObjectManager vbom) {
    getInstance().engine = engine;
    getInstance().activity = activity;
    getInstance().camera = camera;
    getInstance().vbom = vbom;
    
  }

  // ---------------------------------------------
  // GETTERS AND SETTERS
  // ---------------------------------------------

  public static ResourcesManager getInstance() {
    return INSTANCE;
  }
  
  public void playMusic(){
    music.play();
  }
  
  public void pauseMusic() {
    music.pause();
  }
  
  
}




Java Source Code List

com.lifestudio.GameActivity.java
com.lifestudio.base.BaseScene.java
com.lifestudio.extras.LevelCompleteWindow.java
com.lifestudio.manager.ResourcesManager.java
com.lifestudio.manager.SceneManager.java
com.lifestudio.object.Player.java
com.lifestudio.scene.GameScene.java
com.lifestudio.scene.LoadingScene.java
com.lifestudio.scene.MainMenuScene.java
com.lifestudio.scene.SplashScene.java