From a61d32161501bd5758232e7f4745e9b320b65b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Sun, 14 Jun 2020 17:52:03 +0200 Subject: [PATCH 1/3] expose glfw.WaitEvent using Window.WaitInput --- pixelgl/input.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pixelgl/input.go b/pixelgl/input.go index 541afb1..a0aa8e9 100644 --- a/pixelgl/input.go +++ b/pixelgl/input.go @@ -1,6 +1,8 @@ package pixelgl import ( + "time" + "github.com/faiface/mainthread" "github.com/faiface/pixel" "github.com/go-gl/glfw/v3.3/glfw" @@ -408,7 +410,24 @@ func (w *Window) UpdateInput() { mainthread.Call(func() { glfw.PollEvents() }) + w.doUpdateInput() +} +// WaitInput blocks until an event is received or a timeout. If timeout is 0 +// then it will wait indefinitely +func (w *Window) WaitInput(timeout time.Duration) { + mainthread.Call(func() { + if timeout == 0 { + glfw.WaitEvents() + } else { + glfw.WaitEventsTimeout(timeout.Seconds()) + } + }) + w.doUpdateInput() +} + +// internal input bookkeeping +func (w *Window) doUpdateInput() { w.prevInp = w.currInp w.currInp = w.tempInp From 1f914181d8c5ed1545871c25256c5b874b61d43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Sun, 14 Jun 2020 18:28:14 +0200 Subject: [PATCH 2/3] add entry to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 958c75f..f76339c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support setting an initial window position - Support hiding the window initially - Support creating maximized windows +- Support waiting for events to reduce CPU load ## [v0.10.0-beta] 2020-05-10 - Add `WindowConfig.TransparentFramebuffer` option to support window transparency onto the background From 6cef9cf77991bc69afdd57ce504fd86ca17aa8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 16 Jun 2020 21:11:41 +0200 Subject: [PATCH 3/3] rename method and handle negative timeouts --- pixelgl/input.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pixelgl/input.go b/pixelgl/input.go index a0aa8e9..22468ef 100644 --- a/pixelgl/input.go +++ b/pixelgl/input.go @@ -413,11 +413,11 @@ func (w *Window) UpdateInput() { w.doUpdateInput() } -// WaitInput blocks until an event is received or a timeout. If timeout is 0 +// UpdateInputWait blocks until an event is received or a timeout. If timeout is 0 // then it will wait indefinitely -func (w *Window) WaitInput(timeout time.Duration) { +func (w *Window) UpdateInputWait(timeout time.Duration) { mainthread.Call(func() { - if timeout == 0 { + if timeout <= 0 { glfw.WaitEvents() } else { glfw.WaitEventsTimeout(timeout.Seconds())