Skip to content

Commit d6878b4

Browse files
committed
Greatly simplify font handling.
By using GL_SCISSOR_TEST, the scrolling text can be clipped without having to rewrite font rendering code. As a result, ClippedFontRenderer is no longer needed, and compatibility with mods like OptiFine and SmoothFont will be greatly improved.
1 parent 298706b commit d6878b4

File tree

2 files changed

+14
-184
lines changed

2 files changed

+14
-184
lines changed

java/io/github/d0sboots/enchantmentrevealer/ClippedFontRenderer.java

-174
This file was deleted.

java/io/github/d0sboots/enchantmentrevealer/GuiEnchantmentWrapper.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919

20+
import org.lwjgl.opengl.GL11;
21+
2022
import net.minecraft.client.gui.FontRenderer;
2123
import net.minecraft.client.gui.GuiEnchantment;
2224
import net.minecraft.client.resources.I18n;
@@ -36,7 +38,6 @@ public class GuiEnchantmentWrapper extends GuiEnchantment {
3638
private static final FontRenderer dummyFontRenderer = new DummyFontRenderer();
3739
private static final long SCROLL_PERIOD_MS = 5000;
3840
private static final long SCROLL_PAUSE_MS = 1000;
39-
private static ClippedFontRenderer clippedRenderer = null;
4041

4142
private final EnchantmentWorker worker;
4243
private final InventoryPlayer inventory;
@@ -83,17 +84,19 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
8384
if (lastState.observation != ((ContainerEnchantmentWrapper) inventorySlots).lastObservation) {
8485
return; // Out-of-sync, happens when the GUI is closed with an item still present
8586
}
87+
FontRenderer renderer = mc.fontRenderer;
8688

87-
if (clippedRenderer == null) {
88-
clippedRenderer = new ClippedFontRenderer(mc.fontRenderer);
89-
}
90-
89+
// Use the scissor test to clip scrolling/oversize text to within the GUI. We have to
90+
// rescale to get screen coordinates from GUI (scaled) coordinates.
9191
int midX = (width - xSize) / 2;
9292
int midY = (height - ySize) / 2;
9393
final int leftBound = 78;
9494
final int rightBound = 166;
95-
clippedRenderer.clipMinX = midX + leftBound;
96-
clippedRenderer.clipMaxX = midX + rightBound;
95+
int clipMinX = (midX + leftBound) * mc.displayWidth / width;
96+
int scissorWidth = (rightBound - leftBound) * mc.displayWidth / width;
97+
GL11.glEnable(GL11.GL_SCISSOR_TEST);
98+
GL11.glScissor(clipMinX, 0, scissorWidth, mc.displayHeight);
99+
97100
float scrollFraction = getScrollFraction();
98101
for (int i = 0; i < 3; ++i) {
99102
String[] enchants = lastState.enchants[i];
@@ -102,10 +105,10 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
102105
}
103106

104107
String ench = enchants[0];
105-
int stringWidth = clippedRenderer.getStringWidth(ench);
108+
int stringWidth = renderer.getStringWidth(ench);
106109
float adjust = (stringWidth <= rightBound - leftBound ? 0.5F : scrollFraction)
107110
* (rightBound - leftBound - stringWidth);
108-
clippedRenderer.drawString(ench, midX + leftBound + adjust,
111+
renderer.drawString(ench, midX + leftBound + adjust,
109112
midY + 15 + 19 * i, 0x222222, false);
110113

111114
int[] enchantCounts = lastState.counts[i];
@@ -125,9 +128,10 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
125128
int total = (int) (((acc * 200L + 1) / enchantCounts[0]) >>> 1);
126129
plusX = String.format("%d.%02d", total / 100, total % 100);
127130
}
128-
clippedRenderer.drawString(I18n.format("enchantmentrevealer.text.plusx", plusX),
131+
renderer.drawString(I18n.format("enchantmentrevealer.text.plusx", plusX),
129132
midX + leftBound + 2, midY + 24 + 19 * i, 0x222222);
130133
}
134+
GL11.glDisable(GL11.GL_SCISSOR_TEST);
131135
}
132136

133137
private float getScrollFraction() {

0 commit comments

Comments
 (0)