Skip to content

Commit

Permalink
perf(render): Create dynamic texture for resources so swaps are only …
Browse files Browse the repository at this point in the history
…done on resource layer when we need the number (strategic resources)
  • Loading branch information
yairm210 committed Jan 24, 2025
1 parent 31aaf3c commit 9f8c93d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ abstract class TileLayer(val tileGroup: TileGroup, size: Float) : Group() {
protected abstract fun doUpdate(
viewingCiv: Civilization?,
localUniqueCache: LocalUniqueCache = LocalUniqueCache(false))

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import com.unciv.ui.components.MapArrowType
import com.unciv.ui.components.MiscArrowTypes
import com.unciv.ui.components.TintedMapArrow
import com.unciv.ui.components.UnitMovementMemoryType
import com.unciv.ui.components.extensions.brighten
import com.unciv.ui.components.extensions.center
import com.unciv.ui.components.extensions.centerX
import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.extensions.toPrettyString
import com.unciv.ui.components.extensions.*
import com.unciv.ui.components.tilegroups.CityTileGroup
import com.unciv.ui.components.tilegroups.TileGroup
import com.unciv.ui.components.tilegroups.TileSetStrings
Expand Down Expand Up @@ -124,17 +120,31 @@ class TileLayerResource(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup
if (resourceName != tile.resource || resourceAmount != tile.resourceAmount) {
resourceName = tile.resource
resourceAmount = tile.resourceAmount
resourceIcon?.remove()
clear()
resourceIcon = null
}

// Get a fresh Icon if and only if necessary
if (resourceName != null && effectiveVisible && resourceIcon == null) {
val icon = ImageGetter.getResourcePortrait(resourceName!!, 20f, resourceAmount)
val icon = ImageGetter.getImage(resourceName).apply { setSize(24f) }
icon.center(tileGroup)
icon.x -= 22 // left
icon.y += 10 // top
addActor(icon)

if (resourceAmount > 0) {
val label = resourceAmount.tr().toLabel(
fontSize = 8,
fontColor = Color.WHITE,
alignment = Align.center)
val amountGroup = label.surroundWithCircle(10f, true, ImageGetter.CHARCOAL)

label.y -= 0.5f
amountGroup.setPosition(icon.x + icon.width, icon.y, Align.bottomRight)

addActor(amountGroup)
}

resourceIcon = icon
}

Expand All @@ -151,7 +161,7 @@ class TileLayerResource(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup
updateResourceIcon(null, false)
}

fun dimResource(dim: Boolean) { resourceIcon?.color?.a = if (dim) 0.5f else 1f }
private fun dimResource(dim: Boolean) { resourceIcon?.color?.a = if (dim) 0.5f else 1f }

override fun doUpdate(viewingCiv: Civilization?, localUniqueCache: LocalUniqueCache) {
val showResourcesAndImprovements = if (tileGroup is WorldTileGroup)
Expand All @@ -163,6 +173,10 @@ class TileLayerResource(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup
override fun determineVisibility() {
isVisible = resourceIcon?.isVisible == true
}

override fun act(delta: Float) {}
override fun hit(x: Float, y: Float, touchable: Boolean): Actor? = null
override fun draw(batch: Batch?, parentAlpha: Float) = super.draw(batch, parentAlpha)
}


Expand Down
16 changes: 11 additions & 5 deletions core/src/com/unciv/ui/images/ImageGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ object ImageGetter {
FontRulesetIcons.addRulesetImages(ruleset)

setupStatImages()
setupResourcePortraits()
}

private fun setupStatImages() {
Expand All @@ -107,14 +108,19 @@ object ImageGetter {

val nameToActorList = Stat.entries.map { it.name to getActor(it.name) }

packTexture(nameToActorList)
packTexture(nameToActorList, 100)
}

private fun setupResourcePortraits() {
val nameToActorList = ruleset.tileResources.values.map { it.name to getResourcePortrait(it.name, 100f, borderSize = 10f) }
packTexture(nameToActorList, 120)
}

private fun packTexture(nameToActorList: List<Pair<String, IconCircleGroup>>) {
private fun packTexture(nameToActorList: List<Pair<String, Group>>, size: Int) {
val pixmapPacker = PixmapPacker(2048, 2048, Pixmap.Format.RGBA8888, 2, false).apply { packToTexture = true }
for ((name, actor) in nameToActorList) {
actor.apply { isTransform = true; setScale(1f, -1f); setPosition(0f, height) } // flip Y axis
pixmapPacker.pack(name, FontRulesetIcons.getPixmapFromActorBase(actor, 100, 100))
pixmapPacker.pack(name, FontRulesetIcons.getPixmapFromActorBase(actor, size, size))
}

val yieldAtlas = pixmapPacker.generateTextureAtlas(
Expand Down Expand Up @@ -316,8 +322,8 @@ object ImageGetter {

fun getPromotionPortrait(promotionName: String, size: Float = 30f): Group = PortraitPromotion(promotionName, size)

fun getResourcePortrait(resourceName: String, size: Float, amount: Int = 0): Group =
PortraitResource(resourceName, size, amount)
fun getResourcePortrait(resourceName: String, size: Float, borderSize: Float = 2f): Group =
PortraitResource(resourceName, size, borderSize)

fun getTechIconPortrait(techName: String, circleSize: Float): Group = PortraitTech(techName, circleSize)

Expand Down
27 changes: 2 additions & 25 deletions core/src/com/unciv/ui/images/Portrait.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ import com.badlogic.gdx.utils.Align
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.unit.Promotion
import com.unciv.models.stats.Stats
import com.unciv.models.translations.tr
import com.unciv.ui.components.extensions.center
import com.unciv.ui.components.extensions.centerX
import com.unciv.ui.components.extensions.colorFromRGB
import com.unciv.ui.components.extensions.darken
import com.unciv.ui.components.extensions.surroundWithCircle
import com.unciv.ui.components.extensions.toGroup
import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.extensions.*

/**
* ### Manages "portraits" for a subset of RulesetObjects
Expand Down Expand Up @@ -145,23 +138,7 @@ open class Portrait(val type: Type, val imageName: String, val size: Float, val

}

class PortraitResource(name: String, size: Float, amount: Int = 0) : Portrait(Type.Resource, name, size) {

init {
if (amount > 0) {
val label = amount.tr().toLabel(
fontSize = 8,
fontColor = Color.WHITE,
alignment = Align.center)
val amountGroup = label.surroundWithCircle(size/2, true, ImageGetter.CHARCOAL)

label.y -= 0.5f
amountGroup.x = width - amountGroup.width * 3 / 4
amountGroup.y = -amountGroup.height / 4

addActor(amountGroup)
}
}
class PortraitResource(name: String, size: Float, borderSize: Float) : Portrait(Type.Resource, name, size, borderSize) {

override fun getDefaultInnerBackgroundTint(): Color =
ruleset.tileResources[imageName]?.resourceType?.getColor() ?: Color.WHITE
Expand Down

0 comments on commit 9f8c93d

Please sign in to comment.