Skip to content

Commit

Permalink
opengl is actual hell
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Dec 22, 2024
1 parent a28024e commit f5e7513
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
41 changes: 36 additions & 5 deletions src/main/java/org/polyfrost/crosshair/mixin/GuiIngameMixin.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.gui.Gui;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.GuiIngameForge;
import org.lwjgl.opengl.GL11;
import org.polyfrost.crosshair.CrosshairHUD;
import org.polyfrost.oneconfig.api.platform.v1.Platform;
import org.polyfrost.polyui.color.PolyColor;
import org.polyfrost.polyui.component.Drawable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(GuiIngameForge.class)
public abstract class GuiIngameMixin {
@Shadow
private ScaledResolution res;

@Redirect(method = "renderCrosshairs", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;bind(Lnet/minecraft/util/ResourceLocation;)V"), remap = false)
private void bindTexture(GuiIngameForge it, ResourceLocation res) {
if (CrosshairHUD.INSTANCE.getUseVanilla()) bind(res);
else GlStateManager.bindTexture(CrosshairHUD.INSTANCE.getId());
}

@WrapWithCondition(method = "renderCrosshairs", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;tryBlendFuncSeparate(IIII)V", ordinal = 0))
private boolean blendFunc(int srcFactor, int dstFactor, int srcFactorAlpha, int dstFactorAlpha) {
return CrosshairHUD.INSTANCE.getUseVanillaBlending();
}

@Redirect(method = "renderCrosshairs", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;drawTexturedModalRect(IIIIII)V"))
private void drawCrosshair(GuiIngameForge it, int x, int y, int u, int v, int w, int h) {
final float tex = CrosshairHUD.INSTANCE.getUseVanilla() ? 256f : CrosshairHUD.INSTANCE.getWidth();
// #optimize-fold-zeroes
Gui.drawModalRectWithCustomSizedTexture(x, y, 0f, 0f, w, h, tex, tex);
private void drawCrosshair(GuiIngameForge it, int xIn, int yIn, int u, int v, int wIn, int hIn) {
Drawable wrapper = CrosshairHUD.INSTANCE.get();
float scale = Platform.screen().pixelRatio() / res.getScaleFactor();
double size = 15f * wrapper.getScaleX();
double x = wrapper.getX() * scale;
double y = wrapper.getY() * scale;
double tex = CrosshairHUD.INSTANCE.getUseVanilla() ? 15d / 256d : 1d;

PolyColor color = CrosshairHUD.INSTANCE.getColor();
GL11.glColor4ub((byte) color.red(), (byte) color.green(), (byte) color.blue(), (byte) color.alpha());
Tessellator tessellator = Tessellator.getInstance();
WorldRenderer worldRenderer = tessellator.getWorldRenderer();
worldRenderer.begin(7, DefaultVertexFormats.POSITION_TEX);
worldRenderer.pos(x, y + size, 10d).tex(0d, tex).endVertex();
worldRenderer.pos(x + size, y + size, 10d).tex(tex, tex).endVertex();
worldRenderer.pos(x + size, y, 10d).tex(tex, 0d).endVertex();
worldRenderer.pos(x, y, 10d).tex(0d, 0d).endVertex();
tessellator.draw();
GL11.glColor4f(1f, 1f, 1f, 1f);
}

@Shadow(remap = false)
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/org/polyfrost/crosshair/CrosshairHUD.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package org.polyfrost.crosshair

import net.minecraft.client.renderer.texture.TextureUtil
import org.polyfrost.oneconfig.api.config.v1.annotations.Button
import org.polyfrost.oneconfig.api.config.v1.annotations.Color
import org.polyfrost.oneconfig.api.config.v1.annotations.Include
import org.polyfrost.oneconfig.api.config.v1.annotations.Switch
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud
import org.polyfrost.polyui.color.rgba
import org.polyfrost.polyui.unit.Vec2
import org.polyfrost.polyui.utils.getResourceStream
import org.polyfrost.universal.UMatrixStack
Expand All @@ -19,6 +21,9 @@ object CrosshairHUD : LegacyHud() {
@Switch(title = "Use Vanilla")
var useVanilla = false

@Switch(title = "Use Vanilla Blending")
var useVanillaBlending = true

@Switch(title = "Show in F3")
var showInDebug = false

Expand All @@ -28,14 +33,18 @@ object CrosshairHUD : LegacyHud() {
@Switch(title = "Show in 3rd Person")
var showInThirdPerson = true

@Color(title = "Color")
var color = rgba(255, 255, 255)

@Include
var currentCrosshair: String? = null

@Button(title = "Open Editor")
fun openEditor() { PolyCrosshairUI.open() }

val id = GL.generateTexture()
private var texSize = 15f
var texSize = 15f
private set

override var width: Float
get() = texSize
Expand Down

0 comments on commit f5e7513

Please sign in to comment.