Skip to content

Commit

Permalink
Fix crosshair being off-center and scaled weirdly
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Jun 23, 2024
1 parent ee59a8a commit f9807c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
override fun getHeight() = 256

override fun keyTyped(key: Char, keyCode: Int) {

if (mc.currentScreen !is OneConfigGui) return
if (keyCode == UKeyboard.KEY_W) moveQueue.add(MoveType.UP)
if (keyCode == UKeyboard.KEY_S) {
if (UKeyboard.isCtrlKeyDown()) {
Expand Down
60 changes: 43 additions & 17 deletions src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import cc.polyfrost.oneconfig.images.OneImage
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.*
import net.minecraft.client.renderer.EntityRenderer
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraft.entity.monster.IMob
import net.minecraft.entity.passive.*
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.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11
import org.polyfrost.crosshair.config.*
import org.polyfrost.crosshair.config.ModConfig
import org.polyfrost.crosshair.mixin.GuiIngameAccessor
import java.awt.Color
import java.awt.image.BufferedImage

object CrosshairRenderer {
private var texture = DynamicTexture(15, 15)
Expand All @@ -25,15 +30,19 @@ object CrosshairRenderer {
private var whiteTextureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture)

fun updateTexture(image: OneImage) {
texture = DynamicTexture(image.image)
val paddedImage = if (ModConfig.canvaSize % 2 == 1) {
addPixel(image.image)
} else {
image.image
}
texture = DynamicTexture(paddedImage)
textureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)
val size = ModConfig.canvaSize
whiteTexture = DynamicTexture(size, size)
for (posY in 0..<size) {
for (posX in 0..<size) {
val color = image.image.getRGB(posX, posY)
whiteTexture = DynamicTexture(paddedImage.width, paddedImage.height)
for (posY in 0..<paddedImage.height) {
for (posX in 0..<paddedImage.width) {
val color = paddedImage.getRGB(posX, posY)
if (color shr 24 == 0) continue
whiteTexture.textureData[posX + posY * size] = -1
whiteTexture.textureData[posX + posY * paddedImage.width] = -1
}
}
whiteTexture.updateDynamicTexture()
Expand Down Expand Up @@ -69,17 +78,17 @@ object CrosshairRenderer {
val crosshair = ModConfig.newCurrentCrosshair
GlStateManager.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
GlStateManager.scale(mcScale, mcScale, 1f)
GlStateManager.translate(UResolution.scaledWidth / 2f, UResolution.scaledHeight / 2f, 0f)
GlStateManager.translate((UResolution.scaledWidth / 2).toDouble(), (UResolution.scaledHeight / 2).toDouble(), 0.0)
GlStateManager.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
GlStateManager.scale(crosshair.scale / 100f, crosshair.scale / 100f, 1f)
val size = ModConfig.canvaSize
GlStateManager.translate(-size / 2f, -size / 2f, 0f)
Gui.drawModalRectWithCustomSizedTexture(0, 0, 0f, 0f, size, size, size.toFloat(), size.toFloat())
val padded = ModConfig.canvaSize % 2 == 1
val size = ModConfig.canvaSize + if (padded) 1 else 0
Gui.drawModalRectWithCustomSizedTexture(0 - 7 - (ModConfig.canvaSize - 15) / 2, 0 - 7 - (ModConfig.canvaSize - 15) / 2, 0f, 0f, size, size, size.toFloat(), size.toFloat())
val c = getColor()
if (c.toJavaColor() != Color(-1)) {
if (c.rgb != -1) {
mc.textureManager.bindTexture(whiteTextureLocation)
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
Gui.drawModalRectWithCustomSizedTexture(0, 0, 0f, 0f, size, size, size.toFloat(), size.toFloat())
Gui.drawModalRectWithCustomSizedTexture(0 - 7 - (ModConfig.canvaSize - 15) / 2, 0 - 7 - (ModConfig.canvaSize - 15) / 2, 0f, 0f, size, size, size.toFloat(), size.toFloat())
}
if (renderConfig.invertColor) {
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
Expand All @@ -91,7 +100,7 @@ object CrosshairRenderer {

val WHITE = OneColor(-1)

fun getColor(): OneColor {
private fun getColor(): OneColor {
with(ModConfig.renderConfig) {
val entity = mc.pointedEntity ?: return WHITE
if (entity.isInvisible) return WHITE
Expand All @@ -103,4 +112,21 @@ object CrosshairRenderer {
}
return WHITE
}

private fun addPixel(original: BufferedImage): BufferedImage {
// Create a new BufferedImage with 1 pixel added to width and height
val newWidth = original.width + 1
val newHeight = original.height + 1
val resizedImage = BufferedImage(newWidth, newHeight, original.type)

// Get the Graphics2D object to draw on the new BufferedImage
val g2d = resizedImage.createGraphics()

// Draw the original image onto the new image
g2d.drawImage(original, 0, 0, null)

// Dispose of the Graphics2D object to release resources
g2d.dispose()
return resizedImage
}
}
12 changes: 6 additions & 6 deletions src/main/resources/polycrosshair.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f9807c7

Please sign in to comment.