From 4e7f300e77712c27f2b13edeb1e0b3ce9c32c9b4 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 15 Feb 2021 17:42:26 +0100 Subject: [PATCH] rendering: handle line breaks --- .../minosoft/data/text/TextComponent.kt | 44 ++++++++++++++++--- .../minosoft/gui/rendering/RenderWindow.kt | 13 +++--- .../minosoft/gui/rendering/chunk/WorldMesh.kt | 10 ++--- .../elements/text/HUDDebugScreenElement.kt | 4 +- .../hud/elements/text/HUDFontMesh.kt | 12 ++--- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt b/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt index 6e3ab593f..c27165f8f 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt @@ -225,21 +225,51 @@ open class TextComponent : ChatComponent { // reverse text if right bound val charArray = when (binding) { FontBindings.RIGHT_UP, FontBindings.RIGHT_DOWN -> { - text.toCharArray().reversed() + if (text.contains('\n')) { + // ToDo: This needs to be improved + val arrays: MutableList> = mutableListOf() + for (split in text.split('\n')) { + arrays.add(split.toCharArray().reversed()) + arrays.add(listOf('\n')) + } + val outList: MutableList = mutableListOf() + for (list in arrays) { + for (char in list) { + outList.add(char) + } + } + if (outList.last() == '\n') { + outList.removeLast() + } + outList + } else { + text.toCharArray().toList().reversed() + } } FontBindings.LEFT_UP, FontBindings.LEFT_DOWN -> { text.toCharArray().toList() } } - for (c in charArray) { - val fontChar = font.getChar(c) - val scaledX = fontChar.width * (font.charHeight / fontChar.height.toFloat()) * hudScale.scale + for (char in charArray) { val scaledHeight = font.charHeight * hudScale.scale + if (char == '\n') { + val yOffset = offset.y + offset *= 0 + offset += Vec2(0, yOffset + scaledHeight) + maxSize += Vec2(0, yOffset + scaledHeight) + continue + } + val fontChar = font.getChar(char) + val scaledX = fontChar.width * (font.charHeight / fontChar.height.toFloat()) * hudScale.scale drawLetter(startPosition + offset, scaledX, scaledHeight, fontChar, color, meshData) offset += Vec2(scaledX + (hudScale.scale / 2), 0f) - maxSize.x += scaledX + (hudScale.scale / 2) - if (maxSize.y < scaledHeight) { - maxSize.y = scaledHeight + if (offset.x >= maxSize.x) { + maxSize.x += scaledX + (hudScale.scale / 2) + } + if (offset.y >= maxSize.y) { + if (maxSize.y < scaledHeight) { + maxSize.y = scaledHeight + } } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 429281dee..629cf3f12 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -34,7 +34,7 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) lateinit var camera: Camera private val latch = CountUpAndDownLatch(1) - private var renderingPaused = false + private var slowerRendering = false // all renderers val chunkRenderer: ChunkRenderer = ChunkRenderer(connection, connection.player.world, this) @@ -154,7 +154,7 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) glfwSetWindowFocusCallback(windowId, object : GLFWWindowFocusCallback() { override fun invoke(window: Long, focused: Boolean) { - renderingPaused = !focused + slowerRendering = !focused } }) @@ -176,11 +176,6 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) fun startRenderLoop() { while (!glfwWindowShouldClose(windowId)) { - if (renderingPaused) { - glfwSwapBuffers(windowId) - glfwPollEvents() - continue - } renderStats.startFrame() glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) // clear the framebuffer @@ -208,6 +203,10 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) renderQueue.remove(renderQueueElement) } + if (slowerRendering) { + Thread.sleep(100L) + } + renderStats.endFrame() } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldMesh.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldMesh.kt index 2b2b1cba5..97fe27eee 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldMesh.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldMesh.kt @@ -10,18 +10,18 @@ import org.lwjgl.opengl.GL30.* class WorldMesh(data: FloatArray) { var vAO: Int = glGenVertexArrays() var vBO: Int = glGenBuffers() - var trianglesCount: Int = data.size / BYTES_PER_VERTEX + var trianglesCount: Int = data.size / FLOATS_PER_VERTEX init { // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). glBindVertexArray(vAO) glBindBuffer(GL_ARRAY_BUFFER, vBO) glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW) - glVertexAttribPointer(0, 3, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, 0L) + glVertexAttribPointer(0, 3, GL_FLOAT, false, FLOATS_PER_VERTEX * Float.BYTES, 0L) glEnableVertexAttribArray(0) - glVertexAttribPointer(1, 2, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, (3 * Float.BYTES).toLong()) + glVertexAttribPointer(1, 2, GL_FLOAT, false, FLOATS_PER_VERTEX * Float.BYTES, (3 * Float.BYTES).toLong()) glEnableVertexAttribArray(1) - glVertexAttribPointer(2, 1, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, (5 * Float.BYTES).toLong()) + glVertexAttribPointer(2, 1, GL_FLOAT, false, FLOATS_PER_VERTEX * Float.BYTES, (5 * Float.BYTES).toLong()) glEnableVertexAttribArray(2) // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind @@ -39,6 +39,6 @@ class WorldMesh(data: FloatArray) { } companion object { - private const val BYTES_PER_VERTEX = 6 + private const val FLOATS_PER_VERTEX = 6 } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDDebugScreenElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDDebugScreenElement.kt index ee52f3d1f..053cb578d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDDebugScreenElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDDebugScreenElement.kt @@ -40,7 +40,7 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex "", "XYZ ${getLocation()}", "Block ${getBlockPosition()}", - "Chunk ${getChunkLocation()}", + "Chunk \n${getChunkLocation()}", "Facing ${getFacing()}", "Dimension ${hudTextElement.connection.player.world.dimension}", )) @@ -50,7 +50,7 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex "Allocated: ${getAllocatedMemoryPercent()}% ${getFormattedAllocatedMemory()}", "System: $systemMemoryText", "", - "OS: $osText", + "OS:\n $osText", "CPU: $processorText", "", "Display: ${getScreenDimensions()}", diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDFontMesh.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDFontMesh.kt index 08c8ba4e6..8b7f5b7f9 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDFontMesh.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDFontMesh.kt @@ -9,7 +9,7 @@ import org.lwjgl.opengl.GL30.* class HUDFontMesh(data: FloatArray) { var vAO: Int = glGenVertexArrays() var vBO: Int = glGenBuffers() - var trianglesCount: Int = data.size / BYTES_PER_VERTEX + var trianglesCount: Int = data.size / FLOAT_PER_VERTEX fun draw() { glBindVertexArray(vAO) @@ -26,13 +26,13 @@ class HUDFontMesh(data: FloatArray) { glBindVertexArray(vAO) glBindBuffer(GL_ARRAY_BUFFER, vBO) glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW) - glVertexAttribPointer(0, 3, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, 0L) + glVertexAttribPointer(0, 3, GL_FLOAT, false, FLOAT_PER_VERTEX * Float.BYTES, 0L) glEnableVertexAttribArray(0) - glVertexAttribPointer(1, 2, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, (3 * Float.BYTES).toLong()) + glVertexAttribPointer(1, 2, GL_FLOAT, false, FLOAT_PER_VERTEX * Float.BYTES, (3 * Float.BYTES).toLong()) glEnableVertexAttribArray(1) - glVertexAttribPointer(2, 1, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, (5 * Float.BYTES).toLong()) + glVertexAttribPointer(2, 1, GL_FLOAT, false, FLOAT_PER_VERTEX * Float.BYTES, (5 * Float.BYTES).toLong()) glEnableVertexAttribArray(2) - glVertexAttribPointer(3, 1, GL_FLOAT, false, BYTES_PER_VERTEX * Float.BYTES, (6 * Float.BYTES).toLong()) + glVertexAttribPointer(3, 1, GL_FLOAT, false, FLOAT_PER_VERTEX * Float.BYTES, (6 * Float.BYTES).toLong()) glEnableVertexAttribArray(3) // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind @@ -40,6 +40,6 @@ class HUDFontMesh(data: FloatArray) { } companion object { - private const val BYTES_PER_VERTEX = 7 + private const val FLOAT_PER_VERTEX = 7 } }