rendering: don't update screen when not focused

This commit is contained in:
Bixilon 2021-02-14 17:20:53 +01:00
parent 28f9c508a8
commit 7e7059e35a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -14,6 +14,7 @@ import org.lwjgl.*
import org.lwjgl.glfw.Callbacks import org.lwjgl.glfw.Callbacks
import org.lwjgl.glfw.GLFW.* import org.lwjgl.glfw.GLFW.*
import org.lwjgl.glfw.GLFWErrorCallback import org.lwjgl.glfw.GLFWErrorCallback
import org.lwjgl.glfw.GLFWWindowFocusCallback
import org.lwjgl.glfw.GLFWWindowSizeCallback import org.lwjgl.glfw.GLFWWindowSizeCallback
import org.lwjgl.opengl.* import org.lwjgl.opengl.*
import org.lwjgl.opengl.GL11.* import org.lwjgl.opengl.GL11.*
@ -33,6 +34,8 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering)
lateinit var camera: Camera lateinit var camera: Camera
private val latch = CountUpAndDownLatch(1) private val latch = CountUpAndDownLatch(1)
private var renderingPaused = false
// all renderers // all renderers
val chunkRenderer: ChunkRenderer = ChunkRenderer(connection, connection.player.world, this) val chunkRenderer: ChunkRenderer = ChunkRenderer(connection, connection.player.world, this)
val hudRenderer: HUDRenderer = HUDRenderer(connection, this) val hudRenderer: HUDRenderer = HUDRenderer(connection, this)
@ -149,6 +152,12 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering)
} }
}) })
glfwSetWindowFocusCallback(windowId, object : GLFWWindowFocusCallback() {
override fun invoke(window: Long, focused: Boolean) {
renderingPaused = !focused
}
})
hudRenderer.screenChangeResizeCallback(screenWidth, screenHeight) hudRenderer.screenChangeResizeCallback(screenWidth, screenHeight)
@ -166,9 +175,12 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering)
} }
fun startRenderLoop() { fun startRenderLoop() {
var lastPositionChangeTime = 0.0
while (!glfwWindowShouldClose(windowId)) { while (!glfwWindowShouldClose(windowId)) {
if (renderingPaused) {
glfwSwapBuffers(windowId)
glfwPollEvents()
continue
}
renderStats.startFrame() renderStats.startFrame()
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) // clear the framebuffer glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) // clear the framebuffer