From 419980301b66e90787ab62eb48afe819967a0d36 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 4 Feb 2022 11:34:40 +0700 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=BE=D0=BB=D1=8C=D1=88=D0=B5=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/dbotthepony/kstarbound/Main.kt | 9 ++++++--- .../ru/dbotthepony/kstarbound/render/Font.kt | 10 +++++----- .../ru/dbotthepony/kstarbound/util/Formatter.kt | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/kstarbound/util/Formatter.kt diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt index 878f351e..37493a7d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt @@ -16,6 +16,7 @@ import ru.dbotthepony.kstarbound.freetype.LoadFlag import ru.dbotthepony.kstarbound.gl.* import ru.dbotthepony.kstarbound.math.* import ru.dbotthepony.kstarbound.render.* +import ru.dbotthepony.kstarbound.util.formatBytesShort import ru.dbotthepony.kstarbound.world.CHUNK_SIZE import ru.dbotthepony.kstarbound.world.CHUNK_SIZE_FF import ru.dbotthepony.kstarbound.world.ChunkTile @@ -196,6 +197,8 @@ private fun loop() { chunkRenderer.tesselateStatic() chunkRenderer.uploadStatic() + val runtime = Runtime.getRuntime() + // Run the rendering loop until the user has attempted to close // the window or has pressed the ESCAPE key. while (!glfwWindowShouldClose(window)) { @@ -207,10 +210,10 @@ private fun loop() { state.matrixStack.push().scale(x = 20f, y = 20f).translateWithScale(0f, 0f) chunkRenderer.render() - state.matrixStack.clear(viewportMatrixGUI.toMutableMatrix()) + state.matrixStack.clear(viewportMatrixGUI.toMutableMatrix().translate(z = 2f)) - state.matrixStack.translateWithScale(z = 10f, y = 0f) - state.font.render("FPS: %.2f".format(framesPerSecond), y = 0f, scale = 0.5f) + state.font.render("FPS: %.2f".format(framesPerSecond), scale = 0.4f) + state.font.render("Mem: ${formatBytesShort(runtime.totalMemory() - runtime.freeMemory())}", x = viewportWidth.toFloat(), scale = 0.4f, alignX = TextAlignX.RIGHT) glfwSwapBuffers(window) // swap the color buffers diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/render/Font.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/render/Font.kt index 857b9111..9699e9ee 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/render/Font.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/render/Font.kt @@ -92,15 +92,15 @@ class Font( return TextSize(0f, lineHeight * scale) val totalSize = size(text) - val totalX = totalSize.width * scale - val totalY = totalSize.height * scale + val totalX = totalSize.width + val totalY = totalSize.height stack.push() when (alignY) { TextAlignY.TOP -> stack.translateWithScale(x = x, y = lineHeight * scale + y) - TextAlignY.CENTER -> stack.translateWithScale(x = x, y = lineHeight * scale - totalY / 2f + y) - TextAlignY.BOTTOM -> stack.translateWithScale(x = x, y = lineHeight * scale - totalY + y) + TextAlignY.CENTER -> stack.translateWithScale(x = x, y = lineHeight * scale - totalY * scale / 2f + y) + TextAlignY.BOTTOM -> stack.translateWithScale(x = x, y = lineHeight * scale - totalY * scale + y) } if (scale != 1f) @@ -130,7 +130,7 @@ class Font( } TextAlignX.RIGHT -> { - movedX = totalX - lineWidth(line, space) + movedX = -lineWidth(line, space) stack.translateWithScale(x = movedX) } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/Formatter.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Formatter.kt new file mode 100644 index 00000000..8bd718e1 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Formatter.kt @@ -0,0 +1,16 @@ +package ru.dbotthepony.kstarbound.util + +private const val KIBIBYTE = 1024L +private const val MEBIBYTE = KIBIBYTE * 1024L +private const val GIBIBYTE = MEBIBYTE * 1024L +private const val TEBIBYTE = GIBIBYTE * 1024L +private const val PETIBYTE = TEBIBYTE * 1024L + +fun formatBytesShort(input: Long): String { + return when (input) { + in 0 until KIBIBYTE -> "${input}b" + in KIBIBYTE until MEBIBYTE -> "%.2fKiB".format((input / KIBIBYTE).toDouble() + (input % KIBIBYTE).toDouble() / KIBIBYTE) + in MEBIBYTE until GIBIBYTE -> "%.2fMiB".format((input / MEBIBYTE).toDouble() + (input % MEBIBYTE).toDouble() / MEBIBYTE) + else -> "${input}b" + } +}