-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
106 lines (83 loc) · 1.79 KB
/
view.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
package gig
import (
"flag"
"fmt"
"runtime"
"github.com/leonsal/gig/imgui"
)
type View struct {
disp imgui.Display
group *Group
run bool
memStats runtime.MemStats
}
// Command line options
var (
opLogMemallocs = flag.Bool("log_memallocs", false, "Logs memory allocations to the console")
opOpengles = flag.Bool("opengles", false, "Use OpenGL ES instead of OpenGL")
)
// Singleton application
var defaultView *View
func ViewInit(title string, width, height int) (*View, error) {
if defaultView != nil {
return nil, fmt.Errorf("View already initialized")
}
// All GUI functions must run in the same thread
runtime.LockOSThread()
flag.Parse()
// Creates the default display window
cfg := imgui.DisplayConfig{
Evtimeout: 0,
Fullscreen: false,
Opengl: imgui.OpenglConfig{ES: *opOpengles},
}
disp, err := imgui.DisplayInit(title, width, height, &cfg)
if err != nil {
return nil, err
}
v := new(View)
v.disp = disp
v.group = NewGroup()
v.run = true
defaultView = v
return defaultView, nil
}
func GetView() *View {
return defaultView
}
func (v *View) AddChildren(c ...IWidget) {
v.group.AddChildren(c...)
}
func (v *View) Display() imgui.Display {
return v.disp
}
func (v *View) Run() {
if !v.run {
return
}
if *opLogMemallocs {
runtime.ReadMemStats(&v.memStats)
}
for v.run {
close := v.disp.StartFrame()
if close {
break
}
v.group.Render()
v.disp.EndFrame()
if *opLogMemallocs {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
//log.Printf("Mallocs:%v Frees:%v Live:%v TotalAlloc: %v\n", ms.Mallocs, ms.Frees, ms.Mallocs-ms.Frees, ms.TotalAlloc)
if ms.Mallocs != v.memStats.Mallocs {
print(".")
v.memStats = ms
}
}
}
v.disp.Destroy()
runtime.UnlockOSThread()
}
func (v *View) Close() {
v.run = false
}