From 173f99f87d5e8e7adfafa81b0ac3c6766a9bd1e1 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 11 Jul 2023 19:52:56 +0900 Subject: [PATCH] two getthumbnail funs merget into one --- .../torvald/terrarum/SavegameCollection.kt | 50 +++++++++++-------- .../terrarum/modulebasegame/IngameRenderer.kt | 26 +++++----- .../modulebasegame/ui/UILoadDemoSavefiles.kt | 14 ++++-- .../modulebasegame/ui/UILoadManage.kt | 6 +-- .../torvald/terrarum/savegame/DiskSkimmer.kt | 8 +++ .../torvald/terrarum/ui/UIAutosaveNotifier.kt | 2 +- 6 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/net/torvald/terrarum/SavegameCollection.kt b/src/net/torvald/terrarum/SavegameCollection.kt index c462c663b..6c0071c27 100644 --- a/src/net/torvald/terrarum/SavegameCollection.kt +++ b/src/net/torvald/terrarum/SavegameCollection.kt @@ -106,32 +106,38 @@ class SavegameCollection(files0: List) { } } - private fun getThumbnail0(vid: EntryID, width: Int, height: Int, shrinkage: Double): TextureRegion? { - return this.loadable().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) - TextureRegion(Texture(pixmap)).also { - App.disposables.add(it.texture) - // do cropping and resizing - it.setRegion( - ((pixmap.width - width*2) / shrinkage).roundToInt(), - ((pixmap.height - height*2) / shrinkage).roundToInt(), - (width * shrinkage).roundToInt(), - (height * shrinkage).roundToInt() - ) - } - } - else { - null + fun getThumbnail(width: Int, height: Int, shrinkage: Double) = this.loadable().getThumbnail(width, height, shrinkage) +} + +fun DiskSkimmer.getTgaGz(vid: EntryID, width: Int, height: Int, shrinkage: Double): TextureRegion? { + 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) + TextureRegion(Texture(pixmap)).also { + App.disposables.add(it.texture) + // do cropping and resizing + it.setRegion( + ((pixmap.width - width*2) / shrinkage).roundToInt(), + ((pixmap.height - height*2) / shrinkage).roundToInt(), + (width * shrinkage).roundToInt(), + (height * shrinkage).roundToInt() + ) } } + else { + null + } } - fun getPlayerThumbnail(width: Int, height: Int, shrinkage: Double) = getThumbnail0(PLAYER_SCREENSHOT, width, height, shrinkage) - fun getWorldThumbnail(width: Int, height: Int, shrinkage: Double) = getThumbnail0(WORLD_SCREENSHOT, width, height, shrinkage) } +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()}") + } class SavegameCollectionPair(private val player: SavegameCollection?, private val world: SavegameCollection?) { diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index 45153a239..f3b35521c 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -862,27 +862,27 @@ object IngameRenderer : Disposable { } override fun dispose() { - blurWriteQuad.tryDispose() - blurWriteQuad2.tryDispose() - //blurWriteQuad4.tryDispose() + if (::blurWriteQuad.isInitialized) blurWriteQuad.tryDispose() + if (::blurWriteQuad2.isInitialized) blurWriteQuad2.tryDispose() + //if (::blurWriteQuad4.isInitialized) blurWriteQuad4.tryDispose() - fboRGB.tryDispose() - fboA.tryDispose() - fboRGB_lightMixed.tryDispose() - fboA_lightMixed.tryDispose() - fboMixedOut.tryDispose() - lightmapFbo.tryDispose() + if (::fboRGB.isInitialized) fboRGB.tryDispose() + if (::fboA.isInitialized) fboA.tryDispose() + if (::fboRGB_lightMixed.isInitialized) fboRGB_lightMixed.tryDispose() + if (::fboA_lightMixed.isInitialized) fboA_lightMixed.tryDispose() + if (::fboMixedOut.isInitialized) fboMixedOut.tryDispose() + if (::lightmapFbo.isInitialized) lightmapFbo.tryDispose() blurtex0.tryDispose() - fboBlurHalf.tryDispose() - //fboBlurQuarter.tryDispose() + if (::fboBlurHalf.isInitialized) fboBlurHalf.tryDispose() + //if (::fboBlurQuarter.isInitialized) fboBlurQuarter.tryDispose() LightmapRenderer.dispose() BlocksDrawer.dispose() WeatherMixer.dispose() - batch.tryDispose() + if (::batch.isInitialized) batch.tryDispose() shaderBlur.dispose() @@ -896,7 +896,7 @@ object IngameRenderer : Disposable { shaderForActors.dispose() shaderDemultiply.dispose() - fboRGBexport.tryDispose() + if (::fboRGBexport.isInitialized) fboRGBexport.tryDispose() } private fun worldCamToRenderPos(): Pair { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt index 65d3a5c4e..aa177e0ad 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt @@ -45,6 +45,8 @@ import kotlin.math.roundToInt val SAVE_CELL_WIDTH = 480 val SAVE_CELL_HEIGHT = 120 +val SAVE_THUMBNAIL_MAIN_WIDTH = 480 +val SAVE_THUMBNAIL_MAIN_HEIGHT = 320 /** * The pinnacle of the dirty coding! This object exists only because I couldn't make @@ -485,10 +487,11 @@ class UILoadDemoSavefiles(val remoCon: UIRemoCon) : Advanceable() { class UIItemPlayerCells( - parent: Advanceable, - initialX: Int, - initialY: Int, - val playerUUID: UUID) : UIItem(parent, initialX, initialY) { + parent: Advanceable, + initialX: Int, + initialY: Int, + val playerUUID: UUID, +) : UIItem(parent, initialX, initialY) { override val width = SAVE_CELL_WIDTH override val height = SAVE_CELL_HEIGHT @@ -528,7 +531,8 @@ class UIItemPlayerCells( } - savegameStatus = SavegameCollectionPair(App.savegamePlayers[playerUUID], App.savegameWorlds[worldUUID]).status + val savegamePair = SavegameCollectionPair(App.savegamePlayers[playerUUID], App.savegameWorlds[worldUUID]) + savegameStatus = savegamePair.status } private fun parseDuration(seconds: Long): String { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt index 0c43745bf..d9d9c2e39 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt @@ -123,14 +123,14 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() { } private var screencap: TextureRegion? = null - private val screencapW = SAVE_CELL_WIDTH - private val screencapH = SAVE_CELL_HEIGHT * 2 + private val screencapW = SAVE_THUMBNAIL_MAIN_WIDTH + private val screencapH = SAVE_THUMBNAIL_MAIN_HEIGHT override fun doOpening(delta: Float) { full.playerButtonSelected?.forceUnhighlight = true full.playerButtonSelected?.let { button -> screencap?.texture?.tryDispose() - screencap = App.savegamePlayers[button.playerUUID]!!.getPlayerThumbnail(screencapW, screencapH, 2.0) + screencap = App.savegamePlayers[button.playerUUID]!!.getThumbnail(screencapW, screencapH, 2.0) } } diff --git a/src/net/torvald/terrarum/savegame/DiskSkimmer.kt b/src/net/torvald/terrarum/savegame/DiskSkimmer.kt index cb387878a..73a94a591 100644 --- a/src/net/torvald/terrarum/savegame/DiskSkimmer.kt +++ b/src/net/torvald/terrarum/savegame/DiskSkimmer.kt @@ -447,12 +447,20 @@ removefile: fa.close() } + /** + * @return Save type (0b 0000 00ab) + * b: unset - full save; set - quicksave (only applicable to worlds -- quicksave just means the disk is in dirty state) + * a: set - generated by autosave + */ fun getSaveMode(): Int { val fa = RandomAccessFile(diskFile, "rwd") fa.seek(49L) return fa.read().also { fa.close() } } + /** + * @return 1 if the savegame is player data, 2 if the savegame is world data, 0 otherwise + */ fun getSaveKind(): Int { val fa = RandomAccessFile(diskFile, "rwd") fa.seek(50L) diff --git a/src/net/torvald/terrarum/ui/UIAutosaveNotifier.kt b/src/net/torvald/terrarum/ui/UIAutosaveNotifier.kt index 31599b6a5..b56ac97c3 100644 --- a/src/net/torvald/terrarum/ui/UIAutosaveNotifier.kt +++ b/src/net/torvald/terrarum/ui/UIAutosaveNotifier.kt @@ -62,7 +62,7 @@ class UIAutosaveNotifier : UICanvas() { } else { batch.color = errorCol - batch.draw(spinner.get(0,4), offX, offY) + batch.draw(spinner.get(0,4), offX, offY + 2f) } batch.color = if (errored) errorCol else normalCol App.fontGame.draw(batch, text, offX + 30f, offY - 2f)