Skip to content

Commit

Permalink
swap to GL rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Dec 2, 2024
1 parent 090cafe commit 6383895
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
69 changes: 47 additions & 22 deletions src/main/kotlin/org/polyfrost/crosshair/CrosshairHUD.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package org.polyfrost.crosshair

import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.texture.TextureUtil
import org.lwjgl.opengl.GL11
import org.polyfrost.oneconfig.api.config.v1.annotations.Include
import org.polyfrost.oneconfig.api.config.v1.annotations.Switch
import org.polyfrost.oneconfig.api.hud.v1.Hud
import org.polyfrost.polyui.component.impl.Image
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud
import org.polyfrost.polyui.unit.Vec2
import org.polyfrost.polyui.utils.cl1
import org.polyfrost.polyui.utils.getResourceStream
import java.nio.file.Files
import org.polyfrost.universal.UMatrixStack
import org.polyfrost.universal.UResolution
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import javax.imageio.ImageIO
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import net.minecraft.client.renderer.GlStateManager as GL

object CrosshairHUD : Hud<Image>() {
object CrosshairHUD : LegacyHud() {
@Switch(title = "Show in F3")
private var showInDebug = false

Expand All @@ -26,33 +32,52 @@ object CrosshairHUD : Hud<Image>() {

@Include
var currentCrosshair: String? = null
set(value) {
field = value
setCrosshair(value)
}

private var id = GL.generateTexture()
private var texSize = 15f

override var width: Float
get() = texSize
set(value) {}

override var height: Float
get() = texSize
set(value) {}

private val target = Paths.get("polycrosshair.png")

override fun category() = Category.COMBAT

override fun initialize() {
setCrosshair(currentCrosshair)
val img = if (currentCrosshair.isNullOrEmpty() || !target.exists()) ImageIO.read(getResourceStream("assets/polycrosshair/default.png"))
else ImageIO.read(target.inputStream())
setCrosshair(img.getRGB(0, 0, img.width, img.height, null, 0, img.width), img.width)
}

override fun hasBackground() = false

override fun create() = Image(target.toUri().toString())

private fun setCrosshair(crosshair: String?) {
if (crosshair.isNullOrEmpty() || !Paths.get(crosshair).exists()) {
Files.copy(getResourceStream("assets/polycrosshair/default.png"), target, StandardCopyOption.REPLACE_EXISTING)
} else Files.copy(Paths.get(crosshair), target, StandardCopyOption.REPLACE_EXISTING)
val it = get()
if (!it.initialized) return
it.renderer.delete(it.image)
val size = it.polyUI.size
it.x = size.x / 2f - it.size.x / 2f
it.y = size.y / 2f - it.size.y / 2f
override fun render(stack: UMatrixStack, x: Float, y: Float, scaleX: Float, scaleY: Float) {
//val mc = Minecraft.getMinecraft()
//if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return

GL.pushMatrix()
GL.enableBlend()
GL.enableAlpha()
GL.bindTexture(id)
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
GL.color(1f, 1f, 1f, 1f)
val mcScale = UResolution.scaleFactor.toFloat()
GL.scale(1f / mcScale, 1f / mcScale, 1f)
val texSizeI = texSize.toInt()
val size = (texSize * cl1(scaleX, scaleY) * mcScale).toInt()
Gui.drawScaledCustomSizeModalRect((x / mcScale).toInt(), (y / mcScale).toInt(), 0f, 0f, texSizeI, texSizeI, size, size, texSize, texSize)
GL.popMatrix()
}


fun setCrosshair(cdata: IntArray, size: Int) {
texSize = size.toFloat()
TextureUtil.uploadTexture(id, cdata, size, size)
}

override fun defaultPosition() = Vec2(1920f / 2f - 7f, 1080f / 2f - 7f)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/polyfrost/crosshair/PolyCrosshairUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ private var currentCard: Group? = null
null
}
val size = img?.width ?: canvasSize
canvasContainer[0] = genCanvas(size, colorData = img?.getRGB(0, 0, size, size, null, 0, size))
val cdata = img?.getRGB(0, 0, size, size, null, 0, size) ?: intArrayOf(size * size)
canvasContainer[0] = genCanvas(size, colorData = cdata)
crosshairName.text = name
canvasSize = size
ignored = true
canvasSizeDrawable.text = size.toString()
CrosshairHUD.currentCrosshair = path.toUri().toString()
CrosshairHUD.setCrosshair(cdata, size)
value[0].setPalette { brand.fg }
field = value
}
Expand Down

0 comments on commit 6383895

Please sign in to comment.