From a7e0c8dc667bf8357087270ec100d8dcd65f5167 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Sun, 8 Jan 2017 03:21:45 -0500 Subject: [PATCH] Set FPS to 30 when window loses focus; animate FPS counter. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/Options.java | 8 ++++- src/itdelatrisu/opsu/ui/UI.java | 51 ++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/itdelatrisu/opsu/Options.java b/src/itdelatrisu/opsu/Options.java index 0cb6db99..45e4d7c7 100644 --- a/src/itdelatrisu/opsu/Options.java +++ b/src/itdelatrisu/opsu/Options.java @@ -420,7 +420,13 @@ public void read(String s) { } } }, - SHOW_FPS ("Show FPS Counter", "FpsCounter", "Show an FPS counter in the bottom-right hand corner.", true), + SHOW_FPS ("Show FPS Counter", "FpsCounter", "Show an FPS counter in the bottom-right hand corner.", true) { + @Override + public void toggle(GameContainer container) { + super.toggle(container); + UI.resetFPSDisplay(); + } + }, SHOW_UNICODE ("Prefer Non-English Metadata", "ShowUnicode", "Where available, song titles will be shown in their native language.", false) { @Override public void toggle(GameContainer container) { diff --git a/src/itdelatrisu/opsu/ui/UI.java b/src/itdelatrisu/opsu/ui/UI.java index b750e07c..4c1ffb01 100644 --- a/src/itdelatrisu/opsu/ui/UI.java +++ b/src/itdelatrisu/opsu/ui/UI.java @@ -20,6 +20,7 @@ import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.GameImage; +import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Options; import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.audio.SoundController; @@ -45,6 +46,9 @@ * Draws common UI components. */ public class UI { + /** The target frame rate when the window does not have focus. */ + private static final int IDLE_FPS = 30; + /** Cursor. */ private static Cursor cursor = new Cursor(); @@ -75,8 +79,12 @@ public class UI { /** The alpha level of the current tooltip (if any). */ private static AnimatedValue tooltipAlpha = new AnimatedValue(200, 0f, 1f, AnimationEquation.LINEAR); + /** The displayed FPS. */ + private static float fpsDisplay = 0f; + // game-related variables private static GameContainer container; + private static StateBasedGame game; private static Input input; // This class should not be instantiated. @@ -89,6 +97,7 @@ private UI() {} */ public static void init(GameContainer container, StateBasedGame game) { UI.container = container; + UI.game = game; UI.input = container.getInput(); // initialize cursor @@ -117,6 +126,7 @@ public static void update(int delta) { updateVolumeDisplay(delta); updateBarNotification(delta); tooltipAlpha.update(-delta); + updateFPS(delta); } /** @@ -198,16 +208,17 @@ public static void drawFPS() { if (!Options.isFPSCounterEnabled()) return; - String fps = String.format("%dFPS", container.getFPS()); + int fps = Math.round(fpsDisplay); + String s = String.format("%dFPS", fps); Fonts.BOLD.drawString( - container.getWidth() * 0.997f - Fonts.BOLD.getWidth(fps), - container.getHeight() * 0.997f - Fonts.BOLD.getHeight(fps), - Integer.toString(container.getFPS()), Color.white + container.getWidth() * 0.997f - Fonts.BOLD.getWidth(s), + container.getHeight() * 0.997f - Fonts.BOLD.getHeight(s), + Integer.toString(fps), Color.white ); Fonts.DEFAULT.drawString( - container.getWidth() * 0.997f - Fonts.BOLD.getWidth("FPS"), - container.getHeight() * 0.997f - Fonts.BOLD.getHeight("FPS"), - "FPS", Color.white + container.getWidth() * 0.997f - Fonts.BOLD.getWidth("FPS"), + container.getHeight() * 0.997f - Fonts.BOLD.getHeight("FPS"), + "FPS", Color.white ); } @@ -486,6 +497,32 @@ public static void drawBarNotification(Graphics g) { Colors.WHITE_ALPHA.a = oldAlphaW; } + /** + * Updates the FPS display by a delta interval. + * Also changes the frame rate if the window has lost or restored focus. + * @param delta the delta interval since the last call + */ + private static void updateFPS(int delta){ + // change frame rate when focus is lost/restored + boolean focus = (game.getCurrentStateID() == Opsu.STATE_GAME) ? true : container.hasFocus(); + container.setTargetFrameRate(focus ? Options.getTargetFPS() : IDLE_FPS); + + // update displayed FPS + if (Options.isFPSCounterEnabled()) { + int fps = container.getFPS(); + float multiplier = delta / 250f; + if (fpsDisplay < fps) + fpsDisplay = Math.min(fpsDisplay + (fps - fpsDisplay) * multiplier, fps); + else if (fpsDisplay > fps) + fpsDisplay = Math.max(fpsDisplay - (fpsDisplay - fps) * multiplier, fps); + } + } + + /** + * Resets the displayed FPS. + */ + public static void resetFPSDisplay() { fpsDisplay = 0f; } + /** * Shows a confirmation dialog (used before exiting the game). * @param message the message to display