does this fix the segfault?

This commit is contained in:
minjaesong
2024-05-26 14:40:55 +09:00
parent 4607abbbd0
commit de38561eb2
2 changed files with 22 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.gameitems
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.gdx.graphics.Cvec
import net.torvald.terrarum.*
@@ -132,6 +133,8 @@ abstract class GameItem(val originalID: ItemID) : Comparable<GameItem>, Cloneabl
get() = MaterialCodex.getOrDefault(materialId)
abstract val materialId: String
@Transient private var itemImage0: TextureRegion? = null
/**
* DO NOT READ FROM THIS VALUE: USE `ItemCodex.getItemImage(item)`;
* this hack is needed to avoid the unsolvable issue regarding the ItemImage of the tiles, of which they
@@ -160,23 +163,32 @@ abstract class GameItem(val originalID: ItemID) : Comparable<GameItem>, Cloneabl
* ```
*
*/
@Transient var itemImage: TextureRegion? = null
set(tex) {
field = tex
tex?.let {
val texdata = tex.texture.textureData.also {
var itemImage: TextureRegion?
get() = itemImage0
set(textureRegion) {
itemImage0 = textureRegion
textureRegion?.let {
val texdata = textureRegion.texture.textureData.also {
if (!it.isPrepared) it.prepare()
}
itemImagePixmap = Pixmap(tex.regionWidth, tex.regionHeight, texdata.format).also {
itemImagePixmap = Pixmap(textureRegion.regionWidth, textureRegion.regionHeight, texdata.format).also {
it.drawPixmap(
texdata.consumePixmap(),
0, 0, tex.regionX, tex.regionY, tex.regionWidth, tex.regionHeight
0, 0, textureRegion.regionX, textureRegion.regionY, textureRegion.regionWidth, textureRegion.regionHeight
)
App.disposables.add(it)
}
}
}
fun setItemImage(pixmap: Pixmap) {
val texture = TextureRegion(Texture(pixmap))
App.disposables.add(texture.texture)
this.itemImage0 = texture
this.itemImagePixmap = pixmap
}
@Transient open var itemImagePixmap: Pixmap? = null; internal set
@Transient open val itemImageGlow: TextureRegion? = null
@Transient open val itemImageEmissive: TextureRegion? = null

View File

@@ -53,13 +53,13 @@ open class MusicDiscPrototype(originalID: ItemID, module: String, path: String)
}
init {
itemImage = generateSprite()
setItemImage(generateSprite())
}
/**
* Reads a channel-wise black and white image and tints it using HSLuv colour space
*/
private fun generateSprite(): TextureRegion {
private fun generateSprite(): Pixmap {
val authorHash = XXHash64.hash(author.encodeToByteArray(), 54)
val albumHash = XXHash64.hash(collection.encodeToByteArray(), 32)
val nameHash = XXHash64.hash(name.encodeToByteArray(), 10)
@@ -129,10 +129,7 @@ open class MusicDiscPrototype(originalID: ItemID, module: String, path: String)
}
}
val ret = TextureRegion(Texture(pixmap))
pixmap.dispose()
return ret
return pixmap
}
}