hibiscus/src/main/kotlin/codes/som/hibiscus/gui/ImGuiRenderer.kt

59 lines
1.8 KiB
Kotlin

package codes.som.hibiscus.gui
import codes.som.hibiscus.mc
import imgui.ImGui
import imgui.flag.ImGuiConfigFlags
import imgui.gl3.ImGuiImplGl3
import imgui.glfw.ImGuiImplGlfw
import org.lwjgl.glfw.GLFW
object ImGuiRenderer {
private val imGuiGlfw = ImGuiImplGlfw()
private val imGuiGl3 = ImGuiImplGl3()
private var didRender = true
var stalled = false
fun setup() {
ImGui.createContext()
with(ImGui.getIO()) {
iniFilename = null // We don't want to save .ini file
addConfigFlags(ImGuiConfigFlags.NavEnableKeyboard) // Enable Keyboard Controls
addConfigFlags(ImGuiConfigFlags.ViewportsEnable) // Enable Multi-Viewport / Platform Windows
configViewportsNoTaskBarIcon = true
configDockingWithShift = true
}
applyHibiscusImGuiTheme()
imGuiGlfw.init(mc.window.handle, true)
imGuiGl3.init()
}
fun beginFrame(delta: Float) {
if (didRender) {
imGuiGlfw.newFrame()
ImGui.newFrame()
didRender = false
} else {
// If we run into an exception while the game is running (causing finishFrame() to not get hit),
// stall ImGui so that we have time to spit out a stack trace instead of crashing on an ImGui assert
stalled = true
}
}
fun finishFrame(delta: Float) {
ImGui.render()
imGuiGl3.renderDrawData(ImGui.getDrawData())
if (ImGui.getIO().hasConfigFlags(ImGuiConfigFlags.ViewportsEnable)) {
val backupWindowPtr = GLFW.glfwGetCurrentContext()
ImGui.updatePlatformWindows()
ImGui.renderPlatformWindowsDefault()
GLFW.glfwMakeContextCurrent(backupWindowPtr)
}
didRender = true
}
}