|
| 1 | +/* |
| 2 | + * Copyright © 2021-2022 moehreag <[email protected]> & Contributors |
| 3 | + * |
| 4 | + * This file is part of AxolotlClient. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + * |
| 20 | + * For more information, see the LICENSE file. |
| 21 | + */ |
| 22 | + |
| 23 | +package io.github.axolotlclient.modules.unfocusedFpsLimiter; |
| 24 | + |
| 25 | +import java.util.concurrent.locks.LockSupport; |
| 26 | + |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | +import org.lwjgl.input.Mouse; |
| 29 | +import org.lwjgl.opengl.Display; |
| 30 | + |
| 31 | +import io.github.axolotlclient.AxolotlClient; |
| 32 | +import io.github.axolotlclient.AxolotlclientConfig.options.BooleanOption; |
| 33 | +import io.github.axolotlclient.AxolotlclientConfig.options.FloatOption; |
| 34 | +import io.github.axolotlclient.AxolotlclientConfig.options.IntegerOption; |
| 35 | +import io.github.axolotlclient.AxolotlclientConfig.options.OptionCategory; |
| 36 | +import io.github.axolotlclient.modules.AbstractModule; |
| 37 | +import lombok.Getter; |
| 38 | +import net.minecraft.client.MinecraftClient; |
| 39 | +import net.minecraft.client.sound.SoundCategory; |
| 40 | + |
| 41 | +/** |
| 42 | + * This module is based on the mod DynamicFps by juliand665. |
| 43 | + * <p>Original License: MIT</p> |
| 44 | + * <a href="https://github.com/juliand665/dynamic-fps">Original Source Github</a> |
| 45 | + */ |
| 46 | + |
| 47 | +public class UnfocusedFpsLimiter extends AbstractModule { |
| 48 | + |
| 49 | + @Getter |
| 50 | + private static final UnfocusedFpsLimiter Instance = new UnfocusedFpsLimiter(); |
| 51 | + |
| 52 | + private final BooleanOption enabled = new BooleanOption("axolotlclient.enabled", false); |
| 53 | + private final BooleanOption reduceFPSWhenUnfocused = new BooleanOption("axolotlclient.reduceFPS", false); |
| 54 | + private final IntegerOption unfocusedFPS = new IntegerOption("axolotlclient.unfocusedFPS", 10, 0, 60); |
| 55 | + private final BooleanOption restoreOnHover = new BooleanOption("axolotlclient.restoreOnHover", true); |
| 56 | + private final FloatOption unfocusedVolumeMultiplier = new FloatOption("axolotlclient.unfocusedVolumeMultiplier", 0.25f, 0f, 1f); |
| 57 | + private final FloatOption hiddenVolumeMultiplier = new FloatOption("axolotlclient.hiddenVolumeMultiplier", 0f, 0f, 1f); |
| 58 | + private final BooleanOption runGCOnUnfocus = new BooleanOption("axolotlclient.runGCOnUnfocus", false); |
| 59 | + |
| 60 | + @Override |
| 61 | + public void init() { |
| 62 | + OptionCategory category = new OptionCategory("axolotlclient.fpsLimiter"); |
| 63 | + category.add(enabled, reduceFPSWhenUnfocused, unfocusedFPS, restoreOnHover, unfocusedVolumeMultiplier, hiddenVolumeMultiplier, runGCOnUnfocus); |
| 64 | + AxolotlClient.CONFIG.rendering.add(category); |
| 65 | + } |
| 66 | + |
| 67 | + private boolean isFocused, isVisible, isHovered; |
| 68 | + private long lastRender; |
| 69 | + |
| 70 | + public boolean checkForRender(){ |
| 71 | + |
| 72 | + if(!enabled.get()){ |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + isFocused = Display.isActive(); |
| 77 | + isVisible = Display.isVisible(); |
| 78 | + isHovered = Mouse.isInsideWindow(); |
| 79 | + |
| 80 | + checkForStateChanges(); |
| 81 | + |
| 82 | + long currentTime = MinecraftClient.getTime(); |
| 83 | + long timeSinceLastRender = currentTime - lastRender; |
| 84 | + |
| 85 | + if (!checkForRender(timeSinceLastRender)) return false; |
| 86 | + |
| 87 | + lastRender = currentTime; |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean wasFocused = true; |
| 92 | + private boolean wasVisible = true; |
| 93 | + |
| 94 | + private void checkForStateChanges() { |
| 95 | + if (isFocused != wasFocused) { |
| 96 | + wasFocused = isFocused; |
| 97 | + if (isFocused) { |
| 98 | + onFocus(); |
| 99 | + } else { |
| 100 | + onUnfocus(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (isVisible != wasVisible) { |
| 105 | + wasVisible = isVisible; |
| 106 | + if (isVisible) { |
| 107 | + onAppear(); |
| 108 | + } else { |
| 109 | + onDisappear(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void onFocus() { |
| 115 | + setVolumeMultiplier(1); |
| 116 | + } |
| 117 | + |
| 118 | + private void onUnfocus() { |
| 119 | + if (isVisible) { |
| 120 | + setVolumeMultiplier(unfocusedVolumeMultiplier.get()); |
| 121 | + } |
| 122 | + |
| 123 | + if (runGCOnUnfocus.get()) { |
| 124 | + System.gc(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void onAppear() { |
| 129 | + if (!isFocused) { |
| 130 | + setVolumeMultiplier(unfocusedVolumeMultiplier.get()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void onDisappear() { |
| 135 | + setVolumeMultiplier(hiddenVolumeMultiplier.get()); |
| 136 | + } |
| 137 | + |
| 138 | + private void setVolumeMultiplier(float multiplier) { |
| 139 | + // setting the volume to 0 stops all sounds (including music), which we want to avoid if possible. |
| 140 | + boolean clientWillPause = !isFocused && client.options.pauseOnLostFocus && client.currentScreen == null; |
| 141 | + // if the client would pause anyway, we don't need to do anything because that will already pause all sounds. |
| 142 | + if (multiplier == 0 && clientWillPause) return; |
| 143 | + |
| 144 | + float baseVolume = MinecraftClient.getInstance().options.getSoundVolume(SoundCategory.MASTER); |
| 145 | + MinecraftClient.getInstance().getSoundManager().updateSoundVolume( |
| 146 | + SoundCategory.MASTER, |
| 147 | + baseVolume * multiplier |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + // we always render one last frame before actually reducing FPS, so the hud text shows up instantly when forcing low fps. |
| 152 | + // additionally, this would enable mods which render differently while mc is inactive. |
| 153 | + private boolean hasRenderedLastFrame = false; |
| 154 | + private boolean checkForRender(long timeSinceLastRender) { |
| 155 | + Integer fpsOverride = fpsOverride(); |
| 156 | + if (fpsOverride == null) { |
| 157 | + hasRenderedLastFrame = false; |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + if (!hasRenderedLastFrame) { |
| 162 | + // render one last frame before reducing, to make sure differences in this state show up instantly. |
| 163 | + hasRenderedLastFrame = true; |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + if (fpsOverride == 0) { |
| 168 | + idle(1000); |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + long frameTime = 1000 / fpsOverride; |
| 173 | + boolean shouldSkipRender = timeSinceLastRender < frameTime; |
| 174 | + if (!shouldSkipRender) return true; |
| 175 | + |
| 176 | + idle(frameTime); |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + force minecraft to idle because otherwise we'll be busy checking for render again and again |
| 182 | + */ |
| 183 | + private void idle(long waitMillis) { |
| 184 | + // cap at 30 ms before we check again so user doesn't have to wait long after tabbing back in |
| 185 | + waitMillis = Math.min(waitMillis, 30); |
| 186 | + LockSupport.parkNanos("waiting to render", waitMillis * 1_000_000); |
| 187 | + } |
| 188 | + |
| 189 | + @Nullable |
| 190 | + private Integer fpsOverride() { |
| 191 | + if (!isVisible) return 0; |
| 192 | + if (restoreOnHover.get() && isHovered) return null; |
| 193 | + if (reduceFPSWhenUnfocused.get() && !Display.isActive()) return unfocusedFPS.get(); |
| 194 | + return null; |
| 195 | + } |
| 196 | +} |
0 commit comments