Skip to content

Commit

Permalink
Register input observer only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Jan 3, 2024
1 parent 81efdca commit a223ead
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
26 changes: 16 additions & 10 deletions src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import static dynamic_fps.impl.util.Localization.translationKey;

import org.jetbrains.annotations.Nullable;

public class DynamicFPSMod implements ClientModInitializer {
public static final String MOD_ID = "dynamic_fps";
public static final boolean DEBUG = FabricLoader.getInstance().isDevelopmentEnvironment();
Expand All @@ -35,13 +37,13 @@ public class DynamicFPSMod implements ClientModInitializer {

private static Minecraft minecraft;

private static WindowObserver window;
private static InputObserver devices;
private static @Nullable WindowObserver window;
private static @Nullable InputObserver devices;

private static long lastRender;

private static boolean wasIdle = false;
private static boolean tickEventRegistered = false;
private static boolean idleCheckRegistered = false;

// we always render one last frame before actually reducing FPS, so the hud text
// shows up instantly when forcing low fps.
Expand Down Expand Up @@ -72,7 +74,7 @@ public void onInitializeClient() {
toggleForcedKeyBinding.register();
toggleDisabledKeyBinding.register();

registerTickEvent();
initializeIdleCheck();
HudRenderCallback.EVENT.register(new HudInfoRenderer());
}

Expand All @@ -88,7 +90,7 @@ public static void onStatusChanged() {

public static void onConfigChanged() {
modConfig.save();
registerTickEvent();
initializeIdleCheck();
}

public static PowerState powerState() {
Expand All @@ -101,7 +103,7 @@ public static boolean isForcingLowFPS() {

public static void setWindow(long address) {
window = new WindowObserver(address);
devices = new InputObserver(address);
initializeIdleCheck(); // Register input observer if wanted
}

public static boolean checkForRender() {
Expand Down Expand Up @@ -156,16 +158,20 @@ private static boolean isLevelCoveredByOverlay() {
return OVERLAY_OPTIMIZATION_ACTIVE && minecraft.getOverlay() instanceof LoadingOverlay loadingOverlay && loadingOverlay.dynamic_fps$isReloadComplete();
}

private static void registerTickEvent() {
if (tickEventRegistered) {
private static void initializeIdleCheck() {
if (idleCheckRegistered) {
return;
}

if (modConfig.idleTime() == 0) {
if (modConfig.idleTime() == 0 || window == null) {
return;
}

tickEventRegistered = true;
idleCheckRegistered = true;

// We only register the input observer and tick handler
// When it's used to run less unused code at all times.
devices = new InputObserver(window.address());

ClientTickEvents.START_CLIENT_TICK.register((minecraft) -> {
var idle = isIdle();
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/dynamic_fps/impl/util/event/WindowObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dynamic_fps.impl.DynamicFPSMod;

public class WindowObserver {
private final long window;
private final long address;

private boolean isFocused = true;
private final GLFWWindowFocusCallback previousFocusCallback;
Expand All @@ -20,17 +20,21 @@ public class WindowObserver {
private final GLFWWindowIconifyCallback previousIconifyCallback;

public WindowObserver(long address) {
this.window = address;
this.address = address;

this.previousFocusCallback = GLFW.glfwSetWindowFocusCallback(this.window, this::onFocusChanged);
this.previousMouseCallback = GLFW.glfwSetCursorEnterCallback(this.window, this::onMouseChanged);
this.previousFocusCallback = GLFW.glfwSetWindowFocusCallback(this.address, this::onFocusChanged);
this.previousMouseCallback = GLFW.glfwSetCursorEnterCallback(this.address, this::onMouseChanged);

// Vanilla doesn't use this (currently), other mods might register this callback though ...
this.previousIconifyCallback = GLFW.glfwSetWindowIconifyCallback(this.window, this::onIconifyChanged);
this.previousIconifyCallback = GLFW.glfwSetWindowIconifyCallback(this.address, this::onIconifyChanged);
}

private boolean isCurrentWindow(long address) {
return address == this.window;
return address == this.address;
}

public long address() {
return this.address;
}

public boolean isFocused() {
Expand Down

0 comments on commit a223ead

Please sign in to comment.