Skip to content

Commit

Permalink
enable HiDPI support on legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Nov 29, 2024
1 parent c830f6c commit 0df0214
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name=OneConfig
mod_id=oneconfig
version_major=1
version_minor=0
version_patch=0-alpha.39
version_patch=0-alpha.40

polyfrost.defaults.loom=3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class OCKeybindHelper : KeybindHelper() {
) else super.build()
}

fun inScreens(): OCKeybindHelper {
inScreens = true
return this
}

fun register() = build().register()

fun KeyBinder.Bind.register() = KeybindManager.registerKeybind(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public int windowWidth() {
//#if MC>=11502
//$$ return Minecraft.getInstance().getMainWindow().getWidth();
//#else
return Minecraft.getMinecraft().displayWidth;
return (int) (Minecraft.getMinecraft().displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor());
//#endif
}

Expand All @@ -105,7 +105,7 @@ public int windowHeight() {
//#if MC>=11502
//$$ return Minecraft.getInstance().getMainWindow().getHeight();
//#else
return Minecraft.getMinecraft().displayHeight;
return (int) (Minecraft.getMinecraft().displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor());
//#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private static void registerCommands() {

private static void registerKeybinds() {
OCKeybindHelper builder = OCKeybindHelper.builder();
if (Platform.loader().isDevelopmentEnvironment()) builder.inScreens();
builder.mods(KeyModifiers.RSHIFT).does((s) -> {
if (s) OneConfigUI.INSTANCE.open();
return Unit.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public List<String> getMixins() {
} else {
// legacy
mixins.add("GuiScreenMixin");

mixins.add("hidpi.ScaledResolutionMixin");
mixins.add("hidpi.EntityRendererMixin");
mixins.add("hidpi.LoadingScreenRendererMixin");
}

return mixins;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
package org.polyfrost.oneconfig.internal.mixin;
//#if MC<=11202

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.polyfrost.oneconfig.api.event.v1.EventManager;
import org.polyfrost.oneconfig.api.event.v1.events.KeyInputEvent;
import org.polyfrost.oneconfig.api.event.v1.events.MouseInputEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GuiScreen.class)
Expand Down Expand Up @@ -78,5 +81,18 @@ public abstract class GuiScreenMixin {
//$$ }
//$$ }
//#endif


// HiDPI fixes

@Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0))
private int hiDpiFixMouseX(Minecraft mc) {
return (int) (mc.displayWidth / Display.getPixelScaleFactor());
}

@Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0))
private int hiDpiFixMouseY(Minecraft mc) {
return (int) (mc.displayHeight / Display.getPixelScaleFactor());
}
}
//#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

//#if FORGE
Expand All @@ -50,6 +48,11 @@ public abstract class MinecraftMixin {
@Shadow
private Timer timer;

@Shadow public int displayWidth;
@Shadow public int displayHeight;
@Shadow private int tempDisplayWidth;
@Shadow private int tempDisplayHeight;

//@formatter:off
@Unique
private static final String UPDATE_CAMERA_AND_RENDER =
Expand All @@ -75,7 +78,7 @@ public abstract class MinecraftMixin {
}

//#if MC<=11300
@Inject(method = "resize", at = @At("HEAD"))
@Inject(method = "resize", at = @At("TAIL"))
private void ocfg$resizeCallback(int width, int height, CallbackInfo ci) {
EventManager.INSTANCE.post(new ResizeEvent(width, height));
}
Expand Down Expand Up @@ -210,4 +213,37 @@ public abstract class MinecraftMixin {
}

//#endif

// HiDPI fixes

//#if MC<11300
@Inject(method = "startGame", at = @At("HEAD"))
private void hiDpiFixInit(CallbackInfo ci) {
System.setProperty("org.lwjgl.opengl.Display.enableHighDPI", "true");
}

@ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 0, argsOnly = true)
private int hiDpiFixResizeW(int value) {
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
}

@ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 1, argsOnly = true)
private int hiDpiFixResizeH(int value) {
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
}

@ModifyVariable(method = "drawSplashScreen", at = @At("STORE"), ordinal = 0)
private int hiDpiFixSplashScale(int value) {
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
}

@Inject(method = "startGame", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;createDisplay()V", shift = At.Shift.AFTER))
private void hiDpiFixDisplaySizes(CallbackInfo ci) {
float scale = org.lwjgl.opengl.Display.getPixelScaleFactor();
this.displayWidth = (int) (this.displayWidth * scale);
this.displayHeight = (int) (this.displayHeight * scale);
this.tempDisplayWidth = (int) (this.tempDisplayWidth * scale);
this.tempDisplayHeight = (int) (this.tempDisplayHeight * scale);
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021~2024 Polyfrost.
* <https://polyfrost.org> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.org/legal/oneconfig/additional-terms>
*/

package org.polyfrost.oneconfig.internal.mixin.hidpi;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.EntityRenderer;
import org.lwjgl.opengl.Display;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EntityRenderer.class)
public abstract class EntityRendererMixin {
@Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0))
private int hiDpiFixMouseX(Minecraft mc) {
return (int) (mc.displayWidth / Display.getPixelScaleFactor());
}

@Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0))
private int hiDpiFixMouseY(Minecraft mc) {
return (int) (mc.displayHeight / Display.getPixelScaleFactor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021~2024 Polyfrost.
* <https://polyfrost.org> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.org/legal/oneconfig/additional-terms>
*/

package org.polyfrost.oneconfig.internal.mixin.hidpi;

import net.minecraft.client.LoadingScreenRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(LoadingScreenRenderer.class)
public abstract class LoadingScreenRendererMixin {
@ModifyVariable(method = "setLoadingProgress", at = @At("STORE"), ordinal = 1)
private int hiDpiFixSplashScale(int value) {
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021~2024 Polyfrost.
* <https://polyfrost.org> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.org/legal/oneconfig/additional-terms>
*/

package org.polyfrost.oneconfig.internal.mixin.hidpi;

//#if MC<11300
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(ScaledResolution.class)
public abstract class ScaledResolutionMixin {
@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", opcode = Opcodes.GETFIELD))
private int hiDpiFixWidth(Minecraft mc) {
return (int) (mc.displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor());
}

@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", opcode = Opcodes.GETFIELD))
private int hiDpiFixHeight(Minecraft mc) {
return (int) (mc.displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor());
}
}
//#endif

0 comments on commit 0df0214

Please sign in to comment.