diff --git a/src/net/torvald/terrarum/SavegameCollection.kt b/src/net/torvald/terrarum/SavegameCollection.kt index 6c0071c27..747976058 100644 --- a/src/net/torvald/terrarum/SavegameCollection.kt +++ b/src/net/torvald/terrarum/SavegameCollection.kt @@ -132,12 +132,32 @@ fun DiskSkimmer.getTgaGz(vid: EntryID, width: Int, height: Int, shrinkage: Doubl } } } +fun DiskSkimmer.getTgaGzPixmap(vid: EntryID, width: Int, height: Int, shrinkage: Double): Pixmap? { + return this.requestFile(vid).let { file -> + if (file != null) { + val zippedTga = (file.contents as EntryFile).bytes + val gzin = GZIPInputStream(ByteArray64InputStream(zippedTga)) + val tgaFileContents = gzin.readAllBytes(); gzin.close() + val pixmap = Pixmap(tgaFileContents, 0, tgaFileContents.size) + return pixmap + } + else { + null + } + } +} fun DiskSkimmer.getThumbnail(width: Int, height: Int, shrinkage: Double) = when (this.getSaveKind()) { 1 -> this.getTgaGz(PLAYER_SCREENSHOT, width, height, shrinkage) 2 -> this.getTgaGz(WORLD_SCREENSHOT, width, height, shrinkage) else -> throw IllegalArgumentException("Unknown save kind: ${this.getSaveKind()}") } +fun DiskSkimmer.getThumbnailPixmap(width: Int, height: Int, shrinkage: Double) = + when (this.getSaveKind()) { + 1 -> this.getTgaGzPixmap(PLAYER_SCREENSHOT, width, height, shrinkage) + 2 -> this.getTgaGzPixmap(WORLD_SCREENSHOT, width, height, shrinkage) + else -> throw IllegalArgumentException("Unknown save kind: ${this.getSaveKind()}") + } class SavegameCollectionPair(private val player: SavegameCollection?, private val world: SavegameCollection?) { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadAutosave.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadAutosave.kt index 9d7edfb20..4e911b009 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadAutosave.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadAutosave.kt @@ -59,12 +59,23 @@ class UILoadAutosave(val full: UILoadSavegame) : UICanvas() { addUIitem(mainBackButton) } + private var textureManual: TextureRegion? = null + private var textureAuto: TextureRegion? = null + override fun show() { super.show() val loadables = full.loadables - val autoThumb = loadables.getAutoSave()!!.getThumbnail() - val manualThumb = loadables.getManualSave()!!.getThumbnail() + val texPlaceholder = CommonResourcePool.getAsTextureRegion("terrarum-defaultsavegamethumb") + + val pixmapManual = full.playerButtonSelected!!.pixmapManual + val pixmapAuto = full.playerButtonSelected!!.pixmapAuto + + textureManual?.texture?.tryDispose() + textureAuto?.texture?.tryDispose() + + val manualThumb = if (pixmapManual != null) TextureRegion(Texture(pixmapManual)) else texPlaceholder + val autoThumb = if (pixmapAuto != null) TextureRegion(Texture(pixmapAuto)) else texPlaceholder loadManualThumbButton = UIItemImageButton(this, manualThumb, initialX = (Toolkit.drawWidth - altSelDrawW)/2 + altSelQdrawW - imageButtonW/2, @@ -133,6 +144,10 @@ class UILoadAutosave(val full: UILoadSavegame) : UICanvas() { override fun dispose() { if (::loadAutoThumbButton.isInitialized) loadAutoThumbButton.dispose() if (::loadManualThumbButton.isInitialized) loadManualThumbButton.dispose() + + // might throw Texture already disposed + textureManual?.texture?.tryDispose() + textureAuto?.texture?.tryDispose() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt index aa177e0ad..5b647da8c 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt @@ -512,6 +512,8 @@ class UIItemPlayerCells( lateinit var worldUUID: UUID; private set private val savegameStatus: Int + internal val pixmapManual: Pixmap? + internal val pixmapAuto: Pixmap? init { App.savegamePlayers[playerUUID]!!.loadable().getFile(SAVEGAMEINFO)?.bytes?.let { @@ -533,6 +535,8 @@ class UIItemPlayerCells( val savegamePair = SavegameCollectionPair(App.savegamePlayers[playerUUID], App.savegameWorlds[worldUUID]) savegameStatus = savegamePair.status + pixmapManual = savegamePair.getManualSave()?.player?.getThumbnailPixmap(SAVE_THUMBNAIL_MAIN_WIDTH, SAVE_THUMBNAIL_MAIN_HEIGHT, 2.0) + pixmapAuto = savegamePair.getAutoSave()?.player?.getThumbnailPixmap(SAVE_THUMBNAIL_MAIN_WIDTH, SAVE_THUMBNAIL_MAIN_HEIGHT, 2.0) } private fun parseDuration(seconds: Long): String { @@ -696,6 +700,8 @@ class UIItemPlayerCells( override fun dispose() { sprite?.texture?.dispose() + pixmapManual?.dispose() + pixmapAuto?.dispose() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt index d6ea5f88c..083c81e61 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt @@ -110,7 +110,7 @@ class UILoadList(val full: UILoadSavegame) : UICanvas() { e.printStackTrace() } - Thread.sleep(18) // to alleviate the "hiccup" when the UI is being opened + Thread.sleep(1) // to alleviate the "hiccup" when the UI is being opened } printdbg(this, "============== ${this.hashCode()} ============== ") diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt index d9d9c2e39..82a88371a 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt @@ -2,6 +2,7 @@ package net.torvald.terrarum.modulebasegame.ui import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.App @@ -130,7 +131,9 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() { full.playerButtonSelected?.forceUnhighlight = true full.playerButtonSelected?.let { button -> screencap?.texture?.tryDispose() - screencap = App.savegamePlayers[button.playerUUID]!!.getThumbnail(screencapW, screencapH, 2.0) + (button.pixmapAuto ?: button.pixmapManual)?.let { + screencap = TextureRegion(Texture(it)) + } } } @@ -166,6 +169,8 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() { // draw thumbnails of the most recent game val tex = screencap ?: CommonResourcePool.getAsTextureRegion("terrarum-defaultsavegamethumb") + + val tx = (Toolkit.drawWidth - screencapW) / 2 val ty = full.titleTopGradEnd + SAVE_CELL_HEIGHT + buttonGap @@ -192,6 +197,7 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() { } override fun dispose() { + screencap?.texture?.tryDispose() } override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {