Skip to content

Commit

Permalink
Set FPS to 30 when window loses focus; animate FPS counter.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Jan 8, 2017
1 parent 0f40ff4 commit a7e0c8d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/itdelatrisu/opsu/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
51 changes: 44 additions & 7 deletions src/itdelatrisu/opsu/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -117,6 +126,7 @@ public static void update(int delta) {
updateVolumeDisplay(delta);
updateBarNotification(delta);
tooltipAlpha.update(-delta);
updateFPS(delta);
}

/**
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a7e0c8d

Please # to comment.