Skip to content

Commit

Permalink
Fix FontRenderer compatibility to render items
Browse files Browse the repository at this point in the history
  • Loading branch information
TangyKiwi committed Jan 19, 2025
1 parent b61c86c commit 82c943a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/tangykiwi/kiwiclient/gui/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public void renderBackground(DrawContext drawContext, int mouseX, int mouseY, fl
// smooth
colorOffset = (int) (-(Math.cos(Math.PI * (colorOffset / 50d)) - 1) / 2 * 50);

RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderUtils.setupRender();
RenderSystem.setShader(ShaderProgramKeys.POSITION_COLOR);

BufferBuilder bufferBuilder = RenderSystem.renderThreadTesselator().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
Expand All @@ -35,8 +34,7 @@ public void renderBackground(DrawContext drawContext, int mouseX, int mouseY, fl
bufferBuilder.vertex(width, height + 16, 0).color(170 + colorOffset, 103, 45, 255);
BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());

RenderSystem.defaultBlendFunc();
RenderSystem.disableBlend();
RenderUtils.endRender();
} else {
RenderUtils.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
this.applyBlur();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,9 @@ public void render(DrawContext context, int mouseX, int mouseY) {
boolean blockItem = icon != null && icon.getItem() instanceof BlockItem;

/* window icon */
// if (icon != null) {
// matrixStack.push();
// matrixStack.translate(x + (blockItem ? 3 : 2), y + 1, 0);
// matrixStack.scale(0.6f, 0.6f, 1f);
//
// DiffuseLighting.enableGuiDepthLighting();
// context.drawItem(icon, 0, 0);
// DiffuseLighting.disableGuiDepthLighting();
//
// matrixStack.pop();
// }
if (icon != null) {
RenderUtils.drawItem(context, icon, x + (blockItem ? 3 : 2), y + 1, 0.6f);
}

/* window title */
fontRenderer.drawStringWithShadow(matrixStack, title, x + (icon == null || icon.getItem() == Items.AIR ? 4 : (blockItem ? 15 : 14)), y + 3, -1);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/tangykiwi/kiwiclient/mixin/InGameHudMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tangykiwi.kiwiclient.mixin;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.RenderTickCounter;
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.callback.CallbackInfo;

@Mixin(InGameHud.class)
public class InGameHudMixin {
@Inject(method="renderMainHud", at=@At(value="TAIL"), cancellable=true)
private void render(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci){

}
}
27 changes: 25 additions & 2 deletions src/main/java/com/tangykiwi/kiwiclient/util/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.tangykiwi.kiwiclient.KiwiClient;
import net.minecraft.client.gl.ShaderProgramKeys;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.RotationAxis;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;

import java.awt.*;

import static com.tangykiwi.kiwiclient.KiwiClient.mc;

public class RenderUtils {
public static int getGuiScale() {
return (int) KiwiClient.mc.getWindow().getScaleFactor();
return (int) mc.getWindow().getScaleFactor();
}

public static int getRainbowInt(float seconds, float saturation, float brightness) {
Expand Down Expand Up @@ -157,6 +161,25 @@ public static void fillGradient(int startX, int startY, int endX, int endY, int
endRender();
}

public static void drawItem(DrawContext drawContext, ItemStack itemStack, int x, int y, float scale) {
drawItem(drawContext, itemStack, x, y, scale, false, null);
}

public static void drawItem(DrawContext drawContext, ItemStack itemStack, int x, int y, float scale, boolean overlay, String countOverride) {
MatrixStack matrices = drawContext.getMatrices();
matrices.push();
matrices.scale(scale, scale, 1f);
matrices.translate(0, 0, 401); // Thanks Mojang

int scaledX = (int) (x / scale);
int scaledY = (int) (y / scale);

drawContext.drawItem(itemStack, scaledX, scaledY);
if (overlay) drawContext.drawStackOverlay(mc.textRenderer, itemStack, scaledX, scaledY, countOverride);

matrices.pop();
}

public static void setupRender() {
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
Expand All @@ -172,7 +195,7 @@ public static void endRender() {
public static MatrixStack matrixFrom(double x, double y, double z) {
MatrixStack matrices = new MatrixStack();

Camera camera = KiwiClient.mc.gameRenderer.getCamera();
Camera camera = mc.gameRenderer.getCamera();
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch()));
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(camera.getYaw() + 180.0F));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Preconditions;
import com.mojang.blaze3d.systems.RenderSystem;
import com.tangykiwi.kiwiclient.KiwiClient;
import com.tangykiwi.kiwiclient.util.RenderUtils;
import it.unimi.dsi.fastutil.chars.Char2IntArrayMap;
import it.unimi.dsi.fastutil.chars.Char2ObjectArrayMap;
Expand Down Expand Up @@ -183,11 +184,8 @@ public void drawString(MatrixStack stack, String s, float x, float y, Color colo
stack.translate(x, y, 0);
stack.scale(1f / this.scaleMul, 1f / this.scaleMul, 1f);

RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderUtils.setupRender();
RenderSystem.disableCull();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

RenderSystem.setShader(ShaderProgramKeys.POSITION_TEX_COLOR);
BufferBuilder bb;
Expand Down Expand Up @@ -269,8 +267,7 @@ public void drawString(MatrixStack stack, String s, float x, float y, Color colo
BufferRenderer.drawWithGlobalProgram(bb.end());
}

RenderSystem.defaultBlendFunc();
RenderSystem.disableBlend();
RenderUtils.endRender();
stack.pop();
GLYPH_PAGE_CACHE.clear();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/kiwiclient.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mixins": [
"ChatInputSuggestorMixin",
"ClientPlayNetworkHandlerMixin",
"InGameHudMixin",
"KeyboardMixin",
"MinecraftClientMixin",
"SplashOverlayMixin",
Expand Down

0 comments on commit 82c943a

Please sign in to comment.