Actually fixed FPS cap

This commit is contained in:
Anuken
2019-07-03 19:11:22 -04:00
parent 0d39400f8b
commit 77a41b1783
2 changed files with 23 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import static io.anuke.arc.Core.batch;
import static io.anuke.mindustry.Vars.*;
public class Mindustry extends ApplicationCore{
private long lastTime;
@Override
public void setup(){
@ -52,6 +53,27 @@ public class Mindustry extends ApplicationCore{
}));
}
@Override
public void update(){
super.update();
int targetfps = Core.settings.getInt("fpscap", 120);
if(targetfps > 0 && targetfps <= 120){
long target = (1000 * 1000000) / targetfps; //target in nanos
long elapsed = Time.timeSinceNanos(lastTime);
if(elapsed < target){
try{
Thread.sleep((target - elapsed) / 1000000, (int)((target - elapsed) % 1000000));
}catch(InterruptedException ignored){
//ignore
}
}
}
lastTime = Time.nanos();
}
@Override
public void init(){
setup();

View File

@ -172,15 +172,9 @@ public class SettingsMenuDialog extends SettingsDialog{
}
});
graphics.sliderPref("fpscap", 125, 5, 125, 5,
s -> {
Core.app.setTargetFPS(s > 120 ? -1 : s);
return (s > 120 ? Core.bundle.get("setting.fpscap.none") : Core.bundle.format("setting.fpscap.text", s));
});
graphics.sliderPref("fpscap", 125, 5, 125, 5, s -> (s > 120 ? Core.bundle.get("setting.fpscap.none") : Core.bundle.format("setting.fpscap.text", s)));
graphics.sliderPref("chatopacity", 100, 0, 100, 5, s -> s + "%");
Core.app.setTargetFPS(Core.settings.getInt("fpscap", 125) > 120 ? -1 : Core.settings.getInt("fpscap"));
if(!mobile){
graphics.checkPref("vsync", true, b -> Core.graphics.setVSync(b));
graphics.checkPref("fullscreen", false, b -> {