Update #53 - Improved FPS, reduced WebGL context loss crashes

This commit is contained in:
lax1dude
2025-07-06 12:31:55 -07:00
parent f3281c037f
commit 332a7bb11f
53 changed files with 568 additions and 383 deletions

View File

@ -63,6 +63,7 @@ public class PlatformAudio {
static AudioBufferSourceNode recDestSilenceNode = null;
static GainNode micRecGain = null;
static GainNode gameRecGain = null;
static HTMLAudioElement silenceElement = null;
private static final Map<String, BrowserAudioResource> soundCache = new HashMap<>();
private static final List<BrowserAudioHandle> activeSounds = new LinkedList<>();
@ -253,16 +254,33 @@ public class PlatformAudio {
HTMLAudioElement audio = (HTMLAudioElement) PlatformRuntime.doc.createElement("audio");
audio.getClassList().add("_eaglercraftX_keepalive_hack");
audio.setAttribute("style", "display:none;");
audio.setAutoplay(true);
audio.setAutoplay(false);
audio.setLoop(true);
audio.addEventListener("seeked", (e) -> {
// NOP, wakes up the browser's event loop
});
audio.addEventListener("canplay", (e) -> {
if (PlatformRuntime.doc != null && silenceElement != null &&
!PlatformInput.getVisibilityState(PlatformRuntime.doc)) {
silenceElement.play();
}
});
HTMLSourceElement source = (HTMLSourceElement) PlatformRuntime.doc.createElement("source");
source.setType("audio/wav");
source.setSrc(TeaVMBlobURLManager.registerNewURLByte(silenceFile, "audio/wav").toExternalForm());
audio.appendChild(source);
audio.addEventListener("seeked", (e) -> {
// NOP, wakes up the browser's event loop
});
PlatformRuntime.parent.appendChild(audio);
silenceElement = audio;
}
}
}
static void handleVisibilityChange() {
if (silenceElement != null) {
if (!PlatformInput.getVisibilityState(PlatformRuntime.doc)) {
silenceElement.play();
} else {
silenceElement.pause();
}
}
}
@ -607,6 +625,11 @@ public class PlatformAudio {
micRecGain = null;
gameRecGain = null;
}
if(silenceElement != null) {
silenceElement.pause();
silenceElement.delete();
silenceElement = null;
}
}
}

View File

@ -103,6 +103,7 @@ public class PlatformInput {
private static EventListener<?> pointerlock = null;
private static EventListener<?> pointerlockerr = null;
private static EventListener<?> fullscreen = null;
private static EventListener<?> visibilitychange = null;
private static Map<String,LegacyKeycodeTranslator.LegacyKeycode> keyCodeTranslatorMap = null;
@ -636,6 +637,12 @@ public class PlatformInput {
isWindowFocused = true;
}
});
win.getDocument().addEventListener("visibilitychange", visibilitychange = new EventListener<Event>() {
@Override
public void handleEvent(Event evt) {
PlatformAudio.handleVisibilityChange();
}
});
try {
pointerLockSupported = getSupportedPointerLock(win.getDocument());
@ -865,7 +872,7 @@ public class PlatformInput {
}
@JSBody(params = { "doc" }, script = "return (typeof doc.visibilityState !== \"string\") || (doc.visibilityState === \"visible\");")
private static native boolean getVisibilityState(JSObject doc);
static native boolean getVisibilityState(JSObject doc);
@JSBody(params = { "win" }, script = "return (typeof win.devicePixelRatio === \"number\") ? win.devicePixelRatio : 1.0;")
static native double getDevicePixelRatio(Window win);
@ -1513,6 +1520,10 @@ public class PlatformInput {
win.removeEventListener("blur", blur);
blur = null;
}
if(visibilitychange != null) {
win.getDocument().removeEventListener("visibilitychange", blur);
visibilitychange = null;
}
if(wheel != null) {
canvas.removeEventListener("wheel", wheel);
wheel = null;

View File

@ -600,6 +600,11 @@ public class PlatformOpenGL {
//checkErr("_wglDrawElements(" + mode + ", " + count + ", " + type + ", " + offset + ");");
}
public static void _wglDrawRangeElements(int mode, int start, int end, int count, int type, int offset) {
ctx.drawRangeElements(mode, start, end, count, type, offset);
//checkErr("_wglDrawRangeElements(" + mode + ", " + start + ", " + end + ", " + count + ", " + type + ", " + offset + ");");
}
public static void _wglDrawElementsInstanced(int mode, int count, int type, int offset, int instances) {
switch(instancingImpl) {
case INSTANCE_IMPL_CORE:

View File

@ -72,6 +72,8 @@ public interface WebGL2RenderingContext extends WebGLRenderingContext {
void drawElementsInstanced(int p1, int p2, int p3, int p4, int p5);
void drawRangeElements(int p1, int p2, int p3, int p4, int p5, int p6);
int getUniformBlockIndex(WebGLProgram p1, String p2);
void bindBufferRange(int p1, int p2, WebGLBuffer p3, int p4, int p5);