attempt to improve the fpsmeter
This commit is contained in:
@@ -153,12 +153,12 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
|
||||
});
|
||||
|
||||
this.nativeDisplayMode = Display.getDisplayMode();
|
||||
setUPS(1000);
|
||||
setFPS(60);
|
||||
targetBackgroundRenderInterval = 41; // ~24 fps
|
||||
lastFrame = getTime();
|
||||
delta = 1;
|
||||
renderDelta = 1;
|
||||
|
||||
Options.GameOption.displayContainer = this;
|
||||
}
|
||||
|
||||
public void setUPS(int ups) {
|
||||
@@ -172,6 +172,9 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
|
||||
}
|
||||
|
||||
public void init(Class<? extends OpsuState> startingState) {
|
||||
setUPS(Options.getTargetUPS());
|
||||
setFPS(Options.getTargetFPS());
|
||||
|
||||
state = instanceContainer.provide(startingState);
|
||||
state.enter();
|
||||
|
||||
@@ -192,6 +195,8 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
|
||||
mouseX = input.getMouseX();
|
||||
mouseY = input.getMouseY();
|
||||
|
||||
fpsState.update();
|
||||
|
||||
state.update();
|
||||
if (drawCursor) {
|
||||
cursor.setCursorPosition(delta, mouseX, mouseY);
|
||||
|
||||
@@ -17,42 +17,63 @@
|
||||
*/
|
||||
package yugecin.opsudance.core.state.specialstates;
|
||||
|
||||
import itdelatrisu.opsu.Options;
|
||||
import itdelatrisu.opsu.ui.Fonts;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.Graphics;
|
||||
import yugecin.opsudance.core.DisplayContainer;
|
||||
import yugecin.opsudance.core.events.EventListener;
|
||||
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
|
||||
import yugecin.opsudance.utils.FPSMeter;
|
||||
|
||||
public class FpsRenderState implements EventListener<ResolutionOrSkinChangedEvent> {
|
||||
|
||||
private final DisplayContainer displayContainer;
|
||||
|
||||
private final static Color GREEN = new Color(171, 218, 25);
|
||||
private final static Color ORANGE = new Color(255, 204, 34);
|
||||
private final static Color DARKORANGE = new Color(255, 149, 24);
|
||||
|
||||
private final DisplayContainer displayContainer;
|
||||
private final FPSMeter fpsMeter;
|
||||
private final FPSMeter upsMeter;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int singleHeight;
|
||||
|
||||
public FpsRenderState(DisplayContainer displayContainer) {
|
||||
this.displayContainer = displayContainer;
|
||||
fpsMeter = new FPSMeter(10);
|
||||
upsMeter = new FPSMeter(10);
|
||||
displayContainer.eventBus.subscribe(ResolutionOrSkinChangedEvent.class, this);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
upsMeter.update(displayContainer.delta);
|
||||
}
|
||||
|
||||
public void render(Graphics g) {
|
||||
fpsMeter.update(displayContainer.renderDelta);
|
||||
if (!Options.isFPSCounterEnabled()) {
|
||||
return;
|
||||
}
|
||||
int x = this.x;
|
||||
int target = displayContainer.targetRenderInterval + (displayContainer.targetUpdateInterval % displayContainer.targetRenderInterval);
|
||||
x = drawText(g, getColor(target, displayContainer.renderDelta), (1000 / displayContainer.renderDelta) + " fps", x, this.y);
|
||||
drawText(g, getColor(displayContainer.targetUpdateInterval, displayContainer.delta), (1000 / displayContainer.delta) + " ups", x, this.y);
|
||||
int fpsDeviation = displayContainer.delta % displayContainer.targetRenderInterval;
|
||||
x = drawText(g, getColor((int) (Options.getTargetFPS() * 0.9f) - fpsDeviation, fpsMeter.getValue()), getText(fpsMeter.getValue(), "fps"), x, this.y);
|
||||
drawText(g, getColor((int) (Options.getTargetUPS() * 0.9f), upsMeter.getValue()), getText(upsMeter.getValue(), "ups"), x, this.y);
|
||||
}
|
||||
|
||||
private String getText(int value, String unit) {
|
||||
if (Options.useDeltasForFPSCounter()) {
|
||||
return String.format("%.2fms", 1000f / value);
|
||||
}
|
||||
return value + " " + unit;
|
||||
}
|
||||
|
||||
private Color getColor(int targetValue, int realValue) {
|
||||
if (realValue <= targetValue) {
|
||||
if (realValue >= targetValue) {
|
||||
return GREEN;
|
||||
}
|
||||
if (realValue <= targetValue * 1.15f) {
|
||||
if (realValue >= targetValue * 0.85f) {
|
||||
return ORANGE;
|
||||
}
|
||||
return DARKORANGE;
|
||||
@@ -64,7 +85,7 @@ public class FpsRenderState implements EventListener<ResolutionOrSkinChangedEven
|
||||
private int drawText(Graphics g, Color color, String text, int x, int y) {
|
||||
int width = Fonts.SMALL.getWidth(text) + 10;
|
||||
g.setColor(color);
|
||||
g.fillRoundRect(x - width, y, width, singleHeight + 6, 2);
|
||||
g.fillRoundRect(x - width, y, width, singleHeight + 6, 5, 25);
|
||||
Fonts.SMALL.drawString(x - width + 3, y + 3, text, Color.black);
|
||||
return x - width - 6;
|
||||
}
|
||||
|
||||
52
src/yugecin/opsudance/utils/FPSMeter.java
Normal file
52
src/yugecin/opsudance/utils/FPSMeter.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* opsu!dance - fork of opsu! with cursordance auto
|
||||
* Copyright (C) 2017 yugecin
|
||||
*
|
||||
* opsu!dance is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* opsu!dance is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package yugecin.opsudance.utils;
|
||||
|
||||
public class FPSMeter {
|
||||
|
||||
private final int targetTimeBetweenUpdates;
|
||||
|
||||
private int[] measurements;
|
||||
private int timeBetweenUpdates;
|
||||
private int currentMeasureIndex;
|
||||
|
||||
public FPSMeter(int measurements) {
|
||||
targetTimeBetweenUpdates = 1000 / measurements;
|
||||
this.measurements = new int[measurements];
|
||||
}
|
||||
|
||||
public void update(int delta) {
|
||||
timeBetweenUpdates += delta;
|
||||
while (timeBetweenUpdates >= targetTimeBetweenUpdates) {
|
||||
timeBetweenUpdates -= targetTimeBetweenUpdates;
|
||||
measurements[currentMeasureIndex] = 0;
|
||||
currentMeasureIndex = ++currentMeasureIndex % measurements.length;
|
||||
}
|
||||
for (int i = 0; i < measurements.length; i++) {
|
||||
if (i == currentMeasureIndex) {
|
||||
continue;
|
||||
}
|
||||
measurements[i]++;
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return measurements[currentMeasureIndex];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user