Merge pull request #2654 from Card-Forge/refactorTexture

refactor texture loading
This commit is contained in:
Anthony Calosa
2023-03-11 04:17:22 +08:00
committed by GitHub
15 changed files with 65 additions and 50 deletions

View File

@@ -28,6 +28,10 @@
<id>gdx-nightlies</id> <id>gdx-nightlies</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url> <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository> </repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -50,6 +54,11 @@
<artifactId>forge-gui</artifactId> <artifactId>forge-gui</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.tommyettinger</groupId>
<artifactId>textratypist</artifactId>
<version>0.7.9</version>
</dependency>
<dependency> <dependency>
<groupId>com.badlogicgames.gdx-controllers</groupId> <groupId>com.badlogicgames.gdx-controllers</groupId>
<artifactId>gdx-controllers-core</artifactId> <artifactId>gdx-controllers-core</artifactId>

View File

@@ -253,7 +253,7 @@ public class Forge implements ApplicationListener {
public static boolean hasGamepad() { public static boolean hasGamepad() {
//Classic Mode Various Screen GUI are not yet supported, needs control mapping for each screens //Classic Mode Various Screen GUI are not yet supported, needs control mapping for each screens
if (isMobileAdventureMode) { if (isMobileAdventureMode) {
return hasGamepad && Forge.isLandscapeMode(); //portrait is not supported for Gamepad return hasGamepad && isLandscapeMode(); //portrait is not supported for Gamepad
} }
return false; return false;
} }
@@ -348,14 +348,14 @@ public class Forge implements ApplicationListener {
//override transition & title bg //override transition & title bg
try { try {
FileHandle transitionFile = Config.instance().getFile("ui/transition.png"); FileHandle transitionFile = Config.instance().getFile("ui/transition.png");
FileHandle titleBGFile = Forge.isLandscapeMode() ? Config.instance().getFile("ui/title_bg.png") : Config.instance().getFile("ui/title_bg_portrait.png"); FileHandle titleBGFile = isLandscapeMode() ? Config.instance().getFile("ui/title_bg.png") : Config.instance().getFile("ui/title_bg_portrait.png");
FileHandle vsIcon = Config.instance().getFile("ui/vs.png"); FileHandle vsIcon = Config.instance().getFile("ui/vs.png");
if (vsIcon.exists()) if (vsIcon.exists())
Forge.getAssets().fallback_skins().put(2, new Texture(vsIcon)); getAssets().fallback_skins().put("vs", new Texture(vsIcon));
if (transitionFile.exists()) if (transitionFile.exists())
Forge.getAssets().fallback_skins().put(1, new Texture(transitionFile)); getAssets().fallback_skins().put("transition", new Texture(transitionFile));
if (titleBGFile.exists()) if (titleBGFile.exists())
Forge.getAssets().fallback_skins().put(0, new Texture(titleBGFile)); getAssets().fallback_skins().put("title", new Texture(titleBGFile));
AdventureScreen.preload(); AdventureScreen.preload();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -383,7 +383,7 @@ public class Forge implements ApplicationListener {
if (selector.equals("Adventure")) { if (selector.equals("Adventure")) {
//preload adventure resources to speedup startup if selector is adventure. Needs in edt when setting up worldstage //preload adventure resources to speedup startup if selector is adventure. Needs in edt when setting up worldstage
loadAdventureResources(false); loadAdventureResources(false);
Forge.isMobileAdventureMode = true; isMobileAdventureMode = true;
} }
//selection transition //selection transition
setTransitionScreen(new TransitionScreen(() -> { setTransitionScreen(new TransitionScreen(() -> {
@@ -408,7 +408,7 @@ public class Forge implements ApplicationListener {
SoundSystem.instance.setBackgroundMusic(MusicPlaylist.MENUS); SoundSystem.instance.setBackgroundMusic(MusicPlaylist.MENUS);
safeToClose = true; safeToClose = true;
clearTransitionScreen(); clearTransitionScreen();
}, Forge.takeScreenshot(), false, false, true, false)); }, takeScreenshot(), false, false, true, false));
}); });
}); });
})); }));
@@ -829,7 +829,7 @@ public class Forge implements ApplicationListener {
@Override @Override
public void render() { public void render() {
if (showFPS) if (showFPS)
frameRate.update(ImageCache.counter, Forge.getAssets().manager().getMemoryInMegabytes()); frameRate.update(ImageCache.counter, getAssets().manager().getMemoryInMegabytes());
try { try {
ImageCache.allowSingleLoad(); ImageCache.allowSingleLoad();
@@ -865,8 +865,8 @@ public class Forge implements ApplicationListener {
animationBatch.setColor(1, 1, 1, 1); animationBatch.setColor(1, 1, 1, 1);
animationBatch.draw(lastScreenTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(lastScreenTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.setColor(1, 1, 1, 1 - (1 / transitionTime) * animationTimeout); animationBatch.setColor(1, 1, 1, 1 - (1 / transitionTime) * animationTimeout);
animationBatch.draw(getAssets().fallback_skins().get(1), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(getAssets().fallback_skins().get("transition"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.draw(getAssets().fallback_skins().get(1), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(getAssets().fallback_skins().get("transition"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.end(); animationBatch.end();
if (animationTimeout < 0) { if (animationTimeout < 0) {
currentScene.render(); currentScene.render();
@@ -885,8 +885,8 @@ public class Forge implements ApplicationListener {
animationBatch.setColor(1, 1, 1, 1); animationBatch.setColor(1, 1, 1, 1);
animationBatch.draw(lastScreenTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(lastScreenTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.setColor(1, 1, 1, (1 / transitionTime) * (animationTimeout + transitionTime)); animationBatch.setColor(1, 1, 1, (1 / transitionTime) * (animationTimeout + transitionTime));
animationBatch.draw(getAssets().fallback_skins().get(1), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(getAssets().fallback_skins().get("transition"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.draw(getAssets().fallback_skins().get(1), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); animationBatch.draw(getAssets().fallback_skins().get("transition"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
animationBatch.end(); animationBatch.end();
return; return;
} }

View File

@@ -105,7 +105,7 @@ public class GuiMobile implements IGuiBase {
@Override @Override
public ISkinImage getUnskinnedIcon(final String path) { public ISkinImage getUnskinnedIcon(final String path) {
if (isGuiThread()) { if (isGuiThread()) {
return new FTextureImage(new Texture(Gdx.files.absolute(path))); return new FTextureImage(Forge.getAssets().getTexture(Gdx.files.absolute(path)));
} }
//use a delay load image to avoid an error if called from background thread //use a delay load image to avoid an error if called from background thread
@@ -131,7 +131,7 @@ public class GuiMobile implements IGuiBase {
if (FileUtil.doesFileExist(overlayFilename)) { if (FileUtil.doesFileExist(overlayFilename)) {
try { try {
final Texture overlay = new Texture(Gdx.files.absolute(overlayFilename)); final Texture overlay = Forge.getAssets().getTexture(Gdx.files.absolute(overlayFilename));
g.drawImage(overlay, (background.getWidth() - overlay.getWidth()) / 2, (background.getHeight() - overlay.getHeight()) / 2, overlay.getWidth(), overlay.getHeight()); g.drawImage(overlay, (background.getWidth() - overlay.getWidth()) / 2, (background.getHeight() - overlay.getHeight()) / 2, overlay.getWidth(), overlay.getHeight());
} catch (final Exception e) { } catch (final Exception e) {
} }

View File

@@ -37,7 +37,7 @@ public class InventoryScene extends UIScene {
public InventoryScene() { public InventoryScene() {
super(Forge.isLandscapeMode() ? "ui/inventory.json" : "ui/inventory_portrait.json"); super(Forge.isLandscapeMode() ? "ui/inventory.json" : "ui/inventory_portrait.json");
equipOverlay = new Texture(Config.instance().getFile(Paths.ITEMS_EQUIP)); equipOverlay = Forge.getAssets().getTexture(Config.instance().getFile(Paths.ITEMS_EQUIP));
ui.onButtonPress("return", () -> done()); ui.onButtonPress("return", () -> done());
leave = ui.findActor("return"); leave = ui.findActor("return");
ui.onButtonPress("delete", () -> showConfirm()); ui.onButtonPress("delete", () -> showConfirm());

View File

@@ -305,7 +305,6 @@ public class RewardScene extends UIScene {
if(background!=null) if(background!=null)
background.setVisible(false); background.setVisible(false);
} }
// card.setDrawable(new TextureRegionDrawable(new Texture(Res.CurrentRes.GetFile("ui/transition.png"))));
float targetWidth = card.getWidth(); float targetWidth = card.getWidth();
float targetHeight = card.getHeight(); float targetHeight = card.getHeight();

View File

@@ -95,7 +95,7 @@ public class GameHUD extends Stage {
exitToWorldMapActor = ui.findActor("exittoworldmap"); exitToWorldMapActor = ui.findActor("exittoworldmap");
dialog = Controls.newDialog(""); dialog = Controls.newDialog("");
miniMapPlayer = new Image(new Texture(Config.instance().getFile("ui/minimap_player.png"))); miniMapPlayer = new Image(Forge.getAssets().getTexture(Config.instance().getFile("ui/minimap_player.png")));
//create touchpad //create touchpad
touchpad = new Touchpad(10, Controls.getSkin()); touchpad = new Touchpad(10, Controls.getSkin());
touchpad.setBounds(15, 15, TOUCHPAD_SCALE, TOUCHPAD_SCALE); touchpad.setBounds(15, 15, TOUCHPAD_SCALE, TOUCHPAD_SCALE);

View File

@@ -287,7 +287,7 @@ public class UIActor extends Group {
for (ObjectMap.Entry property : entries) { for (ObjectMap.Entry property : entries) {
switch (property.key.toString()) { switch (property.key.toString()) {
case "image": case "image":
Texture t = new Texture(Config.instance().getFile(property.value.toString())); Texture t = Forge.getAssets().getTexture(Config.instance().getFile(property.value.toString()));
TextureRegion tr = new TextureRegion(t); TextureRegion tr = new TextureRegion(t);
newActor.setDrawable(new TextureRegionDrawable(tr)); newActor.setDrawable(new TextureRegionDrawable(tr));
break; break;

View File

@@ -12,6 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.Json;
import forge.Forge;
import forge.adventure.data.*; import forge.adventure.data.*;
import forge.adventure.pointofintrest.PointOfInterest; import forge.adventure.pointofintrest.PointOfInterest;
import forge.adventure.pointofintrest.PointOfInterestMap; import forge.adventure.pointofintrest.PointOfInterestMap;
@@ -912,7 +913,7 @@ public class World implements Disposable, SaveFileContent {
public Texture getGlobalTexture() { public Texture getGlobalTexture() {
if (globalTexture == null) { if (globalTexture == null) {
globalTexture = new Texture(Config.instance().getFile("ui/sprite_markers.png")); globalTexture = Forge.getAssets().getTexture(Config.instance().getFile("ui/sprite_markers.png"));
System.out.print("Loading auxiliary sprites.\n"); System.out.print("Loading auxiliary sprites.\n");
} }
return globalTexture; return globalTexture;

View File

@@ -40,7 +40,7 @@ public class Assets implements Disposable {
private HashMap<Integer, TextureRegion> cursor; private HashMap<Integer, TextureRegion> cursor;
private ObjectMap<Integer, BitmapFont> counterFonts; private ObjectMap<Integer, BitmapFont> counterFonts;
private ObjectMap<String, Texture> generatedCards; private ObjectMap<String, Texture> generatedCards;
private ObjectMap<Integer, Texture> fallback_skins; private ObjectMap<String, Texture> fallback_skins;
private ObjectMap<String, Texture> tmxMap; private ObjectMap<String, Texture> tmxMap;
private Texture defaultImage, dummy; private Texture defaultImage, dummy;
private TextureParameter textureParameter; private TextureParameter textureParameter;
@@ -54,16 +54,16 @@ public class Assets implements Disposable {
Texture titleBG_LQ = GuiBase.isAndroid() ? Texture titleBG_LQ = GuiBase.isAndroid() ?
new Texture(Gdx.files.internal("fallback_skin").child(titleFilename)) : new Texture(Gdx.files.internal("fallback_skin").child(titleFilename)) :
new Texture(Gdx.files.classpath("fallback_skin").child(titleFilename)); new Texture(Gdx.files.classpath("fallback_skin").child(titleFilename));
fallback_skins().put(0, titleBG_LQ == null ? getDummy() : titleBG_LQ); fallback_skins().put("title", titleBG_LQ == null ? getDummy() : titleBG_LQ);
//init transition //init transition
Texture transitionLQ = GuiBase.isAndroid() ? Texture transitionLQ = GuiBase.isAndroid() ?
new Texture(Gdx.files.internal("fallback_skin").child("transition.png")) : new Texture(Gdx.files.internal("fallback_skin").child("transition.png")) :
new Texture(Gdx.files.classpath("fallback_skin").child("transition.png")); new Texture(Gdx.files.classpath("fallback_skin").child("transition.png"));
fallback_skins().put(1, transitionLQ == null ? getDummy() : transitionLQ); fallback_skins().put("transition", transitionLQ == null ? getDummy() : transitionLQ);
} catch (Exception e) { } catch (Exception e) {
fallback_skins().clear(); fallback_skins().clear();
fallback_skins().put(0, getDummy()); fallback_skins().put("title", getDummy());
fallback_skins().put(1, getDummy()); fallback_skins().put("transition", getDummy());
} }
} }
@@ -202,7 +202,7 @@ public class Assets implements Disposable {
return generatedCards; return generatedCards;
} }
public ObjectMap<Integer, Texture> fallback_skins() { public ObjectMap<String, Texture> fallback_skins() {
if (fallback_skins == null) if (fallback_skins == null)
fallback_skins = new ObjectMap<>(); fallback_skins = new ObjectMap<>();
return fallback_skins; return fallback_skins;
@@ -229,6 +229,28 @@ public class Assets implements Disposable {
return textureParameter; return textureParameter;
} }
public Texture getTexture(FileHandle file) {
if (file == null || !file.exists()) {
System.err.println("Failed to load: " + file +"!. Creating dummy texture.");
return getDummy();
}
//internal path can be inside apk or jar..
if (file.path().contains("fallback_skin")) {
Texture f = fallback_skins().get(file.path());
if (f == null) {
fallback_skins().put(file.path(), new Texture(file));
}
return f;
}
Texture t = manager.get(file.path(), Texture.class, false);
if (t == null) {
manager.load(file.path(), Texture.class, getTextureFilter());
manager.finishLoadingAsset(file.path());
t = manager.get(file.path(), Texture.class);
}
return t;
}
public Texture getDefaultImage() { public Texture getDefaultImage() {
if (defaultImage == null) { if (defaultImage == null) {
FileHandle blankImage = Gdx.files.absolute(ForgeConstants.NO_CARD_FILE); FileHandle blankImage = Gdx.files.absolute(ForgeConstants.NO_CARD_FILE);

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import forge.Forge;
import forge.Graphics; import forge.Graphics;
//Special wrapper for a texture to be loaded later when it's needed //Special wrapper for a texture to be loaded later when it's needed
@@ -28,7 +29,7 @@ public class FDelayLoadImage extends FImageComplex {
@Override @Override
public Texture getTexture() { public Texture getTexture() {
if (texture == null) { if (texture == null) {
texture = new Texture(Gdx.files.absolute(filename)); texture = Forge.getAssets().getTexture(Gdx.files.absolute(filename));
} }
return texture; return texture;
} }
@@ -36,7 +37,7 @@ public class FDelayLoadImage extends FImageComplex {
@Override @Override
public TextureRegion getTextureRegion() { public TextureRegion getTextureRegion() {
if (texture == null) { if (texture == null) {
texture = new Texture(Gdx.files.absolute(filename)); texture = Forge.getAssets().getTexture(Gdx.files.absolute(filename));
} }
return new TextureRegion(texture); return new TextureRegion(texture);
} }

View File

@@ -202,13 +202,7 @@ public enum FSkinTexture implements FImage {
FileHandle preferredFile = isPlanechaseBG ? FSkin.getCachePlanechaseFile(filename) : FSkin.getSkinFile(filename); FileHandle preferredFile = isPlanechaseBG ? FSkin.getCachePlanechaseFile(filename) : FSkin.getSkinFile(filename);
if (preferredFile.exists()) { if (preferredFile.exists()) {
try { try {
if (preferredFile.path().contains("fallback_skin")) { texture = Forge.getAssets().getTexture(preferredFile);
texture = new Texture(preferredFile);
} else {
Forge.getAssets().manager().load(preferredFile.path(), Texture.class);
Forge.getAssets().manager().finishLoadingAsset(preferredFile.path());
texture = Forge.getAssets().manager().get(preferredFile.path(), Texture.class);
}
isloaded = true; isloaded = true;
} }
catch (final Exception e) { catch (final Exception e) {
@@ -229,13 +223,7 @@ public enum FSkinTexture implements FImage {
if (defaultFile.exists()) { if (defaultFile.exists()) {
try { try {
if (defaultFile.path().contains("fallback_skin")) { texture = Forge.getAssets().getTexture(defaultFile);
texture = new Texture(defaultFile);
} else {
Forge.getAssets().manager().load(defaultFile.path(), Texture.class);
Forge.getAssets().manager().finishLoadingAsset(defaultFile.path());
texture = Forge.getAssets().manager().get(defaultFile.path(), Texture.class);
}
isloaded = true; isloaded = true;
} }
catch (final Exception e) { catch (final Exception e) {

View File

@@ -21,7 +21,7 @@ public class ClosingScreen extends FContainer {
private boolean drawStatic = false; private boolean drawStatic = false;
private FileHandle adv_logo = getSkinFile("adv_logo.png"); private FileHandle adv_logo = getSkinFile("adv_logo.png");
private FileHandle existingLogo = adv_logo.exists() ? adv_logo : getDefaultSkinFile("adv_logo.png"); private FileHandle existingLogo = adv_logo.exists() ? adv_logo : getDefaultSkinFile("adv_logo.png");
private Texture logo = existingLogo.exists() && Forge.advStartup ? new Texture(existingLogo) : FSkin.getLogo(); private Texture logo = existingLogo.exists() && Forge.advStartup ? Forge.getAssets().getTexture(existingLogo) : FSkin.getLogo();
public ClosingScreen(boolean restart0) { public ClosingScreen(boolean restart0) {
bgAnimation = new BGAnimation(); bgAnimation = new BGAnimation();

View File

@@ -239,7 +239,7 @@ public class SplashScreen extends FContainer {
} }
void drawTransition(Graphics g, boolean openAdventure, float percentage) { void drawTransition(Graphics g, boolean openAdventure, float percentage) {
TextureRegion tr = new TextureRegion(Forge.getAssets().fallback_skins().get(0)); TextureRegion tr = new TextureRegion(Forge.getAssets().fallback_skins().get("title"));
float oldAlpha = g.getfloatAlphaComposite(); float oldAlpha = g.getfloatAlphaComposite();
g.setAlphaComposite(percentage); g.setAlphaComposite(percentage);
if (openAdventure) { if (openAdventure) {

View File

@@ -67,7 +67,7 @@ public class TransitionScreen extends FContainer {
playerAvatarName = playerName; playerAvatarName = playerName;
enemyAvatarName = enemyName; enemyAvatarName = enemyName;
enemyAtlasPath = enemyAtlas; enemyAtlasPath = enemyAtlas;
vsTexture = Forge.getAssets().fallback_skins().get(2); vsTexture = Forge.getAssets().fallback_skins().get("vs");
layout = new GlyphLayout(); layout = new GlyphLayout();
} }
@@ -207,7 +207,7 @@ public class TransitionScreen extends FContainer {
} else if (isIntro) { } else if (isIntro) {
if (textureRegion != null) { if (textureRegion != null) {
if (Forge.advStartup) { if (Forge.advStartup) {
g.drawGrayTransitionImage(Forge.getAssets().fallback_skins().get(0), 0, 0, Forge.getScreenWidth(), Forge.getScreenHeight(), false, percentage); g.drawGrayTransitionImage(Forge.getAssets().fallback_skins().get("title"), 0, 0, Forge.getScreenWidth(), Forge.getScreenHeight(), false, percentage);
g.setAlphaComposite(1-percentage); g.setAlphaComposite(1-percentage);
g.drawImage(textureRegion, 0, 0, Forge.getScreenWidth(), Forge.getScreenHeight()); g.drawImage(textureRegion, 0, 0, Forge.getScreenWidth(), Forge.getScreenHeight());
g.setAlphaComposite(oldAlpha); g.setAlphaComposite(oldAlpha);

View File

@@ -40,11 +40,6 @@
<artifactId>forge-ai</artifactId> <artifactId>forge-ai</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.tommyettinger</groupId>
<artifactId>textratypist</artifactId>
<version>0.7.9</version>
</dependency>
<dependency> <dependency>
<groupId>com.thoughtworks.xstream</groupId> <groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId> <artifactId>xstream</artifactId>