diff --git a/README.md b/README.md index defc251..9cbdd21 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ This project was generated with a template including simple application launcher - `lwjgl3`: Primary desktop platform using LWJGL3. - `android`: Android mobile platform. Needs Android SDK. - `html`: Web platform using GWT and WebGL. Supports only Java projects. +- `teavm`: Experimental web platform using TeaVM and WebGL. ## Gradle @@ -40,6 +41,8 @@ Useful Gradle tasks and flags: - `idea`: generates IntelliJ project data. - `lwjgl3:jar`: builds application's runnable jar, which can be found at `lwjgl3/build/lib`. - `lwjgl3:run`: starts the application. +- `teavm:build`: builds the JavaScript application into the build/dist/webapp folder. +- `teavm:run`: serves the JavaScript application at http://localhost:8080 via a local Jetty server. - `test`: runs unit tests (if any). Note that most tasks that are not specific to a single project can be run with `name:` prefix, where the `name` should be replaced with the ID of a specific project. diff --git a/build.gradle b/build.gradle index 8f9b246..7fda46b 100644 --- a/build.gradle +++ b/build.gradle @@ -70,6 +70,7 @@ subprojects { maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' } maven { url 'https://jitpack.io' } + maven { url 'https://teavm.org/maven/repository/' } maven { url 'https://repo.openeggbert.com' } maven { url 'https://repo.openeggbert.com/releases/' } diff --git a/gradle.properties b/gradle.properties index 6df1e71..785ea39 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,4 +19,7 @@ gwtFrameworkVersion=2.11.0 gwtPluginVersion=1.1.29 gdxVersion=1.12.1 pixelVersion=0.0.0-SNAPSHOT +gdxTeaVMVersion=1.0.3 +teaVMVersion=0.10.2 netbeans.hint.jdkPlatform=JDK_17 +shapeDrawerVersion=2.6.0 diff --git a/lwjgl3/src/main/java/com/openeggbert/lwjgl3/debugging/DebuggingAssetsTxt.java b/lwjgl3/src/main/java/com/openeggbert/lwjgl3/debugging/DebuggingAssetsTxt.java index f771ffa..32bf7d5 100644 --- a/lwjgl3/src/main/java/com/openeggbert/lwjgl3/debugging/DebuggingAssetsTxt.java +++ b/lwjgl3/src/main/java/com/openeggbert/lwjgl3/debugging/DebuggingAssetsTxt.java @@ -21,12 +21,12 @@ package com.openeggbert.lwjgl3.debugging; import com.pixelgamelibrary.api.Pixel; import com.pixelgamelibrary.backend.libgdx.PixelBackendLibGDX; -import com.pixelgamelibrary.backend.libgdx.assets.AssetsTxt; +import com.pixelgamelibrary.api.utils.AssetsTxt; import java.util.List; import java.util.Scanner; /** - * + *f * @author robertvokac */ public class DebuggingAssetsTxt { diff --git a/settings.gradle b/settings.gradle index 635c719..5fb99f3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ // A list of which subprojects to load as part of the same larger project. // You can remove Strings from the list and reload the Gradle project // if you want to temporarily disable a subproject. -include 'lwjgl3', 'android', 'html', 'core' +include 'lwjgl3', 'android', 'html', 'teavm', 'core' diff --git a/teavm/build.gradle b/teavm/build.gradle new file mode 100644 index 0000000..53ef6e2 --- /dev/null +++ b/teavm/build.gradle @@ -0,0 +1,47 @@ +plugins { + id 'java' + id 'org.gretty' version '3.1.0' +} + +gretty { + contextPath = '/' + extraResourceBase 'build/dist/webapp' +} + +sourceSets.main.resources.srcDirs += [ rootProject.file('assets').path ] +project.ext.mainClassName = 'com.openeggbert.teavm.TeaVMBuilder' +eclipse.project.name = appName + '-teavm' + +// This must be at least 11, and no higher than the JDK version this project is built with. +java.targetCompatibility = "11" +// This should probably be equal to targetCompatibility, above. This only affects the TeaVM module. +java.sourceCompatibility = "11" + + +dependencies { + implementation "com.github.xpenatan.gdx-teavm:backend-teavm:$gdxTeaVMVersion" + implementation "org.teavm:teavm-classlib:$teaVMVersion" + implementation "org.teavm:teavm-core:$teaVMVersion" + implementation "org.teavm:teavm-jso-apis:$teaVMVersion" + implementation "org.teavm:teavm-jso-impl:$teaVMVersion" + implementation "org.teavm:teavm-jso:$teaVMVersion" + implementation "org.teavm:teavm-tooling:$teaVMVersion" + implementation project(':core') + implementation "com.pixelgamelibrary:pixel:$pixelVersion:sources" + api "com.pixelgamelibrary:pixel-backend-libgdx:$pixelVersion" + implementation "com.pixelgamelibrary:pixel-backend-libgdx:$pixelVersion:sources" + +} + +tasks.register('buildJavaScript', JavaExec) { + dependsOn classes + setDescription("Transpile bytecode to JavaScript via TeaVM") + mainClass.set(project.mainClassName) + setClasspath(sourceSets.main.runtimeClasspath) +} +build.dependsOn buildJavaScript + +tasks.register("run") { + description = "Run the JavaScript application hosted via a local Jetty server at http://localhost:8080/" + dependsOn(buildJavaScript, tasks.named("jettyRun")) +} diff --git a/teavm/src/main/java/com/openeggbert/teavm/TeaVMBuilder.java b/teavm/src/main/java/com/openeggbert/teavm/TeaVMBuilder.java new file mode 100644 index 0000000..69c416e --- /dev/null +++ b/teavm/src/main/java/com/openeggbert/teavm/TeaVMBuilder.java @@ -0,0 +1,35 @@ +package com.openeggbert.teavm; + +import com.github.xpenatan.gdx.backends.teavm.config.AssetFileHandle; +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.config.plugins.TeaReflectionSupplier; +import com.github.xpenatan.gdx.backends.teavm.gen.SkipClass; +import java.io.File; +import java.io.IOException; +import org.teavm.tooling.TeaVMTool; +import org.teavm.vm.TeaVMOptimizationLevel; + +/** Builds the TeaVM/HTML application. */ +@SkipClass +public class TeaVMBuilder { + public static void main(String[] args) throws IOException { + TeaBuildConfiguration teaBuildConfiguration = new TeaBuildConfiguration(); + teaBuildConfiguration.assetsPath.add(new AssetFileHandle("../assets")); + teaBuildConfiguration.webappPath = new File("build/dist").getCanonicalPath(); + + // Register any extra classpath assets here: + // teaBuildConfiguration.additionalAssetsClasspathFiles.add("com/openeggbert/asset.extension"); + + // Register any classes or packages that require reflection here: + // TeaReflectionSupplier.addReflectionClass("com.openeggbert.reflect"); + + TeaVMTool tool = TeaBuilder.config(teaBuildConfiguration); + tool.setMainClass(TeaVMLauncher.class.getName()); + // For many (or most) applications, using the highest optimization won't add much to build time. + // If your builds take too long, and runtime performance doesn't matter, you can change FULL to SIMPLE . + tool.setOptimizationLevel(TeaVMOptimizationLevel.FULL); + tool.setObfuscated(true); + TeaBuilder.build(tool); + } +} diff --git a/teavm/src/main/java/com/openeggbert/teavm/TeaVMLauncher.java b/teavm/src/main/java/com/openeggbert/teavm/TeaVMLauncher.java new file mode 100644 index 0000000..de29bc0 --- /dev/null +++ b/teavm/src/main/java/com/openeggbert/teavm/TeaVMLauncher.java @@ -0,0 +1,28 @@ +package com.openeggbert.teavm; + +import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration; +import com.github.xpenatan.gdx.backends.teavm.TeaApplication; +import com.openeggbert.core.main.OpenEggbertApplication; +import com.pixelgamelibrary.api.Pixel; +import com.pixelgamelibrary.backend.libgdx.PixelBackendLibGDX; +import com.pixelgamelibrary.backend.libgdx.game.LibGdxGame; + +/** + * Launches the TeaVM/HTML application. + */ +public class TeaVMLauncher { + public static void main(String[] args) { + TeaApplicationConfiguration config = new TeaApplicationConfiguration("canvas"); + //// If width and height are each greater than 0, then the app will use a fixed size. + //config.width = 640; + //config.height = 480; + //// If width and height are both 0, then the app will use all available space. + //config.width = 0; + //config.height = 0; + //// If width and height are both -1, then the app will fill the canvas size. + config.width = -1; + config.height = -1; + if(!Pixel.isBackendSet()) {Pixel.initBackend(new PixelBackendLibGDX());} + TeaApplication application = new TeaApplication(new LibGdxGame(new OpenEggbertApplication().createGame()), config); + } +}