Skip to content

Commit c32e689

Browse files
committed
Made text rendering 2x faster
1 parent 3893c4d commit c32e689

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

src/main/java/me/pindour/catpuccin/gui/renderer/CatpuccinRenderer.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import meteordevelopment.meteorclient.renderer.Texture;
1414
import meteordevelopment.meteorclient.utils.misc.Pool;
1515
import meteordevelopment.meteorclient.utils.render.color.Color;
16-
import net.minecraft.util.Pair;
1716
import net.minecraft.util.math.MathHelper;
1817

1918
import java.util.ArrayList;
@@ -22,8 +21,8 @@
2221
import java.util.Map;
2322

2423
public class CatpuccinRenderer {
25-
private static CatpuccinRenderer INSTANCE;
26-
private static CatpuccinGuiTheme theme;
24+
private static final CatpuccinRenderer INSTANCE = new CatpuccinRenderer();
25+
private CatpuccinGuiTheme theme;
2726

2827
private static Texture TEXTURE;
2928
private static TextureRegion CIRCLE_TEXTURE;
@@ -32,24 +31,18 @@ public class CatpuccinRenderer {
3231
private final Renderer2D rTex = new Renderer2D(true);
3332

3433
private final Pool<RichTextOperation> textPool = new Pool<>(RichTextOperation::new);
35-
private final List<RichTextOperation> texts = new ArrayList<>();
36-
37-
private final Map<Pair<FontStyle, Double>, List<RichTextOperation>> textsByStyleAndScale = new HashMap<>();
38-
39-
public CatpuccinRenderer() {
40-
INSTANCE = this;
41-
}
34+
private final Map<StyleKey, List<RichTextOperation>> groupedOperations = new HashMap<>();
4235

4336
public static void init(Texture texture) {
4437
TEXTURE = texture;
4538
}
4639

4740
public static CatpuccinRenderer get() {
48-
return (INSTANCE != null ? INSTANCE : new CatpuccinRenderer());
41+
return INSTANCE;
4942
}
5043

5144
public void setTheme(CatpuccinGuiTheme theme) {
52-
if (CatpuccinRenderer.theme != theme) CatpuccinRenderer.theme = theme;
45+
if (this.theme == null) this.theme = theme;
5346
}
5447

5548
public void begin() {
@@ -70,32 +63,24 @@ public void render() {
7063
public void renderText() {
7164
if (theme == null) return;
7265

73-
textsByStyleAndScale.clear();
66+
// Render each style group in batches to minimize font/scale changes
67+
for (Map.Entry<StyleKey, List<RichTextOperation>> entry : groupedOperations.entrySet()) {
68+
List<RichTextOperation> textOps = entry.getValue();
7469

75-
for (RichTextOperation text : texts) {
76-
FontStyle style = text.getStyle();
77-
double scale = text.getScale();
78-
Pair<FontStyle, Double> key = new Pair<>(style, scale);
79-
textsByStyleAndScale.computeIfAbsent(key, k -> new ArrayList<>()).add(text);
80-
}
70+
if (textOps.isEmpty()) continue;
8171

82-
for (Map.Entry<Pair<FontStyle, Double>, List<RichTextOperation>> entry : textsByStyleAndScale.entrySet()) {
83-
FontStyle style = entry.getKey().getLeft();
84-
double scale = entry.getKey().getRight();
72+
StyleKey key = entry.getKey();
8573

86-
theme.richTextRenderer().setFontStyle(style);
87-
theme.richTextRenderer().begin(theme.scale(scale));
74+
theme.richTextRenderer().setFontStyle(key.style());
75+
theme.richTextRenderer().begin(theme.scale(key.scale()));
8876

89-
for (RichTextOperation text : entry.getValue()) {
77+
for (RichTextOperation text : textOps) {
9078
text.run(textPool);
9179
}
9280

9381
theme.richTextRenderer().end();
94-
95-
entry.getValue().clear();
82+
textOps.clear();
9683
}
97-
98-
texts.clear();
9984
}
10085

10186
public void setAlpha(double a) {
@@ -109,11 +94,17 @@ public void text(RichText text, double x, double y, Color color) {
10994
for (RichTextSegment segment : text.getSegments()) {
11095
if (segment.getText() == null || segment.getText().isEmpty()) continue;
11196

112-
double segmentWidth = theme.textWidth(segment);
97+
RichTextOperation operation = getOperation(textPool, segmentX, y, color)
98+
.set(segment, theme.richTextRenderer());
11399

114-
texts.add(getOperation(textPool, segmentX, y, color).set(segment, theme.richTextRenderer()));
100+
// Group operations by style and scale to batch render them later
101+
StyleKey key = new StyleKey(operation.getStyle(), operation.getScale());
115102

116-
segmentX += segmentWidth;
103+
groupedOperations
104+
.computeIfAbsent(key, k -> new ArrayList<>())
105+
.add(operation);
106+
107+
segmentX += theme.textWidth(segment);
117108
}
118109
}
119110

@@ -200,4 +191,6 @@ private <T extends GuiRenderOperation<T>> T getOperation(Pool<T> pool, double x,
200191
op.set(x, y, color);
201192
return op;
202193
}
194+
195+
public record StyleKey(FontStyle style, double scale) { }
203196
}

0 commit comments

Comments
 (0)