refactor ImGUI code files

This commit is contained in:
2022-02-10 19:58:51 +08:00
parent bb33972616
commit 06cd549eb7
13 changed files with 172 additions and 50 deletions

View File

@ -1,7 +1,6 @@
package game
import (
"fmt"
"log"
"math"
"os"
@ -9,6 +8,8 @@ import (
"edgaru089.ml/go/gl01/internal/asset"
"edgaru089.ml/go/gl01/internal/igwrap"
"edgaru089.ml/go/gl01/internal/igwrap/backend"
"edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world"
"github.com/go-gl/glfw/v3.3/glfw"
@ -19,6 +20,8 @@ type guiState struct {
showLog, showDebugInfo bool
logFollow bool
lastframeCgoCalls int64
loadChunkFile string
loadChunkID [2]int32
@ -35,7 +38,7 @@ func (g *Game) initImgui(win *glfw.Window) {
cfg.SetOversampleV(1)
cfg.SetPixelSnapH(true)
g.io.Fonts().AddFontFromMemoryTTFV(asset.Unifont, 16, cfg, g.io.Fonts().GlyphRangesChineseFull())
igwrap.Init(win)
backend.Init(win)
g.gui = guiState{
showLog: true,
@ -49,6 +52,19 @@ func (g *Game) initImgui(win *glfw.Window) {
}
func (g *Game) imgui() {
if io.ShowDebugInfo {
imgui.SetNextWindowPosV(imgui.Vec2{}, imgui.ConditionAlways, imgui.Vec2{})
if igwrap.Begin("F3", nil, imgui.WindowFlagsAlwaysAutoResize|imgui.WindowFlagsNoBackground|imgui.WindowFlagsNoNavFocus|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoInputs|imgui.WindowFlagsNoSavedSettings|imgui.WindowFlagsNoFocusOnAppearing) {
igwrap.Text("Gl01(glfw%s, imgui%s) - compiled by %s[%s/%s]", glfw.GetVersionString(), imgui.Version(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
igwrap.Text("CgoCalls:%d (%d lastframe), Goroutines:%d", runtime.NumCgoCall(), runtime.NumCgoCall()-g.gui.lastframeCgoCalls, runtime.NumGoroutine())
imgui.Text("")
pos := g.player.Position()
igwrap.Text("Player: (%.3f, %.5f, %.3f) - (Y%.2f, Z%.2f)", pos[0], pos[1], pos[2], g.rotY.Degrees(), g.rotZ)
imgui.End()
}
}
g.gui.lastframeCgoCalls = runtime.NumCgoCall()
if imgui.BeginV("Player", nil, imgui.WindowFlagsAlwaysAutoResize) {
pos := g.player.Position()
@ -58,17 +74,6 @@ func (g *Game) imgui() {
}
imgui.End()
if imgui.BeginV("Go Runtime", nil, imgui.WindowFlagsAlwaysAutoResize) {
imgui.Text(fmt.Sprintf("%s/%s, compiler: %s", runtime.GOOS, runtime.GOARCH, runtime.Compiler))
imgui.Text(fmt.Sprintf("NumCPU=%d, NumGOMAXPROCS=%d", runtime.NumCPU(), runtime.GOMAXPROCS(0)))
imgui.Text(fmt.Sprintf("NumCgoCalls=%d, NumGoroutine=%d", runtime.NumCgoCall(), runtime.NumGoroutine()))
imgui.Spacing()
if imgui.ButtonV("!!! PANIC !!!", imgui.Vec2{X: -2, Y: 0}) {
panic("Manual Panic")
}
}
imgui.End()
if igwrap.Begin("Logs", &g.gui.showLog, imgui.WindowFlagsMenuBar) {
if imgui.BeginMenuBar() {
if imgui.Button("Clear") {

View File

@ -6,7 +6,7 @@ import (
"os"
"time"
"edgaru089.ml/go/gl01/internal/igwrap"
"edgaru089.ml/go/gl01/internal/igwrap/backend"
"edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/render"
"edgaru089.ml/go/gl01/internal/util/itype"
@ -105,13 +105,13 @@ func (g *Game) Init(win *glfw.Window) {
win.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
if g.paused {
igwrap.MouseButtonCallback(button, action)
backend.MouseButtonCallback(button, action)
}
})
win.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if g.paused {
igwrap.KeyCallback(key, action)
backend.KeyCallback(key, action)
}
if action == glfw.Press {
if g.paused {
@ -157,13 +157,13 @@ func (g *Game) Init(win *glfw.Window) {
win.SetCharCallback(func(w *glfw.Window, char rune) {
if g.paused {
igwrap.InputCallback(char)
backend.InputCallback(char)
}
})
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
if g.paused {
igwrap.MouseScrollCallback(xpos, ypos)
backend.MouseScrollCallback(xpos, ypos)
}
})
@ -173,7 +173,7 @@ const airAccel = 0.1
// Update updates the game state, not necessarily in the main thread.
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
igwrap.NewFrame()
backend.NewFrame()
imgui.ShowDemoWindow(nil)
if !g.paused {
@ -251,5 +251,5 @@ func (g *Game) Render(win *glfw.Window) {
g.worldrender.Render(g.world, g.view)
render.Framewire.Render(g.view)
igwrap.Render(win)
backend.Render(win)
}