Advertisement
dermetfan

Asset Management in LibGDX

Oct 24th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. package net.dermetfan.util.libgdx;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.assets.AssetDescriptor;
  5. import com.badlogic.gdx.assets.AssetManager;
  6. import com.badlogic.gdx.files.FileHandle;
  7. import com.badlogic.gdx.graphics.g2d.Animation;
  8. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  9.  
  10. /** This class is supposed to be edited by the user.<br/>
  11.  *  Pay attention to the comments.
  12.  *
  13.  *  <p>
  14.  *  To add an asset:
  15.  *      <ul>
  16.  *          <li>If it should be {@link #queue() queued}, add it to {@link Descriptors}, else to {@link OnDemandFileHandles} for manual use</li>
  17.  *          <li>Add a field for direct access</li>
  18.  *          <li>Initialize your asset field in {@link #init()}</li>
  19.  *          <li>If necessary, dispose your asset field in {@link #dispose()}</li>
  20.  *      </ul>
  21.  *  </p>
  22.  *
  23.  *  <p>
  24.  *      To load the {@link #queue() queued} assets:<br/>
  25.  *      <code>Assets.queue();<br/>
  26.  *      while(!Assets.update()) // or just Assets.manager.finishLoading() followed by Assets.init()<br/>
  27.  *          ;<br/>
  28.  *      // all assets are loaded and Assets#init() was called</code>
  29.  *  </p>
  30.  *  
  31.  *  <p>
  32.  *      To access your assets:<br/>
  33.  *      <code>Assets.yourCustomAssetField</code>
  34.  *  </p>
  35.  *  
  36.  *  <p>
  37.  *      To access on-demand assets (example):<br/>
  38.  *      <code>new Texture(Assets.OnDemandFileHandles.blackImg.fileHandle.path())</code>
  39.  *  </p>
  40.  *
  41.  *  @author dermetfan */
  42. public abstract class Assets {
  43.  
  44.     /** responsible for loading the assets */
  45.     public static final AssetManager manager = new AssetManager();
  46.  
  47.     /** {@link AssetManager#load(AssetDescriptor) queues} all not loaded {@link Descriptors assets} for loading */
  48.     public static void queue() {
  49.         for(Descriptors desc : Descriptors.values())
  50.             if(!manager.isLoaded(desc.descriptor.fileName))
  51.                 manager.load(desc.descriptor.fileName, desc.descriptor.type);
  52.     }
  53.  
  54.     /** {@link #init() Initializes} when loading is finished. Assets must have been {@link #queue() queued} before.
  55.      *  @see AssetManager#update() */
  56.     public static boolean update() {
  57.         if(manager.update()) {
  58.             init();
  59.             return true;
  60.         }
  61.         return false;
  62.     }
  63.  
  64.     /* DON'T TOUCH ANYTHING ABOVE THIS LINE
  65.      * ADD Descriptors AND OnDemandFileHandles BELOW */
  66.  
  67.     /** {@link AssetDescriptor AssetDescriptors} of assets that should be {@link Assets#queue() queued} */
  68.     public static enum Descriptors {
  69.         playerAtlas(new AssetDescriptor<TextureAtlas>(Gdx.files.internal("img/player/player.pack"), TextureAtlas.class));
  70.  
  71.         public final AssetDescriptor<?> descriptor;
  72.  
  73.         private Descriptors(AssetDescriptor<?> descriptor) {
  74.             this.descriptor = descriptor;
  75.         }
  76.  
  77.     }
  78.  
  79.     /** {@link FileHandle FileHandles} of assets that should not be {@link Assets#queue() queued} */
  80.     public static enum OnDemandFileHandles {
  81.         map1(Gdx.files.internal("maps/map.tmx"));
  82.  
  83.         public final FileHandle fileHandle;
  84.  
  85.         private OnDemandFileHandles(FileHandle fileHandle) {
  86.             this.fileHandle = fileHandle;
  87.         }
  88.  
  89.     }
  90.  
  91.     /* ADD FIELDS FOR DIRECT ACCESS TO YOUR ASSETS AND GENERATED ASSETS */
  92.  
  93.     public static TextureAtlas playerAtlas;
  94.     public static Animation playerStill, playerLeft, playerRight;
  95.  
  96.     /* ADD INITIALIZATION OF YOUR FIELDS HERE */
  97.     /** Initializes all fields. {@link #update() Loading} must be finished or some fields will be set to null. */
  98.     public static void init() {
  99.         { // player texture atlas and animations
  100.             playerAtlas = manager.get(Descriptors.playerAtlas.descriptor.fileName);
  101.             playerStill = new Animation(1 / 2f, playerAtlas.findRegions("still"));
  102.             playerLeft = new Animation(1 / 6f, playerAtlas.findRegions("left"));
  103.             playerRight = new Animation(1 / 6f, playerAtlas.findRegions("right"));
  104.             playerStill.setPlayMode(Animation.LOOP);
  105.             playerLeft.setPlayMode(Animation.LOOP);
  106.             playerRight.setPlayMode(Animation.LOOP);
  107.         }
  108.     }
  109.  
  110.     /* DISPOSE YOUR ASSET FIELDS HERE */
  111.     /** disposes all assets */
  112.     public static void dispose() {
  113.         { // player texture atlas and animations
  114.             playerAtlas.dispose();
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement