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" + } +}