-
Notifications
You must be signed in to change notification settings - Fork 4
/
masterwindow.go
126 lines (101 loc) · 2.71 KB
/
masterwindow.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
122
123
124
125
126
package gimu
import (
"image/color"
"log"
"time"
"github.com/AllenDang/gimu/nk"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
type BuilderFunc func(w *Window)
// Master window flag
type MasterWindowFlag int
const (
// MasterWindowFlagNoResize - Create an not resizable window
MasterWindowFlagDefault MasterWindowFlag = iota
MasterWindowFlagNoResize
)
func (this MasterWindowFlag) HasFlag(flag MasterWindowFlag) bool {
return this|flag == this
}
// Master window
type MasterWindow struct {
win *glfw.Window
ctx *nk.Context
maxVertexBuffer int
maxElementBuffer int
bgColor nk.Color
defaultFont *nk.Font
}
func NewMasterWindow(title string, width, height int, flags MasterWindowFlag) *MasterWindow {
if err := glfw.Init(); err != nil {
log.Fatalln(err)
}
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
if flags.HasFlag(MasterWindowFlagNoResize) {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
win, err := glfw.CreateWindow(width, height, title, nil, nil)
if err != nil {
log.Fatalln(err)
}
win.MakeContextCurrent()
if err := gl.Init(); err != nil {
log.Fatalln("OpenGL init failed:", err)
}
gl.Viewport(0, 0, int32(width), int32(height))
ctx := nk.NkPlatformInit(win, nk.PlatformInstallCallbacks)
return &MasterWindow{
win: win,
ctx: ctx,
maxVertexBuffer: 512 * 1024,
maxElementBuffer: 128 * 1024,
bgColor: nk.NkRgba(28, 48, 62, 255),
}
}
func (w *MasterWindow) GetSize() (width, height int) {
gw, gh := w.win.GetSize()
return gw, gh
}
func (w *MasterWindow) SetBgColor(color color.RGBA) {
w.bgColor = toNkColor(color)
}
func (w *MasterWindow) SetTitle(title string) {
w.win.SetTitle(title)
}
func (w *MasterWindow) GetContext() *nk.Context {
return w.ctx
}
func (w *MasterWindow) GetDefaultFont() *nk.Font {
return w.defaultFont
}
func (w *MasterWindow) Main(builder BuilderFunc) {
// Load default font
w.defaultFont = LoadDefaultFont()
w.GetContext().SetStyle(nk.THEME_DARK)
window := Window{
ctx: w.ctx,
mw: w,
}
for !w.win.ShouldClose() {
glfw.PollEvents()
nk.NkPlatformNewFrame()
builder(&window)
// Render
bg := make([]float32, 4)
nk.NkColorFv(bg, w.bgColor)
width, height := w.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, w.maxVertexBuffer, w.maxElementBuffer)
w.win.SwapBuffers()
// 30 FPS
time.Sleep(time.Second / 30)
}
nk.NkPlatformShutdown()
glfw.Terminate()
}