Android Open Source - bbg-gdx-project-setup Assets






From Project

Back to project page bbg-gdx-project-setup.

License

The source code is released under:

MIT License

If you think the Android project bbg-gdx-project-setup 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.bluebananagames.gametemplate;
/*from  w ww  .  ja  v a  2  s. c o m*/
import java.util.HashMap;
import java.util.List;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Assets {

  // TODO: maybe add support for asynchronous loading of assets

  private static final String IMAGES_PATH = "data/images/";
  private static final String FONTS_PATH = "data/fonts/";
  private static final String SFX_PATH = "data/sfx/";
  private static final String MUSIC_PATH = "data/music/";

  private AssetManager assetManager = new AssetManager();
  private HashMap<String, Object> assets = new HashMap<String, Object>();

  /**
   * Retrieves an asset from the asset map. Before it can be retrieved, it has
   * to be loaded with the corresponding preload method.
   * 
   * @param filename
   *            The name of the asset file relative to its asset type folder
   *            path.
   * @return
   */
  public Object getAsset(String filename) {
    Object asset = assets.get(filename);
    if (asset == null) {
      warnNotLoaded(filename);
    }
    return asset;
  }

  public void unloadAssets(List<String> filenames) {
    for (String filename : filenames) {
      Object asset = assets.get(filename);
      if (asset != null) {
        assets.remove(filename);

        String assetPath = null;
        if (asset.getClass() == Sprite.class) {
          assetPath = imageFilePath(filename);
        } else if (asset.getClass() == BitmapFont.class) {
          assetPath = fontFilePath(filename);
        } else if (asset.getClass() == Sound.class) {
          assetPath = sfxFilePath(filename);
        } else if (asset.getClass() == Music.class) {
          assetPath = musicFilePath(filename);
        }

        if (assetPath != null) {
          assetManager.unload(assetPath);
        }
      }
    }
  }

  public void preloadSprites(List<String> filenames) {
    for (String filename : filenames) {
      assetManager.load(imageFilePath(filename), Texture.class);
    }
    assetManager.finishLoading();

    for (String filename : filenames) {
      assets.put(filename, loadSprite(filename));
    }
  }

  public Sprite getSprite(String filename) {
    return (Sprite) getAsset(filename);
  }

  public void preloadSFXs(List<String> filenames) {
    for (String filename : filenames) {
      assetManager.load(sfxFilePath(filename), Sound.class);
    }
    assetManager.finishLoading();

    for (String filename : filenames) {
      assets.put(filename, loadSound(filename));
    }
  }

  public Sound getSound(String filename) {
    return (Sound) getAsset(filename);
  }

  public void preloadMusic(List<String> filenames) {
    for (String filename : filenames) {
      assetManager.load(musicFilePath(filename), Music.class);
    }
    assetManager.finishLoading();

    for (String filename : filenames) {
      assets.put(filename, loadMusic(filename));
    }
  }

  public Music getMusic(String filename) {
    return (Music) getAsset(filename);
  }

  /**
   * Loads fonts for later use.
   * 
   * @param filenames
   *            A list of font names. Each font has to have a .fnt and a .png
   *            file which are existing in the FONTS_PATH directory. Pass the
   *            fonts' names with .fnt extension. E.g. "comicsans.fnt".
   */
  public void preloadFonts(List<String> filenames) {
    for (String filename : filenames) {
      assetManager.load(fontFilePath(filename), BitmapFont.class);
    }
    assetManager.finishLoading();

    for (String filename : filenames) {
      assets.put(filename, loadFont(filename));
    }
  }

  public BitmapFont getFont(String fontname) {
    return (BitmapFont) getAsset(fontname);
  }

  public void dispose() {
    // TODO: dispose asset instances??
    assetManager.dispose();
  }

  // HELPERS

  private Sprite loadSprite(String filename) {
    Sprite sprite = (Sprite) assets.get(filename);
    if (sprite != null) {
      return sprite;
    }

    Texture texture = assetManager.get(imageFilePath(filename),
        Texture.class);
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    TextureRegion region = new TextureRegion(texture, 0, 0,
        texture.getWidth(), texture.getHeight());
    sprite = new Sprite(region);
    sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
    sprite.setPosition(0, 0);

    return sprite;
  }

  private BitmapFont loadFont(String fontname) {
    return assetManager.get(fontFilePath(fontname), BitmapFont.class);
  }

  private Sound loadSound(String filename) {
    return assetManager.get(sfxFilePath(filename), Sound.class);
  }

  private Music loadMusic(String filename) {
    return assetManager.get(musicFilePath(filename), Music.class);
  }

  private void warnNotLoaded(String filename) {
    System.out.println("ASSETS WARNING: " + filename + " is not loaded!");
  }

  private static String imageFilePath(String filename) {
    return IMAGES_PATH + filename;
  }

  private static String fontFilePath(String filename) {
    return FONTS_PATH + filename;
  }

  private static String sfxFilePath(String filename) {
    return SFX_PATH + filename;
  }

  private static String musicFilePath(String filename) {
    return MUSIC_PATH + filename;
  }

}




Java Source Code List

com.bluebananagames.gametemplate.Assets.java
com.bluebananagames.gametemplate.GameTemplateGame.java
com.bluebananagames.gametemplate.MainActivity.java
com.bluebananagames.gametemplate.Main.java
com.bluebananagames.gametemplate.game.PlayerColor.java
com.bluebananagames.gametemplate.game.Player.java
com.bluebananagames.gametemplate.input.Control.java
com.bluebananagames.gametemplate.input.ControllerInputHandler.java
com.bluebananagames.gametemplate.input.InputHandler.java
com.bluebananagames.gametemplate.input.InputMapping.java
com.bluebananagames.gametemplate.input.KeyboardInputHandler.java
com.bluebananagames.gametemplate.render.WorldRenderer.java
com.bluebananagames.gametemplate.screen.AbstractScreen.java
com.bluebananagames.gametemplate.screen.GameScreen.java