From 10fdc34ffd75a5be9e2f2426079fb8dccac991e2 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 4 Feb 2022 12:46:12 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B0=D1=80=D0=BE=D1=87=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=BB=D0=B1=D0=B5=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/dbotthepony/kstarbound/Main.kt | 96 ++++++++++--------- .../ru/dbotthepony/kstarbound/Starbound.kt | 22 ++++- 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt index 014bb628..33752453 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt @@ -8,6 +8,7 @@ import org.lwjgl.glfw.GLFWErrorCallback import org.lwjgl.opengl.GL46.* import org.lwjgl.system.MemoryStack.stackPush import org.lwjgl.system.MemoryUtil.NULL +import ru.dbotthepony.kstarbound.defs.TileDefinition import ru.dbotthepony.kstarbound.gl.* import ru.dbotthepony.kstarbound.math.* import ru.dbotthepony.kstarbound.render.* @@ -126,7 +127,10 @@ private fun init() { glfwShowWindow(window) + startupTextList.add("Initialized GLFW window") + Starbound.addFilePath(File("./unpacked_assets/")) + Starbound.initializeGame { finished, replaceStatus, status -> if (replaceStatus) { if (startupTextList.isEmpty()) { @@ -139,7 +143,7 @@ private fun init() { } if (finished) { - finishStartupRendering = System.currentTimeMillis() + 2000L + finishStartupRendering = System.currentTimeMillis() + 4000L } } } @@ -151,6 +155,7 @@ val framesPerSecond get() = 1.0 / frameRenderTime private fun loop() { val state = GLStateTracker() + startupTextList.add("Initialized OpenGL context") camera = Camera() // Set the clear color @@ -159,56 +164,52 @@ private fun loop() { state.blend = true glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) - /*val chunk = Starbound.world.getOrMakeChunk(Vector2i(2, 2)) + var chunkRenderer: ChunkRenderer? = null - var x = 0 - var y = 0 + Starbound.onInitialize { + val chunk = Starbound.world.getOrMakeChunk(Vector2i(2, 2)) - for (tile in Starbound.tilesAccess.values) { - chunk.background[x, y + 1] = tile - chunk.background[x++, y] = tile - chunk.background[x, y + 1] = tile - chunk.background[x++, y] = tile - chunk.background[x, y + 1] = tile - chunk.background[x++, y] = tile - chunk.background[x, y + 1] = tile - chunk.background[x++, y] = tile - chunk.background[x, y + 1] = tile - chunk.background[x++, y] = tile - chunk.background[x, y + 1] = tile + var x = 0 + var y = 0 - if (x >= 32) { - x = 0 - y += 2 + for (tile in Starbound.tilesAccess.values) { + chunk.background[x, y + 1] = tile + chunk.background[x++, y] = tile + chunk.background[x, y + 1] = tile + chunk.background[x++, y] = tile + chunk.background[x, y + 1] = tile + chunk.background[x++, y] = tile + chunk.background[x, y + 1] = tile + chunk.background[x++, y] = tile + chunk.background[x, y + 1] = tile + chunk.background[x++, y] = tile + chunk.background[x, y + 1] = tile + + if (x >= 32) { + x = 0 + y += 2 + } } + + val tile = Starbound.getTileDefinition("glass") + + for (x in 0 .. 32) { + for (y in 0 .. 32) { + chunk.foreground[x, y] = tile + } + } + + for (x in 4 .. 8) { + for (y in 4 .. 8) { + chunk.foreground[x, y] = null as TileDefinition? + } + } + + chunkRenderer = ChunkRenderer(state, chunk, Starbound.world) + chunkRenderer!!.tesselateStatic() + chunkRenderer!!.uploadStatic() } - val tile = Starbound.getTileDefinition("glass") - - for (x in 0 .. 32) { - for (y in 0 .. 32) { - chunk.foreground[x, y] = tile - } - } - - for (x in 4 .. 8) { - for (y in 4 .. 8) { - chunk.foreground[x, y] = null as TileDefinition? - } - }*/ - - /* - for (x in 0 .. 24) { - for (y in 0 .. 24) { - chunk[x, y] = Starbound.getTileDefinition("sewerpipe") - } - } - */ - - //val chunkRenderer = ChunkRenderer(state, chunk, Starbound.world) - //chunkRenderer.tesselateStatic() - //chunkRenderer.uploadStatic() - val runtime = Runtime.getRuntime() // Run the rendering loop until the user has attempted to close @@ -221,7 +222,7 @@ private fun loop() { camera?.translate(state.matrixStack.last) state.matrixStack.push().scale(x = 20f, y = 20f).translateWithScale(0f, 0f) - //chunkRenderer.render() + chunkRenderer?.render() state.matrixStack.clear(viewportMatrixGUI.toMutableMatrix().translate(z = 2f)) @@ -230,7 +231,7 @@ private fun loop() { val thisTime = System.currentTimeMillis() - if (!startupTextList.isEmpty() && thisTime <= finishStartupRendering) { + if (startupTextList.isNotEmpty() && thisTime <= finishStartupRendering) { var alpha = 1f if (finishStartupRendering - thisTime < 1000L) { @@ -258,6 +259,7 @@ private fun loop() { // Poll for window events. The key callback above will only be // invoked during this call. glfwPollEvents() + Starbound.pollCallbacks() frameRenderTime = glfwGetTime() - measure } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index dddac29e..0f9d5a83 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -57,6 +57,8 @@ object Starbound { return tiles[name] } + private val initCallbacks = ArrayList<() -> Unit>() + fun initializeGame(callback: (finished: Boolean, replaceStatus: Boolean, status: String) -> Unit) { if (initializing) { throw IllegalStateException("Already initializing!") @@ -80,7 +82,7 @@ object Starbound { callback(false, true, it) } - callback(false, false, "Loaded materials") + callback(false, true, "Loaded materials") initializing = false initialized = true @@ -88,6 +90,24 @@ object Starbound { }.start() } + fun onInitialize(callback: () -> Unit) { + if (initialized) { + callback() + } else { + initCallbacks.add(callback) + } + } + + fun pollCallbacks() { + if (initialized && initCallbacks.isNotEmpty()) { + for (callback in initCallbacks) { + callback() + } + + initCallbacks.clear() + } + } + private fun loadTileMaterials(callback: (String) -> Unit) { for (sPath in _filepath) { val newPath = File(sPath.path, "tiles/materials")