Replaces `ctx2d.putImageData` with a WebGL2 texture quad in both 117HD and SD modes, aligns the TeaVM game loop with `requestAnimationFrame`, and adds PWA manifest and Service Worker asset caching for mobile/iOS Safari. ### WebGL2 Compositing & SD/HD Rendering - `WebHd117Renderer.java`, `WebGL2.java`: - Decoupled `ensureOverlayContext()` / `ensureOverlayReady()` so the WebGL2 2D overlay quad initializes without compiling 3D shadow/scene shaders when 117HD is off. - Added `drawHudPixels` and `drawHudCanvas` compositing onto `#gl-canvas` with `useBlend` control (disabling `GL_BLEND` for opaque SD frames). - `RSImageProducer.java`, `DesktopHooks.java`: - Routed `RSImageProducer.drawGraphics` and modern HUD draws (`drawModernMinimap`, `drawModernChat`, `drawModernTab`) through the WebGL2 quad whenever `ensureOverlayReady()` succeeds, clearing `#game-canvas` once to avoid `putImageData` overhead. ### Frame Pacing & Mobile PWA - `RSApplet.java`: - Added TeaVM `@Async` `awaitAnimationFrame()` backed by `window.requestAnimationFrame` for `delay <= 16` and capped catch-up ticks per frame (`loopTicks >= 2`). - `index.html`, `manifest.webmanifest`, `sw.js`: - Added Apple Web App meta tags, `manifest.webmanifest`, and `sw.js` Service Worker registration with shell precaching, network-first navigation, and stale-while-revalidate static asset caching.
122 lines
4.8 KiB
Java
122 lines
4.8 KiB
Java
package org.scape2006.web;
|
|
|
|
import org.teavm.jso.JSBody;
|
|
import org.teavm.jso.JSByRef;
|
|
import org.teavm.jso.canvas.CanvasRenderingContext2D;
|
|
import org.teavm.jso.canvas.ImageData;
|
|
import org.teavm.jso.dom.html.HTMLCanvasElement;
|
|
import org.teavm.jso.dom.html.HTMLDocument;
|
|
|
|
/**
|
|
* Web RSImageProducer backed by HTML5 Canvas 2D ImageData.
|
|
*/
|
|
public final class RSImageProducer {
|
|
|
|
public static final int GAME_WIDTH = 765;
|
|
public static final int GAME_HEIGHT = 503;
|
|
public static final int[] masterScreenBuffer = new int[GAME_WIDTH * GAME_HEIGHT];
|
|
public static final Object masterBufferLock = new Object();
|
|
|
|
public final int[] anIntArray315;
|
|
public final int anInt316; // width
|
|
public final int anInt317; // height
|
|
|
|
private static HTMLCanvasElement canvas;
|
|
private static CanvasRenderingContext2D ctx2d;
|
|
private static boolean cleared2dForGpu = false;
|
|
private ImageData imageData;
|
|
private org.teavm.jso.JSObject imageDataU32;
|
|
|
|
public RSImageProducer(int width, int height, Object component) {
|
|
this.anInt316 = width;
|
|
this.anInt317 = height;
|
|
this.anIntArray315 = new int[width * height];
|
|
ensureCanvas();
|
|
if (ctx2d != null) {
|
|
this.imageData = ctx2d.createImageData(width, height);
|
|
this.imageDataU32 = wrapImageDataU32(this.imageData);
|
|
}
|
|
initDrawingArea();
|
|
}
|
|
|
|
public static HTMLCanvasElement getCanvas() {
|
|
ensureCanvas();
|
|
return canvas;
|
|
}
|
|
|
|
private static void ensureCanvas() {
|
|
if (canvas == null) {
|
|
HTMLDocument doc = HTMLDocument.current();
|
|
canvas = (HTMLCanvasElement) doc.getElementById("game-canvas");
|
|
if (canvas != null) {
|
|
ctx2d = create2dContext(canvas);
|
|
}
|
|
}
|
|
}
|
|
|
|
@JSBody(params = {"canvas"}, script =
|
|
"try { return canvas.getContext('2d', { alpha: true, desynchronized: true }) || canvas.getContext('2d'); }\n" +
|
|
"catch (_) { return canvas.getContext('2d'); }")
|
|
private static native CanvasRenderingContext2D create2dContext(HTMLCanvasElement canvas);
|
|
|
|
@JSBody(params = {"imgData"}, script = "return new Uint32Array(imgData.data.buffer);")
|
|
private static native org.teavm.jso.JSObject wrapImageDataU32(ImageData imgData);
|
|
|
|
public void initDrawingArea() {
|
|
DrawingArea.initDrawingArea(anInt317, anInt316, anIntArray315);
|
|
Texture.textureInt1 = anInt316 / 2;
|
|
if (ClientSettings.RESIZABLE && anInt316 < anInt317) {
|
|
Texture.textureInt2 = (int) (anInt317 * 0.38f);
|
|
} else {
|
|
Texture.textureInt2 = anInt317 / 2;
|
|
}
|
|
}
|
|
|
|
public void drawGraphics(int y, Object graphics, int x) {
|
|
if (anIntArray315 == null) return;
|
|
if (ClientSettings.isDesktopModern() && !ModernLayout.inGame) {
|
|
cleared2dForGpu = false;
|
|
DesktopHooks.drawScaledLoginTile(graphics, anIntArray315, x, y, anInt316, anInt317);
|
|
return;
|
|
}
|
|
ensureCanvas();
|
|
if (ctx2d == null) return;
|
|
boolean gpuViewport = HD117Pipeline.isGpuActive() && anInt316 == ClientSettings.VIEWPORT_WIDTH && anInt317 == ClientSettings.VIEWPORT_HEIGHT;
|
|
if (WebHd117Renderer.ensureOverlayReady()) {
|
|
if (!cleared2dForGpu && canvas != null) {
|
|
ctx2d.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
cleared2dForGpu = true;
|
|
}
|
|
WebHd117Renderer.drawHudPixels(anIntArray315, x, y, anInt316, anInt317, gpuViewport);
|
|
return;
|
|
}
|
|
cleared2dForGpu = false;
|
|
if (imageData == null || imageData.getWidth() != anInt316 || imageData.getHeight() != anInt317) {
|
|
imageData = ctx2d.createImageData(anInt316, anInt317);
|
|
imageDataU32 = wrapImageDataU32(imageData);
|
|
}
|
|
if (gpuViewport) {
|
|
blitOverlayPixelsToImageData(anIntArray315, imageDataU32);
|
|
} else {
|
|
blitPixelsToImageData(anIntArray315, imageDataU32);
|
|
}
|
|
ctx2d.putImageData(imageData, x, y);
|
|
}
|
|
|
|
@JSBody(params = {"pixels", "dst"}, script =
|
|
"var len = pixels.length;\n" +
|
|
"for (var i = 0; i < len; i++) {\n" +
|
|
" var c = pixels[i];\n" +
|
|
" dst[i] = -16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255);\n" +
|
|
"}")
|
|
private static native void blitPixelsToImageData(@JSByRef int[] pixels, org.teavm.jso.JSObject dst);
|
|
|
|
@JSBody(params = {"pixels", "dst"}, script =
|
|
"var len = pixels.length;\n" +
|
|
"for (var i = 0; i < len; i++) {\n" +
|
|
" var c = pixels[i];\n" +
|
|
" dst[i] = (c === 66051) ? 0 : (-16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255));\n" +
|
|
"}")
|
|
private static native void blitOverlayPixelsToImageData(@JSByRef int[] pixels, org.teavm.jso.JSObject dst);
|
|
}
|