Avoid a busy loop

This commit is contained in:
Hajime Hoshi 2013-12-13 01:27:02 +09:00
parent d087333e48
commit 5aa0b5f5c2

View File

@ -82,9 +82,7 @@ func main() {
case e := <-inputStateUpdated: case e := <-inputStateUpdated:
game.OnInputStateUpdated(e) game.OnInputStateUpdated(e)
case _, ok := <-screenSizeUpdated: case _, ok := <-screenSizeUpdated:
if ok { if !ok {
// Do nothing
} else {
screenSizeUpdated = nil screenSizeUpdated = nil
} }
case <-windowClosed: case <-windowClosed:
@ -98,15 +96,19 @@ func main() {
} }
}() }()
frameTime := time.Duration(int64(time.Second) / 120)
tick := time.Tick(frameTime)
for { for {
ui.PollEvents() ui.PollEvents()
select { select {
default: default:
drawing <- graphics.NewLazyCanvas()
canvas := <-drawing
window.Draw(func(actualCanvas graphics.Canvas) { window.Draw(func(actualCanvas graphics.Canvas) {
drawing <- graphics.NewLazyCanvas()
canvas := <-drawing
canvas.Flush(actualCanvas) canvas.Flush(actualCanvas)
}) })
// To avoid a busy loop, take a rest after drawing.
<-tick
case <-quit: case <-quit:
return return
} }