add LoadingScreen and TextureAtlas asset support
This commit is contained in:
@@ -15,6 +15,7 @@ import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
import io.github.com.quillraven.asset.AssetService;
|
||||
import io.github.com.quillraven.screen.GameScreen;
|
||||
import io.github.com.quillraven.screen.LoadingScreen;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -43,8 +44,8 @@ public class GdxGame extends Game {
|
||||
glProfiler = new GLProfiler(Gdx.graphics);
|
||||
glProfiler.enable();
|
||||
|
||||
addScreen(new GameScreen(this));
|
||||
setScreen(GameScreen.class);
|
||||
addScreen(new LoadingScreen(this));
|
||||
setScreen(LoadingScreen.class);
|
||||
}
|
||||
|
||||
public void addScreen(Screen screen) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.com.quillraven.asset;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
@@ -16,9 +17,10 @@ public class AssetService implements Disposable {
|
||||
}
|
||||
|
||||
public TiledMap load(MapAsset mapAsset) {
|
||||
this.assetManager.load(mapAsset.getPath(), TiledMap.class);
|
||||
String path = mapAsset.getPath();
|
||||
this.assetManager.load(path, TiledMap.class);
|
||||
this.assetManager.finishLoading();
|
||||
return this.assetManager.get(mapAsset.getPath(), TiledMap.class);
|
||||
return this.assetManager.get(path, TiledMap.class);
|
||||
}
|
||||
|
||||
public void unload(MapAsset mapAsset) {
|
||||
@@ -26,6 +28,14 @@ public class AssetService implements Disposable {
|
||||
this.assetManager.finishLoading();
|
||||
}
|
||||
|
||||
public void queue(AtlasAsset atlasAsset) {
|
||||
this.assetManager.load(atlasAsset.getPath(), TextureAtlas.class);
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
return this.assetManager.update();
|
||||
}
|
||||
|
||||
public void debugDiagnostics() {
|
||||
Gdx.app.debug("AssetService", this.assetManager.getDiagnostics());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum AtlasAsset {
|
||||
OBJECTS("objects.atlas");
|
||||
|
||||
private final String path;
|
||||
|
||||
AtlasAsset(String atlasName) {
|
||||
this.path = "graphics/" + atlasName;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.github.com.quillraven.screen;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.ScreenAdapter;
|
||||
import io.github.com.quillraven.GdxGame;
|
||||
import io.github.com.quillraven.asset.AssetService;
|
||||
import io.github.com.quillraven.asset.AtlasAsset;
|
||||
|
||||
public class LoadingScreen extends ScreenAdapter {
|
||||
|
||||
private final GdxGame game;
|
||||
private final AssetService assetService;
|
||||
|
||||
public LoadingScreen(GdxGame game) {
|
||||
this.game = game;
|
||||
this.assetService = game.getAssetService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
for (AtlasAsset atlasAsset : AtlasAsset.values()) {
|
||||
assetService.queue(atlasAsset);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
if (assetService.update()) {
|
||||
Gdx.app.debug("LoadingScreen", "Finished loading assets");
|
||||
createScreens();
|
||||
this.game.removeScreen(this);
|
||||
this.dispose();
|
||||
this.game.setScreen(GameScreen.class);
|
||||
}
|
||||
}
|
||||
|
||||
private void createScreens() {
|
||||
this.game.addScreen(new GameScreen(this.game));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user