Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Fixed rendering race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama committed Sep 11, 2023
1 parent b99c983 commit 6873fd7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
27 changes: 11 additions & 16 deletions src/main/kotlin/dev/luna5ama/trollhack/gui/AbstractTrollGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.luna5ama.trollhack.graphics.*
import dev.luna5ama.trollhack.graphics.color.ColorRGB
import dev.luna5ama.trollhack.graphics.font.renderer.MainFontRenderer
import dev.luna5ama.trollhack.graphics.shaders.ParticleShader
import dev.luna5ama.trollhack.gui.IGuiScreen.Companion.forEachWindow
import dev.luna5ama.trollhack.gui.rgui.MouseState
import dev.luna5ama.trollhack.gui.rgui.WindowComponent
import dev.luna5ama.trollhack.gui.rgui.windows.ListWindow
Expand All @@ -17,6 +18,7 @@ import dev.luna5ama.trollhack.util.Wrapper
import dev.luna5ama.trollhack.util.accessor.listShaders
import dev.luna5ama.trollhack.util.math.vector.Vec2f
import dev.luna5ama.trollhack.util.state.TimedFlag
import dev.luna5ama.trollhack.util.threads.runSynchronized
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
Expand All @@ -39,7 +41,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()

// Window
override val windows = ObjectLinkedOpenHashSet<WindowComponent>()
private val windowsCachedList = FastObjectArrayList<WindowComponent>()
override val windowsCachedList = FastObjectArrayList<WindowComponent>()

override var lastClicked: WindowComponent? = null
override var hovered: WindowComponent? = null
Expand Down Expand Up @@ -192,7 +194,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()
lastClicked = hovered

lastClicked?.let {
windows.addAndMoveToLast(it)
windows.runSynchronized { addAndMoveToLast(it) }
}
}

Expand All @@ -202,7 +204,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()
mouseState = MouseState.NONE

lastClicked?.let {
windows.addAndMoveToLast(it)
windows.runSynchronized { addAndMoveToLast(it) }
}
}

Expand All @@ -214,7 +216,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()
hovered?.onDrag(mousePos, lastClickPos, clickedMouseButton)

lastClicked?.let {
windows.addAndMoveToLast(it)
windows.runSynchronized { addAndMoveToLast(it) }
}
}
// End of mouse input
Expand Down Expand Up @@ -264,13 +266,13 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()

mc.profiler.endStartSection("windows")
GlStateUtils.rescaleTroll()
GlStateManager.translate(0.0f, -(Resolution.trollHeightF * (1.0f - multiplier)), 0.0f)
GlStateManager.translate(0.0f, -(Resolution.trollHeightF * (1.0f - multiplier)), 0.0f)
drawWindows()
drawTypedString()

mc.profiler.endStartSection("post")
GlStateUtils.rescaleMc()
GlStateManager.translate(0.0f, -(scaledResolution.scaledHeight * (1.0f - multiplier)), 0.0f)
GlStateManager.translate(0.0f, -(scaledResolution.scaledHeight * (1.0f - multiplier)), 0.0f)

glDisable(GL_DEPTH_CLAMP)
GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO)
Expand Down Expand Up @@ -328,26 +330,19 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner()
forEachWindow {
if (!it.visible) return@forEachWindow
GlStateManager.pushMatrix()
GlStateManager.translate(it.renderPosX, it.renderPosY, 0.0f)
GlStateManager.translate(it.renderPosX, it.renderPosY, 0.0f)
renderBlock(it)
GlStateManager.popMatrix()
}
}

private inline fun forEachWindow(crossinline block: (WindowComponent) -> Unit) {
windowsCachedList.addAll(windows)
for (i in windowsCachedList.indices) {
block(windowsCachedList[i])
}
windowsCachedList.clear()
}

