Simplify main()

This commit is contained in:
Hajime Hoshi 2014-05-11 19:54:19 +09:00
parent 86846b0171
commit 214f099ef7

View File

@ -27,53 +27,37 @@ func main() {
const screenHeight = blocks.ScreenHeight const screenHeight = blocks.ScreenHeight
const screenScale = 2 const screenScale = 2
const fps = 60 const fps = 60
const frameTime = time.Duration(int64(time.Second) / int64(fps))
const title = "Ebiten Demo" const title = "Ebiten Demo"
u := cocoa.UI() u := cocoa.UI()
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
drawing := make(chan struct{}) windowEvents := window.Events()
quit := make(chan struct{}) textureFactory := cocoa.TextureFactory()
go func() { var game Game = blocks.NewGame(NewTextures(textureFactory))
defer close(quit) tick := time.Tick(frameTime)
windowEvents := window.Events() sigterm := make(chan os.Signal, 1)
textureFactory := cocoa.TextureFactory() signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
var game Game = blocks.NewGame(NewTextures(textureFactory))
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
for {
select {
case e := <-windowEvents:
game.HandleEvent(e)
if _, ok := e.(ui.WindowClosedEvent); ok {
return
}
case <-tick:
game.Update()
case <-drawing:
window.Draw(func(context graphics.Context) {
game.Draw(context)
})
drawing <- struct{}{}
}
}
}()
u.Start() u.Start()
defer u.Terminate() defer u.Terminate()
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
for { for {
u.DoEvents() u.DoEvents()
select { select {
default: default:
drawing <- struct{}{} window.Draw(func(context graphics.Context) {
<-drawing game.Draw(context)
case <-s: })
return case <-tick:
case <-quit: game.Update()
case e := <-windowEvents:
game.HandleEvent(e)
if _, ok := e.(ui.WindowClosedEvent); ok {
return
}
case <-sigterm:
return return
} }
} }