input events have their own goroutine

This commit is contained in:
Loic Nageleisen 2014-04-26 20:36:34 +02:00
parent 5adbc8b1e0
commit 5248b6b778
2 changed files with 24 additions and 7 deletions

View file

@ -43,7 +43,7 @@ func onError(err glfw.ErrorCode, desc string) {
log.Printf("%v: %v\n", err, desc) log.Printf("%v: %v\n", err, desc)
} }
func onKey(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) { func onKey(input chan int, window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) {
if action != glfw.Press { if action != glfw.Press {
return return
} }
@ -53,6 +53,8 @@ func onKey(window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw
if mods & glfw.ModSuper != 0 { if mods & glfw.ModSuper != 0 {
reexec() reexec()
} }
case glfw.KeyUp, glfw.KeyDown, glfw.KeyLeft, glfw.KeyRight:
input <- 1
case glfw.KeyEscape: case glfw.KeyEscape:
window.SetShouldClose(true) window.SetShouldClose(true)
default: default:
@ -159,17 +161,24 @@ func drawSprite(texture gl.Texture, x float64, y float64, a float64, list uint)
// main // main
func main() { func main() {
c := make(chan int) done := make(chan int)
input := make(chan int, 64)
go renderer(c) go renderer(done, input)
go func() {
for {
_ = <- input
log.Print("input")
}
}()
<-c <-done
} }
// renderer // renderer
func renderer(c chan int) { func renderer(done chan int, input chan int) {
runtime.LockOSThread() runtime.LockOSThread()
glfw.SetErrorCallback(onError) glfw.SetErrorCallback(onError)
@ -184,7 +193,11 @@ func renderer(c chan int) {
log.Panic(err) log.Panic(err)
} }
window.SetKeyCallback(onKey) onKeyClosure := func (window *glfw.Window, k glfw.Key, s int, action glfw.Action, mods glfw.ModifierKey) {
onKey(input, window, k, s, action, mods)
}
window.SetKeyCallback(onKeyClosure)
window.MakeContextCurrent() window.MakeContextCurrent()
@ -197,7 +210,7 @@ func renderer(c chan int) {
glfw.PollEvents() glfw.PollEvents()
} }
c <- 1 done <- 1
} }
func setup() (textures map[string]gl.Texture, lists map[string]uint) { func setup() (textures map[string]gl.Texture, lists map[string]uint) {

View file

@ -93,3 +93,7 @@ All time is CEST
Sprite rotation support Sprite rotation support
![](screenshots/4.gif) ![](screenshots/4.gif)
- 20:34
Going concurrent. Goroutines, channels and closures make passing input events outside the locked OpenGL thread *really* easy.