ebiten/ui/run.go

47 lines
829 B
Go
Raw Normal View History

2014-12-06 17:09:59 +01:00
package ui
import (
"github.com/hajimehoshi/ebiten/graphics"
"os"
"os/signal"
"syscall"
"time"
)
type Game interface {
2014-12-06 20:14:35 +01:00
Update() error
Draw(context graphics.Context) error
2014-12-06 17:09:59 +01:00
}
2014-12-06 20:14:35 +01:00
func Run(u UI, game Game, width, height, scale int, title string, fps int) error {
canvas, err := u.Start(width, height, scale, title)
if err != nil {
return err
}
2014-12-06 17:09:59 +01:00
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
defer u.Terminate()
for {
u.DoEvents()
select {
default:
2014-12-06 20:14:35 +01:00
if err := canvas.Draw(game); err != nil {
return err
}
2014-12-06 17:09:59 +01:00
case <-tick:
2014-12-06 20:14:35 +01:00
if err := game.Update(); err != nil {
return err
}
2014-12-06 17:09:59 +01:00
if canvas.IsClosed() {
2014-12-06 20:14:35 +01:00
return nil
2014-12-06 17:09:59 +01:00
}
case <-sigterm:
2014-12-06 20:14:35 +01:00
return nil
2014-12-06 17:09:59 +01:00
}
}
}