mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Support delay loading images
This commit is contained in:
@@ -13,6 +13,7 @@ import com.badlogic.gdx.utils.Clipboard;
|
|||||||
import forge.assets.AssetsDownloader;
|
import forge.assets.AssetsDownloader;
|
||||||
import forge.assets.FSkin;
|
import forge.assets.FSkin;
|
||||||
import forge.assets.FSkinFont;
|
import forge.assets.FSkinFont;
|
||||||
|
import forge.assets.ImageCache;
|
||||||
import forge.error.BugReporter;
|
import forge.error.BugReporter;
|
||||||
import forge.error.ExceptionHandler;
|
import forge.error.ExceptionHandler;
|
||||||
import forge.model.FModel;
|
import forge.model.FModel;
|
||||||
@@ -205,6 +206,7 @@ public class Forge implements ApplicationListener {
|
|||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
try {
|
try {
|
||||||
|
ImageCache.allowSingleLoad();
|
||||||
Animation.advanceAll();
|
Animation.advanceAll();
|
||||||
|
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen.
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen.
|
||||||
|
|||||||
@@ -58,6 +58,12 @@ public class ImageCache {
|
|||||||
private static final LoadingCache<String, Texture> cache = CacheBuilder.newBuilder().softValues().build(new ImageLoader());
|
private static final LoadingCache<String, Texture> cache = CacheBuilder.newBuilder().softValues().build(new ImageLoader());
|
||||||
public static final Texture defaultImage;
|
public static final Texture defaultImage;
|
||||||
|
|
||||||
|
private static boolean imageLoaded, delayLoadRequested;
|
||||||
|
public static void allowSingleLoad() {
|
||||||
|
imageLoaded = false; //reset at the beginning of each render
|
||||||
|
delayLoadRequested = false;
|
||||||
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Texture defImage = null;
|
Texture defImage = null;
|
||||||
try {
|
try {
|
||||||
@@ -132,9 +138,21 @@ public class ImageCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load from file and add to cache if not found in cache initially.
|
// Load from file and add to cache if not found in cache initially.
|
||||||
Texture image;
|
Texture image = cache.getIfPresent(imageKey);
|
||||||
|
if (image != null) { return image; }
|
||||||
|
|
||||||
|
if (imageLoaded) { //prevent loading more than one image each render for performance
|
||||||
|
if (!delayLoadRequested) {
|
||||||
|
//ensure images continue to load even if no input is being received
|
||||||
|
delayLoadRequested = true;
|
||||||
|
Gdx.graphics.requestRendering();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
imageLoaded = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
image = ImageCache.cache.get(imageKey);
|
image = cache.get(imageKey);
|
||||||
}
|
}
|
||||||
catch (final ExecutionException ex) {
|
catch (final ExecutionException ex) {
|
||||||
if (!(ex.getCause() instanceof NullPointerException)) {
|
if (!(ex.getCause() instanceof NullPointerException)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user