2022-01-24 22:40:53 +08:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-02-01 23:48:39 +08:00
|
|
|
"math"
|
2022-01-24 22:40:53 +08:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"edgaru089.ml/go/gl01/internal/asset"
|
|
|
|
"edgaru089.ml/go/gl01/internal/igwrap"
|
2022-02-10 19:58:51 +08:00
|
|
|
"edgaru089.ml/go/gl01/internal/igwrap/backend"
|
|
|
|
"edgaru089.ml/go/gl01/internal/io"
|
2022-01-24 22:40:53 +08:00
|
|
|
"edgaru089.ml/go/gl01/internal/util/itype"
|
|
|
|
"edgaru089.ml/go/gl01/internal/world"
|
|
|
|
"github.com/go-gl/glfw/v3.3/glfw"
|
|
|
|
"github.com/inkyblackness/imgui-go/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type guiState struct {
|
|
|
|
showLog, showDebugInfo bool
|
|
|
|
logFollow bool
|
|
|
|
|
2022-02-10 19:58:51 +08:00
|
|
|
lastframeCgoCalls int64
|
|
|
|
|
2022-01-24 22:40:53 +08:00
|
|
|
loadChunkFile string
|
|
|
|
loadChunkID [2]int32
|
|
|
|
|
|
|
|
saveChunkFile string
|
|
|
|
saveChunkID [2]int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) initImgui(win *glfw.Window) {
|
|
|
|
imgui.CreateContext(nil)
|
|
|
|
g.io = imgui.CurrentIO()
|
|
|
|
|
|
|
|
cfg := imgui.NewFontConfig()
|
|
|
|
cfg.SetOversampleH(1)
|
|
|
|
cfg.SetOversampleV(1)
|
|
|
|
cfg.SetPixelSnapH(true)
|
|
|
|
g.io.Fonts().AddFontFromMemoryTTFV(asset.Unifont, 16, cfg, g.io.Fonts().GlyphRangesChineseFull())
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.Init(win)
|
2022-01-24 22:40:53 +08:00
|
|
|
|
|
|
|
g.gui = guiState{
|
|
|
|
showLog: true,
|
|
|
|
showDebugInfo: false,
|
|
|
|
logFollow: true,
|
|
|
|
loadChunkFile: "chunk.gob",
|
|
|
|
loadChunkID: [2]int32{0, 0},
|
|
|
|
saveChunkFile: "chunk.gob",
|
|
|
|
saveChunkID: [2]int32{0, 0},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) imgui() {
|
2022-02-10 19:58:51 +08:00
|
|
|
if io.ShowDebugInfo {
|
|
|
|
imgui.SetNextWindowPosV(imgui.Vec2{}, imgui.ConditionAlways, imgui.Vec2{})
|
2022-02-10 21:13:55 +08:00
|
|
|
imgui.SetNextWindowSize(imgui.Vec2{X: float32(io.DisplaySize[0]), Y: float32(io.DisplaySize[1])})
|
2022-02-10 19:58:51 +08:00
|
|
|
if igwrap.Begin("F3", nil, imgui.WindowFlagsAlwaysAutoResize|imgui.WindowFlagsNoBackground|imgui.WindowFlagsNoNavFocus|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoInputs|imgui.WindowFlagsNoSavedSettings|imgui.WindowFlagsNoFocusOnAppearing) {
|
2022-02-10 21:13:55 +08:00
|
|
|
bg := itype.Vec4f{0, 0, 0, 0.5}
|
|
|
|
pad := igwrap.Vec2f(imgui.CurrentStyle().ItemSpacing())
|
|
|
|
|
|
|
|
igwrap.TextBackground(bg, pad, "Gl01 compiled by %s/%s [%s/%s] (120AVG) %.1f FPS (%.3f frame)", runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH, imgui.CurrentIO().Framerate(), 1000/imgui.CurrentIO().Framerate())
|
|
|
|
igwrap.TextBackground(bg, pad, "GLFW %s, Dear ImGUI %s", glfw.GetVersionString(), imgui.Version())
|
|
|
|
igwrap.TextBackground(bg, pad, "CgoCalls:%d (%d lastframe), Goroutines:%d", runtime.NumCgoCall(), runtime.NumCgoCall()-g.gui.lastframeCgoCalls, runtime.NumGoroutine())
|
|
|
|
imgui.Dummy(imgui.Vec2{X: 8, Y: 8})
|
2022-02-10 19:58:51 +08:00
|
|
|
|
|
|
|
pos := g.player.Position()
|
2022-02-10 21:13:55 +08:00
|
|
|
igwrap.TextBackground(bg, pad, "Player: (%.3f, %.5f, %.3f) (Y%.2f, Z%.2f)", pos[0], pos[1], pos[2], g.rotY.Degrees(), g.rotZ)
|
2022-02-10 19:58:51 +08:00
|
|
|
imgui.End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.gui.lastframeCgoCalls = runtime.NumCgoCall()
|
2022-01-24 22:40:53 +08:00
|
|
|
|
|
|
|
if imgui.BeginV("Player", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
|
|
|
pos := g.player.Position()
|
|
|
|
vel := g.player.Speed()
|
2022-02-01 23:48:39 +08:00
|
|
|
igwrap.Text("Pos: (%.5f, %.5f, %.5f), Vel: (%.5f, %.5f, %.5f)", pos[0], pos[1], pos[2], vel[0], vel[1], vel[2])
|
|
|
|
igwrap.Text("VelXZ=%.5f, VelXYZ=%.5f", math.Sqrt(vel[0]*vel[0]+vel[2]*vel[2]), vel.Length())
|
2022-01-24 22:40:53 +08:00
|
|
|
}
|
|
|
|
imgui.End()
|
|
|
|
|
|
|
|
if igwrap.Begin("Logs", &g.gui.showLog, imgui.WindowFlagsMenuBar) {
|
|
|
|
if imgui.BeginMenuBar() {
|
|
|
|
if imgui.Button("Clear") {
|
|
|
|
logs = ""
|
|
|
|
}
|
|
|
|
if imgui.Button("Add Logs") {
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
log.Print("Added logs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imgui.Checkbox("Autoscroll", &g.gui.logFollow)
|
|
|
|
imgui.EndMenuBar()
|
|
|
|
}
|
|
|
|
|
|
|
|
imgui.BeginChildV("LogScroll", imgui.Vec2{}, true, 0)
|
|
|
|
imgui.Text(logs)
|
|
|
|
if g.gui.logFollow && imgui.ScrollY() >= imgui.ScrollMaxY() {
|
|
|
|
imgui.SetScrollHereY(1.0)
|
|
|
|
}
|
|
|
|
imgui.EndChild()
|
|
|
|
imgui.End()
|
|
|
|
}
|
|
|
|
|
|
|
|
if imgui.Begin("Actions") {
|
|
|
|
|
|
|
|
imgui.Text("Chunks")
|
|
|
|
imgui.Separator()
|
|
|
|
imgui.InputText("Load Filename", &g.gui.loadChunkFile)
|
|
|
|
imgui.SliderInt2("Load ID", &g.gui.loadChunkID, -10, 10)
|
|
|
|
if imgui.ButtonV("Load", imgui.Vec2{X: -2, Y: 0}) {
|
|
|
|
c := &world.Chunk{}
|
|
|
|
f, err := os.Open(g.gui.loadChunkFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("LoadChunk: ", err)
|
|
|
|
} else {
|
|
|
|
c.LoadFromGobIndexed(f, int(g.gui.loadChunkID[0]), int(g.gui.loadChunkID[1]))
|
|
|
|
g.world.SetChunk(int(g.gui.loadChunkID[0]), int(g.gui.loadChunkID[1]), c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imgui.Separator()
|
|
|
|
imgui.InputText("Save Filename", &g.gui.saveChunkFile)
|
|
|
|
imgui.SliderInt2("Save ID", &g.gui.saveChunkID, -10, 10)
|
|
|
|
if imgui.ButtonV("Save", imgui.Vec2{X: -2, Y: 0}) {
|
|
|
|
c := g.world.Chunks[itype.Vec2i{int(g.gui.saveChunkID[0]), int(g.gui.saveChunkID[1])}]
|
|
|
|
f, _ := os.Create(g.gui.saveChunkFile)
|
|
|
|
c.WriteToGob(f)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
imgui.Separator()
|
|
|
|
|
|
|
|
}
|
|
|
|
imgui.End()
|
|
|
|
}
|