Fixed MatrixStack leak on RenderScope 1.21.6 #59

This commit is contained in:
2026-03-25 21:04:53 +01:00
parent 9424e2ec71
commit 3a71cd5b02
2 changed files with 25 additions and 51 deletions
@@ -1,76 +1,44 @@
package org.saturnclient.impl.ref; package org.saturnclient.impl.ref;
import java.util.ArrayDeque;
import java.util.Deque;
import org.joml.Matrix3x2fStack; import org.joml.Matrix3x2fStack;
import org.saturnclient.common.ref.render.MatrixStackRef; import org.saturnclient.common.ref.render.MatrixStackRef;
public class Matrix3x2fStackRef implements MatrixStackRef { public class Matrix3x2fStackRef implements MatrixStackRef {
public final Matrix3x2fStack stack; public final Matrix3x2fStack stack;
// Our custom stack stores the reverse transforms
private final Deque<Transform> customStack = new ArrayDeque<>();
public Matrix3x2fStackRef(Matrix3x2fStack stack) { public Matrix3x2fStackRef(Matrix3x2fStack stack) {
this.stack = stack; this.stack = stack;
} }
@Override @Override
public void push() { public void push() {
// Push a new empty Transform stack.pushMatrix();
customStack.push(new Transform());
} }
@Override @Override
public void pop() { public void pop() {
if (customStack.isEmpty()) stack.popMatrix();
return;
// Apply inverse transforms in reverse order
Transform t = customStack.pop();
if (t.scaleX != 1 || t.scaleY != 1)
stack.scale(1f / t.scaleX, 1f / t.scaleY);
stack.translate(-t.translateX, -t.translateY);
stack.rotate(-t.rotation);
} }
@Override @Override
public void scale(float x, float y) { public void scale(float x, float y) {
stack.scale(x, y); stack.scale(x, y);
if (!customStack.isEmpty()) {
Transform t = customStack.peek();
t.scaleX *= x;
t.scaleY *= y;
}
} }
@Override @Override
public void translate(float x, float y) { public void translate(float x, float y) {
stack.translate(x, y); stack.translate(x, y);
if (!customStack.isEmpty()) {
Transform t = customStack.peek();
t.translateX += x;
t.translateY += y;
}
} }
@Override @Override
public void rotate(float angle) { public void rotate(float angle) {
stack.rotate(angle); stack.rotate(angle);
if (!customStack.isEmpty()) {
Transform t = customStack.peek();
t.rotation += angle;
}
} }
@Override @Override
public void rotate(float angle, float originX, float originY, float originZ) { public void rotate(float angle, float originX, float originY, float originZ) {
} translate(originX, originY);
rotate(angle);
private static class Transform { translate(-originX, -originY);
float scaleX = 1f, scaleY = 1f;
float translateX = 0f, translateY = 0f;
float rotation = 0f;
} }
} }
@@ -56,8 +56,8 @@ public class RenderScopeImpl implements RenderScope {
} }
public RenderScopeImpl(Matrix3x2fStack matrices, GuiRenderState state) { public RenderScopeImpl(Matrix3x2fStack matrices, GuiRenderState state) {
this.state = state;
this.matrices = new Matrix3x2fStackRef(matrices); this.matrices = new Matrix3x2fStackRef(matrices);
this.state = state;
this.scissorStack = new ScissorStack(); this.scissorStack = new ScissorStack();
} }
@@ -130,17 +130,21 @@ public class RenderScopeImpl implements RenderScope {
int lineIndex = 0; int lineIndex = 0;
for (String line : text.split("\n")) { for (String line : text.split("\n")) {
int finalColor = getColor(color); int finalColor = getColor(color);
matrices.push();
matrices.translate(x, y + (lineIndex * Fonts.getHeight()));
matrices.scale(scale, scale);
this.state.addText( this.state.addText(
new TextGuiElementRenderState( new TextGuiElementRenderState(
SaturnClient.client.textRenderer, SaturnClient.client.textRenderer,
((Text) Fonts.setFont(line, font)).asOrderedText(), ((Text) Fonts.setFont(line, font)).asOrderedText(),
new Matrix3x2f(this.matrices.stack), new Matrix3x2f(this.matrices.stack),
x, y, 0, 0, // IMPORTANT
finalColor, 0, false, this.scissorStack.peekLast())); finalColor, 0, false, this.scissorStack.peekLast()));
matrices.push(); matrices.pop();
matrices.translate(x, y + (lineIndex * Fonts.getHeight()));
matrices.scale(scale, scale);
lineIndex++; lineIndex++;
} }
} }
@@ -182,17 +186,19 @@ public class RenderScopeImpl implements RenderScope {
return; return;
radius = Math.min(radius, Math.min(width, height)); radius = Math.min(radius, Math.min(width, height));
int cornerWidth = width / 2; fill(x, y, x + width, y + height, color);
int cornerHeight = height / 2;
matrices.push(); // int cornerWidth = width / 2;
matrices.translate(x, y); // int cornerHeight = height / 2;
drawRoundedSide(cornerWidth, cornerHeight, radius, color);
matrices.translate(cornerWidth * 2, cornerHeight * 2); // matrices.push();
matrices.stack.rotate((float) Math.toRadians(90)); // matrices.translate(x, y);
drawRoundedSide(cornerWidth, cornerHeight, radius, color); // drawRoundedSide(cornerWidth, cornerHeight, radius, color);
matrices.pop();
// matrices.translate(cornerWidth * 2, cornerHeight * 2);
// matrices.stack.rotate((float) Math.toRadians(90));
// drawRoundedSide(cornerWidth, cornerHeight, radius, color);
// matrices.pop();
} }
@Override @Override