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