Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vsync option #205

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions platforms/common/src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public static int targetFrameRate() {
return config.frameRateTarget();
}

public static boolean enableVsync() {
return config.enableVsync();
}

public static float volumeMultiplier(SoundSource source) {
return config.volumeMultiplier(source);
}
Expand Down Expand Up @@ -237,6 +241,13 @@ public static void handleStateChange(PowerState previous, PowerState current) {

OptionHolder.applyOptions(minecraft.options, config.graphicsState());
}

// The FOCUSED config doesn't have the user's actual vsync preference sadly ...
boolean enableVsync = current != PowerState.FOCUSED ? config.enableVsync() : minecraft.options.enableVsync().get();

if (enableVsync != before.enableVsync()) {
minecraft.getWindow().updateVsync(enableVsync);
}
}

private static void checkForStateChanges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ public static Screen genConfigScreen(Screen parent) {
.build()
);

category.addEntry(
entryBuilder.startBooleanToggle(
Component.translatable("options.vsync"),
instance.enableVsync()
)
.setDefaultValue(standard.enableVsync())
.setSaveConsumer(instance::setEnableVsync)
.build()
);

// Further options are not allowed since this state is used while active.
if (state.configurabilityLevel == PowerState.ConfigurabilityLevel.SOME) {
continue;
Expand Down
14 changes: 12 additions & 2 deletions platforms/common/src/main/java/dynamic_fps/impl/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

public class Config {
private int frameRateTarget;
private boolean enableVsync;
private final Map<String, Float> volumeMultipliers;
private GraphicsState graphicsState;
private boolean showToasts;
private boolean runGarbageCollector;

protected transient PowerState state; // Set by main config, allows retrieving values from the default power state config

public static final Config ACTIVE = new Config(-1, new HashMap<>(), GraphicsState.DEFAULT, true, false);
public static final Config ACTIVE = new Config(-1, false, new HashMap<>(), GraphicsState.DEFAULT, true, false);

public Config(int frameRateTarget, Map<String, Float> volumeMultipliers, GraphicsState graphicsState, boolean showToasts, boolean runGarbageCollector) {
public Config(int frameRateTarget, boolean enableVsync, Map<String, Float> volumeMultipliers, GraphicsState graphicsState, boolean showToasts, boolean runGarbageCollector) {
this.frameRateTarget = frameRateTarget;
this.enableVsync = enableVsync;
this.volumeMultipliers = new HashMap<>(volumeMultipliers); // Ensure the map is mutable
this.graphicsState = graphicsState;
this.showToasts = showToasts;
Expand All @@ -44,6 +46,14 @@ public void setFrameRateTarget(int value) {
}
}

public boolean enableVsync() {
return this.enableVsync;
}

public void setEnableVsync(boolean value) {
this.enableVsync = value;
}

public float volumeMultiplier(SoundSource source) {
if (this.rawVolumeMultiplier(SoundSource.MASTER) == 0.0f) {
return 0.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ private List<String> getGameInformation(List<String> result) {
PowerState status = DynamicFPSMod.powerState();

if (status != PowerState.FOCUSED) {
int target = DynamicFPSMod.targetFrameRate();
String fps = target == Constants.NO_FRAME_RATE_LIMIT ? "inf" : Integer.toString(target);
int fps = DynamicFPSMod.targetFrameRate();

String vsync = DynamicFPSMod.enableVsync() ? " vsync": "";
String target = fps == Constants.NO_FRAME_RATE_LIMIT ? "inf" : Integer.toString(fps);

result.add(
2,
this.format("§c[Dynamic FPS] FPS: %s P: %s§r", fps, status.toString().toLowerCase())
this.format("§c[Dynamic FPS] FPS: %s%s P: %s§r", target, vsync, status.toString().toLowerCase())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
"states": {
"hovered": {
"frame_rate_target": 60,
"enable_vsync": "false",
"volume_multipliers": {},
"graphics_state": "default",
"show_toasts": true,
"run_garbage_collector": false
},
"unfocused": {
"frame_rate_target": 1,
"enable_vsync": "false",
"volume_multipliers": {
"master": 0.25
},
Expand All @@ -37,6 +39,7 @@
},
"invisible": {
"frame_rate_target": 0,
"enable_vsync": "false",
"volume_multipliers": {
"master": 0.0
},
Expand All @@ -45,14 +48,16 @@
"run_garbage_collector": false
},
"unplugged": {
"frame_rate_target": 60,
"frame_rate_target": -1,
"enable_vsync": "true",
"volume_multipliers": {},
"graphics_state": "default",
"show_toasts": true,
"run_garbage_collector": false
},
"abandoned": {
"frame_rate_target": 10,
"enable_vsync": "false",
"volume_multipliers": {},
"graphics_state": "default",
"show_toasts": false,
Expand Down