rendering: handle line breaks

This commit is contained in:
Bixilon 2021-02-15 17:42:26 +01:00
parent f29b15d95b
commit 4e7f300e77
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 56 additions and 27 deletions

View File

@ -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<List<Char>> = mutableListOf()
for (split in text.split('\n')) {
arrays.add(split.toCharArray().reversed())
arrays.add(listOf('\n'))
}
val outList: MutableList<Char> = 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
}
}
}
}

View File

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

View File

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

View File

@ -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()}",

View File

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