From 1b0b279e3d9c5d23ea50140da83c215b27f84576 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 14 Sep 2023 13:33:24 +0900 Subject: [PATCH] fix: new world from world portal not updating world ref of IngameRenderer --- .../terrarum/modulebasegame/IngameRenderer.kt | 29 ++++--- .../modulebasegame/WorldgenLoadScreen.kt | 2 + .../terrarum/worlddrawer/BlocksDrawer.kt | 14 ++-- .../terrarum/worlddrawer/LightmapRenderer.kt | 79 ++++++++++--------- 4 files changed, 68 insertions(+), 56 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index 7afb61a84..375634c6a 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -175,26 +175,29 @@ object IngameRenderer : Disposable { * - When the game make transition to the new world (advancing to the next level/entering or exiting the room) */ fun setRenderedWorld(world: GameWorld) { - try { - if (this.world != world) { + printdbg(this, "Set new RenderedWorld (UUID=${world.worldIndex}) at time ${System.currentTimeMillis()}, called by:") + printStackTrace(this) + + try { + if (this.world != world) { // printdbg(this, "World change detected -- " + // "old world: ${this.world.hashCode()}, " + // "new world: ${world.hashCode()}") - // change worlds from internal methods - this.world = world - LightmapRenderer.internalSetWorld(world) - BlocksDrawer.world = world - FeaturesDrawer.world = world - - newWorldLoadedLatch = true - } - } - catch (e: UninitializedPropertyAccessException) { - // new init, do nothing + // change worlds from internal methods this.world = world + LightmapRenderer.internalSetWorld(world) + BlocksDrawer.world = world + FeaturesDrawer.world = world + + newWorldLoadedLatch = true } } + catch (e: UninitializedPropertyAccessException) { + // new init, do nothing + this.world = world + } + } private var oldCamX = 0 diff --git a/src/net/torvald/terrarum/modulebasegame/WorldgenLoadScreen.kt b/src/net/torvald/terrarum/modulebasegame/WorldgenLoadScreen.kt index 1d0ff1374..6eb333da3 100644 --- a/src/net/torvald/terrarum/modulebasegame/WorldgenLoadScreen.kt +++ b/src/net/torvald/terrarum/modulebasegame/WorldgenLoadScreen.kt @@ -52,6 +52,8 @@ class WorldgenLoadScreen(screenToBeLoaded: IngameInstance, private val worldwidt previewPixmap.setColor(Color.BLACK) previewPixmap.fill() + + IngameRenderer.setRenderedWorld(world) } override fun render(delta: Float) { diff --git a/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt b/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt index bf1ec677f..c8b2182af 100644 --- a/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt +++ b/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt @@ -201,12 +201,14 @@ internal object BlocksDrawer { } catch (e: ClassCastException) { } - measureDebugTime("Renderer.Tiling") { - drawTiles(WALL) - drawTiles(TERRAIN) // regular tiles - drawTiles(FLUID) - drawTiles(OCCLUSION) - //drawTiles(WIRE) + if (!world.layerTerrain.ptrDestroyed) { + measureDebugTime("Renderer.Tiling") { + drawTiles(WALL) + drawTiles(TERRAIN) // regular tiles + drawTiles(FLUID) + drawTiles(OCCLUSION) + //drawTiles(WIRE) + } } } diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt index 98c8c9166..c1e14eb04 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt @@ -139,7 +139,9 @@ object LightmapRenderer { printdbg(this, "Overscan open: $overscan_open; opaque: $overscan_opaque") } - fun recalculate(actorContainer: List) = _recalculate(actorContainer, lightmap) + fun recalculate(actorContainer: List) { + if (!world.layerTerrain.ptrDestroyed) _recalculate(actorContainer, lightmap) + } private fun _recalculate(actorContainer: List, lightmap: UnsafeCvecArray) { try { @@ -661,35 +663,37 @@ object LightmapRenderer { internal fun draw(): Texture { - // when shader is not used: 0.5 ms on 6700K - App.measureDebugTime("Renderer.LightToScreen") { + if (!world.layerTerrain.ptrDestroyed) { + // when shader is not used: 0.5 ms on 6700K + App.measureDebugTime("Renderer.LightToScreen") { - val this_x_start = for_draw_x_start - val this_y_start = for_draw_y_start - val this_x_end = for_draw_x_end - val this_y_end = for_draw_y_end + val this_x_start = for_draw_x_start + val this_y_start = for_draw_y_start + val this_x_end = for_draw_x_end + val this_y_end = for_draw_y_end - // wipe out beforehand. You DO need this - lightBuffer.blending = Pixmap.Blending.None // gonna overwrite (remove this line causes the world to go bit darker) - lightBuffer.setColor(0) - lightBuffer.fill() + // wipe out beforehand. You DO need this + lightBuffer.blending = + Pixmap.Blending.None // gonna overwrite (remove this line causes the world to go bit darker) + lightBuffer.setColor(0) + lightBuffer.fill() - // write to colour buffer - for (y in this_y_start..this_y_end) { - //println("y: $y, this_y_start: $this_y_start") - //if (y == this_y_start && this_y_start == 0) { - // throw Error("Fuck hits again...") - //} + // write to colour buffer + for (y in this_y_start..this_y_end) { + //println("y: $y, this_y_start: $this_y_start") + //if (y == this_y_start && this_y_start == 0) { + // throw Error("Fuck hits again...") + //} - for (x in this_x_start..this_x_end) { + for (x in this_x_start..this_x_end) { - val solidMultMagic = isSolid(x, y) // one of {1.2f, 1f, null} + val solidMultMagic = isSolid(x, y) // one of {1.2f, 1f, null} - val arrayX = x.convX() - val arrayY = y.convY() + val arrayX = x.convX() + val arrayY = y.convY() - val (red, grn, blu, uvl) = lightmap.getVec(arrayX, arrayY) + val (red, grn, blu, uvl) = lightmap.getVec(arrayX, arrayY) // val redw = (red.sqrt() - 1f) * (7f / 24f) // val grnw = (grn.sqrt() - 1f) // val bluw = (blu.sqrt() - 1f) * (7f / 72f) @@ -698,13 +702,13 @@ object LightmapRenderer { // val uvlwg = (uvl.sqrt() - 1f) * (1f / 10f) // val uvlwb = (uvl.sqrt() - 1f) * (1f / 8f) - if (solidMultMagic == null) - lightBuffer.drawPixel( - x - this_x_start, - lightBuffer.height - 1 - y + this_y_start, // flip Y - 0 - ) - else + if (solidMultMagic == null) + lightBuffer.drawPixel( + x - this_x_start, + lightBuffer.height - 1 - y + this_y_start, // flip Y + 0 + ) + else /*lightBuffer.drawPixel( x - this_x_start, lightBuffer.height - 1 - y + this_y_start, // flip Y @@ -713,28 +717,29 @@ object LightmapRenderer { (max(redw,grnw,blu,uvlwb) * solidMultMagic).hdnorm().times(255f).roundToInt().shl(8) or (max(bluwv,uvl) * solidMultMagic).hdnorm().times(255f).roundToInt() )*/ - lightBuffer.drawPixel( + lightBuffer.drawPixel( x - this_x_start, lightBuffer.height - 1 - y + this_y_start, // flip Y (red * solidMultMagic).hdnorm().times(255f).roundToInt().shl(24) or (grn * solidMultMagic).hdnorm().times(255f).roundToInt().shl(16) or (blu * solidMultMagic).hdnorm().times(255f).roundToInt().shl(8) or (uvl * solidMultMagic).hdnorm().times(255f).roundToInt() - ) + ) + } } - } - // draw to the batch - _lightBufferAsTex.dispose() - _lightBufferAsTex = Texture(lightBuffer) - _lightBufferAsTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) + // draw to the batch + _lightBufferAsTex.dispose() + _lightBufferAsTex = Texture(lightBuffer) + _lightBufferAsTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) - /*Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it + /*Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it // we might not need shader here... //batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width.toFloat(), lightBufferAsTex.height.toFloat()) batch.draw(_lightBufferAsTex, 0f, 0f, _lightBufferAsTex.width * DRAW_TILE_SIZE, _lightBufferAsTex.height * DRAW_TILE_SIZE) */ + } } return _lightBufferAsTex