From f3a35c4540029eb4894e30d48719edf51ac94cc4 Mon Sep 17 00:00:00 2001 From: Firenoo <49818773+firenoo@users.noreply.github.com> Date: Mon, 27 Feb 2023 19:31:09 -0800 Subject: [PATCH] Move tessellator use to its own helper file Because drawing a textured quad requires thinking and thinking is hard --- .../client/gui/GuiAdvancedMemoryCard.kt | 30 ++++++++----------- .../betterp2p/client/gui/GuiHelper.kt | 25 ++++++++++++++++ .../client/gui/widget/WidgetButton.kt | 24 +++++++-------- 3 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt index 3adae15..1165975 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt @@ -272,29 +272,23 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { bindTexture(MODID, "textures/gui/advanced_memory_card.png") val tessellator = Tessellator.instance // Draw the top part - tessellator.startDrawingQuads() - tessellator.addVertexWithUV(guiLeft.toDouble(), guiTop + 60.0, 0.0, 0.0, 60.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(((guiLeft + GUI_WIDTH).toDouble()), guiTop + 60.0, 0.0, 1.0, 60.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV((guiLeft + GUI_WIDTH).toDouble(), guiTop.toDouble(), 0.0, 1.0, 0.0) - tessellator.addVertexWithUV(guiLeft.toDouble(), guiTop.toDouble(), 0.0, 0.0, 0.0) - tessellator.draw() + drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop.toDouble(), + (guiLeft + GUI_WIDTH).toDouble(), guiTop + 60.0, + u0 = 0.0, v0 = 0.0, + u1 = 1.0, v1 = 60.0 / GUI_TEX_HEIGHT) // Draw P2P segments val p2pHeight = P2P_ENTRY_HEIGHT + 1.0 for (i in 0 until scale.size(ySize - 75) - 2) { - tessellator.startDrawingQuads() - tessellator.addVertexWithUV(guiLeft.toDouble(), guiTop + 60.0 + p2pHeight * (i + 1), 0.0, 0.0, 102.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(((guiLeft + GUI_WIDTH).toDouble()), guiTop + 60.0 + p2pHeight * (i + 1), 0.0, 1.0, 102.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV((guiLeft + GUI_WIDTH).toDouble(), guiTop + 60.0 + p2pHeight * i, 0.0, 1.0, 60.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(guiLeft.toDouble(), guiTop + 60.0 + p2pHeight * i, 0.0, 0.0, 60.0 / GUI_TEX_HEIGHT) - tessellator.draw() + drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop + 60.0 + p2pHeight * i, + (guiLeft + GUI_WIDTH).toDouble(), guiTop + 60.0 + p2pHeight * (i + 1), + u0 = 0.0, v0 = 60.0 / GUI_TEX_HEIGHT, + u1 = 1.0, v1 = 102.0 / GUI_TEX_HEIGHT) } // Draw Bottom - tessellator.startDrawingQuads() - tessellator.addVertexWithUV(guiLeft.toDouble(), (guiTop + ySize).toDouble(), 0.0, 0.0, 200.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(((guiLeft + GUI_WIDTH).toDouble()), (guiTop + ySize).toDouble(), 0.0, 1.0, 200.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV((guiLeft + GUI_WIDTH).toDouble(), guiTop + ySize - 98.0, 0.0, 1.0, 102.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(guiLeft.toDouble(), guiTop + ySize - 98.0, 0.0, 0.0, 102.0 / GUI_TEX_HEIGHT) - tessellator.draw() + drawTexturedQuad(tessellator, guiLeft.toDouble(), guiTop + ySize - 98.0, + (guiLeft + GUI_WIDTH).toDouble(), (guiTop + ySize).toDouble(), + u0 = 0.0, v0 = 102.0 / GUI_TEX_HEIGHT, + u1 = 1.0, v1 = 200.0 / GUI_TEX_HEIGHT) } override fun doesGuiPauseGame(): Boolean { diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt new file mode 100644 index 0000000..b95fbe1 --- /dev/null +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiHelper.kt @@ -0,0 +1,25 @@ +package com.projecturanus.betterp2p.client.gui + +import net.minecraft.client.renderer.Tessellator +import net.minecraft.util.ResourceLocation + +fun bindTexture(loc: ResourceLocation) { + +} + +/** + * Draws a textured quad. + * + * x0, y0 - top left corner + * x1, y1 - bottom right corner + * u0, v0 - top left texture corner + * u1, v1 - bottom right texture corner + */ +fun drawTexturedQuad(tessellator: Tessellator, x0 : Double, y0: Double, x1: Double, y1: Double, u0: Double, v0: Double, u1: Double, v1: Double) { + tessellator.startDrawingQuads() + tessellator.addVertexWithUV(x0, y1, 0.0, u0, v1) + tessellator.addVertexWithUV(x1, y1, 0.0, u1, v1) + tessellator.addVertexWithUV(x1, y0, 0.0, u1, v0) + tessellator.addVertexWithUV(x0, y0, 0.0, u0, v0) + tessellator.draw() +} diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt index c855505..f4aaebb 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt @@ -4,6 +4,7 @@ import com.projecturanus.betterp2p.MODID import com.projecturanus.betterp2p.client.gui.GUI_TEX_HEIGHT import com.projecturanus.betterp2p.client.gui.GUI_WIDTH import com.projecturanus.betterp2p.client.gui.GuiAdvancedMemoryCard +import com.projecturanus.betterp2p.client.gui.drawTexturedQuad import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiButton import net.minecraft.client.renderer.OpenGlHelper @@ -35,18 +36,17 @@ abstract class WidgetButton(val gui: GuiAdvancedMemoryCard, x: Int, y: Int, widt GL11.glEnable(GL11.GL_BLEND) OpenGlHelper.glBlendFunc(770, 771, 1, 0) GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA) - tessellator.startDrawingQuads() - tessellator.addVertexWithUV(xPosition.toDouble(), (yPosition + height).toDouble(), 0.0, (32.0 * k) / GUI_WIDTH, (232.0 + height) / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(((xPosition + width).toDouble()), (yPosition + height).toDouble(), 0.0, (32.0 * (k + 1)) / GUI_WIDTH, (232.0 + height) / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV((xPosition + width).toDouble(), yPosition.toDouble(), 0.0, (32.0 * (k + 1)) / GUI_WIDTH, 232.0 / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(xPosition.toDouble(), yPosition.toDouble(), 0.0, (32.0 * k) / GUI_WIDTH, 232.0 / GUI_TEX_HEIGHT) - tessellator.draw() - tessellator.startDrawingQuads() - tessellator.addVertexWithUV(xPosition.toDouble(), (yPosition + height).toDouble(), 0.0, texX / GUI_WIDTH, (texY + height) / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(((xPosition + width).toDouble()), (yPosition + height).toDouble(), 0.0, (texX + width) / GUI_WIDTH, (texY + height) / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV((xPosition + width).toDouble(), yPosition.toDouble(), 0.0, (texX + width) / GUI_WIDTH, texY / GUI_TEX_HEIGHT) - tessellator.addVertexWithUV(xPosition.toDouble(), yPosition.toDouble(), 0.0, texX / GUI_WIDTH, texY / GUI_TEX_HEIGHT) - tessellator.draw() + // Draw button backdrop + drawTexturedQuad(tessellator, xPosition.toDouble(), yPosition.toDouble(), + (xPosition + width).toDouble(), (yPosition + height).toDouble(), + u0 = (32.0 * k) / GUI_WIDTH, v0 = (232.0) / GUI_TEX_HEIGHT, + u1 = (32.0 * (k + 1)) / GUI_WIDTH, v1 = (232.0 + height) / GUI_TEX_HEIGHT) + // Draw button icon + drawTexturedQuad(tessellator, xPosition.toDouble(), yPosition.toDouble(), + (xPosition + width).toDouble(), (yPosition + height).toDouble(), + u0 = texX / GUI_WIDTH, v0 = texY / GUI_TEX_HEIGHT, + u1 = (texX + width) / GUI_WIDTH, v1 = (texY + height) / GUI_TEX_HEIGHT) + } fun setPosition(x: Int, y: Int) { this.xPosition = x