Coverage Summary for Class: ResourceManager (com.mygdx.game.Managers)

Class Class, % Method, % Line, %
ResourceManager 100% (1/1) 25% (6/24) 20.5% (25/122)


1 package com.mygdx.game.Managers; 2  3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.assets.AssetManager; 5 import com.badlogic.gdx.graphics.Texture; 6 import com.badlogic.gdx.graphics.g2d.BitmapFont; 7 import com.badlogic.gdx.graphics.g2d.Sprite; 8 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 9 import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; 10 import com.badlogic.gdx.maps.tiled.TiledMap; 11 import com.badlogic.gdx.maps.tiled.TmxMapLoader; 12  13 import java.util.ArrayList; 14 import java.util.HashMap; 15  16 /** 17  * Manages all assets and disposes of them when appropriate 18  */ 19 public final class ResourceManager { 20  private static boolean initialized = false; 21  private static boolean loaded; 22  private static AssetManager manager; 23  private static ArrayList<String> ids; 24  private static ArrayList<TiledMap> tileMaps; 25  private static HashMap<String, FreeTypeFontGenerator> fontGenerators; 26  private static HashMap<String, BitmapFont> fonts; 27  28  /** 29  * The equivalent to a constructor 30  */ 31  public static void Initialize() { 32  if (initialized) { 33  return; 34  } 35  initialized = true; 36  manager = new AssetManager(); 37  loaded = false; 38  ids = new ArrayList<>(); 39  tileMaps = new ArrayList<>(); 40  fontGenerators = new HashMap<>(); 41  fonts = new HashMap<>(); 42  } 43  44  /** 45  * Schedules an asset for loading 46  * 47  * @param fPath the file path of the asset 48  * @return returns the id of the asset can be used in place of the name; 49  */ 50  public static int addTexture(String fPath) { 51  tryInit(); 52  checkAdd(); 53  manager.load(fPath, Texture.class); 54  ids.add(fPath); 55  return ids.size(); 56  } 57  58  /** 59  * Schedules an asset for loading 60  * 61  * @param fPath the file path of the asset 62  * @return returns the id of the asset can be used in place of the name; 63  */ 64  public static int addTextureAtlas(String fPath) { 65  tryInit(); 66  checkAdd(); 67  manager.load(fPath, TextureAtlas.class); 68  ids.add(fPath); 69  return ids.size(); 70  } 71  72  /** 73  * Prefaces name with |TM| followed by the internal index of the tilemap however this isn't required to access this asset 74  * 75  * @param fPath the file location of the asset 76  * @return id of the asset 77  */ 78  public static int addTileMap(String fPath) { 79  tryInit(); 80  checkAdd(); 81  TiledMap map = new TmxMapLoader().load(fPath); 82  tileMaps.add(map); 83  ids.add("|TM|" + tileMaps.size() + fPath); 84  return ids.size(); 85  } 86  87  /** 88  * loads the font file this doesn't create a font (MAY NOT WORK) 89  * 90  * @param fontPath the path of the font file 91  * @return the id of the font generator 92  */ 93  public static int addFontGenerator(String fontPath) { 94  tryInit(); 95  checkAdd(); 96  if (fontGenerators.containsKey(fontPath)) { 97  return -1; 98  } 99  fontGenerators.put(fontPath, new FreeTypeFontGenerator(Gdx.files.internal(fontPath))); 100  ids.add("|FG|" + fontPath); 101  102  return ids.size(); 103  } 104  105  /** 106  * Actually creates a font. Can be created after the final load request (MAY NOT WORK) 107  * 108  * @param font_generator_id the id of the font generator 109  * @param fontSize the size of the desired font this can't be changed later (would have load another font) 110  * @return id of the font -1 not found 111  */ 112  public static int createFont(int font_generator_id, int fontSize) { 113  tryInit(); 114  String fontName = ids.get(font_generator_id - 1); 115  fontName = fontName.substring(4); 116  FreeTypeFontGenerator generator = fontGenerators.get(fontName); 117  118  FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter(); 119  params.size = fontSize; 120  params.color.r = 0; 121  params.color.g = 0; 122  params.color.b = 0; 123  124  BitmapFont font = generator.generateFont(params); 125  126  ids.add("|FT|" + fontSize + fontName); 127  fonts.put(fontSize + fontName, font); 128  129  return ids.size(); 130  } 131  132  /** 133  * Actually creates a font. Can be created after the final load request (MAY NOT WORK) 134  * 135  * @param fontName the file name of the font 136  * @param fontSize the size of the desired font this can't be changed later (would have load another font) 137  * @return id of the font -1 if not found 138  */ 139  public static int createFont(String fontName, int fontSize) { 140  tryInit(); 141  142  if (!fontGenerators.containsKey(fontName)) { 143  return -1; 144  } 145  FreeTypeFontGenerator generator = fontGenerators.get(fontName); 146  147  FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter(); 148  params.size = fontSize; 149  params.color.r = 1; 150  params.color.g = 1; 151  params.color.b = 1; 152  153  BitmapFont font = generator.generateFont(params); 154  155  ids.add("|FT|" + fontSize + fontName); 156  fonts.put(fontSize + fontName, font); 157  158  return ids.size(); 159  } 160  161  /** 162  * Actually loads the assets 163  */ 164  public static void loadAssets() { 165  tryInit(); 166  loaded = true; 167  manager.finishLoading(); 168  } 169  170  public static Texture getTexture(String fPath) { 171  tryInit(); 172  return manager.get(fPath); 173  } 174  175  public static TextureAtlas getTextureAtlas(String fPath) { 176  tryInit(); 177  TextureAtlas t = manager.get(fPath); 178  for (Texture t0 : t.getTextures()) { 179  t0.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); 180  } 181  return manager.get(fPath); 182  } 183  184  /** 185  * Looks for fPath in ids then determines if it is a tile map and returns the corresponding map 186  * 187  * @param fPath the fPath to the asset 188  * @return The asset if found or null 189  */ 190  public static TiledMap getTileMap(String fPath) { 191  tryInit(); 192  int id = -1; 193  for (String fName : ids) { 194  if (fName.length() < 4) { 195  continue; 196  } 197  String slice = fName.substring(0, 4); 198  if (slice.equals("|TM|") && fName.contains(fPath)) { 199  id = Character.getNumericValue(fName.charAt(4)); 200  break; 201  } 202  } 203  if (id < 0) { 204  return null; 205  } 206  return tileMaps.get(id - 1); 207  } 208  209  public static Texture getTexture(int id) { 210  tryInit(); 211  String fPath = ids.get(id - 1); 212  return manager.get(fPath); 213  } 214  215  public static TextureAtlas getTextureAtlas(int id) { 216  217  tryInit(); 218  String fPath = ids.get(id - 1); 219  return getTextureAtlas(fPath); 220  } 221  222  /** 223  * @param atlas_id the id of the source texture atlas 224  * @param name the name of the texture 225  * @return the found Sprite in the given atlas 226  */ 227  public static Sprite getSprite(int atlas_id, String name) { 228  // Sprite s = getTextureAtlas(atlas_id).createSprite(name); 229  // s.setU(0); 230  // s.setV(0); 231  // s.setU2(0.125f); 232  // s.setV2(0.25f); 233  TextureAtlas t = getTextureAtlas(atlas_id); 234  return getTextureAtlas(atlas_id).createSprite(name); 235  } 236  237  /**NEW 238  * 239  * @param img image name 240  * @return sprite 241  */ 242  public static Sprite getSprite(String img) { 243  Texture texture = new Texture(img); 244  return new Sprite(texture); 245  } 246  247  /** 248  * Gets the tile map returns null if not a tile map 249  * 250  * @param id the id of the tile map 251  * @return the tile map 252  */ 253  public static TiledMap getTileMap(int id) { 254  tryInit(); 255  256  String fPath = ids.get(id - 1); 257  if (fPath.length() < 4) { 258  return null; 259  } 260  String slice = fPath.substring(0, 4); 261  if (!slice.equals("|TM|")) { 262  return null; 263  } 264  int id_ = Character.getNumericValue(fPath.charAt(4)); 265  return tileMaps.get(id_ - 1); 266  } 267  268  /** 269  * only looks for simple assets not specialty ones so largely only textures 270  * 271  * @param name the desired asset name 272  * @return the id of the asset found if found 273  */ 274  public static int getId(String name) { 275  //tryInit(); 276  return ids.indexOf(name) + 1; 277  } 278  279  public static BitmapFont getFont(int font_id) { 280  String fontName = ids.get(font_id - 1); 281  fontName = fontName.substring(4); 282  283  return fonts.get(fontName); 284  } 285  286  public static BitmapFont getFont(String fontName) { 287  return fonts.get(fontName); 288  } 289  290  /** 291  * It is imperative that this is called unless you want memory leeks 292  */ 293  public static void cleanUp() { 294  tryInit(); 295  manager.dispose(); 296  for (TiledMap map : tileMaps) { 297  map.dispose(); 298  } 299  for (BitmapFont font : fonts.values()) { 300  font.dispose(); 301  } 302  for (FreeTypeFontGenerator generator : fontGenerators.values()) { 303  generator.dispose(); 304  } 305  } 306  307  /** 308  * Will check if new assets can be added if not throw an error 309  */ 310  private static void checkAdd() { 311  if (loaded) { 312  throw new RuntimeException("Can't load assets at this stage"); 313  } 314  } 315  316  /** 317  * Calls Initialize if not already done so 318  */ 319  private static void tryInit() { 320  if (!initialized) { 321  Initialize(); 322  } 323  } 324 }