From 3d41a12158ef416cbd9ed36d07d27e31653b40e7 Mon Sep 17 00:00:00 2001 From: Park Hyunwoo Date: Fri, 22 Nov 2024 10:49:15 +0900 Subject: [PATCH] feat: Non-renderframe-synced keysound/updates `timer.update()` is tied to `MainController::render` call, and it is used as a source of truth for keysound & judging system. This patch creates a separate thread that continuously updates the timer. Note that this patch isn't yet tested on the wild or had undergone any unit tests, so it might break some other functionalities. Further testing required. --- src/bms/player/beatoraja/MainController.java | 43 +++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/bms/player/beatoraja/MainController.java b/src/bms/player/beatoraja/MainController.java index b22f1cfca..c3f0b0b1e 100644 --- a/src/bms/player/beatoraja/MainController.java +++ b/src/bms/player/beatoraja/MainController.java @@ -3,6 +3,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; import org.lwjgl.input.Mouse; @@ -77,6 +79,7 @@ public class MainController { private MainState current; private TimerManager timer; + private Lock timerLock; private Config config; private PlayerConfig player; @@ -192,6 +195,7 @@ public MainController(Path f, Config config, PlayerConfig player, BMSPlayerMode } timer = new TimerManager(); + timerLock = new ReentrantLock(); sound = new SystemSoundManager(this); if(config.isUseDiscordRPC()) { @@ -361,6 +365,13 @@ public void create() { if (time != now) { time = now; input.poll(); + if (timerLock.tryLock()) { + try { + timer.update(); + } finally { + timerLock.unlock(); + } + } } else { try { Thread.sleep(0, 500000); @@ -410,22 +421,26 @@ public void create() { public void render() { // input.poll(); - timer.update(); - - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + timerLock.lock(); + try { + timer.update(); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - current.render(); - sprite.begin(); - if (current.getSkin() != null) { - current.getSkin().updateCustomObjects(current); - current.getSkin().drawAllObjects(sprite, current); - } - sprite.end(); + current.render(); + sprite.begin(); + if (current.getSkin() != null) { + current.getSkin().updateCustomObjects(current); + current.getSkin().drawAllObjects(sprite, current); + } + sprite.end(); - final Stage stage = current.getStage(); - if (stage != null) { - stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f)); - stage.draw(); + final Stage stage = current.getStage(); + if (stage != null) { + stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f)); + stage.draw(); + } + } finally { + timerLock.unlock(); } // show fps