This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wdeglfw3.go
224 lines (173 loc) · 4.68 KB
/
wdeglfw3.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright 2012 the go.wde authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package glfw3
import (
// "fmt"
"github.com/go-gl/gl"
glfw "github.com/grd/glfw3"
"github.com/rcoreilly/goki/gi/oswin"
"image"
"os"
)
func init() {
oswin.BackendNewWindow = func(width, height int) (w oswin.OSWindow, err error) {
var window *OSWindow
window, err = NewOSWindow(width, height)
w = window
return
}
oswin.BackendRun = glfw.Main
oswin.BackendStop = func() {
glfw.Terminate()
os.Exit(0)
}
go doRun()
// Don't show the window context before using the Show function.
go glfw.WindowHint(glfw.Visible, glfw.False)
go flushBuffer()
go setGlyph()
}
func doRun() {
for {
// Poll for and process events
glfw.PollEvents()
}
}
type OSWindow struct {
win *glfw.Window
buffer WinImage
events chan interface{}
}
var windowMap = make(map[uintptr]*OSWindow)
func NewOSWindow(width, height int) (w *OSWindow, err error) {
w = new(OSWindow)
w.win, err = glfw.CreateWindow(width, height, "", nil, nil)
if err != nil {
return nil, err
}
windowMap[w.win.C()] = w
w.buffer.RGBA = image.NewRGBA(image.Rect(0, 0, width, height))
// Events and callback functions for events
w.events = make(chan interface{})
w.win.SetMouseButtonCallback(mouseButtonCallback)
w.win.SetCursorEnterCallback(cursorEnterCallback)
w.win.SetCursorPositionCallback(cursorPositionCallback)
w.win.SetFramebufferSizeCallback(framebufferSizeCallback)
w.win.SetCharacterCallback(characterCallback)
w.win.SetKeyCallback(keyCallback)
w.checkShouldClose()
return
}
func (w *OSWindow) SetTitle(title string) {
w.win.SetTitle(title)
}
func (w *OSWindow) SetCursor(cursor oswin.Cursor) {
// TODO
}
func (w *OSWindow) SetSize(width, height int) {
w.win.SetSize(width, height)
}
func (w *OSWindow) Size() (width, height int) {
return w.win.GetSize()
}
func (w *OSWindow) LockSize(lock bool) {
// glfw supports only window size locking when the
// parameter is set before the creation of the window.
// It doesn't work on an existing window.
if lock {
glfw.WindowHint(glfw.Resizable, glfw.False)
} else {
glfw.WindowHint(glfw.Resizable, glfw.True)
}
}
func (w *OSWindow) Show() {
w.win.Show()
}
func (w *OSWindow) Screen() oswin.WinImage {
return w.buffer
}
func (w *OSWindow) FlushImage(bounds ...image.Rectangle) {
if w.win.ShouldClose() {
return
}
// TODO: Howto implement ...image.Rectangle
// flush the buffer
windowFlushBuffer <- w
// waiting for the flushing is done before filling the buffer again
<-windowFlushBufferDone
}
func (w *OSWindow) Close() (err error) {
w.win.Destroy()
return
}
func (w *OSWindow) checkShouldClose() {
go func() {
for {
if w.win.ShouldClose() {
var cev oswin.CloseEvent
w.events <- cev
break
}
}
}()
}
type WinImage struct {
*image.RGBA
}
func (buffer WinImage) CopyRGBA(src *image.RGBA, r image.Rectangle) {
// clip r against each image's bounds and move sp accordingly (see draw.clip())
sp := image.ZP
orig := r.Min
r = r.Intersect(buffer.Bounds())
r = r.Intersect(src.Bounds().Add(orig.Sub(sp)))
dx := r.Min.X - orig.X
dy := r.Min.Y - orig.Y
(sp).X += dx
(sp).Y += dy
i0 := (r.Min.X - buffer.Rect.Min.X) * 4
i1 := (r.Max.X - buffer.Rect.Min.X) * 4
si0 := (sp.X - src.Rect.Min.X) * 4
yMax := r.Max.Y - buffer.Rect.Min.Y
y := r.Min.Y - buffer.Rect.Min.Y
sy := sp.Y - src.Rect.Min.Y
for ; y != yMax; y, sy = y+1, sy+1 {
dpix := buffer.Pix[y*buffer.Stride:]
spix := src.Pix[sy*src.Stride:]
for i, si := i0, si0; i < i1; i, si = i+4, si+4 {
dpix[i+0] = spix[si+2]
dpix[i+1] = spix[si+1]
dpix[i+2] = spix[si+0]
dpix[i+3] = spix[si+3]
}
}
}
var (
windowFlushBuffer = make(chan *OSWindow)
windowFlushBufferDone = make(chan bool)
)
func flushBuffer() {
for {
w := <-windowFlushBuffer
w.win.MakeContextCurrent()
imgWidth := w.buffer.Rect.Max.X
imgHeight := w.buffer.Rect.Max.Y
windowWidth, windowHeight := w.Size()
// gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.RasterPos2f(-1, 1)
gl.PixelZoom(1, -1)
gl.Viewport(0, 0, windowWidth, windowHeight)
gl.DrawPixels(imgWidth, imgHeight, gl.RGBA, gl.UNSIGNED_BYTE, &w.buffer.Pix[0])
w.win.SwapBuffers()
windowFlushBufferDone <- true
}
}