/
gooey.go
191 lines (141 loc) · 3.46 KB
/
gooey.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Gooey is a GUI system for Go programs
package gooey
import (
"fmt"
"runtime"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)
func init() {
// GLFW examples do this because some glfw calls (and probably all gl calls) need
// to run on the main thread.
runtime.LockOSThread()
}
//------------------------------------------------------
// COLOR
//------------------------------------------------------
type Color struct {
R float32
G float32
B float32
A float32
}
//------------------------------------------------------
// ELEMENT
//------------------------------------------------------
type Element struct {
name string
x float32
y float32
width float32
height float32
color Color
}
func (e *Element) String() string {
return fmt.Sprintf("Element [%s] of size (%f,%f)", e.name, e.width, e.height)
}
func (e *Element) Name() string {
return e.name
}
func (e *Element) SetName(name string) {
e.name = name
}
func (e *Element) SetPosition(x float32, y float32) {
e.x = x
e.y = y
}
func (e *Element) SetColor(color Color) {
e.color = color
}
func (e *Element) Render() {
gl.Color4f(e.color.R, e.color.G, e.color.B, e.color.A)
gl.Begin(gl.QUADS)
gl.Vertex3f(e.x, e.y, 0)
gl.Vertex3f(e.x, e.y + e.height, 0)
gl.Vertex3f(e.x + e.width, e.y + e.height, 0)
gl.Vertex3f(e.x + e.width, e.y, 0)
gl.End()
}
func Init() {
err := glfw.Init()
if err != nil {
panic(err)
}
}
func Terminate() {
glfw.Terminate()
}
//------------------------------------------------------
// GENERAL
//------------------------------------------------------
type Window struct {
handle *glfw.Window
width int
height int
elements []*Element;
}
func (w *Window) NewElement() *Element {
e := &Element{}
e.width = 50
e.height = 50
e.color = Color{0.0, 0.0, 0.0, 1.0}
w.elements = append(w.elements, e)
return e
}
func (w *Window) Render() {
for i := 0; i < len(w.elements); i++ {
e := w.elements[i]
e.Render()
}
}
func (w *Window) Handle() *glfw.Window {
return w.handle
}
//TODO: how to handle multiple windows? Do we even want to allow that?
func CreateWindow(width int, height int, title string) *Window {
handle, err := glfw.CreateWindow(width, height, title, nil, nil)
if err != nil {
panic(err)
}
w := new(Window)
w.handle = handle
w.width = width
w.height = height
handle.MakeContextCurrent()
err = gl.Init();
if err != nil {
panic(err)
}
return w
}
func Run(window *Window) {
setupScene(window.width, window.height)
for !window.handle.ShouldClose() {
drawScene()
window.Render()
window.handle.SwapBuffers()
glfw.PollEvents()
}
}
func setupScene(width int, height int) {
gl.Disable(gl.DEPTH_TEST)
gl.Disable(gl.LIGHTING)
gl.ClearColor(0.5, 0.5, 0.5, 0.0)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(0, float64(width), 0, float64(height), -1, 1)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
}
func drawScene() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.LineWidth(1)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Translatef(0, 0, 0.0)
//gl.Rotatef(rotationX, 1, 0, 0)
//gl.Rotatef(rotationY, 0, 1, 0)
//rotationX += 0.5
//rotationY += 0.5
//gl.BindTexture(gl.TEXTURE_2D, texture)
}