Skip to content

Commit

Permalink
Use AssetInstance to obtain loader or downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Nov 14, 2024
1 parent 8d9dbb9 commit 5e3b8fc
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[-SNAPSHOT]
- Add Config Asset preloadListener
- Add AssetInstance to obtain AssetLoader or AssetDownloader

[1.0.4]
- Fix music id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.badlogic.gdx.utils.async.AsyncResult;
import com.badlogic.gdx.utils.async.AsyncTask;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetInstance;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetType;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoader;
Expand Down Expand Up @@ -86,7 +87,7 @@ public Void call() throws Exception {
*/
public boolean update() {
// GTW: check if we have a file that was not preloaded and is not done loading yet
AssetLoader assetLoader = AssetLoader.getInstance();
AssetLoader assetLoader = AssetInstance.getLoaderInstance();
FileHandle fileHandle = resolve(loader, assetDesc);
String path = fileHandle.path();
Files.FileType type = fileHandle.type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.badlogic.gdx.utils.BufferUtils;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetInstance;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoaderListener;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void onSuccess(String url, Blob result) {
responseListener.downloadComplete(pixmapEmu);
}
};
AssetDownloader.getInstance().load(true, url, AssetType.Binary, listener);
AssetInstance.getDownloaderInstance().load(true, url, AssetType.Binary, listener);
}

public PixmapEmu(FileHandle file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.badlogic.gdx.utils;

import com.badlogic.gdx.Gdx;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetInstance;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoaderListener;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;
Expand All @@ -11,7 +12,7 @@ public class SharedLibraryLoaderEmu {

public void load (String libraryName) {
TeaApplication app = (TeaApplication)Gdx.app;
AssetLoader assetLoader = AssetLoader.getInstance();
AssetLoader assetLoader = AssetInstance.getLoaderInstance();
assetLoader.loadScript(false, libraryName + ".js", new AssetLoaderListener<>() {
@Override
public void onSuccess(String url, String result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ else if(agentInfo.isLinux())
else
System.setProperty("os.name", "no OS");

AssetInstance.setInstance(new AssetDownloadImpl(config.showDownloadLogs));
AssetDownloadImpl assetDownload = new AssetDownloadImpl(config.showDownloadLogs);
AssetInstance.setInstance(assetDownload);

TeaWindow currentWindow = TeaWindow.get();
LocationWrapper location = currentWindow.getLocation();
Expand All @@ -119,7 +120,7 @@ else if(agentInfo.isLinux())

graphics = new TeaGraphics(config);

assetLoader = new AssetLoadImpl(hostPageBaseURL, graphics.canvas, this);
assetLoader = new AssetLoadImpl(hostPageBaseURL, graphics.canvas, this, assetDownload);
AssetInstance.setInstance(assetLoader);

input = new TeaInput(this, graphics.canvas);
Expand Down Expand Up @@ -222,7 +223,7 @@ public void run() {
initState = AppState.DOWNLOAD_ASSETS;
break;
case DOWNLOAD_ASSETS:
int queue = AssetDownloader.getInstance().getQueue();
int queue = assetLoader.getQueue();
if(queue == 0) {
initState = AppState.APP_LOOP;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public class AssetLoadImpl implements AssetLoader {

private HashSet<String> assetInQueue;

public AssetLoadImpl(String newBaseURL, HTMLCanvasElementWrapper canvas, TeaApplication teaApplication) {
private AssetDownloader assetDownloader;

public AssetLoadImpl(String newBaseURL, HTMLCanvasElementWrapper canvas, TeaApplication teaApplication, AssetDownloader assetDownloader) {
this.assetDownloader = assetDownloader;
baseUrl = newBaseURL;
assetInQueue = new HashSet<>();
setupFileDrop(canvas, teaApplication);
Expand Down Expand Up @@ -189,7 +192,7 @@ public void onFailure(String url) {
}
};

AssetDownloader.getInstance().load(true, getAssetUrl() + assetFileUrl, AssetType.Binary, listener);
assetDownloader.load(true, getAssetUrl() + assetFileUrl, AssetType.Binary, listener);
}

@Override
Expand Down Expand Up @@ -236,7 +239,7 @@ public void loadAsset(boolean async, String path, AssetType assetType, FileType
}

assetInQueue.add(path1);
AssetDownloader.getInstance().load(async, getAssetUrl() + path1, AssetType.Binary, new AssetLoaderListener<Blob>() {
assetDownloader.load(async, getAssetUrl() + path1, AssetType.Binary, new AssetLoaderListener<Blob>() {
@Override
public void onProgress(int total, int loaded) {
if(listener != null) {
Expand Down Expand Up @@ -277,12 +280,12 @@ public void onSuccess(String url, Blob result) {

@Override
public void loadScript(boolean async, String url, AssetLoaderListener<String> listener) {
AssetDownloader.getInstance().loadScript(async, getScriptUrl() + url, listener);
assetDownloader.loadScript(async, getScriptUrl() + url, listener);
}

@Override
public int getQueue() {
return AssetDownloader.getInstance().getQueue();
return assetDownloader.getQueue();
}

private String fixPath(String path1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.badlogic.gdx.utils.viewport.Viewport;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetInstance;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoader;
import com.github.xpenatan.gdx.examples.tests.LoadingTest;
import com.github.xpenatan.gdx.examples.utils.LoadingBar;
Expand Down Expand Up @@ -61,7 +62,7 @@ enum STEPS {
public void create() {
teaApplication = TeaApplication.get();

assetLoader = AssetLoader.getInstance();
assetLoader = AssetInstance.getLoaderInstance();

stage = new Stage();
stage.setViewport(new ScreenViewport());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.xpenatan.gdx.backends.teavm.config.TeaBuildConfiguration;
import com.github.xpenatan.gdx.backends.teavm.config.TeaBuilder;
import com.github.xpenatan.gdx.backends.teavm.gen.SkipClass;
import com.github.xpenatan.gdx.examples.teavm.launcher.FreetypeTestLauncher;
import java.io.File;
import java.io.IOException;
import org.teavm.tooling.TeaVMTool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.gdx.examples.teavm.launcher;
package com.github.xpenatan.gdx.examples.teavm;

import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration;
Expand All @@ -11,6 +11,11 @@ public static void main(String[] args) {
config.width = 0;
config.height = 0;
config.showDownloadLogs = true;

config.preloadListener = assetLoader -> {
assetLoader.loadScript(true, "freetype.js", null);
};

new TeaApplication(new FreetypeDemo(), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
* @author xpenatan
*/
public interface AssetDownloader {

static AssetDownloader getInstance() {
return AssetInstance.downloaderInstance;
}

void load(boolean async, final String url, AssetType type, AssetLoaderListener<Blob> listener);
void loadScript(boolean async, final String url, final AssetLoaderListener<String> listener);
int getQueue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public static void setInstance(AssetLoader instance) {
public static void setInstance(AssetDownloader instance) {
AssetInstance.downloaderInstance = instance;
}

public static AssetLoader getLoaderInstance() {
return AssetInstance.instance;
}

public static AssetDownloader getDownloaderInstance() {
return AssetInstance.downloaderInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
*/
public interface AssetLoader {

static AssetLoader getInstance() {
return AssetInstance.instance;
}

String getAssetUrl();

String getScriptUrl();
Expand Down

0 comments on commit 5e3b8fc

Please sign in to comment.