/
main.go
121 lines (102 loc) · 2.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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"), 26, nil)
sansFont := nk.NkFontAtlasAddFromFile(atlas, s("assets/Roboto-Light.ttf"), 26, 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()
node_editor_draw(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
const (
Easy Option = 0
Hard Option = 1
)
type State struct {
bgColor nk.Color
prop int32
opt Option
}
func onError(code int32, msg string) {
log.Printf("[glfw ERR]: error %d: %s", code, msg)
}