Split the event loop

This commit is contained in:
Hajime Hoshi 2013-12-02 21:27:06 +09:00
parent 3e5d58ef85
commit 5eb08941e0

View File

@ -75,14 +75,14 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
context) context)
currentUI = ui currentUI = ui
go ui.chLoop() ui.eventLoop()
return ui return ui
} }
func (ui *UI) chLoop() { func (ui *UI) eventLoop() {
go func() {
inputStateUpdated := []chan ebiten.InputStateUpdatedEvent{} inputStateUpdated := []chan ebiten.InputStateUpdatedEvent{}
screenSizeUpdated := []chan ebiten.ScreenSizeUpdatedEvent{}
for { for {
select { select {
case ch := <-ui.inputStateUpdatedChs: case ch := <-ui.inputStateUpdatedChs:
@ -92,7 +92,15 @@ func (ui *UI) chLoop() {
ch <- e ch <- e
close(ch) close(ch)
} }
inputStateUpdated = []chan ebiten.InputStateUpdatedEvent{} inputStateUpdated = inputStateUpdated[0:0]
}
}
}()
go func() {
screenSizeUpdated := []chan ebiten.ScreenSizeUpdatedEvent{}
for {
select {
case ch := <-ui.screenSizeUpdatedChs: case ch := <-ui.screenSizeUpdatedChs:
screenSizeUpdated = append(screenSizeUpdated, ch) screenSizeUpdated = append(screenSizeUpdated, ch)
case e := <-ui.screenSizeUpdatedNotified: case e := <-ui.screenSizeUpdatedNotified:
@ -103,6 +111,7 @@ func (ui *UI) chLoop() {
screenSizeUpdated = []chan ebiten.ScreenSizeUpdatedEvent{} screenSizeUpdated = []chan ebiten.ScreenSizeUpdatedEvent{}
} }
} }
}()
} }
func (ui *UI) PollEvents() { func (ui *UI) PollEvents() {