From a628fdb434f9df1073f3b5b03aa38b56784bb2bb Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Thu, 10 Feb 2022 21:13:55 +0800 Subject: [PATCH] add background for F3 text, fix single-channel texture display --- cmd/main.cfg.json | 6 +++--- internal/game/imgui.go | 13 +++++++++---- internal/igwrap/backend/render.go | 2 +- internal/igwrap/backend/shader.frag | 7 +++++-- internal/igwrap/convert.go | 10 ++++++++++ internal/igwrap/imagewrap.go | 3 ++- internal/igwrap/wrap.go | 14 ++++++++++++++ 7 files changed, 44 insertions(+), 11 deletions(-) diff --git a/cmd/main.cfg.json b/cmd/main.cfg.json index 287a602..1c07991 100644 --- a/cmd/main.cfg.json +++ b/cmd/main.cfg.json @@ -1,7 +1,7 @@ { - "WindowWidth": 1600, - "WindowHeight": 900, + "WindowWidth": 800, + "WindowHeight": 600, - "FramerateLimit": 60 + "FramerateLimit": 0 } diff --git a/internal/game/imgui.go b/internal/game/imgui.go index 7ceb1c6..f8c2cb5 100644 --- a/internal/game/imgui.go +++ b/internal/game/imgui.go @@ -54,13 +54,18 @@ func (g *Game) initImgui(win *glfw.Window) { func (g *Game) imgui() { if io.ShowDebugInfo { imgui.SetNextWindowPosV(imgui.Vec2{}, imgui.ConditionAlways, imgui.Vec2{}) + imgui.SetNextWindowSize(imgui.Vec2{X: float32(io.DisplaySize[0]), Y: float32(io.DisplaySize[1])}) 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("") + 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}) 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) + igwrap.TextBackground(bg, pad, "Player: (%.3f, %.5f, %.3f) (Y%.2f, Z%.2f)", pos[0], pos[1], pos[2], g.rotY.Degrees(), g.rotZ) imgui.End() } } diff --git a/internal/igwrap/backend/render.go b/internal/igwrap/backend/render.go index 32cc476..ef76278 100644 --- a/internal/igwrap/backend/render.go +++ b/internal/igwrap/backend/render.go @@ -87,7 +87,7 @@ func CreateFontsTexture() { ) io.Fonts().SetTextureID(imgui.TextureID(tex)) - igwrap.SetTextureFlag(tex, igwrap.TextureFlag_Red) + igwrap.SetTextureFlag(tex, igwrap.TextureFlag_ImGUIFont) gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture)) } diff --git a/internal/igwrap/backend/shader.frag b/internal/igwrap/backend/shader.frag index 12adf13..cb8fc96 100644 --- a/internal/igwrap/backend/shader.frag +++ b/internal/igwrap/backend/shader.frag @@ -6,7 +6,8 @@ #define FLAG_ALPHA (1<<3) #define FLAG_COLORS (FLAG_RED | FLAG_GREEN | FLAG_BLUE | FLAG_ALPHA) -#define FLAG_LINEAR (1<<4) +#define FLAG_LINEAR (1<<4) +#define FLAG_IMGUI_FONT (1<<5) uniform sampler2D tex; uniform int flags; @@ -19,8 +20,10 @@ out vec4 outputColor; const float gamma = 2.2; void main() { - if ((flags & FLAG_COLORS) == FLAG_RED) { + if ((flags & FLAG_IMGUI_FONT) != 0) { outputColor = vec4(fragColor.rgb, fragColor.a * texture(tex, fragUV.st).r); + } else if ((flags & FLAG_COLORS) == FLAG_RED) { + outputColor = vec4(vec3(texture(tex, fragUV.st).r), 1); } else { vec4 color = texture(tex, fragUV.st); if ((flags & FLAG_LINEAR) != 0) diff --git a/internal/igwrap/convert.go b/internal/igwrap/convert.go index 9bc6725..920dcca 100644 --- a/internal/igwrap/convert.go +++ b/internal/igwrap/convert.go @@ -15,11 +15,21 @@ func Color(r, g, b, a uint8) imgui.Vec4 { } } +// PackedColor converts RGBA to imgui.PackedColor. +func PackedColor(color itype.Vec4f) imgui.PackedColor { + return imgui.PackedColorFromVec4(Vec4(color)) +} + // Vec2 converts itype.Vec2f to imgui.Vec2. func Vec2(v itype.Vec2f) imgui.Vec2 { return imgui.Vec2{X: v[0], Y: v[1]} } +// Vec2f converts imgui.Vec2 to itype.Vec2f. +func Vec2f(v imgui.Vec2) itype.Vec2f { + return itype.Vec2f{v.X, v.Y} +} + // Vec4 converts itype.Vec4f to imgui.Vec4. func Vec4(v itype.Vec4f) imgui.Vec4 { return imgui.Vec4{X: v[0], Y: v[1], Z: v[2], W: v[3]} diff --git a/internal/igwrap/imagewrap.go b/internal/igwrap/imagewrap.go index 3a4708b..56ebca4 100644 --- a/internal/igwrap/imagewrap.go +++ b/internal/igwrap/imagewrap.go @@ -46,7 +46,8 @@ const ( TextureFlag_Blue // Renders the Blue channel. TextureFlag_Alpha // Renders the Alpha channel. - TextureFlag_Linear // Linear source data, requires gamma correction. + TextureFlag_Linear // Linear source data, requires gamma correction. + TextureFlag_ImGUIFont // This is a font texture from ImGUI, with a single red channel. TextureFlag_RGB = TextureFlag_Red | TextureFlag_Green | TextureFlag_Blue TextureFlag_RGBA = TextureFlag_Red | TextureFlag_Green | TextureFlag_Blue | TextureFlag_Alpha diff --git a/internal/igwrap/wrap.go b/internal/igwrap/wrap.go index bbfc152..5fc713f 100644 --- a/internal/igwrap/wrap.go +++ b/internal/igwrap/wrap.go @@ -21,6 +21,20 @@ func Text(format string, a ...interface{}) { imgui.Text(fmt.Sprintf(format, a...)) } +// TextBackground wraps imgui.Text to create a +// shortcut for fmt.Sprintf. +// +// It also fills the background of the text with the given color. +func TextBackground(color itype.Vec4f, padding itype.Vec2f, format string, a ...interface{}) { + text := fmt.Sprintf(format, a...) + orig := imgui.CursorScreenPos() + size := imgui.CalcTextSize(text, false, 0) + halfpad := padding.Multiply(0.5) + imgui.BackgroundDrawList().AddRectFilledV(orig.Minus(Vec2(halfpad)), orig.Plus(size).Plus(Vec2(halfpad)), PackedColor(color), 0, 0) + + imgui.Text(text) +} + // Begin wraps imgui.BeginV to create a // easy-to-use and intuitive interface. //