private fun drawTypedString() {
if (searchString.isNotBlank() && System.currentTimeMillis() - renderStringPosX.time <= 5000L) {
val posX = Resolution.trollWidthF / 2.0f - renderStringPosX.get() / 2.0f
val posY = Resolution.trollHeightF / 2.0f - MainFontRenderer.getHeight(2.0f) / 2.0f
var color = GuiSetting.text
color = color.alpha(Easing.IN_CUBIC.dec(Easing.toDelta(renderStringPosX.time, 5000.0f), 0.0f, 255.0f).toInt())
color =
color.alpha(Easing.IN_CUBIC.dec(Easing.toDelta(renderStringPosX.time, 5000.0f), 0.0f, 255.0f).toInt())
MainFontRenderer.drawString(searchString, posX, posY, color, 2.0f)
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/kotlin/dev/luna5ama/trollhack/gui/IGuiScreen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.luna5ama.trollhack.gui

import dev.fastmc.common.collection.FastObjectArrayList
import dev.luna5ama.trollhack.gui.rgui.MouseState
import dev.luna5ama.trollhack.gui.rgui.WindowComponent
import dev.luna5ama.trollhack.util.math.vector.Vec2f
import dev.luna5ama.trollhack.util.threads.runSynchronized
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet

interface IGuiScreen {
Expand All @@ -16,7 +18,7 @@ interface IGuiScreen {

fun closeWindow(window: WindowComponent): Boolean {
var closed = false
if (windows.remove(window)) {
if (windows.runSynchronized { remove(window) }) {
window.onClosed()
closed = true
}
Expand All @@ -26,11 +28,23 @@ interface IGuiScreen {
}

fun displayWindow(window: WindowComponent): Boolean {
return if (windows.addAndMoveToLast(window)) {
return if (windows.runSynchronized { addAndMoveToLast(window) }) {
window.onDisplayed()
true
} else {
false
}
}

val windowsCachedList: FastObjectArrayList<WindowComponent>

companion object {
inline fun IGuiScreen.forEachWindow(crossinline block: (WindowComponent) -> Unit) {
windows.runSynchronized { windowsCachedList.addAll(this) }
for (i in windowsCachedList.indices) {
block(windowsCachedList[i])
}
windowsCachedList.clear()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.luna5ama.trollhack.module.Category
import dev.luna5ama.trollhack.module.ModuleManager
import dev.luna5ama.trollhack.module.modules.client.ClickGUI
import dev.luna5ama.trollhack.util.extension.remove
import dev.luna5ama.trollhack.util.threads.runSynchronized
import org.lwjgl.input.Keyboard

object TrollClickGui : AbstractTrollGui() {
Expand Down Expand Up @@ -55,7 +56,7 @@ object TrollClickGui : AbstractTrollGui() {
}
}

windows.addAll(moduleWindows.values)
windows.runSynchronized { addAll(moduleWindows.values) }
}

override fun onGuiClosed() {
Expand Down
18 changes: 10 additions & 8 deletions src/main/kotlin/dev/luna5ama/trollhack/gui/hudgui/TrollHudGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import dev.luna5ama.trollhack.event.listener
import dev.luna5ama.trollhack.graphics.GlStateUtils
import dev.luna5ama.trollhack.graphics.Resolution
import dev.luna5ama.trollhack.gui.AbstractTrollGui
import dev.luna5ama.trollhack.gui.IGuiScreen.Companion.forEachWindow
import dev.luna5ama.trollhack.gui.hudgui.component.HudButton
import dev.luna5ama.trollhack.gui.rgui.Component
import dev.luna5ama.trollhack.gui.rgui.windows.ListWindow
import dev.luna5ama.trollhack.module.modules.client.Hud
import dev.luna5ama.trollhack.module.modules.client.HudEditor
import dev.luna5ama.trollhack.util.extension.remove
import dev.luna5ama.trollhack.util.extension.rootName
import dev.luna5ama.trollhack.util.threads.runSynchronized
import net.minecraft.client.renderer.GlStateManager
import org.lwjgl.input.Keyboard
import org.lwjgl.opengl.GL11.*
Expand Down Expand Up @@ -59,14 +61,14 @@ object TrollHudGui : AbstractTrollGui() {

}

windows.addAll(hudWindows.values)
windows.runSynchronized { addAll(hudWindows.values) }

listener<InputEvent.Keyboard> {
if (!it.state || it.key == Keyboard.KEY_NONE || Keyboard.isKeyDown(Keyboard.KEY_F3)) return@listener

for (child in windows) {
if (child !is AbstractHudElement) continue
if (!child.bind.isDown(it.key)) continue
forEachWindow { child ->
if (child !is AbstractHudElement) return@forEachWindow
if (!child.bind.isDown(it.key)) return@forEachWindow
child.visible = !child.visible
}
}
Expand All @@ -75,12 +77,12 @@ object TrollHudGui : AbstractTrollGui() {
internal fun register(hudElement: AbstractHudElement) {
val button = HudButton(this, hudElement)
hudWindows[hudElement.category]!!.children.add(button)
windows.addAndMoveToLast(hudElement)
windows.runSynchronized { addAndMoveToLast(hudElement) }
}

internal fun unregister(hudElement: AbstractHudElement) {
hudWindows[hudElement.category]!!.children.removeIf { it is HudButton && it.hudElement == hudElement }
windows.remove(hudElement)
windows.runSynchronized { remove(hudElement) }
}

override fun onGuiClosed() {
Expand Down Expand Up @@ -110,8 +112,8 @@ object TrollHudGui : AbstractTrollGui() {
if (Hud.isEnabled) {
GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE)

for (window in windows) {
if (window !is AbstractHudElement || !window.visible) continue
forEachWindow { window ->
if (window !is AbstractHudElement || !window.visible) return@forEachWindow
mc.profiler.startSection(window.rootName)
renderHudElement(window)
mc.profiler.endSection()
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/dev/luna5ama/trollhack/gui/rgui/WindowComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.luna5ama.trollhack.setting.configs.AbstractConfig
import dev.luna5ama.trollhack.util.delegate.FrameFloat
import dev.luna5ama.trollhack.util.interfaces.Nameable
import dev.luna5ama.trollhack.util.math.vector.Vec2f
import dev.luna5ama.trollhack.util.threads.runSynchronized
import kotlin.math.max
import kotlin.math.min

Expand Down Expand Up @@ -89,13 +90,14 @@ open class WindowComponent(
if (minimizable
&& prevState != MouseState.DRAG
&& buttonId == 1
&& mousePos.y - posY < draggableHeight) minimized = !minimized
&& mousePos.y - posY < draggableHeight
) minimized = !minimized

if (mouseState != MouseState.DRAG) {
updatePreDrag(mousePos.minus(posX, posY))
}

if (dockingOverlay in screen.windows) {
if (screen.windows.runSynchronized { contains(dockingOverlay) }) {
dockingOverlay.onRelease(mousePos, clickPos, buttonId)
}
}
Expand Down Expand Up @@ -130,11 +132,12 @@ open class WindowComponent(
else -> null
}

val centerSplitterVCenter = if (draggableHeight != height && horizontalSide == dev.luna5ama.trollhack.graphics.HAlign.CENTER) {
2.5
} else {
min(15.0, preDragSize.x / 3.0)
}
val centerSplitterVCenter =
if (draggableHeight != height && horizontalSide == dev.luna5ama.trollhack.graphics.HAlign.CENTER) {
2.5
} else {
min(15.0, preDragSize.x / 3.0)
}

val verticalSide = when (relativeClickPos.y) {
in -2.0..centerSplitterVCenter -> dev.luna5ama.trollhack.graphics.VAlign.TOP
Expand Down

0 comments on commit 6873fd7

Please sign in to comment.