input: Use sync.Once

This commit is contained in:
Hajime Hoshi 2019-03-31 20:20:52 +09:00
parent f5c68d2a61
commit d56668bfdb

View File

@ -30,7 +30,7 @@ import (
type Input struct {
keyPressed map[glfw.Key]bool
mouseButtonPressed map[glfw.MouseButton]bool
callbacksInitialized bool
onceCallback sync.Once
scrollX float64
scrollY float64
cursorX int
@ -120,15 +120,14 @@ func (i *Input) Update(window *glfw.Window, scale float64) {
i.m.Lock()
defer i.m.Unlock()
if !i.callbacksInitialized {
i.onceCallback.Do(func() {
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
i.appendRuneBuffer(char)
})
window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
i.setWheel(xoff, yoff)
})
i.callbacksInitialized = true
}
})
if i.keyPressed == nil {
i.keyPressed = map[glfw.Key]bool{}
}