From b9c0ce8097b505dcf2eb6a8ea1a1c2eb2d24ed84 Mon Sep 17 00:00:00 2001 From: gaogao-qwq Date: Tue, 4 Nov 2025 10:56:38 +0800 Subject: [PATCH] Optimize GUI render performance Signed-off-by: gaogao-qwq --- .../eydamos/guiadvanced/ContainerWindow.java | 22 ++-- .../java/de/eydamos/guiadvanced/Window.java | 22 ++-- .../de/eydamos/guiadvanced/form/Button.java | 20 ++-- .../eydamos/guiadvanced/subpart/GuiSlot.java | 20 ++-- .../de/eydamos/guiadvanced/subpart/Icon.java | 4 +- .../eydamos/guiadvanced/util/Rectangle.java | 29 ++--- .../guiadvanced/util/RenderHelper.java | 104 ++++++++++-------- 7 files changed, 125 insertions(+), 96 deletions(-) diff --git a/src/main/java/de/eydamos/guiadvanced/ContainerWindow.java b/src/main/java/de/eydamos/guiadvanced/ContainerWindow.java index be89b4c3..64eea680 100644 --- a/src/main/java/de/eydamos/guiadvanced/ContainerWindow.java +++ b/src/main/java/de/eydamos/guiadvanced/ContainerWindow.java @@ -8,6 +8,7 @@ import de.eydamos.guiadvanced.misc.AbstractGui; import de.eydamos.guiadvanced.misc.AbstractGuiPart; +import de.eydamos.guiadvanced.util.Rectangle; import de.eydamos.guiadvanced.util.RenderHelper; public class ContainerWindow extends GuiContainer implements AbstractGui { @@ -69,15 +70,18 @@ public void clearSubParts() { @Override protected void drawGuiContainerBackgroundLayer(float something, int mouseX, int mouseY) { // draw background - RenderHelper.drawOuterCornerTopLeft(guiLeft, guiTop); - RenderHelper.drawBorderTop(guiLeft + 4, guiTop, xSize - 8, 4); - RenderHelper.drawOuterCornerTopRight(guiLeft + xSize - 4, guiTop); - RenderHelper.drawBorderLeft(guiLeft, guiTop + 4, 4, ySize - 8); - RenderHelper.drawBackground(guiLeft + 4, guiTop + 4, xSize - 8, ySize - 8); - RenderHelper.drawBorderRight(guiLeft + xSize - 4, guiTop + 4, 4, ySize - 8); - RenderHelper.drawOuterCornerBottomLeft(guiLeft, guiTop + ySize - 4); - RenderHelper.drawBorderBottom(guiLeft + 4, guiTop + ySize - 4, xSize - 8, 4); - RenderHelper.drawOuterCornerBottomRight(guiLeft + xSize - 4, guiTop + ySize - 4); + Rectangle rect = new Rectangle(); + rect.startDrawing(); + RenderHelper.drawOuterCornerTopLeft(rect, guiLeft, guiTop); + RenderHelper.drawBorderTop(rect, guiLeft + 4, guiTop, xSize - 8, 4); + RenderHelper.drawOuterCornerTopRight(rect, guiLeft + xSize - 4, guiTop); + RenderHelper.drawBorderLeft(rect, guiLeft, guiTop + 4, 4, ySize - 8); + RenderHelper.drawBackground(rect, guiLeft + 4, guiTop + 4, xSize - 8, ySize - 8); + RenderHelper.drawBorderRight(rect, guiLeft + xSize - 4, guiTop + 4, 4, ySize - 8); + RenderHelper.drawOuterCornerBottomLeft(rect, guiLeft, guiTop + ySize - 4); + RenderHelper.drawBorderBottom(rect, guiLeft + 4, guiTop + ySize - 4, xSize - 8, 4); + RenderHelper.drawOuterCornerBottomRight(rect, guiLeft + xSize - 4, guiTop + ySize - 4); + rect.performDrawing(); // draw subparts for (AbstractGuiPart guiPart : subParts) { diff --git a/src/main/java/de/eydamos/guiadvanced/Window.java b/src/main/java/de/eydamos/guiadvanced/Window.java index 483bce10..9924be09 100644 --- a/src/main/java/de/eydamos/guiadvanced/Window.java +++ b/src/main/java/de/eydamos/guiadvanced/Window.java @@ -7,6 +7,7 @@ import de.eydamos.guiadvanced.misc.AbstractGui; import de.eydamos.guiadvanced.misc.AbstractGuiPart; +import de.eydamos.guiadvanced.util.Rectangle; import de.eydamos.guiadvanced.util.RenderHelper; public class Window extends GuiScreen implements AbstractGui { @@ -69,15 +70,18 @@ public void clearSubParts() { @Override public void drawScreen(int mouseX, int mouseY, float something) { // draw background - RenderHelper.drawOuterCornerTopLeft(guiLeft, guiTop); - RenderHelper.drawBorderTop(guiLeft + 4, guiTop, xSize - 8, 4); - RenderHelper.drawOuterCornerTopRight(guiLeft + xSize - 4, guiTop); - RenderHelper.drawBorderLeft(guiLeft, guiTop + 4, 4, ySize - 8); - RenderHelper.drawBackground(guiLeft + 4, guiTop + 4, xSize - 8, ySize - 8); - RenderHelper.drawBorderRight(guiLeft + xSize - 4, guiTop + 4, 4, ySize - 8); - RenderHelper.drawOuterCornerBottomLeft(guiLeft, guiTop + ySize - 4); - RenderHelper.drawBorderBottom(guiLeft + 4, guiTop + ySize - 4, xSize - 8, 4); - RenderHelper.drawOuterCornerBottomRight(guiLeft + xSize - 4, guiTop + ySize - 4); + Rectangle rect = new Rectangle(); + rect.startDrawing(); + RenderHelper.drawOuterCornerTopLeft(rect, guiLeft, guiTop); + RenderHelper.drawBorderTop(rect, guiLeft + 4, guiTop, xSize - 8, 4); + RenderHelper.drawOuterCornerTopRight(rect, guiLeft + xSize - 4, guiTop); + RenderHelper.drawBorderLeft(rect, guiLeft, guiTop + 4, 4, ySize - 8); + RenderHelper.drawBackground(rect, guiLeft + 4, guiTop + 4, xSize - 8, ySize - 8); + RenderHelper.drawBorderRight(rect, guiLeft + xSize - 4, guiTop + 4, 4, ySize - 8); + RenderHelper.drawOuterCornerBottomLeft(rect, guiLeft, guiTop + ySize - 4); + RenderHelper.drawBorderBottom(rect, guiLeft + 4, guiTop + ySize - 4, xSize - 8, 4); + RenderHelper.drawOuterCornerBottomRight(rect, guiLeft + xSize - 4, guiTop + ySize - 4); + rect.performDrawing(); // draw subparts for (AbstractGuiPart guiPart : subParts) { diff --git a/src/main/java/de/eydamos/guiadvanced/form/Button.java b/src/main/java/de/eydamos/guiadvanced/form/Button.java index ff9e4088..3a19f0cc 100644 --- a/src/main/java/de/eydamos/guiadvanced/form/Button.java +++ b/src/main/java/de/eydamos/guiadvanced/form/Button.java @@ -69,28 +69,29 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) { Rectangle rectangle = new Rectangle(2, 2); rectangle.setBackground(buttonTextures); rectangle.setBackgroundSize(2, 2); + rectangle.startDrawing(); // draw upper left corner rectangle.setBackgroundPosition(0, 46 + offset * 20); - rectangle.draw(xPosition, yPosition); + rectangle.addBoxVertices(xPosition, yPosition); // draw upper right corner rectangle.setBackgroundPosition(198, 46 + offset * 20); - rectangle.draw(xPosition + width - 2, yPosition); + rectangle.addBoxVertices(xPosition + width - 2, yPosition); // draw lower left corner rectangle.setBackgroundPosition(0, 64 + offset * 20); - rectangle.draw(xPosition, yPosition + height - 2); + rectangle.addBoxVertices(xPosition, yPosition + height - 2); // draw lower right corner rectangle.setBackgroundPosition(198, 64 + offset * 20); - rectangle.draw(xPosition + width - 2, yPosition + height - 2); + rectangle.addBoxVertices(xPosition + width - 2, yPosition + height - 2); // borders top/bottom rectangle.setWidth(width - 4); rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); // draw top border rectangle.setBackgroundPosition(2, 46 + offset * 20); - rectangle.draw(xPosition + 2, yPosition); + rectangle.addBoxVertices(xPosition + 2, yPosition); // draw bottom border rectangle.setBackgroundPosition(2, 64 + offset * 20); - rectangle.draw(xPosition + 2, yPosition + height - 2); + rectangle.addBoxVertices(xPosition + 2, yPosition + height - 2); // borders left/right rectangle.setWidth(2); @@ -98,10 +99,10 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) { rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); // draw left border rectangle.setBackgroundPosition(0, 48 + offset * 20); - rectangle.draw(xPosition, yPosition + 2); + rectangle.addBoxVertices(xPosition, yPosition + 2); // draw right border rectangle.setBackgroundPosition(198, 48 + offset * 20); - rectangle.draw(xPosition + width - 2, yPosition + 2); + rectangle.addBoxVertices(xPosition + width - 2, yPosition + 2); // draw background rectangle.setWidth(width - 4); @@ -109,7 +110,8 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) { rectangle.setBackgroundSize(18, 18); rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT); rectangle.setBackgroundPosition(2, 48 + offset * 20); - rectangle.draw(xPosition + 2, yPosition + 2); + rectangle.addBoxVertices(xPosition + 2, yPosition + 2); + rectangle.performDrawing(); mouseDragged(mc, mouseX, mouseY); int l = 14737632; diff --git a/src/main/java/de/eydamos/guiadvanced/subpart/GuiSlot.java b/src/main/java/de/eydamos/guiadvanced/subpart/GuiSlot.java index 6fae410e..7189f5a8 100644 --- a/src/main/java/de/eydamos/guiadvanced/subpart/GuiSlot.java +++ b/src/main/java/de/eydamos/guiadvanced/subpart/GuiSlot.java @@ -53,28 +53,29 @@ public void setHeight(int value) { @Override public void draw(Minecraft mc, int mouseX, int mouseY, float something) { Rectangle rectangle = new Rectangle(1, 1); + rectangle.startDrawing(); // draw upper left corner rectangle.setBackgroundPosition(201, 0); - rectangle.draw(xPosition, yPosition); + rectangle.addBoxVertices(xPosition, yPosition); // draw upper right corner rectangle.setBackgroundPosition(218, 0); - rectangle.draw(xPosition + width - 1, yPosition); + rectangle.addBoxVertices(xPosition + width - 1, yPosition); // draw lower left corner rectangle.setBackgroundPosition(201, 17); - rectangle.draw(xPosition, yPosition + height - 1); + rectangle.addBoxVertices(xPosition, yPosition + height - 1); // draw lower right corner rectangle.setBackgroundPosition(218, 17); - rectangle.draw(xPosition + width - 1, yPosition + height - 1); + rectangle.addBoxVertices(xPosition + width - 1, yPosition + height - 1); // borders top/bottom rectangle.setWidth(width - 2); rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); // draw top border rectangle.setBackgroundPosition(202, 0); - rectangle.draw(xPosition + 1, yPosition); + rectangle.addBoxVertices(xPosition + 1, yPosition); // draw bottom border rectangle.setBackgroundPosition(202, 17); - rectangle.draw(xPosition + 1, yPosition + height - 1); + rectangle.addBoxVertices(xPosition + 1, yPosition + height - 1); // borders left/right rectangle.setWidth(1); @@ -82,10 +83,10 @@ public void draw(Minecraft mc, int mouseX, int mouseY, float something) { rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); // draw left border rectangle.setBackgroundPosition(201, 1); - rectangle.draw(xPosition, yPosition + 1); + rectangle.addBoxVertices(xPosition, yPosition + 1); // draw right border rectangle.setBackgroundPosition(218, 1); - rectangle.draw(xPosition + width - 1, yPosition + 1); + rectangle.addBoxVertices(xPosition + width - 1, yPosition + 1); // draw background rectangle.setWidth(width - 2); @@ -93,7 +94,8 @@ public void draw(Minecraft mc, int mouseX, int mouseY, float something) { rectangle.setBackgroundSize(14, 14); rectangle.setBackgroundRepeat(BackgroundRepeat.REPEAT); rectangle.setBackgroundPosition(202, 1); - rectangle.draw(xPosition + 1, yPosition + 1); + rectangle.addBoxVertices(xPosition + 1, yPosition + 1); + rectangle.performDrawing(); } @Override diff --git a/src/main/java/de/eydamos/guiadvanced/subpart/Icon.java b/src/main/java/de/eydamos/guiadvanced/subpart/Icon.java index 705dd801..5be6f0e3 100644 --- a/src/main/java/de/eydamos/guiadvanced/subpart/Icon.java +++ b/src/main/java/de/eydamos/guiadvanced/subpart/Icon.java @@ -67,12 +67,14 @@ public void setHeight(int value) { @Override public void draw(Minecraft mc, int mouseX, int mouseY, float something) { Rectangle icon = new Rectangle(width, height); + icon.startDrawing(); if (image != null) { icon.setBackground(image); } icon.setBackgroundPosition(uPosition, vPosition); icon.setBackgroundSize(width, height); - icon.draw(xPosition, yPosition); + icon.addBoxVertices(xPosition, yPosition); + icon.performDrawing(); } @Override diff --git a/src/main/java/de/eydamos/guiadvanced/util/Rectangle.java b/src/main/java/de/eydamos/guiadvanced/util/Rectangle.java index 02880047..97ac9da6 100644 --- a/src/main/java/de/eydamos/guiadvanced/util/Rectangle.java +++ b/src/main/java/de/eydamos/guiadvanced/util/Rectangle.java @@ -22,6 +22,10 @@ public class Rectangle { protected ResourceLocation graphic = Constants.guiCombined; protected BackgroundRepeat repeat = BackgroundRepeat.NONE; + public Rectangle() { + this(0, 0); + } + public Rectangle(int width, int height) { this.width = width; this.height = height; @@ -57,20 +61,19 @@ public void setBackground(ResourceLocation resourceLocation) { graphic = resourceLocation; } - public void draw(int x, int y) { - GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - getMinecraft().getTextureManager().bindTexture(graphic); + public void startDrawing() { + Tessellator.instance.startDrawingQuads(); + } + public void addBoxVertices(int x, int y) { + Tessellator tessellator = Tessellator.instance; float f = 0.00390625F; float f1 = 0.00390625F; - Tessellator tessellator = Tessellator.instance; if (repeat == BackgroundRepeat.NONE) { - tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x, y + height, z, u * f, (v + height) * f1); tessellator.addVertexWithUV(x + width, y + height, z, (u + width) * f, (v + height) * f1); tessellator.addVertexWithUV(x + width, y, z, (u + width) * f, v * f1); tessellator.addVertexWithUV(x, y, z, u * f, v * f1); - tessellator.draw(); } else if (repeat == BackgroundRepeat.REPEAT) { uMax = Math.min(width, uMax); vMax = Math.min(height, vMax); @@ -79,12 +82,10 @@ public void draw(int x, int y) { for (int j = 0; j <= height; j += vMax) { drawWidth = Math.min(i + uMax, width); drawHeight = Math.min(j + vMax, height); - tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x + i, y + drawHeight, z, u * f, (v + vMax) * f1); tessellator.addVertexWithUV(x + drawWidth, y + drawHeight, z, (u + uMax) * f, (v + vMax) * f1); tessellator.addVertexWithUV(x + drawWidth, y, z, (u + uMax) * f, v * f1); tessellator.addVertexWithUV(x + i, y, z, u * f, v * f1); - tessellator.draw(); } } } else if (repeat == BackgroundRepeat.REPEAT_X) { @@ -92,32 +93,32 @@ public void draw(int x, int y) { int drawWidth; for (int i = 0; i <= width; i += uMax) { drawWidth = Math.min(i + uMax, width); - tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x + i, y + drawHeight, z, u * f, (v + drawHeight) * f1); tessellator.addVertexWithUV(x + drawWidth, y + drawHeight, z, (u + uMax) * f, (v + drawHeight) * f1); tessellator.addVertexWithUV(x + drawWidth, y, z, (u + uMax) * f, v * f1); tessellator.addVertexWithUV(x + i, y, z, u * f, v * f1); - tessellator.draw(); } } else if (repeat == BackgroundRepeat.REPEAT_Y) { int drawWidth = uMax = Math.min(width, uMax); int drawHeight; for (int i = 0; i <= height; i += vMax) { drawHeight = Math.min(i + vMax, height); - tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x, y + drawHeight, z, u * f, (v + vMax) * f1); tessellator.addVertexWithUV(x + drawWidth, y + drawHeight, z, (u + drawWidth) * f, (v + vMax) * f1); tessellator.addVertexWithUV(x + drawWidth, y + i, z, (u + drawWidth) * f, v * f1); tessellator.addVertexWithUV(x, y + i, z, u * f, v * f1); - tessellator.draw(); } } else { - tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x, y + height, z, u * f, vMax * f1); tessellator.addVertexWithUV(x + width, y + height, z, uMax * f, vMax * f1); tessellator.addVertexWithUV(x + width, y, z, uMax * f, v * f1); tessellator.addVertexWithUV(x, y, z, u * f, v * f1); - tessellator.draw(); } } + + public void performDrawing() { + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + getMinecraft().getTextureManager().bindTexture(graphic); + Tessellator.instance.draw(); + } } diff --git a/src/main/java/de/eydamos/guiadvanced/util/RenderHelper.java b/src/main/java/de/eydamos/guiadvanced/util/RenderHelper.java index c32e76e4..406514db 100644 --- a/src/main/java/de/eydamos/guiadvanced/util/RenderHelper.java +++ b/src/main/java/de/eydamos/guiadvanced/util/RenderHelper.java @@ -10,66 +10,80 @@ public enum BackgroundRepeat { STRETCH } - public static void drawOuterCornerTopLeft(int posX, int posY) { - Rectangle outerCornerTopLeft = new Rectangle(4, 4); - outerCornerTopLeft.draw(posX, posY); + public static void drawOuterCornerTopLeft(Rectangle rect, int posX, int posY) { + rect.setWidth(4); + rect.setHeight(4); + rect.setBackgroundPosition(0, 0); + rect.setBackgroundSize(4, 4); + rect.addBoxVertices(posX, posY); } - public static void drawOuterCornerTopRight(int posX, int posY) { - Rectangle outerCornerTopRight = new Rectangle(4, 4); - outerCornerTopRight.setBackgroundPosition(172, 0); - outerCornerTopRight.draw(posX, posY); + public static void drawOuterCornerTopRight(Rectangle rect, int posX, int posY) { + rect.setWidth(4); + rect.setHeight(4); + rect.setBackgroundSize(4, 4); + rect.setBackgroundPosition(172, 0); + rect.addBoxVertices(posX, posY); } - public static void drawOuterCornerBottomLeft(int posX, int posY) { - Rectangle outerCornerBottomLeft = new Rectangle(4, 4); - outerCornerBottomLeft.setBackgroundPosition(0, 163); - outerCornerBottomLeft.draw(posX, posY); + public static void drawOuterCornerBottomLeft(Rectangle rect, int posX, int posY) { + rect.setWidth(4); + rect.setHeight(4); + rect.setBackgroundSize(4, 4); + rect.setBackgroundPosition(0, 163); + rect.addBoxVertices(posX, posY); } - public static void drawOuterCornerBottomRight(int posX, int posY) { - Rectangle outerCornerBottomRight = new Rectangle(4, 4); - outerCornerBottomRight.setBackgroundPosition(172, 163); - outerCornerBottomRight.draw(posX, posY); + public static void drawOuterCornerBottomRight(Rectangle rect, int posX, int posY) { + rect.setWidth(4); + rect.setHeight(4); + rect.setBackgroundSize(4, 4); + rect.setBackgroundPosition(172, 163); + rect.addBoxVertices(posX, posY); } - public static void drawBorderTop(int posX, int posY, int width, int height) { - Rectangle borderTop = new Rectangle(width, height); - borderTop.setBackgroundPosition(4, 0); - borderTop.setBackgroundSize(100, 4); - borderTop.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); - borderTop.draw(posX, posY); + public static void drawBorderTop(Rectangle rect, int posX, int posY, int width, int height) { + rect.setWidth(width); + rect.setHeight(height); + rect.setBackgroundPosition(4, 0); + rect.setBackgroundSize(100, 4); + rect.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); + rect.addBoxVertices(posX, posY); } - public static void drawBorderRight(int posX, int posY, int width, int height) { - Rectangle borderRight = new Rectangle(width, height); - borderRight.setBackgroundPosition(172, 4); - borderRight.setBackgroundSize(4, 100); - borderRight.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); - borderRight.draw(posX, posY); + public static void drawBorderRight(Rectangle rect, int posX, int posY, int width, int height) { + rect.setWidth(width); + rect.setHeight(height); + rect.setBackgroundPosition(172, 4); + rect.setBackgroundSize(4, 100); + rect.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); + rect.addBoxVertices(posX, posY); } - public static void drawBorderBottom(int posX, int posY, int width, int height) { - Rectangle borderBottom = new Rectangle(width, height); - borderBottom.setBackgroundPosition(4, 163); - borderBottom.setBackgroundSize(100, 4); - borderBottom.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); - borderBottom.draw(posX, posY); + public static void drawBorderBottom(Rectangle rect, int posX, int posY, int width, int height) { + rect.setWidth(width); + rect.setHeight(height); + rect.setBackgroundPosition(4, 163); + rect.setBackgroundSize(100, 4); + rect.setBackgroundRepeat(BackgroundRepeat.REPEAT_X); + rect.addBoxVertices(posX, posY); } - public static void drawBorderLeft(int posX, int posY, int width, int height) { - Rectangle borderLeft = new Rectangle(width, height); - borderLeft.setBackgroundPosition(0, 4); - borderLeft.setBackgroundSize(4, 100); - borderLeft.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); - borderLeft.draw(posX, posY); + public static void drawBorderLeft(Rectangle rect, int posX, int posY, int width, int height) { + rect.setWidth(width); + rect.setHeight(height); + rect.setBackgroundPosition(0, 4); + rect.setBackgroundSize(4, 100); + rect.setBackgroundRepeat(BackgroundRepeat.REPEAT_Y); + rect.addBoxVertices(posX, posY); } - public static void drawBackground(int posX, int posY, int width, int height) { - Rectangle borderBottom = new Rectangle(width, height); - borderBottom.setBackgroundPosition(4, 4); - borderBottom.setBackgroundSize(100, 100); - borderBottom.setBackgroundRepeat(BackgroundRepeat.REPEAT); - borderBottom.draw(posX, posY); + public static void drawBackground(Rectangle rect, int posX, int posY, int width, int height) { + rect.setWidth(width); + rect.setHeight(height); + rect.setBackgroundPosition(4, 4); + rect.setBackgroundSize(100, 100); + rect.setBackgroundRepeat(BackgroundRepeat.REPEAT); + rect.addBoxVertices(posX, posY); } }