Skip to content

Commit

Permalink
Merge pull request #142 from xpenatan/master
Browse files Browse the repository at this point in the history
Release v1.0.5
  • Loading branch information
xpenatan authored Dec 22, 2024
2 parents a8c6bad + 1a71564 commit c3edd5f
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 202 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
[-SNAPSHOT]

[1.0.5]
- Add Config Asset preloadListener
- Update AssetLoader and AssetDownloader
- Add AssetInstance to obtain AssetLoader or AssetDownloader
- Fix drawing to Gdx2DPixmapEmu
- Update Freetype emulation to fix build errors and script loading solution

[1.0.4]
- Fix music id
- add custom gltf resource path
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.AssetLoad assetLoader = AssetLoader.getInstance();
AssetLoader assetLoader = AssetInstance.getLoaderInstance();
FileHandle fileHandle = resolve(loader, assetDesc);
String path = fileHandle.path();
Files.FileType type = fileHandle.type();
Expand All @@ -97,7 +98,7 @@ public boolean update() {
cancel = true;
}
else {
assetLoader.loadAsset(true, path, AssetType.Binary, type, null);
assetLoader.loadAsset(path, AssetType.Binary, type, null);
}
}
}
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
Expand Up @@ -298,8 +298,7 @@ static private String getFormatString(int format) {
"var startIndex = pixels;" +
"var endIndex = startIndex + bytesSize;" +
"var heapArray = Gdx.HEAPU8.subarray(startIndex, endIndex);" +
"var newArray = new Uint8Array(heapArray);" +
"return newArray;"
"return heapArray;"
)
private static native Uint8ArrayWrapper load(@JSByRef() int[] nativeData, @JSByRef() byte[] buffer, int offset, int len); /*MANUAL
const unsigned char* p_buffer = (const unsigned char*)env->GetPrimitiveArrayCritical(buffer, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badlogic.gdx.utils;

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

public void load (String libraryName) {
TeaApplication app = (TeaApplication)Gdx.app;
AssetLoader.AssetLoad assetLoader = AssetLoader.getInstance();
assetLoader.loadScript(false, libraryName + ".js", new AssetLoaderListener<>() {
@Override
public void onSuccess(String url, String result) {
}

@Override
public void onFailure(String url) {
}
});
AssetLoader assetLoader = AssetInstance.getLoaderInstance();
assetLoader.loadScript(libraryName + ".js", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import com.badlogic.gdx.utils.ObjectMap;
import com.github.xpenatan.gdx.backends.teavm.agent.TeaAgentInfo;
import com.github.xpenatan.gdx.backends.teavm.agent.TeaWebAgent;
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.dom.EventListenerWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.EventWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.LocationWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.impl.TeaWindow;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetDownloadImpl;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetDownloader;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetDownloader.AssetDownload;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoader;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoadImpl;
import com.github.xpenatan.gdx.backends.teavm.utils.TeaNavigator;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -100,10 +99,12 @@ else if(agentInfo.isLinux())
else
System.setProperty("os.name", "no OS");

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

AssetDownload instance = AssetDownloader.getInstance();
hostPageBaseURL = instance.getHostPageBaseURL();
TeaWindow currentWindow = TeaWindow.get();
LocationWrapper location = currentWindow.getLocation();
hostPageBaseURL = location.getHref();

if(hostPageBaseURL.contains(".html")) {
// TODO use regex
Expand All @@ -117,8 +118,8 @@ else if(agentInfo.isLinux())

graphics = new TeaGraphics(config);

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

input = new TeaInput(this, graphics.canvas);
files = new TeaFiles(config, this);
Expand All @@ -129,6 +130,10 @@ else if(agentInfo.isLinux())
initGdx();
initSound();

if(config.preloadListener != null) {
config.preloadListener.onPreload(assetLoader);
}

Gdx.app = this;
Gdx.graphics = graphics;
Gdx.gl = graphics.getGL20();
Expand Down Expand Up @@ -216,7 +221,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 Expand Up @@ -476,10 +481,10 @@ public enum AppState {
// ##################### NATIVE CALLS #####################

private void initGdx() {
assetLoader.loadScript(true, "gdx.wasm.js", new AssetLoaderListener<>() {});
assetLoader.loadScript("gdx.wasm.js", new AssetLoaderListener<>() {});
}

private void initSound() {
assetLoader.loadScript(true, "howler.js", new AssetLoaderListener<>() {});
assetLoader.loadScript("howler.js", new AssetLoaderListener<>() {});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.xpenatan.gdx.backends.teavm;


import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoader;

/**
* @author xpenatan
*/
Expand Down Expand Up @@ -94,6 +96,11 @@ public class TeaApplicationConfiguration {
*/
public String powerPreference = "high-performance";

/**
* Used for preloading asset and libraries
*/
public TeaAssetPreloadListener preloadListener;

public boolean isFixedSizeApplication() {
return width != 0 && height != 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.xpenatan.gdx.backends.teavm;

import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetLoader;

public interface TeaAssetPreloadListener {
void onPreload(AssetLoader assetLoader);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.xpenatan.gdx.backends.teavm.assetloader;

import com.badlogic.gdx.utils.GdxRuntimeException;
import com.github.xpenatan.gdx.backends.teavm.assetloader.AssetDownloader.AssetDownload;
import com.github.xpenatan.gdx.backends.teavm.dom.LocationWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.impl.TeaWindow;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
Expand All @@ -16,7 +13,7 @@
import org.teavm.jso.typedarrays.ArrayBuffer;
import org.teavm.jso.typedarrays.Int8Array;

public class AssetDownloadImpl implements AssetDownload {
public class AssetDownloadImpl implements AssetDownloader {

private static final int MAX_DOWNLOAD_ATTEMPT = 3;

Expand All @@ -28,14 +25,6 @@ public AssetDownloadImpl(boolean showDownloadLogs) {
showLogs = showDownloadLogs;
}

@Override
public String getHostPageBaseURL() {
TeaWindow currentWindow = TeaWindow.get();
LocationWrapper location = currentWindow.getLocation();
String href = location.getHref();
return href;
}

@Override
public int getQueue() {
return queue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* @author xpenatan
*/
public class AssetLoadImpl implements AssetLoader.AssetLoad {
public class AssetLoadImpl implements AssetLoader {
public int assetTotal = -1;

private static final String ASSET_FOLDER = "assets/";
Expand All @@ -42,7 +42,10 @@ public class AssetLoadImpl implements AssetLoader.AssetLoad {

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 All @@ -206,12 +209,36 @@ public boolean isAssetLoaded(FileType fileType, String path) {
}

@Override
public void loadAsset(boolean async, String path, AssetType assetType, FileType fileType, AssetLoaderListener<Blob> listener) {
loadAsset(async, path, assetType, fileType, listener, false);
public void loadAsset(String path, AssetType assetType, FileType fileType) {
loadAsset(true, path, assetType, fileType, null, false);
}

@Override
public void loadAsset(String path, AssetType assetType, FileType fileType, AssetLoaderListener<Blob> listener) {
loadAsset(true, path, assetType, fileType, listener, false);
}

@Override
public void loadAsset(String path, AssetType assetType, FileType fileType, AssetLoaderListener<Blob> listener, boolean overwrite) {
loadAsset(true, path, assetType, fileType, listener, overwrite);
}

@Override
public void loadScript(String path) {
assetDownloader.loadScript(true, getScriptUrl() + path, null);
}

@Override
public void loadAsset(boolean async, String path, AssetType assetType, FileType fileType, AssetLoaderListener<Blob> listener, boolean overwrite) {
public void loadScript(String path, AssetLoaderListener<String> listener) {
assetDownloader.loadScript(true, getScriptUrl() + path, listener);
}

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

private void loadAsset(boolean async, String path, AssetType assetType, FileType fileType, AssetLoaderListener<Blob> listener, boolean overwrite) {
String path1 = fixPath(path);

if(path1.isEmpty()) {
Expand All @@ -236,7 +263,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 All @@ -255,7 +282,6 @@ public void onFailure(String url) {
@Override
public void onSuccess(String url, Blob result) {
assetInQueue.remove(path1);
AssetType type = AssetType.Binary;
Int8ArrayWrapper data = (Int8ArrayWrapper)result.getData();
byte[] byteArray = TypedArrays.toByteArray(data);
OutputStream output = fileHandle.write(false, 4096);
Expand All @@ -275,16 +301,6 @@ 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);
}

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

private String fixPath(String path1) {
path1 = path1.trim().replace("\\", "/");
if(path1.startsWith("/")) {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object LibExt {
var libVersion = ""

const val gdxVersion = "1.12.1"
const val teaVMVersion = "0.10.1"
const val teaVMVersion = "0.11.0"

const val gdxImGuiVersion = "-SNAPSHOT"
const val gdxMultiViewVersion = "-SNAPSHOT"
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 @@ -50,7 +51,7 @@ enum STEPS {
private Actor loadingBar;

private TeaApplication teaApplication;
private AssetLoader.AssetLoad assetLoader;
private AssetLoader assetLoader;

// Using manager just to load scene2d assets after downloading.
private AssetManager manager;
Expand All @@ -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("freetype.js");
};

new TeaApplication(new FreetypeDemo(), config);
}
}
Loading

0 comments on commit c3edd5f

Please sign in to comment.