Adjusted for the web port

This commit is contained in:
2025-04-14 17:05:14 +03:00
parent 44546b1d72
commit f6d85c5315
19 changed files with 598 additions and 35 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 KiB

View File

@@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This file is derived from micronaut-libgdx-teavm (https://github.com/hollingsworthd/micronaut-libgdx-teavm),
* originally licensed under the Apache License, Version 2.0.
*
* Modifications made by Konsthol on 13/4/25:
* - Adjusted to compile Shattered Pixel Dungeon
*
* Copyright 2022 Daniel Hollingsworth
*/
package com.shatteredpixel.shatteredpixeldungeon.html;
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.gen.SkipClass;
import java.io.File;
import java.io.IOException;
import org.teavm.tooling.TeaVMTool;
import org.teavm.vm.TeaVMOptimizationLevel;
@SkipClass
public class Compile {
public static void main(String[] args) throws IOException {
deleteDir(new File("../release/webapp"));
TeaBuildConfiguration conf = new TeaBuildConfiguration();
conf.webappPath = new File("../release").getAbsolutePath();
conf.assetsPath.add(new AssetFileHandle("../core/src/main/assets"));
TeaVMTool tool = TeaBuilder.config(conf);
tool.setMainClass(TeaVMLauncher.class.getName());
tool.setOptimizationLevel(TeaVMOptimizationLevel.ADVANCED);
tool.setObfuscated(true);
tool.setShortFileNames(true);
tool.setSourceFilesCopied(false);
tool.setStrict(false);
tool.setSourceMapsFileGenerated(false);
tool.setDebugInformationGenerated(false);
tool.setIncremental(false);
TeaBuilder.build(tool);
}
private static void deleteDir(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
deleteDir(file);
}
}
dir.delete();
}
}

View File

@@ -0,0 +1,122 @@
package com.shatteredpixel.shatteredpixeldungeon.html;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.watabou.input.ControllerHandler;
import com.watabou.noosa.Game;
import com.watabou.utils.PlatformSupport;
import org.teavm.jso.JSBody;
import java.util.HashMap;
import java.util.regex.Pattern;
public class HtmlPlatformSupport extends PlatformSupport {
@Override
public void updateDisplaySize() {
if (Gdx.app == null || Gdx.graphics == null) {
System.err.println("Gdx is not initialized yet. Skipping display size update.");
return;
}
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void updateSystemUI() {
Gdx.app.postRunnable( new Runnable() {
@Override
public void run() {
Object canvas = getCanvasElement("canvas");
if (SPDSettings.fullscreen()) {
System.out.println("Requesting fullscreen.");
requestFullscreen("canvas");
} else {
System.out.println("Exiting fullscreen.");
exitFullscreen();
setCanvasSize("canvas", SPDSettings.windowResolution().x, SPDSettings.windowResolution().y);
}
}
});
}
@JSBody(script = ""
+ "document.body.addEventListener('click', function() {"
+ " console.log('User clicked. Triggering fullscreen and audio...');"
+ " document.getElementById('canvas').requestFullscreen();"
+ " if (!window.audioContext) {"
+ " window.audioContext = new (window.AudioContext || window.webkitAudioContext)();"
+ " }"
+ " window.audioContext.resume().then(() => {"
+ " console.log('AudioContext resumed successfully.');"
+ " }).catch((err) => {"
+ " console.error('Failed to resume AudioContext:', err);"
+ " });"
+ "}, { once: true });")
public static native void setupClickListener();
@JSBody(params = {"id"}, script = "document.getElementById(id).requestFullscreen();")
private static native void requestFullscreen(String id);
@JSBody(params = {"id"}, script = "return document.getElementById(id);")
private static native Object getCanvasElement(String id);
@JSBody(params = {}, script = "document.exitFullscreen();")
private static native void exitFullscreen();
@JSBody(params = {"id", "width", "height"}, script = "var canvas = document.getElementById(id); canvas.width = width; canvas.height = height;")
private static native void setCanvasSize(String id, int width, int height);
@Override
public boolean connectedToUnmeteredNetwork() {
return true;
}
@Override
public boolean supportsVibration() {
return ControllerHandler.vibrationSupported();
}
private static FreeTypeFontGenerator basicFontGenerator;
@Override
public void setupFontGenerators(int pageSize, boolean systemfont) {
//don't bother doing anything if nothing has changed
if (fonts != null && this.pageSize == pageSize && this.systemfont == systemfont) {
return;
}
this.pageSize = pageSize;
this.systemfont = systemfont;
resetGenerators(false);
fonts = new HashMap<>();
basicFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/pixel_font.ttf"));
fonts.put(basicFontGenerator, new HashMap<>());
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
}
@Override
protected FreeTypeFontGenerator getGeneratorForString( String input ) {
return basicFontGenerator;
}
private final Pattern regularsplitter = Pattern.compile("(?<=\n)|(?=\n)|(?<=_)|(?=_)|(?<=\\*\\*)|(?=\\*\\*)");
private final Pattern regularsplitterMultiline = Pattern.compile("(?<= )|(?= )|(?<=\n)|(?=\n)|(?<=_)|(?=_)|(?<=\\*\\*)|(?=\\*\\*)");
@Override
public String[] splitforTextBlock(String text, boolean multiline) {
if (multiline) {
return regularsplitterMultiline.split(text);
} else {
return regularsplitter.split(text);
}
}
}

View File

@@ -0,0 +1,46 @@
package com.shatteredpixel.shatteredpixeldungeon.html;
import com.badlogic.gdx.Files;
import com.watabou.noosa.Game;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
import com.github.xpenatan.gdx.backends.teavm.TeaApplicationConfiguration;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.watabou.utils.FileUtils;
public class TeaVMLauncher {
public static void main(String[] args) {
TeaApplicationConfiguration config = new TeaApplicationConfiguration("canvas");
config.width = 0;
config.height = 0;
config.useGL30 = true;
config.preloadListener = assetLoader -> {
try {
assetLoader.loadScript("freetype.js");
} catch (Exception e) {
System.err.println("Error loading freetype.js: " + e.getMessage());
}
};
initializeServices();
FileUtils.setDefaultFileProperties(Files.FileType.Local, "");
try {
HtmlPlatformSupport platformSupport = new HtmlPlatformSupport();
platformSupport.setupClickListener();
new TeaApplication(new ShatteredPixelDungeon(platformSupport), config);
} catch (Exception e) {
System.err.println("Error launching TeaApplication: " + e.getMessage());
e.printStackTrace();
}
}
private static void initializeServices() {
Game.version = "3.0.2";
Game.versionCode = 833;
}
}