From 7162f8bc7d47bf7947ef561abf6b73478d33f8e9 Mon Sep 17 00:00:00 2001 From: yugecin Date: Tue, 10 Jan 2017 13:02:40 +0100 Subject: [PATCH] handle input at higher rate than render rate --- src/yugecin/opsudance/core/Demux.java | 4 ++ .../opsudance/core/DisplayContainer.java | 56 ++++++++++++++----- .../opsudance/core/state/OpsuState.java | 1 + .../state/transitions/TransitionState.java | 4 ++ .../opsudance/states/EmptyRedState.java | 5 ++ src/yugecin/opsudance/states/EmptyState.java | 4 ++ 6 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/yugecin/opsudance/core/Demux.java b/src/yugecin/opsudance/core/Demux.java index ac47b35b..0b3014ae 100644 --- a/src/yugecin/opsudance/core/Demux.java +++ b/src/yugecin/opsudance/core/Demux.java @@ -85,6 +85,10 @@ public class Demux implements KeyListener, MouseListener { state.update(delta); } + public void preRenderUpdate(int delta) { + state.update(delta); + } + public void render(Graphics g) { state.render(g); } diff --git a/src/yugecin/opsudance/core/DisplayContainer.java b/src/yugecin/opsudance/core/DisplayContainer.java index 02ffaa92..bbbc247e 100644 --- a/src/yugecin/opsudance/core/DisplayContainer.java +++ b/src/yugecin/opsudance/core/DisplayContainer.java @@ -54,6 +54,14 @@ public class DisplayContainer { public int width; public int height; + public int targetRenderInterval; + public int targetBackgroundRenderInterval; + + public int realRenderInterval; + public int delta; + + public int timeSinceLastRender; + private long lastFrame; @Inject @@ -61,6 +69,8 @@ public class DisplayContainer { this.demux = demux; this.nativeDisplayMode = Display.getDisplayMode(); this.resolutionChangeListeners = new LinkedList<>(); + targetRenderInterval = 16; // ~60 fps + targetBackgroundRenderInterval = 41; // ~24 fps lastFrame = getTime(); } @@ -81,20 +91,40 @@ public class DisplayContainer { setup(); log("GL ready"); while(!(Display.isCloseRequested() && demux.onCloseRequest())) { - // TODO: lower fps when not visible Display.isVisible - int delta = getDelta(); + delta = getDelta(); + + timeSinceLastRender += delta; + input.poll(width, height); - GL.glClear(SGL.GL_COLOR_BUFFER_BIT); - /* - graphics.resetTransform(); - graphics.resetFont(); - graphics.resetLineWidth(); - graphics.resetTransform(); - */ - demux.update(delta); - demux.render(graphics); - Display.update(true); - Display.sync(60); + + int maxRenderInterval; + if (Display.isVisible() && Display.isActive()) { + maxRenderInterval = targetRenderInterval; + } else { + maxRenderInterval = targetBackgroundRenderInterval; + } + + if (timeSinceLastRender >= maxRenderInterval) { + GL.glClear(SGL.GL_COLOR_BUFFER_BIT); + + /* + graphics.resetTransform(); + graphics.resetFont(); + graphics.resetLineWidth(); + graphics.resetTransform(); + */ + + demux.update(timeSinceLastRender); + demux.render(graphics); + + realRenderInterval = timeSinceLastRender; + timeSinceLastRender = 0; + + Display.update(false); + } + + Display.processMessages(); + Display.sync(1000); } teardown(); } diff --git a/src/yugecin/opsudance/core/state/OpsuState.java b/src/yugecin/opsudance/core/state/OpsuState.java index 9620284a..b2e070fb 100644 --- a/src/yugecin/opsudance/core/state/OpsuState.java +++ b/src/yugecin/opsudance/core/state/OpsuState.java @@ -22,6 +22,7 @@ import org.newdawn.slick.Graphics; public interface OpsuState { void update(int delta); + void preRenderUpdate(int delta); void render(Graphics g); void enter(); void leave(); diff --git a/src/yugecin/opsudance/core/state/transitions/TransitionState.java b/src/yugecin/opsudance/core/state/transitions/TransitionState.java index 1658a57b..e90310f7 100644 --- a/src/yugecin/opsudance/core/state/transitions/TransitionState.java +++ b/src/yugecin/opsudance/core/state/transitions/TransitionState.java @@ -45,6 +45,10 @@ public abstract class TransitionState extends BaseOpsuState { } } + @Override + public void preRenderUpdate(int delta) { + } + @Override public void render(Graphics g) { applicableState.render(g); diff --git a/src/yugecin/opsudance/states/EmptyRedState.java b/src/yugecin/opsudance/states/EmptyRedState.java index d9cf5ccd..f1073d0c 100644 --- a/src/yugecin/opsudance/states/EmptyRedState.java +++ b/src/yugecin/opsudance/states/EmptyRedState.java @@ -42,6 +42,10 @@ public class EmptyRedState implements OpsuState { } } + @Override + public void preRenderUpdate(int delta) { + } + @Override public void render(Graphics g) { g.setColor(Color.red); @@ -69,6 +73,7 @@ public class EmptyRedState implements OpsuState { @Override public boolean mouseWheelMoved(int delta) { + System.out.println("moved"); return false; } diff --git a/src/yugecin/opsudance/states/EmptyState.java b/src/yugecin/opsudance/states/EmptyState.java index 958e6071..484795ac 100644 --- a/src/yugecin/opsudance/states/EmptyState.java +++ b/src/yugecin/opsudance/states/EmptyState.java @@ -42,6 +42,10 @@ public class EmptyState implements OpsuState { } } + @Override + public void preRenderUpdate(int delta) { + } + @Override public void render(Graphics g) { g.setColor(Color.green);