From c12ed70e5562a80adaaeaff7a272f4fd6b436eab Mon Sep 17 00:00:00 2001 From: Saad Nadeem Date: Sat, 21 Nov 2026 15:53:16 -0500 Subject: [PATCH 1/2] fix(items): show new HUD item icons on their first frame --- core/api/core.api | 4 ++++ .../kotlin/org/polyfrost/compose/host/ContentScale.kt | 6 ++++++ .../org/polyfrost/compose/minecraft/mc/McItemAtlas.kt | 11 ++++++++--- .../org/polyfrost/compose/minecraft/mc/McItemIcon.kt | 10 ++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 core/src/main/kotlin/org/polyfrost/compose/host/ContentScale.kt diff --git a/core/api/core.api b/core/api/core.api index 6d06217..847671f 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -718,6 +718,10 @@ public final class org/polyfrost/compose/host/ComposeOptionsKt { public static final fun getLocalComposeOptions ()Landroidx/compose/runtime/ProvidableCompositionLocal; } +public final class org/polyfrost/compose/host/ContentScaleKt { + public static final fun getLocalContentScale ()Landroidx/compose/runtime/ProvidableCompositionLocal; +} + public abstract interface class org/polyfrost/compose/host/GameInputLayer { public static final field ABOVE_HUD_LAYERS I public static final field BELOW_HUD_LAYERS I diff --git a/core/src/main/kotlin/org/polyfrost/compose/host/ContentScale.kt b/core/src/main/kotlin/org/polyfrost/compose/host/ContentScale.kt new file mode 100644 index 0000000..99ae3ae --- /dev/null +++ b/core/src/main/kotlin/org/polyfrost/compose/host/ContentScale.kt @@ -0,0 +1,6 @@ +package org.polyfrost.compose.host + +import androidx.compose.runtime.ProvidableCompositionLocal +import androidx.compose.runtime.compositionLocalOf + +public val LocalContentScale: ProvidableCompositionLocal = compositionLocalOf { 1f } diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemAtlas.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemAtlas.kt index d99bdbe..505a7f6 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemAtlas.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemAtlas.kt @@ -379,8 +379,13 @@ internal object McItemAtlas { return entry } - private fun renderPx(ctx: RenderContext, size: Float): Int { - return ceil(size * ctx.deviceScale() - 0.01f).toInt().coerceAtLeast(1) + private fun renderPx(ctx: RenderContext, size: Float): Int = renderPx(size * ctx.deviceScale()) + + private fun renderPx(devicePx: Float): Int = ceil(devicePx - 0.01f).toInt().coerceAtLeast(1) + + fun requestSize(handle: McItemIconHandle, devicePx: Float) { + val entry = handle as? IconEntry ?: return + if (entry.requestedSizePx == 0) entry.setRenderSizePx(renderPx(devicePx)) } private fun ensureHooks() { @@ -535,7 +540,7 @@ internal object McItemAtlas { if (active.none { it !== entry && it.failedStack === variant }) failedVariants.removeAll { it === variant } } - private fun renderAll() { + fun renderAll() { if (!available.value || renderingFailed) return try { reconcileTopology() diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt index 5edf374..3a589d8 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt @@ -21,6 +21,8 @@ import org.polyfrost.compose.backend.drawPoly import org.polyfrost.compose.composables.NodeCanvas import org.polyfrost.compose.composables.PolyModifier import org.polyfrost.compose.composables.size +import org.polyfrost.compose.host.LocalContentScale +import org.polyfrost.compose.minecraft.platform.McWindow import org.polyfrost.compose.render.RenderContext object McItemIcons { @@ -49,6 +51,12 @@ object McItemIcons { McItemAtlas.requestAvailability() } + /** Renders icons needed by HUD consumers before the HUD dirty gate. */ + @JvmStatic + fun renderPending() { + McItemAtlas.renderAll() + } + @JvmStatic @JvmOverloads fun drawDecorations(ctx: RenderContext, stack: ItemStack, x: Float, y: Float, size: Float = ITEM_SIZE, countOverride: String? = null) { @@ -129,6 +137,8 @@ fun NodeMcItemIcon( alpha: Float = 1f, ) { val handle = rememberItemIconHandle(stack, decorations) + val scale = LocalContentScale.current + SideEffect { McItemAtlas.requestSize(handle, size * McWindow.guiScale() * scale) } val revision = handle.revision val available = McItemIcons.available val onDraw: RenderContext.(Float, Float, Float, Float) -> Unit = remember(handle, revision, available, decorations, countOverride, alpha) { From bedd48a7c1f5897310844f07454f138c5abde66f Mon Sep 17 00:00:00 2001 From: Deftu Date: Sat, 21 Nov 2026 23:10:06 +0200 Subject: [PATCH 2/2] fix(items): request atlas size for McItemIcon before the first draw --- .../kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt index 3a589d8..9803191 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.SideEffect import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp //? if > 1.8.9 { @@ -96,6 +97,8 @@ fun McItemIcon( alpha: Float = 1f, ) { val handle = rememberItemIconHandle(stack, decorations) + val density = LocalDensity.current + SideEffect { McItemAtlas.requestSize(handle, with(density) { size.toPx() }) } Box( modifier .size(size)