From 5248b6b7787af30acb7fb9b81eee8dfae79120d0 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Sat, 26 Apr 2014 20:36:34 +0200 Subject: [PATCH] input events have their own goroutine --- ld48-29.go | 27 ++++++++++++++++++++------- log.mdown | 4 ++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/ld48-29.go b/ld48-29.go index cdc50f1..f26dcb6 100644 --- a/ld48-29.go +++ b/ld48-29.go @@ -43,7 +43,7 @@ func onError(err glfw.ErrorCode, desc string) { 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 { 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 { reexec() } + case glfw.KeyUp, glfw.KeyDown, glfw.KeyLeft, glfw.KeyRight: + input <- 1 case glfw.KeyEscape: window.SetShouldClose(true) default: @@ -159,17 +161,24 @@ func drawSprite(texture gl.Texture, x float64, y float64, a float64, list uint) // 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 -func renderer(c chan int) { +func renderer(done chan int, input chan int) { runtime.LockOSThread() glfw.SetErrorCallback(onError) @@ -184,7 +193,11 @@ func renderer(c chan int) { 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() @@ -197,7 +210,7 @@ func renderer(c chan int) { glfw.PollEvents() } - c <- 1 + done <- 1 } func setup() (textures map[string]gl.Texture, lists map[string]uint) { diff --git a/log.mdown b/log.mdown index 44b0047..e408ae7 100644 --- a/log.mdown +++ b/log.mdown @@ -93,3 +93,7 @@ All time is CEST Sprite rotation support ![](screenshots/4.gif) + +- 20:34 + + Going concurrent. Goroutines, channels and closures make passing input events outside the locked OpenGL thread *really* easy.