Skip to content

Commit f23e4e3

Browse files
Improve dynamic resolution and make it configurable
1 parent 0967e68 commit f23e4e3

4 files changed

Lines changed: 51 additions & 27 deletions

File tree

src/main/java/io/github/ultimateboomer/resolutioncontrol/ResolutionControlMod.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.fabricmc.api.ModInitializer;
77
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
88
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
9+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
910
import net.fabricmc.loader.api.FabricLoader;
1011
import net.minecraft.client.MinecraftClient;
1112
import net.minecraft.client.gl.Framebuffer;
@@ -116,6 +117,12 @@ && getWindow().getX() != -32000) {
116117
}
117118
});
118119

120+
ServerWorldEvents.LOAD.register((server, world) -> {
121+
if (ConfigHandler.instance.getConfig().enableDynamicResolution) {
122+
DynamicResolutionHandler.INSTANCE.reset();
123+
}
124+
});
125+
119126
optifineInstalled = FabricLoader.getInstance().isModLoaded("optifabric");
120127
}
121128

@@ -258,11 +265,11 @@ public void initScreenshotFramebuffer() {
258265
true, MinecraftClient.IS_SYSTEM_MAC);
259266
}
260267

261-
public double getScaleFactor() {
268+
public float getScaleFactor() {
262269
return Config.getInstance().scaleFactor;
263270
}
264271

265-
public void setScaleFactor(double scaleFactor) {
272+
public void setScaleFactor(float scaleFactor) {
266273
Config.getInstance().scaleFactor = scaleFactor;
267274

268275
updateFramebufferSize();

src/main/java/io/github/ultimateboomer/resolutioncontrol/client/gui/screen/MainSettingsScreen.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
public final class MainSettingsScreen extends SettingsScreen {
2020
private static final Identifier backgroundTexture = ResolutionControlMod.identifier("textures/gui/settings.png");
2121

22-
private static final double[] scaleValues = {0.0, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 0.75, 1.0,
23-
1.25, 1.5, 2.0, 3.0, 4.0, 6.0, 8.0};
22+
private static final float[] scaleValues = {0.0f, 0.01f, 0.025f, 0.05f, 0.1f, 0.25f, 0.5f, 0.75f, 1.0f,
23+
1.25f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f, 8.0f};
2424

2525
private static final double redValue = 2.0;
2626

@@ -88,7 +88,7 @@ protected void init() {
8888
if (manualEntry) {
8989
setManualEntry(false, true);
9090
} else {
91-
mod.setScaleFactor(1.0);
91+
mod.setScaleFactor(1.0f);
9292
updateButtons();
9393
}
9494
}
@@ -194,7 +194,7 @@ public void tick() {
194194
}
195195

196196
private void changeScaleFactor(boolean add) {
197-
double currentScale = mod.getScaleFactor();
197+
float currentScale = mod.getScaleFactor();
198198
int nextIndex = ArrayUtils.indexOf(scaleValues, currentScale);
199199
if (nextIndex == -1) {
200200
for (int i = -1; i < scaleValues.length; ++i) {
@@ -236,7 +236,7 @@ public void setManualEntry(boolean manualEntry, boolean cancel) {
236236
if (!cancel) {
237237
String text = entryTextField.getText();
238238
if (NumberUtils.isParsable(text)) {
239-
double value = Math.abs(Double.parseDouble(text));
239+
float value = Math.abs(Float.parseFloat(text));
240240
mod.setScaleFactor(value);
241241
}
242242
}

src/main/java/io/github/ultimateboomer/resolutioncontrol/util/Config.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.ultimateboomer.resolutioncontrol.util;
22

33
public final class Config {
4-
public double scaleFactor = 1;
4+
public float scaleFactor = 1.0f;
55

66
public ScalingAlgorithm upscaleAlgorithm = ScalingAlgorithm.NEAREST;
77
public ScalingAlgorithm downscaleAlgorithm = ScalingAlgorithm.LINEAR;
@@ -16,6 +16,13 @@ public final class Config {
1616
public boolean enableDynamicResolution = false;
1717
public boolean fastDynamicResolution = false;
1818

19+
public float drMinScale = 0.5f;
20+
public float drMaxScale = 2.0f;
21+
public float drResStep = 0.0625f;
22+
public int drMinFps = 60;
23+
public int drMaxFps = 70;
24+
public int drFpsSmoothAmount = 10;
25+
1926
public static Config getInstance() {
2027
return ConfigHandler.instance.getConfig();
2128
}

src/main/java/io/github/ultimateboomer/resolutioncontrol/util/DynamicResolutionHandler.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ public class DynamicResolutionHandler {
1717
private final FloatList scales = new FloatArrayList();
1818
private int baseScale;
1919

20-
private int timer = 0;
20+
private int timer = 10;
2121
private int currentScale;
2222

2323
private int lastWidth;
2424
private int lastHeight;
2525

2626
private DynamicResolutionHandler() {
27-
for (float i = 0.5f; i <= 2.0f; i += 0.125f) {
27+
reset();
28+
}
29+
30+
public void tick() {
31+
timer--;
32+
33+
if (timer <= 0) {
34+
update();
35+
}
36+
}
37+
38+
public void reset() {
39+
for (float i = Config.getInstance().drMinScale; i <= Config.getInstance().drMaxScale;
40+
i += Config.getInstance().drResStep) {
2841
scales.add(i);
2942

3043
if (i == 1.0f) {
@@ -34,29 +47,26 @@ private DynamicResolutionHandler() {
3447
}
3548
}
3649

37-
public void tick() {
38-
timer++;
39-
40-
if (timer > 5) {
41-
timer = 0;
42-
update();
43-
}
44-
}
45-
4650
private void update() {
4751
MinecraftClient client = MinecraftClient.getInstance();
4852

49-
final int smoothAmount = 10;
50-
long accumulatedFrameTime =
51-
Arrays.stream(client.metricsData.getSamples()).limit(smoothAmount).sum();
53+
final int smoothAmount = Config.getInstance().drFpsSmoothAmount;
54+
float sum = 0;
55+
for (int i = client.metricsData.getCurrentIndex() - smoothAmount;
56+
i < client.metricsData.getCurrentIndex(); i++) {
5257

53-
double fps = 1_000_000_000.0 / accumulatedFrameTime * smoothAmount;
54-
System.out.println(fps);
58+
sum += client.metricsData.getSamples()[Math.floorMod(i, 240)];
59+
}
60+
float fps = 1_000_000_000.0f / (sum / smoothAmount);
5561

56-
if (fps > 80) {
62+
if (fps > Config.getInstance().drMaxFps) {
5763
setCurrentScale(Math.min(currentScale + 1, scales.size() - 1));
58-
} else if (fps < 60) {
59-
setCurrentScale(Math.max(currentScale - 2, 0));
64+
timer = 15;
65+
} else if (fps < Config.getInstance().drMinFps) {
66+
setCurrentScale(Math.max(currentScale - 1, 0));
67+
timer = 5;
68+
} else {
69+
timer = 3;
6070
}
6171
}
6272

0 commit comments

Comments
 (0)