Больше форматирования текста

This commit is contained in:
DBotThePony 2022-02-04 11:34:40 +07:00
parent 44157d666f
commit 419980301b
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 27 additions and 8 deletions

View File

@ -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

View File

@ -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)
}
}

View File

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