Skip to content

Commit

Permalink
faster scaling, fixed vanilla crosshair
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jun 26, 2024
1 parent b040562 commit 3f088fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/org/polyfrost/crosshair/PolyCrosshair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import cc.polyfrost.oneconfig.libs.universal.UResolution
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent
import org.polyfrost.crosshair.config.Drawer.loadImage
import org.polyfrost.crosshair.config.ModConfig
import org.polyfrost.crosshair.render.CrosshairRenderer
Expand Down Expand Up @@ -42,6 +43,11 @@ object PolyCrosshair {
EventManager.INSTANCE.register(this)
}

@Mod.EventHandler
fun onFMLPostInitialization(event: FMLPostInitializationEvent) {
CrosshairRenderer.updateVanilla()
}

@Subscribe
fun onTick(event: TickEvent) {
if (event.stage != Stage.END) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RenderConfig {
var dynamicColor = false

@Switch(name = "Invert Color")
var invertColor = false
var invertColor = true

@Slider(name = "Overlay Opacity", min = 0f, max = 100f)
var dynamicOpacity = 100
Expand Down
48 changes: 29 additions & 19 deletions src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import cc.polyfrost.oneconfig.libs.universal.UResolution
import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.EntityRenderer
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.GlStateManager as GL
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import net.minecraft.client.renderer.texture.TextureUtil
import net.minecraft.entity.monster.IMob
import net.minecraft.entity.passive.EntityAmbientCreature
import net.minecraft.entity.passive.EntityAnimal
import net.minecraft.entity.passive.EntityVillager
import net.minecraft.entity.passive.EntityWaterMob
import net.minecraft.entity.player.EntityPlayer
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.client.event.TextureStitchEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11
import org.polyfrost.crosshair.config.ModConfig
Expand All @@ -31,9 +31,11 @@ object CrosshairRenderer {
private var textureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)
private var whiteTexture = DynamicTexture(15, 15)
private var whiteTextureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture)
private var vanilla = DynamicTexture(15, 15)
private var vanillaLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", vanilla)

fun updateTexture(image: OneImage) {
drawingImage = scaleImage(image.image, UResolution.scaleFactor.toFloat() * ModConfig.newCurrentCrosshair.scale / 100f)
drawingImage = image.image
texture = DynamicTexture(drawingImage)
textureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)
whiteTexture = DynamicTexture(drawingImage.width, drawingImage.height)
Expand All @@ -48,13 +50,32 @@ object CrosshairRenderer {
whiteTextureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture)
}

fun updateVanilla() {
val icon = TextureUtil.readBufferedImage(mc.resourceManager.getResource(Gui.icons).inputStream)
vanilla = DynamicTexture(15, 15)
for (y in 0..15) {
for (x in 0..15) {
if (icon.getRGB(x, y) == -1) {
vanilla.textureData[x + y * 15] = -1
}
}
}
vanilla.updateDynamicTexture()
vanillaLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", vanilla)
}

@SubscribeEvent
fun cancel(event: RenderGameOverlayEvent.Pre) {
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS || !ModConfig.enabled) return
GL.enableAlpha()
event.isCanceled = true
}

@SubscribeEvent
fun onPackSwitch(event: TextureStitchEvent) {
updateVanilla()
}

fun drawCrosshair(entityRenderer: EntityRenderer) {
if (!ModConfig.enabled) return
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return
Expand All @@ -71,25 +92,24 @@ object CrosshairRenderer {

GL11.glColor4f(1f, 1f, 1f, 1f)

(if (ModConfig.mode) textureLocation else Gui.icons).let { mc.textureManager.bindTexture(it) }
(if (ModConfig.mode) textureLocation else vanillaLocation).let { mc.textureManager.bindTexture(it) }
val mcScale = UResolution.scaleFactor.toFloat()
GL.scale(1 / mcScale, 1 / mcScale, 1f)
val crosshair = ModConfig.newCurrentCrosshair
GL.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
GL.translate(UResolution.windowWidth / 2f, UResolution.windowHeight / 2f, 0f)
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
val scale = ModConfig.newCurrentCrosshair.scale / 100f
val size = if (ModConfig.mode) drawingImage.width else ceil(15 * mcScale * scale).toInt()
val textureSize = if (ModConfig.mode) size else 256
val textureSize = if (ModConfig.mode) drawingImage.width else 15
val size = ceil(textureSize * mcScale * scale).toInt()
val translation = if (crosshair.centered) (-size / 2).toFloat() else (-(size - mcScale) / 2).toInt().toFloat()
GL.translate(translation, translation, 0f)
val uv = if (ModConfig.mode) size else 15
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, uv, uv, size, size, textureSize.toFloat(), textureSize.toFloat())
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
val c = getColor()
if (c.rgb != -1) {
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, uv, uv, size, size, textureSize.toFloat(), textureSize.toFloat())
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
}
if (renderConfig.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
Expand All @@ -114,14 +134,4 @@ object CrosshairRenderer {
return WHITE
}

fun scaleImage(image: BufferedImage, scale: Float): BufferedImage {
val size = ceil(image.width * scale).toInt()
val resizedImage = BufferedImage(size, size, image.type)
val g2d = resizedImage.createGraphics()
g2d.drawImage(image, 0, 0, size, size, null)
g2d.dispose()

return resizedImage
}

}

0 comments on commit 3f088fb

Please sign in to comment.