5

I am trying to trim demo file from nuklear package to get minimum code for just creating a GUI window with one button. Following is my attempt:

package main

import (
    "log"
    "runtime"
    "time"

    "github.com/go-gl/gl/v3.2-core/gl"
    "github.com/go-gl/glfw/v3.2/glfw"
    "github.com/golang-ui/nuklear/nk"
    "github.com/xlab/closer"
)

const (
    winWidth         = 400
    winHeight        = 500
    maxVertexBuffer  = 512 * 1024
    maxElementBuffer = 128 * 1024
)

func init() {
    runtime.LockOSThread()
}
func main() {
    if err := glfw.Init(); err != nil {
        closer.Fatalln(err)
    }
    glfw.WindowHint(glfw.ContextVersionMajor, 3)
    glfw.WindowHint(glfw.ContextVersionMinor, 2)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
    win, err := glfw.CreateWindow(winWidth, winHeight, "Nuklear Demo", nil, nil)
    if err != nil {
        closer.Fatalln(err)
    }
    win.MakeContextCurrent()

    width, height := win.GetSize()
    log.Printf("glfw: created window %dx%d", width, height)

    if err := gl.Init(); err != nil {
        closer.Fatalln("opengl: init failed:", err)
    }
    gl.Viewport(0, 0, int32(width), int32(height))

    ctx := nk.NkPlatformInit(win, nk.PlatformInstallCallbacks)

    atlas := nk.NewFontAtlas()
    nk.NkFontStashBegin(&atlas)
    //sansFont := nk.NkFontAtlasAddFromBytes(atlas, MustAsset("assets/FreeSans.ttf"), 16, nil)
    sansFont := nk.NkFontAtlasAddDefault(atlas, 16, nil)
    nk.NkFontStashEnd()
    if sansFont != nil {
        nk.NkStyleSetFont(ctx, sansFont.Handle())
    }

    exitC := make(chan struct{}, 1)
    doneC := make(chan struct{}, 1)
    closer.Bind(func() {
        close(exitC)
        <-doneC
    })

    state := &State{
        bgColor: nk.NkRgba(28, 48, 62, 255),
    }
    fpsTicker := time.NewTicker(time.Second / 30)
    for {
        select {
        case <-exitC:
            nk.NkPlatformShutdown()
            glfw.Terminate()
            fpsTicker.Stop()
            close(doneC)
            return
        case <-fpsTicker.C:
            if win.ShouldClose() {
                close(exitC)
                continue
            }
            glfw.PollEvents()
            gfxMain(win, ctx, state)
        }
    }
}

func gfxMain(win *glfw.Window, ctx *nk.Context, state *State) {
    nk.NkPlatformNewFrame()
    // Layout
    bounds := nk.NkRect(50, 50, 230, 250)
    update := nk.NkBegin(ctx, "Demo", bounds,
        nk.WindowBorder|nk.WindowMovable|nk.WindowScalable|nk.WindowMinimizable|nk.WindowTitle)
    if update > 0 {
        nk.NkLayoutRowStatic(ctx, 30, 80, 1)
        {
            if nk.NkButtonLabel(ctx, "A_Button") > 0 {
                log.Println("[INFO] button pressed!")
            }
        }
    }
    nk.NkEnd(ctx)
    // Render
    bg := make([]float32, 4)
    nk.NkColorFv(bg, state.bgColor)
    width, height := win.GetSize()
    gl.Viewport(0, 0, int32(width), int32(height))
    gl.Clear(gl.COLOR_BUFFER_BIT)
    gl.ClearColor(bg[0], bg[1], bg[2], bg[3])
    nk.NkPlatformRender(nk.AntiAliasingOn, maxVertexBuffer, maxElementBuffer)
    win.SwapBuffers()
}

type Option uint8
type State struct {
    bgColor nk.Color
    prop    int32
    opt     Option
}

It seems a lot of code to just create a window with a button. I am sure more code can be removed but I can't seem to find it. What further unneeded code can I remove from above to trim it further?

RimeBeliskner
  • 352
  • 3
  • 14
rnso
  • 23,686
  • 25
  • 112
  • 234
  • If you're looking for simple/short code then a toolkit like nuklear may not be your best choice - are you able to pick a different API for simple UI elements, or has someone chosen for you? – andy.xyz Jul 12 '18 at 09:17
  • Which package do you recommend for simple GUI creation in golang? – rnso Jul 12 '18 at 09:19
  • Well, I am not completely unbiased as I'm building Fyne - https://github.com/fyne-io/fyne The window with a button can be done in 13 lines total (see the README) – andy.xyz Jul 12 '18 at 09:43
  • Looks really fine! Congratulations on creating it. The syntax looks really simple. I will certainly try it. – rnso Jul 12 '18 at 09:59
  • @ajwillia.ms : Pl see my new question on Fyne: https://stackoverflow.com/questions/57643992/how-to-install-opengl-for-this-to-run – rnso Aug 25 '19 at 07:15

0 Answers0